clray

view src/rt.cc @ 27:8b2f2ad14ae7

semi-fixed the kdtree construction
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 17 Aug 2010 20:35:00 +0100
parents 13091c00d7ca
children 97cfd9675310
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);
40 static Face *faces;
41 static Ray *prim_rays;
42 static CLProgram *prog;
43 static int global_size;
45 static Light lightlist[] = {
46 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
47 };
50 static RendInfo rinf;
53 bool init_renderer(int xsz, int ysz, Scene *scn)
54 {
55 // render info
56 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
57 rinf.ambient[3] = 0.0;
59 rinf.xsz = xsz;
60 rinf.ysz = ysz;
61 rinf.num_faces = scn->get_num_faces();
62 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
63 rinf.max_iter = 6;
65 /* calculate primary rays */
66 prim_rays = new Ray[xsz * ysz];
68 for(int i=0; i<ysz; i++) {
69 for(int j=0; j<xsz; j++) {
70 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
71 }
72 }
74 /* setup opencl */
75 prog = new CLProgram("render");
76 if(!prog->load("rt.cl")) {
77 return false;
78 }
80 if(!(faces = (Face*)scn->get_face_buffer())) {
81 fprintf(stderr, "failed to create face buffer\n");
82 return false;
83 }
85 /* setup argument buffers */
86 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
87 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
88 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
89 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
90 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
91 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
92 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
93 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
95 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
96 return false;
97 }
99 if(!prog->build()) {
100 return false;
101 }
103 delete [] prim_rays;
105 global_size = xsz * ysz;
106 return true;
107 }
109 void destroy_renderer()
110 {
111 delete prog;
112 }
114 bool render()
115 {
116 if(!prog->run(1, global_size)) {
117 return false;
118 }
120 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
121 void *fb = map_mem_buffer(mbuf, MAP_RD);
122 if(!fb) {
123 fprintf(stderr, "FAILED\n");
124 return false;
125 }
127 static int foo = 0;
128 if(!foo++) {
129 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
130 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
131 }
133 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
134 unmap_mem_buffer(mbuf);
135 return true;
136 }
138 #define MIN(a, b) ((a) < (b) ? (a) : (b))
139 static void dbg_set_gl_material(Material *mat)
140 {
141 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
143 if(!mat) mat = &def_mat;
145 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
146 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
147 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
148 }
150 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
151 {
152 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
154 for(int i=0; i<rinf.num_lights; i++) {
155 float lpos[4];
157 memcpy(lpos, lightlist[i].pos, sizeof lpos);
158 lpos[3] = 1.0;
160 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
161 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
162 glEnable(GL_LIGHT0 + i);
163 }
165 glDisable(GL_TEXTURE_2D);
166 glEnable(GL_DEPTH_TEST);
167 glEnable(GL_LIGHTING);
169 glMatrixMode(GL_PROJECTION);
170 glPushMatrix();
171 glLoadIdentity();
172 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
174 if(show_obj) {
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();
196 }
198 if(show_tree) {
199 scn->draw_kdtree();
200 }
202 glPopMatrix();
203 glPopAttrib();
205 assert(glGetError() == GL_NO_ERROR);
206 }
208 void set_xform(float *matrix, float *invtrans)
209 {
210 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
211 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
212 assert(mbuf_xform && mbuf_invtrans);
214 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
215 memcpy(mem, matrix, 16 * sizeof *mem);
216 unmap_mem_buffer(mbuf_xform);
218 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
219 memcpy(mem, invtrans, 16 * sizeof *mem);
220 unmap_mem_buffer(mbuf_invtrans);
221 }
223 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
224 {
225 float vfov = M_PI * vfov_deg / 180.0;
226 float aspect = (float)w / (float)h;
228 float ysz = 2.0;
229 float xsz = aspect * ysz;
231 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
232 float py = 1.0 - ((float)y / (float)h) * ysz;
233 float pz = 1.0 / tan(0.5 * vfov);
235 px *= 100.0;
236 py *= 100.0;
237 pz *= 100.0;
239 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
240 return ray;
241 }