clray
view src/rt.cc @ 20:63a6b46f58a0
fixed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 09 Aug 2010 12:55:40 +0100 |
parents | 8baea9b66b50 |
children | bd6c2b25f6e7 |
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 "mesh.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 int xsz, ysz;
25 int num_faces, num_lights;
26 int max_iter;
27 float ambient[4];
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.075;
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 printf("Running kernel...");
119 fflush(stdout);
120 if(!prog->run(1, global_size)) {
121 return false;
122 }
123 printf("done\n");
126 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
127 void *fb = map_mem_buffer(mbuf, MAP_RD);
128 if(!fb) {
129 fprintf(stderr, "FAILED\n");
130 return false;
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 void dbg_render_gl(Scene *scn)
139 {
140 float lpos[] = {-1, 1, 10, 0};
141 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
143 glDisable(GL_TEXTURE_2D);
144 glEnable(GL_DEPTH_TEST);
145 glEnable(GL_LIGHTING);
146 glEnable(GL_LIGHT0);
147 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
148 glEnable(GL_COLOR_MATERIAL);
150 glMatrixMode(GL_PROJECTION);
151 glPushMatrix();
152 glLoadIdentity();
153 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
155 Material *materials = scn->get_materials();
157 glBegin(GL_TRIANGLES);
158 int num_faces = scn->get_num_faces();
159 for(int i=0; i<num_faces; i++) {
160 Material *mat = materials ? materials + faces[i].matid : 0;
162 if(mat) {
163 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
164 } else {
165 glColor3f(1, 1, 1);
166 }
168 for(int j=0; j<3; j++) {
169 float *pos = faces[i].v[j].pos;
170 float *norm = faces[i].normal;
171 glNormal3fv(norm);
172 glVertex3fv(pos);
173 }
174 }
175 glEnd();
177 glPopMatrix();
178 glPopAttrib();
179 }
181 void set_xform(float *matrix, float *invtrans)
182 {
183 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
184 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
185 assert(mbuf_xform && mbuf_invtrans);
187 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
188 memcpy(mem, matrix, 16 * sizeof *mem);
189 /*printf("-- xform:\n");
190 for(int i=0; i<16; i++) {
191 printf("%2.3f\t", mem[i]);
192 if(i % 4 == 3) putchar('\n');
193 }*/
194 unmap_mem_buffer(mbuf_xform);
196 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
197 memcpy(mem, invtrans, 16 * sizeof *mem);
198 /*printf("-- inverse-transpose:\n");
199 for(int i=0; i<16; i++) {
200 printf("%2.3f\t", mem[i]);
201 if(i % 4 == 3) putchar('\n');
202 }*/
203 unmap_mem_buffer(mbuf_invtrans);
204 }
206 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
207 {
208 float vfov = M_PI * vfov_deg / 180.0;
209 float aspect = (float)w / (float)h;
211 float ysz = 2.0;
212 float xsz = aspect * ysz;
214 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
215 float py = 1.0 - ((float)y / (float)h) * ysz;
216 float pz = 1.0 / tan(0.5 * vfov);
218 px *= 100.0;
219 py *= 100.0;
220 pz *= 100.0;
222 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
223 return ray;
224 }
226 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
227 {
228 int num_faces = 0;
229 for(int i=0; i<num_meshes; i++) {
230 num_faces += meshes[i]->faces.size();
231 }
232 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
234 Face *faces = new Face[num_faces];
235 memset(faces, 0, num_faces * sizeof *faces);
236 Face *fptr = faces;
238 for(int i=0; i<num_meshes; i++) {
239 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
240 *fptr++ = meshes[i]->faces[j];
241 }
242 }
243 return faces;
244 }