clray

annotate src/rt.cc @ 19:8baea9b66b50

added reflection
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 06:45:57 +0100
parents 4b1604f9798a
children 63a6b46f58a0
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@15 19 KARG_OUTFACES, /* DBG */
John@14 20
John@14 21 NUM_KERNEL_ARGS
nuclear@12 22 };
John@11 23
nuclear@2 24 struct RendInfo {
nuclear@2 25 int xsz, ysz;
nuclear@9 26 int num_faces, num_lights;
nuclear@2 27 int max_iter;
nuclear@16 28 float ambient[4];
John@15 29 int dbg;
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@13 41 static Face *create_face_buffer(Mesh **meshes, int num_meshes);
nuclear@3 42
nuclear@13 43 static Face *faces;
nuclear@3 44 static Ray *prim_rays;
nuclear@3 45 static CLProgram *prog;
nuclear@3 46 static int global_size;
nuclear@3 47
nuclear@4 48 static Light lightlist[] = {
nuclear@19 49 {{-8, 15, -18, 0}, {1, 1, 1, 1}}
nuclear@4 50 };
nuclear@4 51
nuclear@7 52
nuclear@4 53 static RendInfo rinf;
nuclear@4 54
nuclear@4 55
nuclear@13 56 bool init_renderer(int xsz, int ysz, Scene *scn)
nuclear@0 57 {
nuclear@4 58 // render info
nuclear@16 59 rinf.ambient[0] = rinf.ambient[1] = rinf.ambient[2] = 0.075;
nuclear@16 60 rinf.ambient[3] = 0.0;
nuclear@16 61
nuclear@4 62 rinf.xsz = xsz;
nuclear@4 63 rinf.ysz = ysz;
nuclear@13 64 rinf.num_faces = scn->get_num_faces();
nuclear@4 65 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
nuclear@4 66 rinf.max_iter = 6;
John@15 67 rinf.dbg = 8;
nuclear@4 68
nuclear@3 69 /* calculate primary rays */
nuclear@3 70 prim_rays = new Ray[xsz * ysz];
nuclear@2 71
nuclear@2 72 for(int i=0; i<ysz; i++) {
nuclear@2 73 for(int j=0; j<xsz; j++) {
nuclear@2 74 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
nuclear@2 75 }
nuclear@0 76 }
nuclear@0 77
nuclear@2 78 /* setup opencl */
nuclear@3 79 prog = new CLProgram("render");
nuclear@3 80 if(!prog->load("rt.cl")) {
nuclear@8 81 return false;
nuclear@0 82 }
nuclear@0 83
nuclear@13 84 /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
nuclear@13 85 if(!faces) {
nuclear@13 86 fprintf(stderr, "failed to create face buffer\n");
nuclear@13 87 return false;
nuclear@13 88 }
nuclear@13 89
nuclear@3 90 /* setup argument buffers */
nuclear@12 91 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
nuclear@12 92 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
John@14 93 prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
John@14 94 prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
nuclear@12 95 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
nuclear@12 96 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
nuclear@12 97 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
nuclear@12 98 prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
John@15 99 prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
nuclear@12 100
John@14 101 if(prog->get_num_args() < NUM_KERNEL_ARGS) {
John@14 102 return false;
John@14 103 }
John@14 104
nuclear@16 105 if(!prog->build()) {
nuclear@16 106 return false;
nuclear@16 107 }
nuclear@16 108
nuclear@12 109 delete [] prim_rays;
nuclear@2 110
nuclear@3 111 global_size = xsz * ysz;
nuclear@3 112 return true;
nuclear@3 113 }
nuclear@3 114
nuclear@3 115 void destroy_renderer()
nuclear@3 116 {
nuclear@3 117 delete prog;
nuclear@3 118 }
nuclear@3 119
nuclear@3 120 bool render()
nuclear@3 121 {
nuclear@13 122 printf("Running kernel...");
nuclear@13 123 fflush(stdout);
nuclear@3 124 if(!prog->run(1, global_size)) {
nuclear@3 125 return false;
nuclear@0 126 }
nuclear@13 127 printf("done\n");
nuclear@0 128
John@15 129 /* DEBUG */
John@15 130 CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
John@15 131 Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
John@15 132 for(int i=0; i<rinf.num_faces; i++) {
John@15 133 if(!(faces[i] == outfaces[i])) {
John@15 134 fprintf(stderr, "SKATA %d\n", i);
John@15 135 return false;
John@15 136 }
John@15 137 faces[i] = outfaces[i];
John@15 138 }
John@15 139 printf("equality test passed\n");
John@15 140 unmap_mem_buffer(dbgbuf);
John@15 141
John@15 142
nuclear@13 143 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
nuclear@12 144 void *fb = map_mem_buffer(mbuf, MAP_RD);
nuclear@13 145 if(!fb) {
nuclear@13 146 fprintf(stderr, "FAILED\n");
nuclear@13 147 return false;
nuclear@13 148 }
nuclear@13 149
nuclear@12 150 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
nuclear@2 151 unmap_mem_buffer(mbuf);
nuclear@3 152 return true;
nuclear@0 153 }
nuclear@2 154
John@15 155 void dbg_set_dbg(int dbg)
John@15 156 {
John@15 157 printf("setting dbg: %d\n", dbg);
John@15 158
John@15 159 CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
John@15 160 RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
John@15 161 rinf->dbg = dbg;
John@15 162 unmap_mem_buffer(mbuf);
John@15 163 }
John@15 164
nuclear@13 165 void dbg_render_gl(Scene *scn)
nuclear@8 166 {
John@15 167 float lpos[] = {-1, 1, 10, 0};
nuclear@12 168 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
nuclear@8 169
nuclear@12 170 glDisable(GL_TEXTURE_2D);
nuclear@12 171 glEnable(GL_DEPTH_TEST);
John@15 172 glEnable(GL_LIGHTING);
John@15 173 glEnable(GL_LIGHT0);
John@15 174 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
John@15 175 glEnable(GL_COLOR_MATERIAL);
nuclear@12 176
nuclear@12 177 glMatrixMode(GL_PROJECTION);
nuclear@12 178 glPushMatrix();
nuclear@12 179 glLoadIdentity();
nuclear@12 180 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
nuclear@12 181
John@14 182 Material *materials = scn->get_materials();
John@14 183
nuclear@12 184 glBegin(GL_TRIANGLES);
nuclear@13 185 int num_faces = scn->get_num_faces();
nuclear@13 186 for(int i=0; i<num_faces; i++) {
John@14 187 Material *mat = materials ? materials + faces[i].matid : 0;
John@14 188
John@14 189 if(mat) {
John@14 190 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
John@14 191 } else {
John@14 192 glColor3f(1, 1, 1);
John@14 193 }
nuclear@12 194
nuclear@12 195 for(int j=0; j<3; j++) {
nuclear@12 196 float *pos = faces[i].v[j].pos;
John@15 197 float *norm = faces[i].normal;
John@15 198 glNormal3fv(norm);
John@15 199 glVertex3fv(pos);
nuclear@12 200 }
nuclear@12 201 }
nuclear@13 202
nuclear@13 203 /*for(size_t i=0; i<scn->meshes.size(); i++) {
nuclear@13 204 Material *mat = &scn->matlib[scn->meshes[i]->matid];
nuclear@13 205
nuclear@13 206 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
nuclear@13 207 for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
nuclear@13 208 for(int k=0; k<3; k++) {
nuclear@13 209 float *pos = scn->meshes[i]->faces[j].v[k].pos;
nuclear@13 210 glVertex3f(pos[0], pos[1], pos[2]);
nuclear@13 211 }
nuclear@13 212 }
nuclear@13 213 }*/
nuclear@12 214 glEnd();
nuclear@12 215
nuclear@12 216 glPopMatrix();
nuclear@12 217 glPopAttrib();
nuclear@12 218 }
nuclear@12 219
nuclear@12 220 void set_xform(float *matrix, float *invtrans)
nuclear@12 221 {
nuclear@12 222 CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
nuclear@12 223 CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
nuclear@12 224 assert(mbuf_xform && mbuf_invtrans);
nuclear@12 225
nuclear@12 226 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
nuclear@12 227 memcpy(mem, matrix, 16 * sizeof *mem);
nuclear@13 228 /*printf("-- xform:\n");
nuclear@12 229 for(int i=0; i<16; i++) {
nuclear@12 230 printf("%2.3f\t", mem[i]);
nuclear@12 231 if(i % 4 == 3) putchar('\n');
nuclear@13 232 }*/
nuclear@12 233 unmap_mem_buffer(mbuf_xform);
nuclear@12 234
nuclear@12 235 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
nuclear@12 236 memcpy(mem, invtrans, 16 * sizeof *mem);
nuclear@13 237 /*printf("-- inverse-transpose:\n");
nuclear@12 238 for(int i=0; i<16; i++) {
nuclear@12 239 printf("%2.3f\t", mem[i]);
nuclear@12 240 if(i % 4 == 3) putchar('\n');
nuclear@13 241 }*/
nuclear@12 242 unmap_mem_buffer(mbuf_invtrans);
nuclear@8 243 }
nuclear@8 244
nuclear@3 245 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
nuclear@2 246 {
nuclear@2 247 float vfov = M_PI * vfov_deg / 180.0;
nuclear@2 248 float aspect = (float)w / (float)h;
nuclear@2 249
nuclear@2 250 float ysz = 2.0;
nuclear@2 251 float xsz = aspect * ysz;
nuclear@2 252
nuclear@2 253 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@2 254 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@2 255 float pz = 1.0 / tan(0.5 * vfov);
nuclear@2 256
nuclear@4 257 px *= 100.0;
nuclear@4 258 py *= 100.0;
nuclear@4 259 pz *= 100.0;
nuclear@2 260
nuclear@18 261 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
nuclear@2 262 return ray;
nuclear@2 263 }
nuclear@13 264
nuclear@13 265 static Face *create_face_buffer(Mesh **meshes, int num_meshes)
nuclear@13 266 {
nuclear@13 267 int num_faces = 0;
nuclear@13 268 for(int i=0; i<num_meshes; i++) {
nuclear@13 269 num_faces += meshes[i]->faces.size();
nuclear@13 270 }
nuclear@13 271 printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
nuclear@13 272
nuclear@13 273 Face *faces = new Face[num_faces];
nuclear@13 274 memset(faces, 0, num_faces * sizeof *faces);
nuclear@13 275 Face *fptr = faces;
nuclear@13 276
nuclear@13 277 for(int i=0; i<num_meshes; i++) {
nuclear@13 278 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
nuclear@13 279 *fptr++ = meshes[i]->faces[j];
nuclear@13 280 }
nuclear@13 281 }
nuclear@13 282 return faces;
nuclear@13 283 }