clray
diff 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 |
line diff
1.1 --- a/src/rt.cc Tue Aug 03 13:06:59 2010 +0100 1.2 +++ b/src/rt.cc Wed Aug 04 04:51:06 2010 +0100 1.3 @@ -41,35 +41,13 @@ 1.4 }; 1.5 1.6 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg); 1.7 +static Face *create_face_buffer(Mesh **meshes, int num_meshes); 1.8 1.9 +static Face *faces; 1.10 static Ray *prim_rays; 1.11 static CLProgram *prog; 1.12 static int global_size; 1.13 1.14 -static Face faces[] = { 1.15 - {/* face0 */ 1.16 - { 1.17 - {{-1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 1.18 - {{0, 1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 1.19 - {{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} 1.20 - }, 1.21 - {0, 0, -1, 0}, 0, {0, 0, 0} 1.22 - }, 1.23 - {/* face1 */ 1.24 - { 1.25 - {{-5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 1.26 - {{0, 0, 3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 1.27 - {{5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} 1.28 - }, 1.29 - {0, 1, 0, 0}, 1, {0, 0, 0} 1.30 - } 1.31 -}; 1.32 - 1.33 -static Material matlib[] = { 1.34 - {{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0, 0}, 1.35 - {{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0, 0} 1.36 -}; 1.37 - 1.38 static Light lightlist[] = { 1.39 {{-10, 10, -20, 0}, {1, 1, 1, 1}} 1.40 }; 1.41 @@ -78,12 +56,12 @@ 1.42 static RendInfo rinf; 1.43 1.44 1.45 -bool init_renderer(int xsz, int ysz) 1.46 +bool init_renderer(int xsz, int ysz, Scene *scn) 1.47 { 1.48 // render info 1.49 rinf.xsz = xsz; 1.50 rinf.ysz = ysz; 1.51 - rinf.num_faces = sizeof faces / sizeof *faces; 1.52 + rinf.num_faces = scn->get_num_faces(); 1.53 rinf.num_lights = sizeof lightlist / sizeof *lightlist; 1.54 rinf.max_iter = 6; 1.55 1.56 @@ -102,11 +80,17 @@ 1.57 return false; 1.58 } 1.59 1.60 + /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size()); 1.61 + if(!faces) { 1.62 + fprintf(stderr, "failed to create face buffer\n"); 1.63 + return false; 1.64 + } 1.65 + 1.66 /* setup argument buffers */ 1.67 prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float)); 1.68 prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf); 1.69 - prog->set_arg_buffer(KARG_FACES, ARG_RD, sizeof faces, faces); 1.70 - prog->set_arg_buffer(KARG_MATLIB, ARG_RD, sizeof matlib, matlib); 1.71 + prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces, faces); 1.72 + prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->matlib.size() * sizeof(Material), &scn->matlib[0]); 1.73 prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist); 1.74 prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays); 1.75 prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float)); 1.76 @@ -125,18 +109,26 @@ 1.77 1.78 bool render() 1.79 { 1.80 + printf("Running kernel..."); 1.81 + fflush(stdout); 1.82 if(!prog->run(1, global_size)) { 1.83 return false; 1.84 } 1.85 + printf("done\n"); 1.86 1.87 - CLMemBuffer *mbuf = prog->get_arg_buffer(0); 1.88 + CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER); 1.89 void *fb = map_mem_buffer(mbuf, MAP_RD); 1.90 + if(!fb) { 1.91 + fprintf(stderr, "FAILED\n"); 1.92 + return false; 1.93 + } 1.94 + 1.95 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb); 1.96 unmap_mem_buffer(mbuf); 1.97 return true; 1.98 } 1.99 1.100 -void dbg_render_gl() 1.101 +void dbg_render_gl(Scene *scn) 1.102 { 1.103 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT); 1.104 1.105 @@ -149,8 +141,9 @@ 1.106 gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0); 1.107 1.108 glBegin(GL_TRIANGLES); 1.109 - for(int i=0; i<rinf.num_faces; i++) { 1.110 - Material *mat = matlib + faces[i].matid; 1.111 + int num_faces = scn->get_num_faces(); 1.112 + for(int i=0; i<num_faces; i++) { 1.113 + Material *mat = &scn->matlib[faces[i].matid]; 1.114 glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]); 1.115 1.116 for(int j=0; j<3; j++) { 1.117 @@ -158,6 +151,18 @@ 1.118 glVertex3f(pos[0], pos[1], pos[2]); 1.119 } 1.120 } 1.121 + 1.122 + /*for(size_t i=0; i<scn->meshes.size(); i++) { 1.123 + Material *mat = &scn->matlib[scn->meshes[i]->matid]; 1.124 + 1.125 + glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]); 1.126 + for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) { 1.127 + for(int k=0; k<3; k++) { 1.128 + float *pos = scn->meshes[i]->faces[j].v[k].pos; 1.129 + glVertex3f(pos[0], pos[1], pos[2]); 1.130 + } 1.131 + } 1.132 + }*/ 1.133 glEnd(); 1.134 1.135 glPopMatrix(); 1.136 @@ -172,20 +177,20 @@ 1.137 1.138 float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR); 1.139 memcpy(mem, matrix, 16 * sizeof *mem); 1.140 - printf("-- xform:\n"); 1.141 + /*printf("-- xform:\n"); 1.142 for(int i=0; i<16; i++) { 1.143 printf("%2.3f\t", mem[i]); 1.144 if(i % 4 == 3) putchar('\n'); 1.145 - } 1.146 + }*/ 1.147 unmap_mem_buffer(mbuf_xform); 1.148 1.149 mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR); 1.150 memcpy(mem, invtrans, 16 * sizeof *mem); 1.151 - printf("-- inverse-transpose:\n"); 1.152 + /*printf("-- inverse-transpose:\n"); 1.153 for(int i=0; i<16; i++) { 1.154 printf("%2.3f\t", mem[i]); 1.155 if(i % 4 == 3) putchar('\n'); 1.156 - } 1.157 + }*/ 1.158 unmap_mem_buffer(mbuf_invtrans); 1.159 } 1.160 1.161 @@ -208,3 +213,23 @@ 1.162 Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}}; 1.163 return ray; 1.164 } 1.165 + 1.166 +static Face *create_face_buffer(Mesh **meshes, int num_meshes) 1.167 +{ 1.168 + int num_faces = 0; 1.169 + for(int i=0; i<num_meshes; i++) { 1.170 + num_faces += meshes[i]->faces.size(); 1.171 + } 1.172 + printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes); 1.173 + 1.174 + Face *faces = new Face[num_faces]; 1.175 + memset(faces, 0, num_faces * sizeof *faces); 1.176 + Face *fptr = faces; 1.177 + 1.178 + for(int i=0; i<num_meshes; i++) { 1.179 + for(size_t j=0; j<meshes[i]->faces.size(); j++) { 1.180 + *fptr++ = meshes[i]->faces[j]; 1.181 + } 1.182 + } 1.183 + return faces; 1.184 +}