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@3
|
39
|
nuclear@13
|
40 static Face *faces;
|
nuclear@3
|
41 static Ray *prim_rays;
|
nuclear@3
|
42 static CLProgram *prog;
|
nuclear@3
|
43 static int global_size;
|
nuclear@3
|
44
|
nuclear@4
|
45 static Light lightlist[] = {
|
nuclear@22
|
46 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
|
nuclear@4
|
47 };
|
nuclear@4
|
48
|
nuclear@7
|
49
|
nuclear@4
|
50 static RendInfo rinf;
|
nuclear@4
|
51
|
nuclear@4
|
52
|
nuclear@13
|
53 bool init_renderer(int xsz, int ysz, Scene *scn)
|
nuclear@0
|
54 {
|
nuclear@4
|
55 // render info
|
nuclear@22
|
56 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
|
nuclear@16
|
57 rinf.ambient[3] = 0.0;
|
nuclear@16
|
58
|
nuclear@4
|
59 rinf.xsz = xsz;
|
nuclear@4
|
60 rinf.ysz = ysz;
|
nuclear@13
|
61 rinf.num_faces = scn->get_num_faces();
|
nuclear@4
|
62 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
|
nuclear@4
|
63 rinf.max_iter = 6;
|
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@24
|
80 if(!(faces = (Face*)scn->get_face_buffer())) {
|
nuclear@13
|
81 fprintf(stderr, "failed to create face buffer\n");
|
nuclear@13
|
82 return false;
|
nuclear@13
|
83 }
|
nuclear@13
|
84
|
nuclear@3
|
85 /* setup argument buffers */
|
nuclear@12
|
86 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
|
nuclear@12
|
87 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
|
John@14
|
88 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
|
John@14
|
89 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
|
nuclear@12
|
90 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
|
nuclear@12
|
91 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
|
nuclear@12
|
92 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@12
|
93 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
|
nuclear@12
|
94
|
John@14
|
95 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
|
John@14
|
96 return false;
|
John@14
|
97 }
|
John@14
|
98
|
nuclear@16
|
99 if(!prog->build()) {
|
nuclear@16
|
100 return false;
|
nuclear@16
|
101 }
|
nuclear@16
|
102
|
nuclear@12
|
103 delete [] prim_rays;
|
nuclear@2
|
104
|
nuclear@3
|
105 global_size = xsz * ysz;
|
nuclear@3
|
106 return true;
|
nuclear@3
|
107 }
|
nuclear@3
|
108
|
nuclear@3
|
109 void destroy_renderer()
|
nuclear@3
|
110 {
|
nuclear@3
|
111 delete prog;
|
nuclear@3
|
112 }
|
nuclear@3
|
113
|
nuclear@3
|
114 bool render()
|
nuclear@3
|
115 {
|
nuclear@3
|
116 if(!prog->run(1, global_size)) {
|
nuclear@3
|
117 return false;
|
nuclear@0
|
118 }
|
John@15
|
119
|
nuclear@13
|
120 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
|
nuclear@12
|
121 void *fb = map_mem_buffer(mbuf, MAP_RD);
|
nuclear@13
|
122 if(!fb) {
|
nuclear@13
|
123 fprintf(stderr, "FAILED\n");
|
nuclear@13
|
124 return false;
|
nuclear@13
|
125 }
|
nuclear@13
|
126
|
nuclear@22
|
127 static int foo = 0;
|
nuclear@22
|
128 if(!foo++) {
|
nuclear@22
|
129 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
|
nuclear@22
|
130 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
|
nuclear@22
|
131 }
|
nuclear@22
|
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@27
|
138 #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
nuclear@21
|
139 static void dbg_set_gl_material(Material *mat)
|
nuclear@21
|
140 {
|
nuclear@21
|
141 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
|
nuclear@21
|
142
|
nuclear@21
|
143 if(!mat) mat = &def_mat;
|
nuclear@21
|
144
|
nuclear@21
|
145 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
|
nuclear@21
|
146 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
|
nuclear@27
|
147 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
|
nuclear@21
|
148 }
|
nuclear@21
|
149
|
nuclear@27
|
150 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
|
nuclear@8
|
151 {
|
nuclear@22
|
152 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
|
nuclear@8
|
153
|
nuclear@21
|
154 for(int i=0; i<rinf.num_lights; i++) {
|
nuclear@21
|
155 float lpos[4];
|
nuclear@21
|
156
|
nuclear@21
|
157 memcpy(lpos, lightlist[i].pos, sizeof lpos);
|
nuclear@21
|
158 lpos[3] = 1.0;
|
nuclear@21
|
159
|
nuclear@21
|
160 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
|
nuclear@21
|
161 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
|
nuclear@22
|
162 glEnable(GL_LIGHT0 + i);
|
nuclear@21
|
163 }
|
nuclear@21
|
164
|
nuclear@12
|
165 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
166 glEnable(GL_DEPTH_TEST);
|
John@15
|
167 glEnable(GL_LIGHTING);
|
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
|
nuclear@27
|
174 if(show_obj) {
|
nuclear@27
|
175 Material *materials = scn->get_materials();
|
John@14
|
176
|
nuclear@27
|
177 int num_faces = scn->get_num_faces();
|
nuclear@27
|
178 int cur_mat = -1;
|
nuclear@21
|
179
|
nuclear@27
|
180 for(int i=0; i<num_faces; i++) {
|
nuclear@27
|
181 if(faces[i].matid != cur_mat) {
|
nuclear@27
|
182 if(cur_mat != -1) {
|
nuclear@27
|
183 glEnd();
|
nuclear@27
|
184 }
|
nuclear@27
|
185 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
|
nuclear@27
|
186 cur_mat = faces[i].matid;
|
nuclear@27
|
187 glBegin(GL_TRIANGLES);
|
nuclear@21
|
188 }
|
nuclear@27
|
189
|
nuclear@27
|
190 for(int j=0; j<3; j++) {
|
nuclear@27
|
191 glNormal3fv(faces[i].v[j].normal);
|
nuclear@27
|
192 glVertex3fv(faces[i].v[j].pos);
|
nuclear@27
|
193 }
|
John@14
|
194 }
|
nuclear@27
|
195 glEnd();
|
nuclear@27
|
196 }
|
nuclear@12
|
197
|
nuclear@27
|
198 if(show_tree) {
|
nuclear@27
|
199 scn->draw_kdtree();
|
nuclear@12
|
200 }
|
nuclear@12
|
201
|
nuclear@12
|
202 glPopMatrix();
|
nuclear@12
|
203 glPopAttrib();
|
nuclear@22
|
204
|
nuclear@22
|
205 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
206 }
|
nuclear@12
|
207
|
nuclear@12
|
208 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
209 {
|
nuclear@12
|
210 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
211 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
212 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
213
|
nuclear@12
|
214 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
215 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@12
|
216 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
217
|
nuclear@12
|
218 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
219 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@12
|
220 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
221 }
|
nuclear@8
|
222
|
nuclear@3
|
223 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
224 {
|
nuclear@2
|
225 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
226 float aspect = (float)w / (float)h;
|
nuclear@2
|
227
|
nuclear@2
|
228 float ysz = 2.0;
|
nuclear@2
|
229 float xsz = aspect * ysz;
|
nuclear@2
|
230
|
nuclear@2
|
231 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
232 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
233 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
234
|
nuclear@4
|
235 px *= 100.0;
|
nuclear@4
|
236 py *= 100.0;
|
nuclear@4
|
237 pz *= 100.0;
|
nuclear@2
|
238
|
nuclear@18
|
239 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
240 return ray;
|
nuclear@2
|
241 }
|