clray

view src/rt.cc @ 22:6c44e4b1726d

OMG alignment is a bitch
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Aug 2010 04:30:35 +0100
parents bd6c2b25f6e7
children 13091c00d7ca
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "ogl.h"
6 #include "ocl.h"
7 #include "scene.h"
9 // kernel arguments
10 enum {
11 KARG_FRAMEBUFFER,
12 KARG_RENDER_INFO,
13 KARG_FACES,
14 KARG_MATLIB,
15 KARG_LIGHTS,
16 KARG_PRIM_RAYS,
17 KARG_XFORM,
18 KARG_INVTRANS_XFORM,
20 NUM_KERNEL_ARGS
21 };
23 struct RendInfo {
24 float ambient[4];
25 int xsz, ysz;
26 int num_faces, num_lights;
27 int max_iter;
28 };
30 struct Ray {
31 float origin[4], dir[4];
32 };
34 struct Light {
35 float pos[4], color[4];
36 };
38 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
39 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
41 static Face *faces;
42 static Ray *prim_rays;
43 static CLProgram *prog;
44 static int global_size;
46 static Light lightlist[] = {
47 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
48 };
51 static RendInfo rinf;
54 bool init_renderer(int xsz, int ysz, Scene *scn)
55 {
56 // render info
57 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
58 rinf.ambient[3] = 0.0;
60 rinf.xsz = xsz;
61 rinf.ysz = ysz;
62 rinf.num_faces = scn->get_num_faces();
63 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
64 rinf.max_iter = 6;
66 /* calculate primary rays */
67 prim_rays = new Ray[xsz * ysz];
69 for(int i=0; i<ysz; i++) {
70 for(int j=0; j<xsz; j++) {
71 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
72 }
73 }
75 /* setup opencl */
76 prog = new CLProgram("render");
77 if(!prog->load("rt.cl")) {
78 return false;
79 }
81 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
82 if(!faces) {
83 fprintf(stderr, "failed to create face buffer\n");
84 return false;
85 }
87 /* setup argument buffers */
88 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
89 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
90 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
91 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
92 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
93 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
94 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
95 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
97 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
98 return false;
99 }
101 if(!prog->build()) {
102 return false;
103 }
105 delete [] prim_rays;
107 global_size = xsz * ysz;
108 return true;
109 }
111 void destroy_renderer()
112 {
113 delete prog;
114 }
116 bool render()
117 {
118 if(!prog->run(1, global_size)) {
119 return false;
120 }
122 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
123 void *fb = map_mem_buffer(mbuf, MAP_RD);
124 if(!fb) {
125 fprintf(stderr, "FAILED\n");
126 return false;
127 }
129 static int foo = 0;
130 if(!foo++) {
131 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
132 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
133 }
135 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
136 unmap_mem_buffer(mbuf);
137 return true;
138 }
140 static void dbg_set_gl_material(Material *mat)
141 {
142 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
144 if(!mat) mat = &def_mat;
146 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
147 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
148 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
149 }
151 void dbg_render_gl(Scene *scn)
152 {
153 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
155 for(int i=0; i<rinf.num_lights; i++) {
156 float lpos[4];
158 memcpy(lpos, lightlist[i].pos, sizeof lpos);
159 lpos[3] = 1.0;
161 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
162 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
163 glEnable(GL_LIGHT0 + i);
164 }
166 glDisable(GL_TEXTURE_2D);
167 glEnable(GL_DEPTH_TEST);
168 glEnable(GL_LIGHTING);
170 glMatrixMode(GL_PROJECTION);
171 glPushMatrix();
172 glLoadIdentity();
173 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
175 Material *materials = scn->get_materials();
177 int num_faces = scn->get_num_faces();
178 int cur_mat = -1;
180 for(int i=0; i<num_faces; i++) {
181 if(faces[i].matid != cur_mat) {
182 if(cur_mat != -1) {
183 glEnd();
184 }
185 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
186 cur_mat = faces[i].matid;
187 glBegin(GL_TRIANGLES);
188 }
190 for(int j=0; j<3; j++) {
191 glNormal3fv(faces[i].v[j].normal);
192 glVertex3fv(faces[i].v[j].pos);
193 }
194 }
195 glEnd();
197 glPopMatrix();
198 glPopAttrib();
200 assert(glGetError() == GL_NO_ERROR);
201 }
203 void set_xform(float *matrix, float *invtrans)
204 {
205 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
206 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
207 assert(mbuf_xform && mbuf_invtrans);
209 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
210 memcpy(mem, matrix, 16 * sizeof *mem);
211 unmap_mem_buffer(mbuf_xform);
213 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
214 memcpy(mem, invtrans, 16 * sizeof *mem);
215 unmap_mem_buffer(mbuf_invtrans);
216 }
218 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
219 {
220 float vfov = M_PI * vfov_deg / 180.0;
221 float aspect = (float)w / (float)h;
223 float ysz = 2.0;
224 float xsz = aspect * ysz;
226 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
227 float py = 1.0 - ((float)y / (float)h) * ysz;
228 float pz = 1.0 / tan(0.5 * vfov);
230 px *= 100.0;
231 py *= 100.0;
232 pz *= 100.0;
234 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
235 return ray;
236 }
238 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
239 {
240 int num_faces = 0;
241 for(int i=0; i<num_meshes; i++) {
242 num_faces += meshes[i]->faces.size();
243 }
244 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
246 Face *faces = new Face[num_faces];
247 memset(faces, 0, num_faces * sizeof *faces);
248 Face *fptr = faces;
250 for(int i=0; i<num_meshes; i++) {
251 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
252 *fptr++ = meshes[i]->faces[j];
253 }
254 }
255 return faces;
256 }