# HG changeset patch # User John Tsiombikas # Date 1278981509 -10800 # Node ID 88ac4eb2d18a260b4f038805a8ff374a14801658 # Parent 41d6253492ad6e027cc8ad573ca788bccbcadddd added OpenGL display of the framebuffer diff -r 41d6253492ad -r 88ac4eb2d18a Makefile --- a/Makefile Mon Jul 12 10:38:07 2010 +0300 +++ b/Makefile Tue Jul 13 03:38:29 2010 +0300 @@ -4,7 +4,7 @@ CXX = g++ CXXFLAGS = -pedantic -Wall -g -LDFLAGS = -framework OpenCL +LDFLAGS = -framework OpenGL -framework GLUT -framework OpenCL $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) diff -r 41d6253492ad -r 88ac4eb2d18a rt.cl --- a/rt.cl Mon Jul 12 10:38:07 2010 +0300 +++ b/rt.cl Tue Jul 13 03:38:29 2010 +0300 @@ -1,6 +1,6 @@ struct RendInfo { int xsz, ysz; - int num_sph; + int num_sph, num_lights; int max_iter; }; @@ -10,6 +10,10 @@ float4 color; }; +struct Light { + float4 pos, color; +}; + struct Ray { float4 origin, dir; }; @@ -26,6 +30,7 @@ __kernel void render(__global float4 *fb, __global const struct RendInfo *rinf, __global const struct Sphere *sphlist, + __global const struct Light *lights, __global const struct Ray *primrays) { int idx = get_global_id(0); @@ -38,6 +43,8 @@ } else { fb[idx] = (float4)(0, 0, 0, 1); } + + fb[idx] = primrays[idx].dir * 0.5 + 0.5; } bool intersect(struct Ray ray, diff -r 41d6253492ad -r 88ac4eb2d18a src/clray.cc --- a/src/clray.cc Mon Jul 12 10:38:07 2010 +0300 +++ b/src/clray.cc Tue Jul 13 03:38:29 2010 +0300 @@ -1,94 +1,126 @@ #include +#include #include -#include #include -#include -#include "ocl.h" +#ifndef __APPLE__ +#include +#else +#include +#endif +#include "rt.h" -struct RendInfo { - int xsz, ysz; - int num_sph; - int max_iter; -} __attribute__((packed)); - -struct Sphere { - cl_float4 pos; - cl_float radius; - - cl_float4 color; -} __attribute__((packed)); - -struct Ray { - cl_float4 origin, dir; -} __attribute__((packed)); - - -Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg); +void cleanup(); +void disp(); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); +void mouse(int bn, int status, int x, int y); +void motion(int x, int y); bool write_ppm(const char *fname, float *fb, int xsz, int ysz); -int main() +static float *fb; +static int xsz, ysz; +static bool need_update = true; + +int main(int argc, char **argv) { - const int xsz = 800; - const int ysz = 600; + glutInitWindowSize(800, 600); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutCreateWindow("OpenCL Raytracer"); - Sphere sphlist[] = { - {{0, 0, 10, 1}, 1.0, {1, 0, 0, 1}} - }; - RendInfo rinf = {xsz, ysz, sizeof sphlist / sizeof *sphlist, 6}; - Ray *prim_rays = new Ray[xsz * ysz]; - float *fb = new float[xsz * ysz * 4]; + xsz = glutGet(GLUT_WINDOW_WIDTH); + ysz = glutGet(GLUT_WINDOW_HEIGHT); - /* calculate primary rays */ - for(int i=0; i +#include +#include +#include "ocl.h" + +struct RendInfo { + int xsz, ysz; + int num_sph, num_lights; + int max_iter; +} __attribute__((packed)); + +struct Sphere { + cl_float4 pos; + cl_float radius; + + cl_float4 color; +} __attribute__((packed)); + +struct Ray { + cl_float4 origin, dir; +} __attribute__((packed)); + +struct Light { + cl_float4 pos, color; +} __attribute__((packed)); + + +static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg); + +static Ray *prim_rays; +static CLProgram *prog; +static int global_size; + +bool init_renderer(int xsz, int ysz, float *fb) +{ + Sphere sphlist[] = { + {{0, 0, 10, 1}, 1.0, {1, 0, 0, 1}} + }; + Light lightlist[] = { + {{-10, 10, -20, 1}, {1, 1, 1, 1}} + }; + RendInfo rinf = { + xsz, ysz, + sizeof sphlist / sizeof *sphlist, + sizeof lightlist / sizeof *lightlist, + 6 + }; + + /* calculate primary rays */ + prim_rays = new Ray[xsz * ysz]; + + for(int i=0; iload("rt.cl")) { + return 1; + } + + /* setup argument buffers */ + prog->set_arg_buffer(0, ARG_WR, xsz * ysz * 4 * sizeof(float), fb); + prog->set_arg_buffer(1, ARG_RD, sizeof rinf, &rinf); + prog->set_arg_buffer(2, ARG_RD, sizeof sphlist, sphlist); + prog->set_arg_buffer(3, ARG_RD, sizeof lightlist, lightlist); + prog->set_arg_buffer(4, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays); + + global_size = xsz * ysz; + return true; +} + +void destroy_renderer() +{ + delete [] prim_rays; + delete prog; +} + +bool render() +{ + if(!prog->run(1, global_size)) { + return false; + } + + CLMemBuffer *mbuf = prog->get_arg_buffer(0); + map_mem_buffer(mbuf, MAP_RD); + /*if(!write_ppm("out.ppm", fb, xsz, ysz)) { + return 1; + }*/ + unmap_mem_buffer(mbuf); + return true; +} + +static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg) +{ + float vfov = M_PI * vfov_deg / 180.0; + float aspect = (float)w / (float)h; + + float ysz = 2.0; + float xsz = aspect * ysz; + + float px = ((float)x / (float)w) * xsz - xsz / 2.0; + float py = 1.0 - ((float)y / (float)h) * ysz; + float pz = 1.0 / tan(0.5 * vfov); + + pz *= 1000.0; + + Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}}; + return ray; +} diff -r 41d6253492ad -r 88ac4eb2d18a src/rt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rt.h Tue Jul 13 03:38:29 2010 +0300 @@ -0,0 +1,8 @@ +#ifndef RT_H_ +#define RT_H_ + +bool init_renderer(int xsz, int ysz, float *fb); +void destroy_renderer(); +bool render(); + +#endif /* RT_H_ */