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