stereoview

view src/zscn.cc @ 2:30c7a5df0523

- added focus distance cmdline argument - changed the code to use the regular henge::Scene when using the zbuffer
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 04 Mar 2011 15:51:03 +0200
parents
children
line source
1 #include <list>
2 #include "zscn.h"
4 struct Face {
5 Vector3 v[3];
6 Vector3 n[3];
7 Vector2 t[3];
9 const henge::Material *mat;
11 Face()
12 {
13 memset(this, 0, sizeof(Face));
14 }
15 };
17 static void proc_mesh(std::list<Face> *facelist, const henge::TriMesh *mesh,
18 const henge::Material *mat, const Matrix4x4 &xform);
19 static bool operator <(const Face &a, const Face &b);
21 ZScene::~ZScene()
22 {
23 }
25 void ZScene::render(unsigned int msec) const
26 {
27 setup_lights(msec);
29 glMatrixMode(GL_MODELVIEW);
31 Matrix4x4 view_mat;
32 henge::store_matrix(&view_mat);
34 std::list<Face> facelist;
36 for(size_t i=0; i<objects.size(); i++) {
37 const henge::RObject *obj = objects[i];
38 Matrix4x4 xform = obj->get_xform_matrix(msec) * view_mat;
40 const henge::Material *mat = ((henge::RObject*)obj)->get_material_ptr();
41 const henge::TriMesh *mesh = obj->get_mesh();
43 proc_mesh(&facelist, mesh, mat, xform);
44 }
46 facelist.sort();
48 glPushAttrib(GL_ENABLE_BIT);
49 glDisable(GL_DEPTH_TEST);
51 glPushMatrix();
52 glLoadIdentity();
54 glBegin(GL_TRIANGLES);
56 const henge::Material *cur_mat = 0;
57 std::list<Face>::iterator face = facelist.begin();
58 while(face != facelist.end()) {
59 if(face->mat != cur_mat) {
60 glEnd();
62 face->mat->bind();
63 cur_mat = face->mat;
65 glBegin(GL_TRIANGLES);
66 }
68 for(int i=0; i<3; i++) {
69 glNormal3f(face->n[i].x, face->n[i].y, face->n[i].z);
70 glTexCoord2f(face->t[i].x, face->t[i].y);
71 glVertex3f(face->v[i].x, face->v[i].y, face->v[i].z);
72 }
74 face++;
75 }
76 glEnd();
78 glMatrixMode(GL_MODELVIEW);
79 glPopMatrix();
80 glPopAttrib();
81 }
83 static void proc_mesh(std::list<Face> *facelist, const henge::TriMesh *mesh,
84 const henge::Material *mat, const Matrix4x4 &xform)
85 {
86 Matrix3x3 norm_xform = xform;
88 const Vector3 *vert = mesh->get_data_vec3(henge::EL_VERTEX);
89 const Vector3 *norm = mesh->get_data_vec3(henge::EL_NORMAL);
90 const Vector2 *tc = mesh->get_data_vec2(henge::EL_TEXCOORD);
91 const unsigned int *index = mesh->get_data_int(henge::EL_INDEX);
93 int nvert = mesh->get_count(henge::EL_VERTEX);
94 int nindex = mesh->get_count(henge::EL_INDEX);
96 if(!vert || !nvert) {
97 return;
98 }
100 int face_count = (index ? nindex : nvert) / 3;
102 for(int i=0; i<face_count; i++) {
103 Face face;
105 for(int j=0; j<3; j++) {
106 int idx = index ? index[i * 3 + j] : i * 3 + j;
108 if(norm) {
109 face.n[j] = norm[idx].transformed(norm_xform);
110 }
111 if(tc) {
112 face.t[j] = tc[idx];
113 }
114 if(vert) {
115 face.v[j] = vert[idx].transformed(xform);
116 }
117 }
119 face.mat = mat;
120 facelist->push_back(face);
121 }
122 }
124 static bool operator <(const Face &a, const Face &b)
125 {
126 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);
127 }