stereoview

diff src/zscn.cc @ 0:dc1723a8bf6f

initial import
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 04 Mar 2011 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/zscn.cc	Fri Mar 04 06:51:16 2011 +0200
     1.3 @@ -0,0 +1,127 @@
     1.4 +#include <list>
     1.5 +#include "zscn.h"
     1.6 +
     1.7 +struct Face {
     1.8 +	Vector3 v[3];
     1.9 +	Vector3 n[3];
    1.10 +	Vector2 t[3];
    1.11 +
    1.12 +	const henge::Material *mat;
    1.13 +
    1.14 +	Face()
    1.15 +	{
    1.16 +		memset(this, 0, sizeof(Face));
    1.17 +	}
    1.18 +};
    1.19 +
    1.20 +static void proc_mesh(std::list<Face> *facelist, const henge::TriMesh *mesh,
    1.21 +		const henge::Material *mat, const Matrix4x4 &xform);
    1.22 +static bool operator <(const Face &a, const Face &b);
    1.23 +
    1.24 +ZScene::~ZScene()
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +void ZScene::render(unsigned int msec) const
    1.29 +{
    1.30 +	setup_lights(msec);
    1.31 +
    1.32 +	glMatrixMode(GL_MODELVIEW);
    1.33 +
    1.34 +	Matrix4x4 view_mat;
    1.35 +	henge::store_matrix(&view_mat);
    1.36 +
    1.37 +	std::list<Face> facelist;
    1.38 +
    1.39 +	for(size_t i=0; i<objects.size(); i++) {
    1.40 +		const henge::RObject *obj = objects[i];
    1.41 +		Matrix4x4 xform = obj->get_xform_matrix(msec) * view_mat;
    1.42 +
    1.43 +		const henge::Material *mat = ((henge::RObject*)obj)->get_material_ptr();
    1.44 +		const henge::TriMesh *mesh = obj->get_mesh();
    1.45 +
    1.46 +		proc_mesh(&facelist, mesh, mat, xform);
    1.47 +	}
    1.48 +
    1.49 +	facelist.sort();
    1.50 +
    1.51 +	glPushAttrib(GL_ENABLE_BIT);
    1.52 +	glDisable(GL_DEPTH_TEST);
    1.53 +
    1.54 +	glPushMatrix();
    1.55 +	glLoadIdentity();
    1.56 +
    1.57 +	glBegin(GL_TRIANGLES);
    1.58 +
    1.59 +	const henge::Material *cur_mat = 0;
    1.60 +	std::list<Face>::iterator face = facelist.begin();
    1.61 +	while(face != facelist.end()) {
    1.62 +		if(face->mat != cur_mat) {
    1.63 +			glEnd();
    1.64 +
    1.65 +			face->mat->bind();
    1.66 +			cur_mat = face->mat;
    1.67 +
    1.68 +			glBegin(GL_TRIANGLES);
    1.69 +		}
    1.70 +
    1.71 +		for(int i=0; i<3; i++) {
    1.72 +			glNormal3f(face->n[i].x, face->n[i].y, face->n[i].z);
    1.73 +			glTexCoord2f(face->t[i].x, face->t[i].y);
    1.74 +			glVertex3f(face->v[i].x, face->v[i].y, face->v[i].z);
    1.75 +		}
    1.76 +
    1.77 +		face++;
    1.78 +	}
    1.79 +	glEnd();
    1.80 +
    1.81 +	glMatrixMode(GL_MODELVIEW);
    1.82 +	glPopMatrix();
    1.83 +	glPopAttrib();
    1.84 +}
    1.85 +
    1.86 +static void proc_mesh(std::list<Face> *facelist, const henge::TriMesh *mesh,
    1.87 +		const henge::Material *mat, const Matrix4x4 &xform)
    1.88 +{
    1.89 +	Matrix3x3 norm_xform = xform;
    1.90 +
    1.91 +	const Vector3 *vert = mesh->get_data_vec3(henge::EL_VERTEX);
    1.92 +	const Vector3 *norm = mesh->get_data_vec3(henge::EL_NORMAL);
    1.93 +	const Vector2 *tc = mesh->get_data_vec2(henge::EL_TEXCOORD);
    1.94 +	const unsigned int *index = mesh->get_data_int(henge::EL_INDEX);
    1.95 +
    1.96 +	int nvert = mesh->get_count(henge::EL_VERTEX);
    1.97 +	int nindex = mesh->get_count(henge::EL_INDEX);
    1.98 +
    1.99 +	if(!vert || !nvert) {
   1.100 +		return;
   1.101 +	}
   1.102 +
   1.103 +	int face_count = (index ? nindex : nvert) / 3;
   1.104 +
   1.105 +	for(int i=0; i<face_count; i++) {
   1.106 +		Face face;
   1.107 +
   1.108 +		for(int j=0; j<3; j++) {
   1.109 +			int idx = index ? index[i * 3 + j] : i * 3 + j;
   1.110 +
   1.111 +			if(norm) {
   1.112 +				face.n[j] = norm[idx].transformed(norm_xform);
   1.113 +			}
   1.114 +			if(tc) {
   1.115 +				face.t[j] = tc[idx];
   1.116 +			}
   1.117 +			if(vert) {
   1.118 +				face.v[j] = vert[idx].transformed(xform);
   1.119 +			}
   1.120 +		}
   1.121 +
   1.122 +		face.mat = mat;
   1.123 +		facelist->push_back(face);
   1.124 +	}
   1.125 +}
   1.126 +
   1.127 +static bool operator <(const Face &a, const Face &b)
   1.128 +{
   1.129 +	return (a.v[0].z + a.v[1].z + a.v[2].z) < (b.v[0].z + b.v[1].z + b.v[2].z);
   1.130 +}