clray

annotate src/rt.cc @ 13:407935b73af3

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