Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
396 jab 1
#include <GL/glew.h>
2
 
299 jrf 3
#include "CGLA/Mat4x4f.h"
362 jab 4
#include "CGLA/Vec3d.h"
167 jab 5
#include "draw.h"
331 jab 6
#include "HMesh/FaceCirculator.h"
167 jab 7
 
396 jab 8
#include "SinglePassWireframeRenderer.h"
9
#include "IDBufferWireFrameRenderer.h"
10
 
167 jab 11
using namespace CGLA;
331 jab 12
using namespace HMesh;
167 jab 13
using namespace std;
14
 
15
namespace 
16
{
339 jab 17
	void set_material(const Geometry::Material& material)
396 jab 18
	{
368 jrf 19
		if(material.has_texture && material.tex_id >=0)
339 jab 20
		{
21
			glEnable(GL_TEXTURE_2D);
22
			glBindTexture(GL_TEXTURE_2D, material.tex_id);
23
			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
24
		}
25
		else
26
			glDisable(GL_TEXTURE_2D);
27
 
28
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
29
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
30
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
31
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
396 jab 32
	}
33
 
167 jab 34
}
35
 
299 jrf 36
namespace GLGraphics
37
{
396 jab 38
 
39
	void draw(Manifold& m, bool per_vertex_norms)
339 jab 40
	{
331 jab 41
		for(FaceIter f=m.faces_begin(); f != m.faces_end(); ++f)
42
		{
339 jab 43
			FaceCirculator fc(f);
44
			if(!per_vertex_norms) 
45
				glNormal3fv(normal(f).get());
362 jab 46
			if(no_edges(f)== 3) 
47
				glBegin(GL_TRIANGLES);
48
			else 
49
				glBegin(GL_POLYGON);
339 jab 50
			while(!fc.end())
51
			{
52
				Vec3f n = normal(fc.get_vertex());
362 jab 53
				if(per_vertex_norms) 
54
					glNormal3fv(n.get());
339 jab 55
				glVertex3fv(fc.get_vertex()->pos.get());
56
				++fc;
57
			}
58
			glEnd();
59
		}
60
	}
61
 
62
 
63
	void draw(const Geometry::TriMesh& tm, bool per_vertex_norms)
64
	{
65
		int old_mat_idx = -1;
66
		glBegin(GL_TRIANGLES);
67
		for(int i=0;i<tm.geometry.no_faces();i++)
68
		{
373 jrf 69
			int new_mat_idx = i<static_cast<int>(tm.mat_idx.size()) ? tm.mat_idx[i] : -1;
362 jab 70
			if(new_mat_idx != old_mat_idx)
339 jab 71
			{
72
				glEnd();
73
				set_material(tm.materials[tm.mat_idx[i]]);
74
				glBegin(GL_TRIANGLES);
362 jab 75
				old_mat_idx = new_mat_idx;
339 jab 76
			}
77
			Vec3i n_face = tm.normals.face(i);
78
			Vec3i g_face = tm.geometry.face(i);
79
			Vec3i t_face = tm.texcoords.face(i);
80
 
81
			if(!per_vertex_norms)
82
			{
83
				Vec3f vert0 = tm.geometry.vertex(g_face[0]);
84
				Vec3f vert1 = tm.geometry.vertex(g_face[1]);
85
				Vec3f vert2 = tm.geometry.vertex(g_face[2]);
86
				Vec3f norm = normalize(cross(vert1-vert0, vert2-vert0));
87
				glNormal3fv(norm.get());
88
			}
89
			for(int j=0;j<3;j++)
90
			{
91
				if(per_vertex_norms && n_face != Geometry::NULL_FACE)
331 jab 92
				{
339 jab 93
					Vec3f norm = tm.normals.vertex(n_face[j]);
94
					glNormal3fv(norm.get());
331 jab 95
				}
339 jab 96
				if(t_face != Geometry::NULL_FACE)
331 jab 97
				{
339 jab 98
					Vec3f texc = tm.texcoords.vertex(t_face[j]);
99
					glTexCoord2fv(texc.get());
331 jab 100
				}
339 jab 101
				Vec3f vert = tm.geometry.vertex(g_face[j]);
102
				glVertex3fv(vert.get());
103
			}
104
		}
105
		glEnd();
106
		glDisable(GL_TEXTURE_2D);
107
	}
108
 
396 jab 109
 
110
 
111
	template<class T>
112
	void draw_wireframe_oldfashioned(T& m, bool per_vertex_norms, const Vec3f& line_color)
113
	{
114
		// Store state that we change
115
		glPushAttrib(GL_POLYGON_BIT);
116
		GLboolean lights_on;
117
		glGetBooleanv(GL_LIGHTING, &lights_on);
118
		Vec4f current_color;
119
		glGetFloatv(GL_CURRENT_COLOR, &current_color[0]);
120
 
121
		// Draw filled
122
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
123
		draw(m, per_vertex_norms);
124
 
125
		// Draw lines
126
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
127
		glDisable(GL_LIGHTING);
128
		glEnable(GL_POLYGON_OFFSET_LINE);
129
		glPolygonOffset(0,-5);
130
		glColor3fv(line_color.get());
131
		draw(m, per_vertex_norms);
132
 
133
		// Put back old state
134
		glColor3fv(current_color.get());
135
		if(lights_on) glEnable(GL_LIGHTING);
136
		glPopAttrib();
137
	}
138
 
139
	template
140
	void draw_wireframe_oldfashioned<HMesh::Manifold>(HMesh::Manifold& m, bool per_vertex_norms, const Vec3f& line_color);
141
 
142
	template
143
	void draw_wireframe_oldfashioned(Geometry::TriMesh& m, bool per_vertex_norms, const Vec3f& line_color);
144
 
145
 
339 jab 146
	void draw(const Geometry::AABox& box)
147
	{
148
		glBegin(GL_QUADS);
149
		Vec3f norm_neg[] = {Vec3f(0,0,-1), Vec3f(-1,0,0), Vec3f(0,-1,0)};
150
		Vec3f norm_pos[] = {Vec3f(0,0, 1), Vec3f( 1,0,0), Vec3f(0, 1,0)};
151
		for(int j=0;j<3;++j)
299 jrf 152
		{
153
			glNormal3fv(norm_neg[j].get());
154
			Vec3f p = box.get_pmin();
155
			glVertex3f(p[0], p[1], p[2]);
156
			p[(j+1)%3] = box.get_pmax()[(j+1)%3];
157
			glVertex3f(p[0], p[1], p[2]);
158
			p[j] = box.get_pmax()[j];
159
			glVertex3f(p[0], p[1], p[2]);
160
			p[(j+1)%3] = box.get_pmin()[(j+1)%3];
161
			glVertex3f(p[0], p[1], p[2]);
162
		}
339 jab 163
		glEnd();
164
		glBegin(GL_QUADS);
165
		for(int j=0;j<3;++j)
299 jrf 166
		{
167
			glNormal3fv(norm_pos[j].get());
168
			Vec3f p = box.get_pmax();
169
			glVertex3f(p[0], p[1], p[2]);
170
			p[j] = box.get_pmin()[j];
171
			glVertex3f(p[0], p[1], p[2]);
172
			p[(j+1)%3] = box.get_pmin()[(j+1)%3];
173
			glVertex3f(p[0], p[1], p[2]);
174
			p[j] = box.get_pmax()[j];
175
			glVertex3f(p[0], p[1], p[2]);
176
		}
339 jab 177
		glEnd();
178
	}
179
 
180
	void draw(const Geometry::OBox& box)
181
	{
182
		Mat4x4f m = identity_Mat4x4f();
183
		copy_matrix(box.get_rotation(), m);
184
		glPushMatrix();
185
		glMultMatrixf(m.get());
186
		draw(box.get_aabox());
187
		glPopMatrix();
188
	}
189
 
190
	/** Draw the tree. The first argument is the level counter, the second
396 jab 191
	 argument is the level at which to stop drawing. */
339 jab 192
	template <class BoxType>
193
	void draw(const Geometry::BoundingINode<BoxType>& node, int level, int max_level)
194
	{
195
		if(level == max_level)
196
		{
197
			draw(node); 
198
			return;
199
		}
200
		node->left->draw(level + 1, max_level);
201
		node->right->draw(level + 1, max_level);  
202
	}
203
 
204
	template <class BoxType>
205
	void draw(const Geometry::BoundingLNode<BoxType>& node, int level, int max_level)
206
	{
299 jrf 207
#if USE_LEAF_BOXES
339 jab 208
		draw(node); 
299 jrf 209
#endif
339 jab 210
	}
211
 
212
	template <class BoxType>
213
	void draw(const Geometry::BoundingTree<BoxType>& tree, int max_level)
214
	{
215
		draw(*tree.root, 0, max_level);
216
	}
299 jrf 217
}