clray

annotate src/rt.cc @ 20:63a6b46f58a0

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