clray
view src/rt.cc @ 29:353d80127627
doh ... it doesn't work
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Aug 2010 20:51:57 +0100 |
parents | 97cfd9675310 |
children | 4cf4919c3812 |
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,
19 KARG_KDTREE,
21 NUM_KERNEL_ARGS
22 };
24 struct RendInfo {
25 float ambient[4];
26 int xsz, ysz;
27 int num_faces, num_lights;
28 int max_iter;
29 int kd_depth;
30 };
32 struct Ray {
33 float origin[4], dir[4];
34 };
36 struct Light {
37 float pos[4], color[4];
38 };
40 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
42 static Face *faces;
43 static Ray *prim_rays;
44 static CLProgram *prog;
45 static int global_size;
47 static Light lightlist[] = {
48 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
49 };
52 static RendInfo rinf;
55 bool init_renderer(int xsz, int ysz, Scene *scn)
56 {
57 // render info
58 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
59 rinf.ambient[3] = 0.0;
61 rinf.xsz = xsz;
62 rinf.ysz = ysz;
63 rinf.num_faces = scn->get_num_faces();
64 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
65 rinf.max_iter = 6;
66 rinf.kd_depth = kdtree_depth(scn->kdtree);
68 /* calculate primary rays */
69 prim_rays = new Ray[xsz * ysz];
71 for(int i=0; i<ysz; i++) {
72 for(int j=0; j<xsz; j++) {
73 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
74 }
75 }
77 /* setup opencl */
78 prog = new CLProgram("render");
79 if(!prog->load("rt.cl")) {
80 return false;
81 }
83 if(!(faces = (Face*)scn->get_face_buffer())) {
84 fprintf(stderr, "failed to create face buffer\n");
85 return false;
86 }
88 const KDNodeGPU *kdbuf = scn->get_kdtree_buffer();
89 if(!kdbuf) {
90 fprintf(stderr, "failed to create kdtree buffer\n");
91 return false;
92 }
94 /* setup argument buffers */
95 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
96 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
97 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
98 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
99 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
100 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
101 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
102 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
103 prog->set_arg_buffer(KARG_KDTREE, ARG_RD, scn->get_kdtree_buffer_size(), kdbuf);
105 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
106 return false;
107 }
109 if(!prog->build()) {
110 return false;
111 }
113 delete [] prim_rays;
115 global_size = xsz * ysz;
116 return true;
117 }
119 void destroy_renderer()
120 {
121 delete prog;
122 }
124 bool render()
125 {
126 if(!prog->run(1, global_size)) {
127 return false;
128 }
130 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
131 void *fb = map_mem_buffer(mbuf, MAP_RD);
132 if(!fb) {
133 fprintf(stderr, "FAILED\n");
134 return false;
135 }
137 static int foo = 0;
138 if(!foo++) {
139 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
140 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
141 }
143 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
144 unmap_mem_buffer(mbuf);
145 return true;
146 }
148 #define MIN(a, b) ((a) < (b) ? (a) : (b))
149 static void dbg_set_gl_material(Material *mat)
150 {
151 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
153 if(!mat) mat = &def_mat;
155 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
156 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
157 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
158 }
160 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
161 {
162 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
164 for(int i=0; i<rinf.num_lights; i++) {
165 float lpos[4];
167 memcpy(lpos, lightlist[i].pos, sizeof lpos);
168 lpos[3] = 1.0;
170 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
171 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
172 glEnable(GL_LIGHT0 + i);
173 }
175 glDisable(GL_TEXTURE_2D);
176 glEnable(GL_DEPTH_TEST);
177 glEnable(GL_LIGHTING);
179 glMatrixMode(GL_PROJECTION);
180 glPushMatrix();
181 glLoadIdentity();
182 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
184 if(show_obj) {
185 Material *materials = scn->get_materials();
187 int num_faces = scn->get_num_faces();
188 int cur_mat = -1;
190 for(int i=0; i<num_faces; i++) {
191 if(faces[i].matid != cur_mat) {
192 if(cur_mat != -1) {
193 glEnd();
194 }
195 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
196 cur_mat = faces[i].matid;
197 glBegin(GL_TRIANGLES);
198 }
200 for(int j=0; j<3; j++) {
201 glNormal3fv(faces[i].v[j].normal);
202 glVertex3fv(faces[i].v[j].pos);
203 }
204 }
205 glEnd();
206 }
208 if(show_tree) {
209 scn->draw_kdtree();
210 }
212 glPopMatrix();
213 glPopAttrib();
215 assert(glGetError() == GL_NO_ERROR);
216 }
218 void set_xform(float *matrix, float *invtrans)
219 {
220 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
221 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
222 assert(mbuf_xform && mbuf_invtrans);
224 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
225 memcpy(mem, matrix, 16 * sizeof *mem);
226 unmap_mem_buffer(mbuf_xform);
228 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
229 memcpy(mem, invtrans, 16 * sizeof *mem);
230 unmap_mem_buffer(mbuf_invtrans);
231 }
233 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
234 {
235 float vfov = M_PI * vfov_deg / 180.0;
236 float aspect = (float)w / (float)h;
238 float ysz = 2.0;
239 float xsz = aspect * ysz;
241 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
242 float py = 1.0 - ((float)y / (float)h) * ysz;
243 float pz = 1.0 / tan(0.5 * vfov);
245 px *= 100.0;
246 py *= 100.0;
247 pz *= 100.0;
249 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
250 return ray;
251 }