rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@8
|
2 #include <string.h>
|
nuclear@2
|
3 #include <math.h>
|
nuclear@0
|
4 #include <assert.h>
|
John@14
|
5 #include "ogl.h"
|
nuclear@0
|
6 #include "ocl.h"
|
nuclear@9
|
7 #include "mesh.h"
|
nuclear@0
|
8
|
nuclear@12
|
9 // kernel arguments
|
nuclear@12
|
10 enum {
|
nuclear@12
|
11 KARG_FRAMEBUFFER,
|
nuclear@12
|
12 KARG_RENDER_INFO,
|
nuclear@12
|
13 KARG_FACES,
|
nuclear@12
|
14 KARG_MATLIB,
|
nuclear@12
|
15 KARG_LIGHTS,
|
nuclear@12
|
16 KARG_PRIM_RAYS,
|
nuclear@12
|
17 KARG_XFORM,
|
John@14
|
18 KARG_INVTRANS_XFORM,
|
John@15
|
19 KARG_OUTFACES, /* DBG */
|
John@14
|
20
|
John@14
|
21 NUM_KERNEL_ARGS
|
nuclear@12
|
22 };
|
John@11
|
23
|
nuclear@2
|
24 struct RendInfo {
|
nuclear@2
|
25 int xsz, ysz;
|
nuclear@9
|
26 int num_faces, num_lights;
|
nuclear@2
|
27 int max_iter;
|
John@15
|
28 int dbg;
|
nuclear@12
|
29 };
|
nuclear@2
|
30
|
nuclear@1
|
31 struct Ray {
|
nuclear@8
|
32 float origin[4], dir[4];
|
nuclear@12
|
33 };
|
nuclear@1
|
34
|
nuclear@3
|
35 struct Light {
|
nuclear@8
|
36 float pos[4], color[4];
|
nuclear@12
|
37 };
|
nuclear@1
|
38
|
nuclear@3
|
39 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
|
nuclear@13
|
40 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
|
nuclear@3
|
41
|
nuclear@13
|
42 static Face *faces;
|
nuclear@3
|
43 static Ray *prim_rays;
|
nuclear@3
|
44 static CLProgram *prog;
|
nuclear@3
|
45 static int global_size;
|
nuclear@3
|
46
|
nuclear@4
|
47 static Light lightlist[] = {
|
nuclear@12
|
48 {{-10, 10, -20, 0}, {1, 1, 1, 1}}
|
nuclear@4
|
49 };
|
nuclear@4
|
50
|
nuclear@7
|
51
|
nuclear@4
|
52 static RendInfo rinf;
|
nuclear@4
|
53
|
nuclear@4
|
54
|
nuclear@13
|
55 bool init_renderer(int xsz, int ysz, Scene *scn)
|
nuclear@0
|
56 {
|
nuclear@4
|
57 // render info
|
nuclear@4
|
58 rinf.xsz = xsz;
|
nuclear@4
|
59 rinf.ysz = ysz;
|
nuclear@13
|
60 rinf.num_faces = scn->get_num_faces();
|
nuclear@4
|
61 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
|
nuclear@4
|
62 rinf.max_iter = 6;
|
John@15
|
63 rinf.dbg = 8;
|
nuclear@4
|
64
|
nuclear@3
|
65 /* calculate primary rays */
|
nuclear@3
|
66 prim_rays = new Ray[xsz * ysz];
|
nuclear@2
|
67
|
nuclear@2
|
68 for(int i=0; i<ysz; i++) {
|
nuclear@2
|
69 for(int j=0; j<xsz; j++) {
|
nuclear@2
|
70 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
|
nuclear@2
|
71 }
|
nuclear@0
|
72 }
|
nuclear@0
|
73
|
nuclear@2
|
74 /* setup opencl */
|
nuclear@3
|
75 prog = new CLProgram("render");
|
nuclear@3
|
76 if(!prog->load("rt.cl")) {
|
nuclear@8
|
77 return false;
|
nuclear@0
|
78 }
|
nuclear@0
|
79
|
nuclear@13
|
80 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
|
nuclear@13
|
81 if(!faces) {
|
nuclear@13
|
82 fprintf(stderr, "failed to create face buffer\n");
|
nuclear@13
|
83 return false;
|
nuclear@13
|
84 }
|
nuclear@13
|
85
|
nuclear@3
|
86 /* setup argument buffers */
|
nuclear@12
|
87 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
|
nuclear@12
|
88 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
|
John@14
|
89 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
|
John@14
|
90 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
|
nuclear@12
|
91 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
|
nuclear@12
|
92 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
|
nuclear@12
|
93 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@12
|
94 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
|
John@15
|
95 prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
|
nuclear@12
|
96
|
John@14
|
97 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
|
John@14
|
98 return false;
|
John@14
|
99 }
|
John@14
|
100
|
nuclear@12
|
101 delete [] prim_rays;
|
nuclear@2
|
102
|
nuclear@3
|
103 global_size = xsz * ysz;
|
nuclear@3
|
104 return true;
|
nuclear@3
|
105 }
|
nuclear@3
|
106
|
nuclear@3
|
107 void destroy_renderer()
|
nuclear@3
|
108 {
|
nuclear@3
|
109 delete prog;
|
nuclear@3
|
110 }
|
nuclear@3
|
111
|
nuclear@3
|
112 bool render()
|
nuclear@3
|
113 {
|
nuclear@13
|
114 printf("Running kernel...");
|
nuclear@13
|
115 fflush(stdout);
|
nuclear@3
|
116 if(!prog->run(1, global_size)) {
|
nuclear@3
|
117 return false;
|
nuclear@0
|
118 }
|
nuclear@13
|
119 printf("done\n");
|
nuclear@0
|
120
|
John@15
|
121 /* DEBUG */
|
John@15
|
122 CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
|
John@15
|
123 Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
|
John@15
|
124 for(int i=0; i<rinf.num_faces; i++) {
|
John@15
|
125 if(!(faces[i] == outfaces[i])) {
|
John@15
|
126 fprintf(stderr, "SKATA %d\n", i);
|
John@15
|
127 return false;
|
John@15
|
128 }
|
John@15
|
129 faces[i] = outfaces[i];
|
John@15
|
130 }
|
John@15
|
131 printf("equality test passed\n");
|
John@15
|
132 unmap_mem_buffer(dbgbuf);
|
John@15
|
133
|
John@15
|
134
|
nuclear@13
|
135 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@12
|
136 void *fb = map_mem_buffer(mbuf, MAP_RD);
|
nuclear@13
|
137 if(!fb) {
|
nuclear@13
|
138 fprintf(stderr, "FAILED\n");
|
nuclear@13
|
139 return false;
|
nuclear@13
|
140 }
|
nuclear@13
|
141
|
nuclear@12
|
142 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
|
nuclear@2
|
143 unmap_mem_buffer(mbuf);
|
nuclear@3
|
144 return true;
|
nuclear@0
|
145 }
|
nuclear@2
|
146
|
John@15
|
147 void dbg_set_dbg(int dbg)
|
John@15
|
148 {
|
John@15
|
149 printf("setting dbg: %d\n", dbg);
|
John@15
|
150
|
John@15
|
151 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
|
John@15
|
152 RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
|
John@15
|
153 rinf->dbg = dbg;
|
John@15
|
154 unmap_mem_buffer(mbuf);
|
John@15
|
155 }
|
John@15
|
156
|
nuclear@13
|
157 void dbg_render_gl(Scene *scn)
|
nuclear@8
|
158 {
|
John@15
|
159 float lpos[] = {-1, 1, 10, 0};
|
nuclear@12
|
160 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
|
nuclear@8
|
161
|
nuclear@12
|
162 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
163 glEnable(GL_DEPTH_TEST);
|
John@15
|
164 glEnable(GL_LIGHTING);
|
John@15
|
165 glEnable(GL_LIGHT0);
|
John@15
|
166 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
John@15
|
167 glEnable(GL_COLOR_MATERIAL);
|
nuclear@12
|
168
|
nuclear@12
|
169 glMatrixMode(GL_PROJECTION);
|
nuclear@12
|
170 glPushMatrix();
|
nuclear@12
|
171 glLoadIdentity();
|
nuclear@12
|
172 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
|
nuclear@12
|
173
|
John@14
|
174 Material *materials = scn->get_materials();
|
John@14
|
175
|
nuclear@12
|
176 glBegin(GL_TRIANGLES);
|
nuclear@13
|
177 int num_faces = scn->get_num_faces();
|
nuclear@13
|
178 for(int i=0; i<num_faces; i++) {
|
John@14
|
179 Material *mat = materials ? materials + faces[i].matid : 0;
|
John@14
|
180
|
John@14
|
181 if(mat) {
|
John@14
|
182 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
|
John@14
|
183 } else {
|
John@14
|
184 glColor3f(1, 1, 1);
|
John@14
|
185 }
|
nuclear@12
|
186
|
nuclear@12
|
187 for(int j=0; j<3; j++) {
|
nuclear@12
|
188 float *pos = faces[i].v[j].pos;
|
John@15
|
189 float *norm = faces[i].normal;
|
John@15
|
190 glNormal3fv(norm);
|
John@15
|
191 glVertex3fv(pos);
|
nuclear@12
|
192 }
|
nuclear@12
|
193 }
|
nuclear@13
|
194
|
nuclear@13
|
195 /*for(size_t i=0; i<scn->meshes.size(); i++) {
|
nuclear@13
|
196 Material *mat = &scn->matlib[scn->meshes[i]->matid];
|
nuclear@13
|
197
|
nuclear@13
|
198 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
|
nuclear@13
|
199 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
|
nuclear@13
|
200 for(int k=0; k<3; k++) {
|
nuclear@13
|
201 float *pos = scn->meshes[i]->faces[j].v[k].pos;
|
nuclear@13
|
202 glVertex3f(pos[0], pos[1], pos[2]);
|
nuclear@13
|
203 }
|
nuclear@13
|
204 }
|
nuclear@13
|
205 }*/
|
nuclear@12
|
206 glEnd();
|
nuclear@12
|
207
|
nuclear@12
|
208 glPopMatrix();
|
nuclear@12
|
209 glPopAttrib();
|
nuclear@12
|
210 }
|
nuclear@12
|
211
|
nuclear@12
|
212 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
213 {
|
nuclear@12
|
214 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
215 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
216 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
217
|
nuclear@12
|
218 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
219 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@13
|
220 /*printf("-- xform:\n");
|
nuclear@12
|
221 for(int i=0; i<16; i++) {
|
nuclear@12
|
222 printf("%2.3f\t", mem[i]);
|
nuclear@12
|
223 if(i % 4 == 3) putchar('\n');
|
nuclear@13
|
224 }*/
|
nuclear@12
|
225 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
226
|
nuclear@12
|
227 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
228 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@13
|
229 /*printf("-- inverse-transpose:\n");
|
nuclear@12
|
230 for(int i=0; i<16; i++) {
|
nuclear@12
|
231 printf("%2.3f\t", mem[i]);
|
nuclear@12
|
232 if(i % 4 == 3) putchar('\n');
|
nuclear@13
|
233 }*/
|
nuclear@12
|
234 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
235 }
|
nuclear@8
|
236
|
nuclear@3
|
237 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
238 {
|
nuclear@2
|
239 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
240 float aspect = (float)w / (float)h;
|
nuclear@2
|
241
|
nuclear@2
|
242 float ysz = 2.0;
|
nuclear@2
|
243 float xsz = aspect * ysz;
|
nuclear@2
|
244
|
nuclear@2
|
245 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
246 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
247 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
248
|
nuclear@4
|
249 px *= 100.0;
|
nuclear@4
|
250 py *= 100.0;
|
nuclear@4
|
251 pz *= 100.0;
|
nuclear@2
|
252
|
nuclear@12
|
253 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
254 return ray;
|
nuclear@2
|
255 }
|
nuclear@13
|
256
|
nuclear@13
|
257 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
|
nuclear@13
|
258 {
|
nuclear@13
|
259 int num_faces = 0;
|
nuclear@13
|
260 for(int i=0; i<num_meshes; i++) {
|
nuclear@13
|
261 num_faces += meshes[i]->faces.size();
|
nuclear@13
|
262 }
|
nuclear@13
|
263 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
|
nuclear@13
|
264
|
nuclear@13
|
265 Face *faces = new Face[num_faces];
|
nuclear@13
|
266 memset(faces, 0, num_faces * sizeof *faces);
|
nuclear@13
|
267 Face *fptr = faces;
|
nuclear@13
|
268
|
nuclear@13
|
269 for(int i=0; i<num_meshes; i++) {
|
nuclear@13
|
270 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
|
nuclear@13
|
271 *fptr++ = meshes[i]->faces[j];
|
nuclear@13
|
272 }
|
nuclear@13
|
273 }
|
nuclear@13
|
274 return faces;
|
nuclear@13
|
275 }
|