1
0
Fork 0

pridane texturovanie

master
Dušan Poizl 11 years ago
parent 83a51e7ac9
commit 0ae624bc79
  1. 22
      intersect_kernel.cl
  2. BIN
      ss00005.jpg
  3. 16
      tracer.cpp

@ -2,10 +2,13 @@
typedef struct
{
float3 a,b,c;
float3 v1,v2,v3;
float2 t1,t2,t3;
}Triangle;
__kernel void ray_intersect(int num_triangles, __global Triangle *triangles, __write_only image2d_t output)
const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_REPEAT | CLK_FILTER_LINEAR;
__kernel void ray_intersect(int num_triangles, __global Triangle *triangles, __write_only image2d_t output, __read_only image2d_t tex)
{
int2 gid = (int2)(get_global_id(0), get_global_id(1));
int2 gsize = (int2)(get_global_size(0), get_global_size(1));
@ -17,9 +20,9 @@ __kernel void ray_intersect(int num_triangles, __global Triangle *triangles, __w
for(int i=0;i<num_triangles;i++)
{
float3 e1 = triangles[i].b-triangles[i].a;
float3 e2 = triangles[i].c-triangles[i].a;
float3 T = -triangles[i].a;
float3 e1 = triangles[i].v2-triangles[i].v1;
float3 e2 = triangles[i].v3-triangles[i].v1;
float3 T = -triangles[i].v1;
float3 d = view;
float3 p = cross(d, e2);
@ -30,8 +33,13 @@ __kernel void ray_intersect(int num_triangles, __global Triangle *triangles, __w
float u = r*dot(p, T);
float v = r*dot(q, d);
if(u >= 0 && u <= 1 && v >= 0 && v <= 1 && u+v <= 1)write_imagef(output, gid, (float4)(1));
else write_imagef(output, gid, (float4)(0));
if(u >= 0 && u <= 1 && v >= 0 && v <= 1 && u+v <= 1)
{
float2 st = triangles[i].t1*(1-u-v) + triangles[i].t2*u + triangles[i].t3*v;
float4 color = read_imagef(tex, sampler, st);
write_imagef(output, gid, color.xyzw);
}
//else write_imagef(output, gid, (float4)(0));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

@ -1,4 +1,5 @@
#include "tracer.h"
#include <QImage>
#include <vector>
#include <iostream>
#include <fstream>
@ -58,10 +59,14 @@ void Tracer::setTex(GLuint texture)
typedef struct
{
cl_float3 a,b,c;
cl_float3 v1,v2,v3;
cl_float2 t1,t2,t3;
}Triangle;
Triangle triangle = {{{-1,-1,38}},{{1,-1,3}},{{3,1,3}}};
Triangle triangle[] = {
{{{-2,-2,3}},{{2,-2,3}},{{2,2,8}},{{0,1}},{{1,1}},{{1,0}}},
{{{-2,2,8}},{{2,2,8}},{{-2,-2,3}},{{0,0}},{{1,0}},{{0,1}}}
};
void Tracer::draw()
{
@ -73,11 +78,16 @@ void Tracer::draw()
//err = draw_kernel->setArg(0, *tex);cout << err << " ";
//err = queue->enqueueNDRangeKernel(*draw_kernel, cl::NullRange, cl::NDRange(512, 512), cl::NullRange);cout << err << " ";
QImage img = QImage("ss00005.jpg").convertToFormat(QImage::Format_ARGB32);
cl::ImageFormat format(CL_BGRA, CL_UNORM_INT8);
cl::Image2D texture(*context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, format, img.width(), img.height(), 0, img.bits(), &err);cout << err << " ";
cl::Buffer buffer(*context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(triangle), &triangle);
intersect_kernel->setArg(0, (int)1);
intersect_kernel->setArg(0, (int)2);
intersect_kernel->setArg(1, buffer);
intersect_kernel->setArg(2, *tex);
intersect_kernel->setArg(3, texture);
err = queue->enqueueNDRangeKernel(*intersect_kernel, cl::NullRange, cl::NDRange(512, 512), cl::NullRange);cout << err << " ";
queue->enqueueReleaseGLObjects(&textures);

Loading…
Cancel
Save