Subversion Repositories gelsvn

Rev

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