clray

view src/rt.cc @ 13:407935b73af3

bollocks
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Aug 2010 04:51:06 +0100
parents 85fd61f374d9
children 29f9330cfa4b
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);
44 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
46 static Face *faces;
47 static Ray *prim_rays;
48 static CLProgram *prog;
49 static int global_size;
51 static Light lightlist[] = {
52 {{-10, 10, -20, 0}, {1, 1, 1, 1}}
53 };
56 static RendInfo rinf;
59 bool init_renderer(int xsz, int ysz, Scene *scn)
60 {
61 // render info
62 rinf.xsz = xsz;
63 rinf.ysz = ysz;
64 rinf.num_faces = scn->get_num_faces();
65 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
66 rinf.max_iter = 6;
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 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
84 if(!faces) {
85 fprintf(stderr, "failed to create face buffer\n");
86 return false;
87 }
89 /* setup argument buffers */
90 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
91 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
92 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces, faces);
93 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->matlib.size() * sizeof(Material), &scn->matlib[0]);
94 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
95 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
96 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
97 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
99 delete [] prim_rays;
101 global_size = xsz * ysz;
102 return true;
103 }
105 void destroy_renderer()
106 {
107 delete prog;
108 }
110 bool render()
111 {
112 printf("Running kernel...");
113 fflush(stdout);
114 if(!prog->run(1, global_size)) {
115 return false;
116 }
117 printf("done\n");
119 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
120 void *fb = map_mem_buffer(mbuf, MAP_RD);
121 if(!fb) {
122 fprintf(stderr, "FAILED\n");
123 return false;
124 }
126 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
127 unmap_mem_buffer(mbuf);
128 return true;
129 }
131 void dbg_render_gl(Scene *scn)
132 {
133 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
135 glDisable(GL_TEXTURE_2D);
136 glEnable(GL_DEPTH_TEST);
138 glMatrixMode(GL_PROJECTION);
139 glPushMatrix();
140 glLoadIdentity();
141 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
143 glBegin(GL_TRIANGLES);
144 int num_faces = scn->get_num_faces();
145 for(int i=0; i<num_faces; i++) {
146 Material *mat = &scn->matlib[faces[i].matid];
147 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
149 for(int j=0; j<3; j++) {
150 float *pos = faces[i].v[j].pos;
151 glVertex3f(pos[0], pos[1], pos[2]);
152 }
153 }
155 /*for(size_t i=0; i<scn->meshes.size(); i++) {
156 Material *mat = &scn->matlib[scn->meshes[i]->matid];
158 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
159 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
160 for(int k=0; k<3; k++) {
161 float *pos = scn->meshes[i]->faces[j].v[k].pos;
162 glVertex3f(pos[0], pos[1], pos[2]);
163 }
164 }
165 }*/
166 glEnd();
168 glPopMatrix();
169 glPopAttrib();
170 }
172 void set_xform(float *matrix, float *invtrans)
173 {
174 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
175 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
176 assert(mbuf_xform && mbuf_invtrans);
178 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
179 memcpy(mem, matrix, 16 * sizeof *mem);
180 /*printf("-- xform:\n");
181 for(int i=0; i<16; i++) {
182 printf("%2.3f\t", mem[i]);
183 if(i % 4 == 3) putchar('\n');
184 }*/
185 unmap_mem_buffer(mbuf_xform);
187 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
188 memcpy(mem, invtrans, 16 * sizeof *mem);
189 /*printf("-- inverse-transpose:\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_invtrans);
195 }
197 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
198 {
199 float vfov = M_PI * vfov_deg / 180.0;
200 float aspect = (float)w / (float)h;
202 float ysz = 2.0;
203 float xsz = aspect * ysz;
205 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
206 float py = 1.0 - ((float)y / (float)h) * ysz;
207 float pz = 1.0 / tan(0.5 * vfov);
209 px *= 100.0;
210 py *= 100.0;
211 pz *= 100.0;
213 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
214 return ray;
215 }
217 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
218 {
219 int num_faces = 0;
220 for(int i=0; i<num_meshes; i++) {
221 num_faces += meshes[i]->faces.size();
222 }
223 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
225 Face *faces = new Face[num_faces];
226 memset(faces, 0, num_faces * sizeof *faces);
227 Face *fptr = faces;
229 for(int i=0; i<num_meshes; i++) {
230 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
231 *fptr++ = meshes[i]->faces[j];
232 }
233 }
234 return faces;
235 }