clray
view src/rt.cc @ 12:85fd61f374d9
fixed the bloody intersection bug
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 03 Aug 2010 13:06:59 +0100 |
parents | d9a1bab1c3f5 |
children | 407935b73af3 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
6 #ifndef __APPLE__
7 #include <GL/gl.h>
8 #include <GL/glu.h>
9 #else
10 #include <OpenGL/gl.h>
11 #include <OpenGL/glu.h>
12 #endif
14 #include "ocl.h"
15 #include "mesh.h"
17 // kernel arguments
18 enum {
19 KARG_FRAMEBUFFER,
20 KARG_RENDER_INFO,
21 KARG_FACES,
22 KARG_MATLIB,
23 KARG_LIGHTS,
24 KARG_PRIM_RAYS,
25 KARG_XFORM,
26 KARG_INVTRANS_XFORM
27 };
29 struct RendInfo {
30 int xsz, ysz;
31 int num_faces, num_lights;
32 int max_iter;
33 };
35 struct Ray {
36 float origin[4], dir[4];
37 };
39 struct Light {
40 float pos[4], color[4];
41 };
43 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
45 static Ray *prim_rays;
46 static CLProgram *prog;
47 static int global_size;
49 static Face faces[] = {
50 {/* face0 */
51 {
52 {{-1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
53 {{0, 1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
54 {{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
55 },
56 {0, 0, -1, 0}, 0, {0, 0, 0}
57 },
58 {/* face1 */
59 {
60 {{-5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
61 {{0, 0, 3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
62 {{5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
63 },
64 {0, 1, 0, 0}, 1, {0, 0, 0}
65 }
66 };
68 static Material matlib[] = {
69 {{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0, 0},
70 {{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0, 0}
71 };
73 static Light lightlist[] = {
74 {{-10, 10, -20, 0}, {1, 1, 1, 1}}
75 };
78 static RendInfo rinf;
81 bool init_renderer(int xsz, int ysz)
82 {
83 // render info
84 rinf.xsz = xsz;
85 rinf.ysz = ysz;
86 rinf.num_faces = sizeof faces / sizeof *faces;
87 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
88 rinf.max_iter = 6;
90 /* calculate primary rays */
91 prim_rays = new Ray[xsz * ysz];
93 for(int i=0; i<ysz; i++) {
94 for(int j=0; j<xsz; j++) {
95 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
96 }
97 }
99 /* setup opencl */
100 prog = new CLProgram("render");
101 if(!prog->load("rt.cl")) {
102 return false;
103 }
105 /* setup argument buffers */
106 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
107 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
108 prog->set_arg_buffer(KARG_FACES, ARG_RD, sizeof faces, faces);
109 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, sizeof matlib, matlib);
110 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
111 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
112 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
113 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
115 delete [] prim_rays;
117 global_size = xsz * ysz;
118 return true;
119 }
121 void destroy_renderer()
122 {
123 delete prog;
124 }
126 bool render()
127 {
128 if(!prog->run(1, global_size)) {
129 return false;
130 }
132 CLMemBuffer *mbuf = prog->get_arg_buffer(0);
133 void *fb = map_mem_buffer(mbuf, MAP_RD);
134 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
135 unmap_mem_buffer(mbuf);
136 return true;
137 }
139 void dbg_render_gl()
140 {
141 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
143 glDisable(GL_TEXTURE_2D);
144 glEnable(GL_DEPTH_TEST);
146 glMatrixMode(GL_PROJECTION);
147 glPushMatrix();
148 glLoadIdentity();
149 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
151 glBegin(GL_TRIANGLES);
152 for(int i=0; i<rinf.num_faces; i++) {
153 Material *mat = matlib + faces[i].matid;
154 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
156 for(int j=0; j<3; j++) {
157 float *pos = faces[i].v[j].pos;
158 glVertex3f(pos[0], pos[1], pos[2]);
159 }
160 }
161 glEnd();
163 glPopMatrix();
164 glPopAttrib();
165 }
167 void set_xform(float *matrix, float *invtrans)
168 {
169 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
170 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
171 assert(mbuf_xform && mbuf_invtrans);
173 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
174 memcpy(mem, matrix, 16 * sizeof *mem);
175 printf("-- xform:\n");
176 for(int i=0; i<16; i++) {
177 printf("%2.3f\t", mem[i]);
178 if(i % 4 == 3) putchar('\n');
179 }
180 unmap_mem_buffer(mbuf_xform);
182 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
183 memcpy(mem, invtrans, 16 * sizeof *mem);
184 printf("-- inverse-transpose:\n");
185 for(int i=0; i<16; i++) {
186 printf("%2.3f\t", mem[i]);
187 if(i % 4 == 3) putchar('\n');
188 }
189 unmap_mem_buffer(mbuf_invtrans);
190 }
192 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
193 {
194 float vfov = M_PI * vfov_deg / 180.0;
195 float aspect = (float)w / (float)h;
197 float ysz = 2.0;
198 float xsz = aspect * ysz;
200 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
201 float py = 1.0 - ((float)y / (float)h) * ysz;
202 float pz = 1.0 / tan(0.5 * vfov);
204 px *= 100.0;
205 py *= 100.0;
206 pz *= 100.0;
208 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
209 return ray;
210 }