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@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@22
|
151 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_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@22
|
161 glEnable(GL_LIGHT0 + i);
|
nuclear@21
|
162 }
|
nuclear@21
|
163
|
nuclear@12
|
164 glDisable(GL_TEXTURE_2D);
|
nuclear@12
|
165 glEnable(GL_DEPTH_TEST);
|
John@15
|
166 glEnable(GL_LIGHTING);
|
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@22
|
197
|
nuclear@22
|
198 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
199 }
|
nuclear@12
|
200
|
nuclear@12
|
201 void set_xform(float *matrix, float *invtrans)
|
nuclear@12
|
202 {
|
nuclear@12
|
203 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
|
nuclear@12
|
204 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
|
nuclear@12
|
205 assert(mbuf_xform && mbuf_invtrans);
|
nuclear@12
|
206
|
nuclear@12
|
207 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
|
nuclear@12
|
208 memcpy(mem, matrix, 16 * sizeof *mem);
|
nuclear@12
|
209 unmap_mem_buffer(mbuf_xform);
|
nuclear@12
|
210
|
nuclear@12
|
211 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
|
nuclear@12
|
212 memcpy(mem, invtrans, 16 * sizeof *mem);
|
nuclear@12
|
213 unmap_mem_buffer(mbuf_invtrans);
|
nuclear@8
|
214 }
|
nuclear@8
|
215
|
nuclear@3
|
216 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
|
nuclear@2
|
217 {
|
nuclear@2
|
218 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@2
|
219 float aspect = (float)w / (float)h;
|
nuclear@2
|
220
|
nuclear@2
|
221 float ysz = 2.0;
|
nuclear@2
|
222 float xsz = aspect * ysz;
|
nuclear@2
|
223
|
nuclear@2
|
224 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@2
|
225 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@2
|
226 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@2
|
227
|
nuclear@4
|
228 px *= 100.0;
|
nuclear@4
|
229 py *= 100.0;
|
nuclear@4
|
230 pz *= 100.0;
|
nuclear@2
|
231
|
nuclear@18
|
232 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
|
nuclear@2
|
233 return ray;
|
nuclear@2
|
234 }
|