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