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@22
|
7 #include "scene.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@22
|
24 float ambient[4];
|
nuclear@2
|
25 int xsz, ysz;
|
nuclear@9
|
26 int num_faces, num_lights;
|
nuclear@2
|
27 int max_iter;
|
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@22
|
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@22
|
57 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
|
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@3
|
118 if(!prog->run(1, global_size)) {
|
nuclear@3
|
119 return false;
|
nuclear@0
|
120 }
|
John@15
|
121
|
nuclear@13
|
122 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@12
|
123 void *fb = map_mem_buffer(mbuf, MAP_RD);
|
nuclear@13
|
124 if(!fb) {
|
nuclear@13
|
125 fprintf(stderr, "FAILED\n");
|
nuclear@13
|
126 return false;
|
nuclear@13
|
127 }
|
nuclear@13
|
128
|
nuclear@22
|
129 static int foo = 0;
|
nuclear@22
|
130 if(!foo++) {
|
nuclear@22
|
131 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
|
nuclear@22
|
132 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
|
nuclear@22
|
133 }
|
nuclear@22
|
134
|
nuclear@12
|
135 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
|
nuclear@2
|
136 unmap_mem_buffer(mbuf);
|
nuclear@3
|
137 return true;
|
nuclear@0
|
138 }
|
nuclear@2
|
139
|
nuclear@21
|
140 static void dbg_set_gl_material(Material *mat)
|
nuclear@21
|
141 {
|
nuclear@21
|
142 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
|
nuclear@21
|
143
|
nuclear@21
|
144 if(!mat) mat = &def_mat;
|
nuclear@21
|
145
|
nuclear@21
|
146 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
|
nuclear@21
|
147 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
|
nuclear@21
|
148 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
|
nuclear@21
|
149 }
|
nuclear@21
|
150
|
nuclear@13
|
151 void dbg_render_gl(Scene *scn)
|
nuclear@8
|
152 {
|
nuclear@22
|
153 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
|
nuclear@8
|
154
|
nuclear@21
|
155 for(int i=0; i<rinf.num_lights; i++) {
|
nuclear@21
|
156 float lpos[4];
|
nuclear@21
|
157
|
nuclear@21
|
158 memcpy(lpos, lightlist[i].pos, sizeof lpos);
|
nuclear@21
|
159 lpos[3] = 1.0;
|
nuclear@21
|
160
|
nuclear@21
|
161 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
|
nuclear@21
|
162 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
|
nuclear@22
|
163 glEnable(GL_LIGHT0 + i);
|
nuclear@21
|
164 }
|
nuclear@21
|
165
|
nuclear@12
|
166 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
167 glEnable(GL_DEPTH_TEST);
|
John@15
|
168 glEnable(GL_LIGHTING);
|
nuclear@12
|
169
|
nuclear@12
|
170 glMatrixMode(GL_PROJECTION);
|
nuclear@12
|
171 glPushMatrix();
|
nuclear@12
|
172 glLoadIdentity();
|
nuclear@12
|
173 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
|
nuclear@12
|
174
|
John@14
|
175 Material *materials = scn->get_materials();
|
John@14
|
176
|
nuclear@13
|
177 int num_faces = scn->get_num_faces();
|
nuclear@21
|
178 int cur_mat = -1;
|
nuclear@21
|
179
|
nuclear@13
|
180 for(int i=0; i<num_faces; i++) {
|
nuclear@21
|
181 if(faces[i].matid != cur_mat) {
|
nuclear@21
|
182 if(cur_mat != -1) {
|
nuclear@21
|
183 glEnd();
|
nuclear@21
|
184 }
|
nuclear@21
|
185 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
|
nuclear@21
|
186 cur_mat = faces[i].matid;
|
nuclear@21
|
187 glBegin(GL_TRIANGLES);
|
John@14
|
188 }
|
nuclear@12
|
189
|
nuclear@12
|
190 for(int j=0; j<3; j++) {
|
nuclear@21
|
191 glNormal3fv(faces[i].v[j].normal);
|
nuclear@21
|
192 glVertex3fv(faces[i].v[j].pos);
|
nuclear@12
|
193 }
|
nuclear@12
|
194 }
|
nuclear@12
|
195 glEnd();
|
nuclear@12
|
196
|
nuclear@12
|
197 glPopMatrix();
|
nuclear@12
|
198 glPopAttrib();
|
nuclear@22
|
199
|
nuclear@22
|
200 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
201 }
|
nuclear@12
|
202
|
nuclear@12
|
203 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
204 {
|
nuclear@12
|
205 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
206 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
207 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
208
|
nuclear@12
|
209 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
210 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@12
|
211 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
212
|
nuclear@12
|
213 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
214 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@12
|
215 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
216 }
|
nuclear@8
|
217
|
nuclear@3
|
218 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
219 {
|
nuclear@2
|
220 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
221 float aspect = (float)w / (float)h;
|
nuclear@2
|
222
|
nuclear@2
|
223 float ysz = 2.0;
|
nuclear@2
|
224 float xsz = aspect * ysz;
|
nuclear@2
|
225
|
nuclear@2
|
226 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
227 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
228 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
229
|
nuclear@4
|
230 px *= 100.0;
|
nuclear@4
|
231 py *= 100.0;
|
nuclear@4
|
232 pz *= 100.0;
|
nuclear@2
|
233
|
nuclear@18
|
234 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
235 return ray;
|
nuclear@2
|
236 }
|
nuclear@13
|
237
|
nuclear@13
|
238 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
|
nuclear@13
|
239 {
|
nuclear@13
|
240 int num_faces = 0;
|
nuclear@13
|
241 for(int i=0; i<num_meshes; i++) {
|
nuclear@13
|
242 num_faces += meshes[i]->faces.size();
|
nuclear@13
|
243 }
|
nuclear@13
|
244 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
|
nuclear@13
|
245
|
nuclear@13
|
246 Face *faces = new Face[num_faces];
|
nuclear@13
|
247 memset(faces, 0, num_faces * sizeof *faces);
|
nuclear@13
|
248 Face *fptr = faces;
|
nuclear@13
|
249
|
nuclear@13
|
250 for(int i=0; i<num_meshes; i++) {
|
nuclear@13
|
251 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
|
nuclear@13
|
252 *fptr++ = meshes[i]->faces[j];
|
nuclear@13
|
253 }
|
nuclear@13
|
254 }
|
nuclear@13
|
255 return faces;
|
nuclear@13
|
256 }
|