clray

changeset 13:407935b73af3

bollocks
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Aug 2010 04:51:06 +0100
parents 85fd61f374d9
children 29f9330cfa4b
files Makefile src/clray.cc src/matrix.cc src/matrix.h src/mesh.cc src/mesh.h src/ocl.cc src/rt.cc src/rt.h
diffstat 9 files changed, 325 insertions(+), 52 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Tue Aug 03 13:06:59 2010 +0100
     1.2 +++ b/Makefile	Wed Aug 04 04:51:06 2010 +0100
     1.3 @@ -5,7 +5,7 @@
     1.4  
     1.5  CXX = g++
     1.6  CXXFLAGS = -pedantic -Wall -g
     1.7 -LDFLAGS = $(libgl) $(libcl)
     1.8 +LDFLAGS = $(libgl) $(libcl) -lpthread
     1.9  
    1.10  ifeq ($(shell uname -s), Darwin)
    1.11  	libgl = -framework OpenGL -framework GLUT
     2.1 --- a/src/clray.cc	Tue Aug 03 13:06:59 2010 +0100
     2.2 +++ b/src/clray.cc	Wed Aug 04 04:51:06 2010 +0100
     2.3 @@ -9,6 +9,7 @@
     2.4  #endif
     2.5  #include "rt.h"
     2.6  #include "matrix.h"
     2.7 +#include "mesh.h"
     2.8  
     2.9  void cleanup();
    2.10  void disp();
    2.11 @@ -24,12 +25,33 @@
    2.12  static float cam_theta, cam_phi = 25.0;
    2.13  static float cam_dist = 10.0;
    2.14  
    2.15 -static bool dbg_glrender;
    2.16 +static bool dbg_glrender = true;
    2.17 +
    2.18 +static Scene scn;
    2.19  
    2.20  int main(int argc, char **argv)
    2.21  {
    2.22  	glutInitWindowSize(800, 600);
    2.23  	glutInit(&argc, argv);
    2.24 +
    2.25 +	int loaded = 0;
    2.26 +	for(int i=1; i<argc; i++) {
    2.27 +		if(!scn.load(argv[i])) {
    2.28 +			fprintf(stderr, "failed to load scene: %s\n", argv[i]);
    2.29 +			return false;
    2.30 +		}
    2.31 +		loaded++;
    2.32 +	}
    2.33 +
    2.34 +	if(!loaded) {
    2.35 +		fprintf(stderr, "you must specify a scene file to load\n");
    2.36 +		return false;
    2.37 +	}
    2.38 +	if(!scn.get_num_faces()) {
    2.39 +		fprintf(stderr, "didn't load any polygons\n");
    2.40 +		return false;
    2.41 +	}
    2.42 +
    2.43  	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    2.44  	glutCreateWindow("OpenCL Raytracer");
    2.45  
    2.46 @@ -42,7 +64,7 @@
    2.47  	glutMouseFunc(mouse);
    2.48  	glutMotionFunc(motion);
    2.49  
    2.50 -	if(!init_renderer(xsz, ysz)) {
    2.51 +	if(!init_renderer(xsz, ysz, &scn)) {
    2.52  		return 1;
    2.53  	}
    2.54  	atexit(cleanup);
    2.55 @@ -92,16 +114,18 @@
    2.56  		set_xform(mat.m, inv_trans.m);
    2.57  		glPopMatrix();
    2.58  
    2.59 -		if(!render()) {
    2.60 -			exit(1);
    2.61 +		if(!dbg_glrender) {
    2.62 +			if(!render()) {
    2.63 +				exit(1);
    2.64 +			}
    2.65 +			need_update = false;
    2.66  		}
    2.67 -		need_update = false;
    2.68  	}
    2.69  
    2.70  	if(dbg_glrender) {
    2.71  		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2.72  		glLoadMatrixf(inv_mat.m);
    2.73 -		dbg_render_gl();
    2.74 +		dbg_render_gl(&scn);
    2.75  	} else {
    2.76  		glEnable(GL_TEXTURE_2D);
    2.77  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/matrix.cc	Wed Aug 04 04:51:06 2010 +0100
     3.3 @@ -0,0 +1,155 @@
     3.4 +#include <string.h>
     3.5 +#include "matrix.h"
     3.6 +
     3.7 +#define M(x, y)	((y) * 4 + (x))
     3.8 +
     3.9 +Matrix4x4::Matrix4x4()
    3.10 +{
    3.11 +	memset(m, 0, sizeof m);
    3.12 +	m[0] = m[5] = m[10] = m[15] = 1.0;
    3.13 +}
    3.14 +
    3.15 +Matrix4x4::Matrix4x4(const float *mat)
    3.16 +{
    3.17 +	memcpy(m, mat, sizeof m);
    3.18 +}
    3.19 +
    3.20 +Matrix4x4::Matrix4x4(float m00, float m01, float m02, float m03,
    3.21 +		float m10, float m11, float m12, float m13,
    3.22 +		float m20, float m21, float m22, float m23,
    3.23 +		float m30, float m31, float m32, float m33)
    3.24 +{
    3.25 +	m[M(0, 0)] = m00; m[M(0, 1)] = m01; m[M(0, 2)] = m02; m[M(0, 3)] = m03;
    3.26 +	m[M(1, 0)] = m10; m[M(1, 1)] = m11; m[M(1, 2)] = m12; m[M(1, 3)] = m13;
    3.27 +	m[M(2, 0)] = m20; m[M(2, 1)] = m21; m[M(2, 2)] = m22; m[M(2, 3)] = m23;
    3.28 +	m[M(3, 0)] = m30; m[M(3, 1)] = m31; m[M(3, 2)] = m32; m[M(3, 3)] = m33;
    3.29 +}
    3.30 +
    3.31 +Matrix4x4::Matrix4x4(const Matrix4x4 &mat)
    3.32 +{
    3.33 +	memcpy(m, mat.m, sizeof m);
    3.34 +}
    3.35 +
    3.36 +Matrix4x4 &Matrix4x4::operator =(const Matrix4x4 &mat)
    3.37 +{
    3.38 +	memcpy(m, mat.m, sizeof m);
    3.39 +	return *this;
    3.40 +}
    3.41 +
    3.42 +void Matrix4x4::identity()
    3.43 +{
    3.44 +	memset(m, 0, sizeof m);
    3.45 +	m[0] = m[5] = m[10] = m[15] = 1.0;
    3.46 +}
    3.47 +
    3.48 +float Matrix4x4::determinant() const
    3.49 +{
    3.50 +	float det11 =	(m[M(1, 1)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.51 +					(m[M(1, 2)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) +
    3.52 +					(m[M(1, 3)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)]));
    3.53 +
    3.54 +	float det12 =	(m[M(1, 0)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.55 +					(m[M(1, 2)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.56 +					(m[M(1, 3)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)]));
    3.57 +
    3.58 +	float det13 =	(m[M(1, 0)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) -
    3.59 +					(m[M(1, 1)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.60 +					(m[M(1, 3)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.61 +
    3.62 +	float det14 =	(m[M(1, 0)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)])) -
    3.63 +					(m[M(1, 1)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)])) +
    3.64 +					(m[M(1, 2)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.65 +
    3.66 +	return m[M(0, 0)] * det11 - m[M(0, 1)] * det12 + m[M(0, 2)] * det13 - m[M(0, 3)] * det14;
    3.67 +}
    3.68 +
    3.69 +Matrix4x4 Matrix4x4::adjoint() const
    3.70 +{
    3.71 +	Matrix4x4 coef;
    3.72 +
    3.73 +	coef.m[M(0, 0)] =	(m[M(1, 1)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.74 +						(m[M(1, 2)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) +
    3.75 +						(m[M(1, 3)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)]));
    3.76 +	coef.m[M(0, 1)] =	(m[M(1, 0)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.77 +						(m[M(1, 2)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.78 +						(m[M(1, 3)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)]));
    3.79 +	coef.m[M(0, 2)] =	(m[M(1, 0)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) -
    3.80 +						(m[M(1, 1)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.81 +						(m[M(1, 3)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.82 +	coef.m[M(0, 3)] =	(m[M(1, 0)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)])) -
    3.83 +						(m[M(1, 1)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)])) +
    3.84 +						(m[M(1, 2)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.85 +
    3.86 +	coef.m[M(1, 0)] =	(m[M(0, 1)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.87 +						(m[M(0, 2)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) +
    3.88 +						(m[M(0, 3)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)]));
    3.89 +	coef.m[M(1, 1)] =	(m[M(0, 0)] * (m[M(2, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(2, 3)])) -
    3.90 +						(m[M(0, 2)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.91 +						(m[M(0, 3)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)]));
    3.92 +	coef.m[M(1, 2)] =	(m[M(0, 0)] * (m[M(2, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(2, 3)])) -
    3.93 +						(m[M(0, 1)] * (m[M(2, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(2, 3)])) +
    3.94 +						(m[M(0, 3)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.95 +	coef.m[M(1, 3)] =	(m[M(0, 0)] * (m[M(2, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(2, 2)])) -
    3.96 +						(m[M(0, 1)] * (m[M(2, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(2, 2)])) +
    3.97 +						(m[M(0, 2)] * (m[M(2, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(2, 1)]));
    3.98 +
    3.99 +	coef.m[M(2, 0)] =	(m[M(0, 1)] * (m[M(1, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(1, 3)])) -
   3.100 +						(m[M(0, 2)] * (m[M(1, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(1, 3)])) +
   3.101 +						(m[M(0, 3)] * (m[M(1, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(1, 2)]));
   3.102 +	coef.m[M(2, 1)] =	(m[M(0, 0)] * (m[M(1, 2)] * m[M(3, 3)] - m[M(3, 2)] * m[M(1, 3)])) -
   3.103 +						(m[M(0, 2)] * (m[M(1, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(1, 3)])) +
   3.104 +						(m[M(0, 3)] * (m[M(1, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(1, 2)]));
   3.105 +	coef.m[M(2, 2)] =	(m[M(0, 0)] * (m[M(1, 1)] * m[M(3, 3)] - m[M(3, 1)] * m[M(1, 3)])) -
   3.106 +						(m[M(0, 1)] * (m[M(1, 0)] * m[M(3, 3)] - m[M(3, 0)] * m[M(1, 3)])) +
   3.107 +						(m[M(0, 3)] * (m[M(1, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(1, 1)]));
   3.108 +	coef.m[M(2, 3)] =	(m[M(0, 0)] * (m[M(1, 1)] * m[M(3, 2)] - m[M(3, 1)] * m[M(1, 2)])) -
   3.109 +						(m[M(0, 1)] * (m[M(1, 0)] * m[M(3, 2)] - m[M(3, 0)] * m[M(1, 2)])) +
   3.110 +						(m[M(0, 2)] * (m[M(1, 0)] * m[M(3, 1)] - m[M(3, 0)] * m[M(1, 1)]));
   3.111 +
   3.112 +	coef.m[M(3, 0)] =	(m[M(0, 1)] * (m[M(1, 2)] * m[M(2, 3)] - m[M(2, 2)] * m[M(1, 3)])) -
   3.113 +						(m[M(0, 2)] * (m[M(1, 1)] * m[M(2, 3)] - m[M(2, 1)] * m[M(1, 3)])) +
   3.114 +						(m[M(0, 3)] * (m[M(1, 1)] * m[M(2, 2)] - m[M(2, 1)] * m[M(1, 2)]));
   3.115 +	coef.m[M(3, 1)] =	(m[M(0, 0)] * (m[M(1, 2)] * m[M(2, 3)] - m[M(2, 2)] * m[M(1, 3)])) -
   3.116 +						(m[M(0, 2)] * (m[M(1, 0)] * m[M(2, 3)] - m[M(2, 0)] * m[M(1, 3)])) +
   3.117 +						(m[M(0, 3)] * (m[M(1, 0)] * m[M(2, 2)] - m[M(2, 0)] * m[M(1, 2)]));
   3.118 +	coef.m[M(3, 2)] =	(m[M(0, 0)] * (m[M(1, 1)] * m[M(2, 3)] - m[M(2, 1)] * m[M(1, 3)])) -
   3.119 +						(m[M(0, 1)] * (m[M(1, 0)] * m[M(2, 3)] - m[M(2, 0)] * m[M(1, 3)])) +
   3.120 +						(m[M(0, 3)] * (m[M(1, 0)] * m[M(2, 1)] - m[M(2, 0)] * m[M(1, 1)]));
   3.121 +	coef.m[M(3, 3)] =	(m[M(0, 0)] * (m[M(1, 1)] * m[M(2, 2)] - m[M(2, 1)] * m[M(1, 2)])) -
   3.122 +						(m[M(0, 1)] * (m[M(1, 0)] * m[M(2, 2)] - m[M(2, 0)] * m[M(1, 2)])) +
   3.123 +						(m[M(0, 2)] * (m[M(1, 0)] * m[M(2, 1)] - m[M(2, 0)] * m[M(1, 1)]));
   3.124 +
   3.125 +	coef.transpose();
   3.126 +
   3.127 +	for(int i=0; i<4; i++) {
   3.128 +		for(int j=0; j<4; j++) {
   3.129 +			coef.m[M(i, j)] = j % 2 ? -coef.m[M(i, j)] : coef.m[M(i, j)];
   3.130 +			if(i % 2) coef.m[M(i, j)] = -coef.m[M(i, j)];
   3.131 +		}
   3.132 +	}
   3.133 +
   3.134 +	return coef;
   3.135 +}
   3.136 +
   3.137 +void Matrix4x4::invert()
   3.138 +{
   3.139 +	Matrix4x4 adj = adjoint();
   3.140 +
   3.141 +	float det = determinant();
   3.142 +
   3.143 +	for(int i=0; i<16; i++) {
   3.144 +		m[i] = adj.m[i] / det;
   3.145 +	}
   3.146 +}
   3.147 +
   3.148 +void Matrix4x4::transpose()
   3.149 +{
   3.150 +	float tmp[16];
   3.151 +
   3.152 +	memcpy(tmp, m, sizeof tmp);
   3.153 +	for(int i=0; i<4; i++) {
   3.154 +		for(int j=0; j<4; j++) {
   3.155 +			m[M(i, j)] = tmp[M(j, i)];
   3.156 +		}
   3.157 +	}
   3.158 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/matrix.h	Wed Aug 04 04:51:06 2010 +0100
     4.3 @@ -0,0 +1,30 @@
     4.4 +#ifndef MATRIX_H_
     4.5 +#define MATRIX_H_
     4.6 +
     4.7 +class Matrix4x4 {
     4.8 +public:
     4.9 +	float m[16];
    4.10 +
    4.11 +	Matrix4x4();
    4.12 +	Matrix4x4(const float *mat);
    4.13 +	Matrix4x4(float m00, float m01, float m02, float m03,
    4.14 +			float m10, float m11, float m12, float m13,
    4.15 +			float m20, float m21, float m22, float m23,
    4.16 +			float m30, float m31, float m32, float m33);
    4.17 +	Matrix4x4(const Matrix4x4 &mat);
    4.18 +	Matrix4x4 &operator =(const Matrix4x4 &mat);
    4.19 +
    4.20 +	void identity();
    4.21 +
    4.22 +	float determinant() const;
    4.23 +	Matrix4x4 adjoint() const;
    4.24 +	void invert();
    4.25 +	void transpose();
    4.26 +
    4.27 +
    4.28 +	float *operator [](int idx);
    4.29 +	const float *operator [](int idx) const;
    4.30 +};
    4.31 +
    4.32 +
    4.33 +#endif	/* MATRIX_H_ */
     5.1 --- a/src/mesh.cc	Tue Aug 03 13:06:59 2010 +0100
     5.2 +++ b/src/mesh.cc	Wed Aug 04 04:51:06 2010 +0100
     5.3 @@ -115,6 +115,26 @@
     5.4  
     5.5  static map<string, int> matnames;
     5.6  
     5.7 +bool Scene::add_mesh(Mesh *m)
     5.8 +{
     5.9 +	// make sure triangles have material ids
    5.10 +	for(size_t i=0; i<m->faces.size(); i++) {
    5.11 +		m->faces[i].matid = m->matid;
    5.12 +	}
    5.13 +	meshes.push_back(m);
    5.14 +	return true;
    5.15 +}
    5.16 +
    5.17 +int Scene::get_num_faces() const
    5.18 +{
    5.19 +	int num_faces = 0;
    5.20 +	for(size_t i=0; i<meshes.size(); i++) {
    5.21 +		num_faces += meshes[i]->faces.size();
    5.22 +	}
    5.23 +	printf("get_num_faces() = %d\n", num_faces);
    5.24 +	return num_faces;
    5.25 +}
    5.26 +
    5.27  
    5.28  #define INVALID_IDX		INT_MIN
    5.29  
    5.30 @@ -200,7 +220,7 @@
    5.31  			if(!obj.f.empty()) {
    5.32  				Mesh *mesh = cons_mesh(&obj);
    5.33  				mesh->matid = matnames[obj.cur_mat];
    5.34 -				meshes.push_back(mesh);
    5.35 +				add_mesh(mesh);
    5.36  				obj_added++;
    5.37  
    5.38  				obj.f.clear();	// clean the face list
    5.39 @@ -252,6 +272,7 @@
    5.40  					mat.kr = 0.0;	// TODO
    5.41  					mat.spow = vmtl[i].shininess;
    5.42  
    5.43 +					matlib.push_back(mat);
    5.44  					matnames[vmtl[i].name] = i;
    5.45  				}
    5.46  			}
    5.47 @@ -309,7 +330,7 @@
    5.48  	if(!obj.f.empty()) {
    5.49  		Mesh *mesh = cons_mesh(&obj);
    5.50  		mesh->matid = matnames[obj.cur_mat];
    5.51 -		meshes.push_back(mesh);
    5.52 +		add_mesh(mesh);
    5.53  		obj_added++;
    5.54  	}
    5.55  
    5.56 @@ -342,16 +363,24 @@
    5.57  			face.v[j].pos[0] = obj->v[f->v[j]].x;
    5.58  			face.v[j].pos[1] = obj->v[f->v[j]].y;
    5.59  			face.v[j].pos[2] = obj->v[f->v[j]].z;
    5.60 +			face.v[j].pos[3] = 0.0;
    5.61  
    5.62  			int nidx = f->n[j] < 0 ? 0 : f->n[j];
    5.63  			face.v[j].normal[0] = obj->vn[nidx].x;
    5.64  			face.v[j].normal[1] = obj->vn[nidx].y;
    5.65  			face.v[j].normal[2] = obj->vn[nidx].z;
    5.66 +			face.v[j].normal[3] = 0.0;
    5.67  
    5.68  			int tidx = f->t[j] < 0 ? 0 : f->t[j];
    5.69  			face.v[j].tex[0] = obj->vt[tidx].x;
    5.70  			face.v[j].tex[1] = obj->vt[tidx].y;
    5.71  		}
    5.72 +
    5.73 +		face.normal[0] = face.v[0].normal[0];
    5.74 +		face.normal[1] = face.v[1].normal[1];
    5.75 +		face.normal[2] = face.v[2].normal[2];
    5.76 +		face.normal[3] = 0.0;
    5.77 +
    5.78  		mesh->faces.push_back(face);
    5.79  	}
    5.80  
    5.81 @@ -390,6 +419,7 @@
    5.82  		case CMD_NEWMTL:
    5.83  			// add the previous material, and start a new one
    5.84  			if(mat.name.length() > 0) {
    5.85 +				printf("Adding material: %s\n", mat.name.c_str());
    5.86  				vmtl->push_back(mat);
    5.87  				mat.reset();
    5.88  			}
    5.89 @@ -442,6 +472,7 @@
    5.90  	}
    5.91  
    5.92  	if(mat.name.length() > 0) {
    5.93 +		printf("Adding material: %s\n", mat.name.c_str());
    5.94  		vmtl->push_back(mat);
    5.95  	}
    5.96  	return true;
    5.97 @@ -603,9 +634,10 @@
    5.98  			end++;
    5.99  		}
   5.100  
   5.101 -		int sz = end - beg + 1;
   5.102 -		char *pathname = (char*)alloca(sz + fnamelen + 2);
   5.103 -		memcpy(pathname, beg, sz);
   5.104 +		int res_len = end - beg;
   5.105 +		char *pathname = (char*)alloca(res_len + fnamelen + 2);
   5.106 +		memcpy(pathname, beg, res_len);
   5.107 +		pathname[res_len] = 0;
   5.108  		strcat(pathname, "/");
   5.109  		strcat(pathname, fname);
   5.110  
   5.111 @@ -615,7 +647,7 @@
   5.112  			return true;
   5.113  		}
   5.114  
   5.115 -		beg += sz;
   5.116 +		beg += res_len;
   5.117  	}
   5.118  	return false;
   5.119  }
   5.120 @@ -630,7 +662,7 @@
   5.121  		strncpy(buf, str, PATH_MAX);
   5.122  		char *ptr = strrchr(buf, '/');
   5.123  
   5.124 -		if(*ptr) *ptr = 0;
   5.125 +		if(ptr && *ptr) *ptr = 0;
   5.126  	}
   5.127  	return buf;
   5.128  }
     6.1 --- a/src/mesh.h	Tue Aug 03 13:06:59 2010 +0100
     6.2 +++ b/src/mesh.h	Wed Aug 04 04:51:06 2010 +0100
     6.3 @@ -34,6 +34,10 @@
     6.4  	std::vector<Mesh*> meshes;
     6.5  	std::vector<Material> matlib;
     6.6  
     6.7 +	bool add_mesh(Mesh *m);
     6.8 +
     6.9 +	int get_num_faces() const;
    6.10 +
    6.11  	bool load(const char *fname);
    6.12  	bool load(FILE *fp);
    6.13  };
     7.1 --- a/src/ocl.cc	Tue Aug 03 13:06:59 2010 +0100
     7.2 +++ b/src/ocl.cc	Wed Aug 04 04:51:06 2010 +0100
     7.3 @@ -239,6 +239,7 @@
     7.4  
     7.5  bool CLProgram::set_arg_buffer(int idx, int rdwr, size_t sz, void *ptr)
     7.6  {
     7.7 +	printf("create argument %d buffer: %d bytes\n", idx, (int)sz);
     7.8  	CLMemBuffer *buf;
     7.9  
    7.10  	if(!(buf = create_mem_buffer(rdwr, sz, ptr))) {
     8.1 --- a/src/rt.cc	Tue Aug 03 13:06:59 2010 +0100
     8.2 +++ b/src/rt.cc	Wed Aug 04 04:51:06 2010 +0100
     8.3 @@ -41,35 +41,13 @@
     8.4  };
     8.5  
     8.6  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
     8.7 +static Face *create_face_buffer(Mesh **meshes, int num_meshes);
     8.8  
     8.9 +static Face *faces;
    8.10  static Ray *prim_rays;
    8.11  static CLProgram *prog;
    8.12  static int global_size;
    8.13  
    8.14 -static Face faces[] = {
    8.15 -	{/* face0 */
    8.16 -		{
    8.17 -			{{-1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    8.18 -			{{0, 1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    8.19 -			{{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
    8.20 -		},
    8.21 -		{0, 0, -1, 0}, 0, {0, 0, 0}
    8.22 -	},
    8.23 -	{/* face1 */
    8.24 -		{
    8.25 -			{{-5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    8.26 -			{{0, 0, 3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
    8.27 -			{{5, 0, -3, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
    8.28 -		},
    8.29 -		{0, 1, 0, 0}, 1, {0, 0, 0}
    8.30 -	}
    8.31 -};
    8.32 -
    8.33 -static Material matlib[] = {
    8.34 -	{{1, 0, 0, 1}, {1, 1, 1, 1}, 0, 0, 60.0, 0},
    8.35 -	{{0.2, 0.8, 0.3, 1}, {0, 0, 0, 0}, 0, 0, 0, 0}
    8.36 -};
    8.37 -
    8.38  static Light lightlist[] = {
    8.39  	{{-10, 10, -20, 0}, {1, 1, 1, 1}}
    8.40  };
    8.41 @@ -78,12 +56,12 @@
    8.42  static RendInfo rinf;
    8.43  
    8.44  
    8.45 -bool init_renderer(int xsz, int ysz)
    8.46 +bool init_renderer(int xsz, int ysz, Scene *scn)
    8.47  {
    8.48  	// render info
    8.49  	rinf.xsz = xsz;
    8.50  	rinf.ysz = ysz;
    8.51 -	rinf.num_faces = sizeof faces / sizeof *faces;
    8.52 +	rinf.num_faces = scn->get_num_faces();
    8.53  	rinf.num_lights = sizeof lightlist / sizeof *lightlist;
    8.54  	rinf.max_iter = 6;
    8.55  
    8.56 @@ -102,11 +80,17 @@
    8.57  		return false;
    8.58  	}
    8.59  
    8.60 +	/*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
    8.61 +	if(!faces) {
    8.62 +		fprintf(stderr, "failed to create face buffer\n");
    8.63 +		return false;
    8.64 +	}
    8.65 +
    8.66  	/* setup argument buffers */
    8.67  	prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
    8.68  	prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
    8.69 -	prog->set_arg_buffer(KARG_FACES, ARG_RD, sizeof faces, faces);
    8.70 -	prog->set_arg_buffer(KARG_MATLIB, ARG_RD, sizeof matlib, matlib);
    8.71 +	prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces, faces);
    8.72 +	prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->matlib.size() * sizeof(Material), &scn->matlib[0]);
    8.73  	prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
    8.74  	prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
    8.75  	prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
    8.76 @@ -125,18 +109,26 @@
    8.77  
    8.78  bool render()
    8.79  {
    8.80 +	printf("Running kernel...");
    8.81 +	fflush(stdout);
    8.82  	if(!prog->run(1, global_size)) {
    8.83  		return false;
    8.84  	}
    8.85 +	printf("done\n");
    8.86  
    8.87 -	CLMemBuffer *mbuf = prog->get_arg_buffer(0);
    8.88 +	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
    8.89  	void *fb = map_mem_buffer(mbuf, MAP_RD);
    8.90 +	if(!fb) {
    8.91 +		fprintf(stderr, "FAILED\n");
    8.92 +		return false;
    8.93 +	}
    8.94 +
    8.95  	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rinf.xsz, rinf.ysz, GL_RGBA, GL_FLOAT, fb);
    8.96  	unmap_mem_buffer(mbuf);
    8.97  	return true;
    8.98  }
    8.99  
   8.100 -void dbg_render_gl()
   8.101 +void dbg_render_gl(Scene *scn)
   8.102  {
   8.103  	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
   8.104  
   8.105 @@ -149,8 +141,9 @@
   8.106  	gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
   8.107  
   8.108  	glBegin(GL_TRIANGLES);
   8.109 -	for(int i=0; i<rinf.num_faces; i++) {
   8.110 -		Material *mat = matlib + faces[i].matid;
   8.111 +	int num_faces = scn->get_num_faces();
   8.112 +	for(int i=0; i<num_faces; i++) {
   8.113 +		Material *mat = &scn->matlib[faces[i].matid];
   8.114  		glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
   8.115  
   8.116  		for(int j=0; j<3; j++) {
   8.117 @@ -158,6 +151,18 @@
   8.118  			glVertex3f(pos[0], pos[1], pos[2]);
   8.119  		}
   8.120  	}
   8.121 +
   8.122 +	/*for(size_t i=0; i<scn->meshes.size(); i++) {
   8.123 +		Material *mat = &scn->matlib[scn->meshes[i]->matid];
   8.124 +
   8.125 +		glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
   8.126 +		for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
   8.127 +			for(int k=0; k<3; k++) {
   8.128 +				float *pos = scn->meshes[i]->faces[j].v[k].pos;
   8.129 +				glVertex3f(pos[0], pos[1], pos[2]);
   8.130 +			}
   8.131 +		}
   8.132 +	}*/
   8.133  	glEnd();
   8.134  
   8.135  	glPopMatrix();
   8.136 @@ -172,20 +177,20 @@
   8.137  
   8.138  	float *mem = (float*)map_mem_buffer(mbuf_xform, MAP_WR);
   8.139  	memcpy(mem, matrix, 16 * sizeof *mem);
   8.140 -	printf("-- xform:\n");
   8.141 +	/*printf("-- xform:\n");
   8.142  	for(int i=0; i<16; i++) {
   8.143  		printf("%2.3f\t", mem[i]);
   8.144  		if(i % 4 == 3) putchar('\n');
   8.145 -	}
   8.146 +	}*/
   8.147  	unmap_mem_buffer(mbuf_xform);
   8.148  
   8.149  	mem = (float*)map_mem_buffer(mbuf_invtrans, MAP_WR);
   8.150  	memcpy(mem, invtrans, 16 * sizeof *mem);
   8.151 -	printf("-- inverse-transpose:\n");
   8.152 +	/*printf("-- inverse-transpose:\n");
   8.153  	for(int i=0; i<16; i++) {
   8.154  		printf("%2.3f\t", mem[i]);
   8.155  		if(i % 4 == 3) putchar('\n');
   8.156 -	}
   8.157 +	}*/
   8.158  	unmap_mem_buffer(mbuf_invtrans);
   8.159  }
   8.160  
   8.161 @@ -208,3 +213,23 @@
   8.162  	Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
   8.163  	return ray;
   8.164  }
   8.165 +
   8.166 +static Face *create_face_buffer(Mesh **meshes, int num_meshes)
   8.167 +{
   8.168 +	int num_faces = 0;
   8.169 +	for(int i=0; i<num_meshes; i++) {
   8.170 +		num_faces += meshes[i]->faces.size();
   8.171 +	}
   8.172 +	printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
   8.173 +
   8.174 +	Face *faces = new Face[num_faces];
   8.175 +	memset(faces, 0, num_faces * sizeof *faces);
   8.176 +	Face *fptr = faces;
   8.177 +
   8.178 +	for(int i=0; i<num_meshes; i++) {
   8.179 +		for(size_t j=0; j<meshes[i]->faces.size(); j++) {
   8.180 +			*fptr++ = meshes[i]->faces[j];
   8.181 +		}
   8.182 +	}
   8.183 +	return faces;
   8.184 +}
     9.1 --- a/src/rt.h	Tue Aug 03 13:06:59 2010 +0100
     9.2 +++ b/src/rt.h	Wed Aug 04 04:51:06 2010 +0100
     9.3 @@ -1,11 +1,13 @@
     9.4  #ifndef RT_H_
     9.5  #define RT_H_
     9.6  
     9.7 -bool init_renderer(int xsz, int ysz);
     9.8 +#include "mesh.h"
     9.9 +
    9.10 +bool init_renderer(int xsz, int ysz, Scene *scn);
    9.11  void destroy_renderer();
    9.12  bool render();
    9.13  void set_xform(float *matrix, float *invtrans);
    9.14  
    9.15 -void dbg_render_gl();
    9.16 +void dbg_render_gl(Scene *scn);
    9.17  
    9.18  #endif	/* RT_H_ */