clray

diff src/rt.cc @ 12:85fd61f374d9

fixed the bloody intersection bug
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 03 Aug 2010 13:06:59 +0100
parents d9a1bab1c3f5
children 407935b73af3
line diff
     1.1 --- a/src/rt.cc	Sat Jul 31 22:23:57 2010 +0100
     1.2 +++ b/src/rt.cc	Tue Aug 03 13:06:59 2010 +0100
     1.3 @@ -2,36 +2,43 @@
     1.4  #include <string.h>
     1.5  #include <math.h>
     1.6  #include <assert.h>
     1.7 +
     1.8 +#ifndef __APPLE__
     1.9 +#include <GL/gl.h>
    1.10 +#include <GL/glu.h>
    1.11 +#else
    1.12 +#include <OpenGL/gl.h>
    1.13 +#include <OpenGL/glu.h>
    1.14 +#endif
    1.15 +
    1.16  #include "ocl.h"
    1.17  #include "mesh.h"
    1.18  
    1.19 -#ifdef __GNUC__
    1.20 -#define PACKED	__attribute__((packed))
    1.21 -#else
    1.22 -#define PACKED
    1.23 -#endif
    1.24 -
    1.25 -#ifdef _MSC_VER
    1.26 -#pragma push(pack, 1)
    1.27 -#endif
    1.28 +// kernel arguments
    1.29 +enum {
    1.30 +	KARG_FRAMEBUFFER,
    1.31 +	KARG_RENDER_INFO,
    1.32 +	KARG_FACES,
    1.33 +	KARG_MATLIB,
    1.34 +	KARG_LIGHTS,
    1.35 +	KARG_PRIM_RAYS,
    1.36 +	KARG_XFORM,
    1.37 +	KARG_INVTRANS_XFORM
    1.38 +};
    1.39  
    1.40  struct RendInfo {
    1.41  	int xsz, ysz;
    1.42  	int num_faces, num_lights;
    1.43  	int max_iter;
    1.44 -} PACKED;
    1.45 +};
    1.46  
    1.47  struct Ray {
    1.48  	float origin[4], dir[4];
    1.49 -} PACKED;
    1.50 +};
    1.51  
    1.52  struct Light {
    1.53  	float pos[4], color[4];
    1.54 -} PACKED;
    1.55 -
    1.56 -#ifdef _MSC_VER
    1.57 -#pragma pop(pack)
    1.58 -#endif
    1.59 +};
    1.60  
    1.61  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
    1.62  
    1.63 @@ -42,39 +49,36 @@
    1.64  static Face faces[] = {
    1.65  	{/* face0 */
    1.66  		{
    1.67 -			{{-1, 0, 0, 1}, {0, 0, -1, 1}, {0, 0}},
    1.68 -			{{0, 1, 0, 1}, {0, 0, -1, 1}, {0, 0}},
    1.69 -			{{1, 0, 0, 1}, {0, 0, -1, 1}, {0, 0}}
    1.70 +			{{-1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    1.71 +			{{0, 1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    1.72 +			{{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
    1.73  		},
    1.74 -		{0, 0, -1, 1}, 0
    1.75 +		{0, 0, -1, 0}, 0, {0, 0, 0}
    1.76  	},
    1.77  	{/* face1 */
    1.78  		{
    1.79 -			{{-5, 0, -3, 1}, {0, 0, -1, 1}, {0, 0}},
    1.80 -			{{0, 0, 3, 1}, {0, 0, -1, 1}, {0, 0}},
    1.81 -			{{5, 0, -3, 1}, {0, 0, -1, 1}, {0, 0}}
    1.82 +			{{-5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    1.83 +			{{0, 0, 3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    1.84 +			{{5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
    1.85  		},
    1.86 -		{0, 0, -1, 1}, 1
    1.87 +		{0, 1, 0, 0}, 1, {0, 0, 0}
    1.88  	}
    1.89  };
    1.90  
    1.91  static Material matlib[] = {
    1.92 -	{{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0},
    1.93 -	{{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0}
    1.94 +	{{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0, 0},
    1.95 +	{{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0, 0}
    1.96  };
    1.97  
    1.98  static Light lightlist[] = {
    1.99 -	{{-10, 10, -20, 1}, {1, 1, 1, 1}}
   1.100 +	{{-10, 10, -20, 0}, {1, 1, 1, 1}}
   1.101  };
   1.102  
   1.103 -static float xform[16] = {
   1.104 -	1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
   1.105 -};
   1.106  
   1.107  static RendInfo rinf;
   1.108  
   1.109  
   1.110 -bool init_renderer(int xsz, int ysz, float *fb)
   1.111 +bool init_renderer(int xsz, int ysz)
   1.112  {
   1.113  	// render info
   1.114  	rinf.xsz = xsz;
   1.115 @@ -99,13 +103,16 @@
   1.116  	}
   1.117  
   1.118  	/* setup argument buffers */
   1.119 -	prog->set_arg_buffer(0, ARG_WR, xsz * ysz * 4 * sizeof(float), fb);
   1.120 -	prog->set_arg_buffer(1, ARG_RD, sizeof rinf, &rinf);
   1.121 -	prog->set_arg_buffer(2, ARG_RD, sizeof faces, faces);
   1.122 -	prog->set_arg_buffer(3, ARG_RD, sizeof matlib, matlib);
   1.123 -	prog->set_arg_buffer(4, ARG_RD, sizeof lightlist, lightlist);
   1.124 -	prog->set_arg_buffer(5, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
   1.125 -	prog->set_arg_buffer(6, ARG_RD, sizeof xform, &xform);
   1.126 +	prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
   1.127 +	prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
   1.128 +	prog->set_arg_buffer(KARG_FACES, ARG_RD, sizeof faces, faces);
   1.129 +	prog->set_arg_buffer(KARG_MATLIB, ARG_RD, sizeof matlib, matlib);
   1.130 +	prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
   1.131 +	prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
   1.132 +	prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
   1.133 +	prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
   1.134 +
   1.135 +	delete [] prim_rays;
   1.136  
   1.137  	global_size = xsz * ysz;
   1.138  	return true;
   1.139 @@ -113,7 +120,6 @@
   1.140  
   1.141  void destroy_renderer()
   1.142  {
   1.143 -	delete [] prim_rays;
   1.144  	delete prog;
   1.145  }
   1.146  
   1.147 @@ -124,19 +130,63 @@
   1.148  	}
   1.149  
   1.150  	CLMemBuffer *mbuf = prog->get_arg_buffer(0);
   1.151 -	map_mem_buffer(mbuf, MAP_RD);
   1.152 +	void *fb = map_mem_buffer(mbuf, MAP_RD);
   1.153 +	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
   1.154  	unmap_mem_buffer(mbuf);
   1.155  	return true;
   1.156  }
   1.157  
   1.158 -void set_xform(float *matrix)
   1.159 +void dbg_render_gl()
   1.160  {
   1.161 -	CLMemBuffer *mbuf = prog->get_arg_buffer(6);
   1.162 -	assert(mbuf);
   1.163 +	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
   1.164  
   1.165 -	assert(map_mem_buffer(mbuf, MAP_WR) == xform);
   1.166 -	memcpy(xform, matrix, sizeof xform);
   1.167 -	unmap_mem_buffer(mbuf);
   1.168 +	glDisable(GL_TEXTURE_2D);
   1.169 +	glEnable(GL_DEPTH_TEST);
   1.170 +
   1.171 +	glMatrixMode(GL_PROJECTION);
   1.172 +	glPushMatrix();
   1.173 +	glLoadIdentity();
   1.174 +	gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
   1.175 +
   1.176 +	glBegin(GL_TRIANGLES);
   1.177 +	for(int i=0; i<rinf.num_faces; i++) {
   1.178 +		Material *mat = matlib + faces[i].matid;
   1.179 +		glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
   1.180 +
   1.181 +		for(int j=0; j<3; j++) {
   1.182 +			float *pos = faces[i].v[j].pos;
   1.183 +			glVertex3f(pos[0], pos[1], pos[2]);
   1.184 +		}
   1.185 +	}
   1.186 +	glEnd();
   1.187 +
   1.188 +	glPopMatrix();
   1.189 +	glPopAttrib();
   1.190 +}
   1.191 +
   1.192 +void set_xform(float *matrix, float *invtrans)
   1.193 +{
   1.194 +	CLMemBuffer *mbuf_xform = prog->get_arg_buffer(KARG_XFORM);
   1.195 +	CLMemBuffer *mbuf_invtrans = prog->get_arg_buffer(KARG_INVTRANS_XFORM);
   1.196 +	assert(mbuf_xform && mbuf_invtrans);
   1.197 +
   1.198 +	float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
   1.199 +	memcpy(mem, matrix, 16 * sizeof *mem);
   1.200 +	printf("-- xform:\n");
   1.201 +	for(int i=0; i<16; i++) {
   1.202 +		printf("%2.3f\t", mem[i]);
   1.203 +		if(i % 4 == 3) putchar('\n');
   1.204 +	}
   1.205 +	unmap_mem_buffer(mbuf_xform);
   1.206 +
   1.207 +	mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
   1.208 +	memcpy(mem, invtrans, 16 * sizeof *mem);
   1.209 +	printf("-- inverse-transpose:\n");
   1.210 +	for(int i=0; i<16; i++) {
   1.211 +		printf("%2.3f\t", mem[i]);
   1.212 +		if(i % 4 == 3) putchar('\n');
   1.213 +	}
   1.214 +	unmap_mem_buffer(mbuf_invtrans);
   1.215  }
   1.216  
   1.217  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
   1.218 @@ -155,6 +205,6 @@
   1.219  	py *= 100.0;
   1.220  	pz *= 100.0;
   1.221  
   1.222 -	Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}};
   1.223 +	Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
   1.224  	return ray;
   1.225  }