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