clray
view src/rt.cc @ 24:13091c00d7ca
- moved create_face_buffer to Scene::get_face_buffer and did a few reorganizations.
- starting work on the kdtree creation
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Aug 2010 03:02:52 +0100 |
parents | 6c44e4b1726d |
children | 8b2f2ad14ae7 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "ogl.h"
6 #include "ocl.h"
7 #include "scene.h"
9 // kernel arguments
10 enum {
11 KARG_FRAMEBUFFER,
12 KARG_RENDER_INFO,
13 KARG_FACES,
14 KARG_MATLIB,
15 KARG_LIGHTS,
16 KARG_PRIM_RAYS,
17 KARG_XFORM,
18 KARG_INVTRANS_XFORM,
20 NUM_KERNEL_ARGS
21 };
23 struct RendInfo {
24 float ambient[4];
25 int xsz, ysz;
26 int num_faces, num_lights;
27 int max_iter;
28 };
30 struct Ray {
31 float origin[4], dir[4];
32 };
34 struct Light {
35 float pos[4], color[4];
36 };
38 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
40 static Face *faces;
41 static Ray *prim_rays;
42 static CLProgram *prog;
43 static int global_size;
45 static Light lightlist[] = {
46 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
47 };
50 static RendInfo rinf;
53 bool init_renderer(int xsz, int ysz, Scene *scn)
54 {
55 // render info
56 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.0;
57 rinf.ambient[3] = 0.0;
59 rinf.xsz = xsz;
60 rinf.ysz = ysz;
61 rinf.num_faces = scn->get_num_faces();
62 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
63 rinf.max_iter = 6;
65 /* calculate primary rays */
66 prim_rays = new Ray[xsz * ysz];
68 for(int i=0; i<ysz; i++) {
69 for(int j=0; j<xsz; j++) {
70 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
71 }
72 }
74 /* setup opencl */
75 prog = new CLProgram("render");
76 if(!prog->load("rt.cl")) {
77 return false;
78 }
80 if(!(faces = (Face*)scn->get_face_buffer())) {
81 fprintf(stderr, "failed to create face buffer\n");
82 return false;
83 }
85 /* setup argument buffers */
86 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
87 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
88 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
89 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
90 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
91 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
92 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
93 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
95 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
96 return false;
97 }
99 if(!prog->build()) {
100 return false;
101 }
103 delete [] prim_rays;
105 global_size = xsz * ysz;
106 return true;
107 }
109 void destroy_renderer()
110 {
111 delete prog;
112 }
114 bool render()
115 {
116 if(!prog->run(1, global_size)) {
117 return false;
118 }
120 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
121 void *fb = map_mem_buffer(mbuf, MAP_RD);
122 if(!fb) {
123 fprintf(stderr, "FAILED\n");
124 return false;
125 }
127 static int foo = 0;
128 if(!foo++) {
129 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
130 write_ppm("foo.ppm", (float*)fb, rinf.xsz, rinf.ysz);
131 }
133 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
134 unmap_mem_buffer(mbuf);
135 return true;
136 }
138 static void dbg_set_gl_material(Material *mat)
139 {
140 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
142 if(!mat) mat = &def_mat;
144 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
145 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
146 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
147 }
149 void dbg_render_gl(Scene *scn)
150 {
151 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
153 for(int i=0; i<rinf.num_lights; i++) {
154 float lpos[4];
156 memcpy(lpos, lightlist[i].pos, sizeof lpos);
157 lpos[3] = 1.0;
159 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
160 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
161 glEnable(GL_LIGHT0 + i);
162 }
164 glDisable(GL_TEXTURE_2D);
165 glEnable(GL_DEPTH_TEST);
166 glEnable(GL_LIGHTING);
168 glMatrixMode(GL_PROJECTION);
169 glPushMatrix();
170 glLoadIdentity();
171 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
173 Material *materials = scn->get_materials();
175 int num_faces = scn->get_num_faces();
176 int cur_mat = -1;
178 for(int i=0; i<num_faces; i++) {
179 if(faces[i].matid != cur_mat) {
180 if(cur_mat != -1) {
181 glEnd();
182 }
183 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
184 cur_mat = faces[i].matid;
185 glBegin(GL_TRIANGLES);
186 }
188 for(int j=0; j<3; j++) {
189 glNormal3fv(faces[i].v[j].normal);
190 glVertex3fv(faces[i].v[j].pos);
191 }
192 }
193 glEnd();
195 glPopMatrix();
196 glPopAttrib();
198 assert(glGetError() == GL_NO_ERROR);
199 }
201 void set_xform(float *matrix, float *invtrans)
202 {
203 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
204 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
205 assert(mbuf_xform && mbuf_invtrans);
207 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
208 memcpy(mem, matrix, 16 * sizeof *mem);
209 unmap_mem_buffer(mbuf_xform);
211 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
212 memcpy(mem, invtrans, 16 * sizeof *mem);
213 unmap_mem_buffer(mbuf_invtrans);
214 }
216 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
217 {
218 float vfov = M_PI * vfov_deg / 180.0;
219 float aspect = (float)w / (float)h;
221 float ysz = 2.0;
222 float xsz = aspect * ysz;
224 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
225 float py = 1.0 - ((float)y / (float)h) * ysz;
226 float pz = 1.0 / tan(0.5 * vfov);
228 px *= 100.0;
229 py *= 100.0;
230 pz *= 100.0;
232 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
233 return ray;
234 }