Subversion Repositories gelsvn

Rev

Rev 373 | Rev 432 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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