Subversion Repositories gelsvn

Rev

Rev 145 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 145 Rev 156
1
// bdl, jab, feb 2005
1
// bdl, jab, feb 2005
2
// Inspired by Nate Robins Obj loader
2
// Inspired by Nate Robins Obj loader
3
 
3
 
4
#include "TriMesh.h"
-
 
5
 
-
 
6
#include "Graphics/gel_glu.h"
-
 
7
 
-
 
8
#include <CGLA/Vec3f.h>
4
#include <CGLA/Vec3f.h>
9
#include <stdio.h>
5
#include <stdio.h>
10
#include <iostream>
6
#include <iostream>
11
 
7
 
-
 
8
#include "TriMesh.h"
-
 
9
 
12
using namespace std;
10
using namespace std;
13
using namespace CGLA;
11
using namespace CGLA;
14
 
12
 
15
namespace Geometry 
13
namespace Geometry 
16
{
14
{
17
	int TriMesh::find_material(const string& name) const
15
	int TriMesh::find_material(const string& name) const
18
	{
16
	{
19
		for(size_t i=0;i<materials.size(); ++i)
17
		for(size_t i=0;i<materials.size(); ++i)
20
			{
18
			{
21
				if(materials[i].name == name)
19
				if(materials[i].name == name)
22
					return i;
20
					return i;
23
			}
21
			}
24
		return 0;
22
		return 0;
25
	}
23
	}
26
 
-
 
27
	int TriMesh::find_texmap(const string& name) const
-
 
28
	{
24
	
29
		for(size_t i=0;i<texmaps.size(); ++i)
-
 
30
			{
-
 
31
				if(texmaps[i].get_name() == name)
-
 
32
					return i;
-
 
33
			}
-
 
34
		return -1;
-
 
35
	}
-
 
36
 
-
 
37
	void TriMesh::compute_normals()
25
	void TriMesh::compute_normals()
38
	{		
26
	{		
39
		// By default the normal faces are the same as the geometry faces
27
		// By default the normal faces are the same as the geometry faces
40
		// and there are just as many normals as vertices, so we simply
28
		// and there are just as many normals as vertices, so we simply
41
		// copy.
29
		// copy.
42
		normals = geometry;
30
		normals = geometry;
43
 
31
 
44
		const int NV = normals.no_vertices();
32
		const int NV = normals.no_vertices();
45
		// The normals are initialized to zero.
33
		// The normals are initialized to zero.
46
		int i;
34
		int i;
47
		for(i=0;i<NV; ++i)
35
		for(i=0;i<NV; ++i)
48
			normals.vertex_rw(i) = Vec3f(0);
36
			normals.vertex_rw(i) = Vec3f(0);
49
 
37
 
50
		// For each face
38
		// For each face
51
		int NF = geometry.no_faces();
39
		int NF = geometry.no_faces();
52
		for(i=0;i<NF; ++i)
40
		for(i=0;i<NF; ++i)
53
      {
41
      {
54
				// Compute the normal
42
				// Compute the normal
55
				const Vec3i& f  = geometry.face(i);
43
				const Vec3i& f  = geometry.face(i);
56
				const Vec3f p0 = geometry.vertex(f[0]);
44
				const Vec3f p0 = geometry.vertex(f[0]);
57
				const Vec3f a  = geometry.vertex(f[1]) - p0;
45
				const Vec3f a  = geometry.vertex(f[1]) - p0;
58
				const Vec3f b  = geometry.vertex(f[2]) - p0;
46
				const Vec3f b  = geometry.vertex(f[2]) - p0;
59
				Vec3f face_normal = cross(a,b);
47
				Vec3f face_normal = cross(a,b);
60
				float l = sqr_length(face_normal);
48
				float l = sqr_length(face_normal);
61
				if(l > 0.0f)
49
				if(l > 0.0f)
62
					face_normal /= sqrt(l);
50
					face_normal /= sqrt(l);
63
				
51
				
64
				// Add the angle weighted normal to each vertex
52
				// Add the angle weighted normal to each vertex
65
				for(int j=0;j<3; ++j)
53
				for(int j=0;j<3; ++j)
66
					{
54
					{
67
						const Vec3f p0 = geometry.vertex(f[j]);
55
						const Vec3f p0 = geometry.vertex(f[j]);
68
						const Vec3f a = normalize(geometry.vertex(f[(j+1)%3]) - p0);
56
						const Vec3f a = normalize(geometry.vertex(f[(j+1)%3]) - p0);
69
						const Vec3f b = normalize(geometry.vertex(f[(j+2)%3]) - p0);
57
						const Vec3f b = normalize(geometry.vertex(f[(j+2)%3]) - p0);
70
						float d = max(-1.0f, min(1.0f, dot(a,b)));
58
						float d = max(-1.0f, min(1.0f, dot(a,b)));
71
						normals.vertex_rw(f[j]) += face_normal * acos(d);
59
						normals.vertex_rw(f[j]) += face_normal * acos(d);
72
					}
60
					}
73
      }
61
      }
74
 
62
 
75
		// Normalize all normals
63
		// Normalize all normals
76
    for(i=0;i<NV; ++i)
64
    for(i=0;i<NV; ++i)
77
			{
65
			{
78
				normals.vertex_rw(i).normalize();
66
				normals.vertex_rw(i).normalize();
79
			}
67
			}
80
	}
68
	}
81
 
69
 
82
	void TriMesh::gl_init_textures()
-
 
83
	{
-
 
84
		for(size_t i=0;i<texmaps.size();++i)
-
 
85
			texmaps[i].gl_init();
-
 
86
	}
-
 
87
 
-
 
88
 
-
 
89
	void TriMesh::gl_set_material(size_t idx)
-
 
90
	{
-
 
91
			assert(idx<materials.size());
-
 
92
		Material& material = materials[idx];
-
 
93
		if(material.tex_id >=0)
-
 
94
			{
-
 
95
				glEnable(GL_TEXTURE_2D);
-
 
96
				texmaps[material.tex_id].gl_bind();
-
 
97
				glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-
 
98
			}
-
 
99
		else
-
 
100
			glDisable(GL_TEXTURE_2D);
-
 
101
			
-
 
102
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
-
 
103
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
-
 
104
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
-
 
105
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
-
 
106
	}
-
 
107
 
-
 
108
	void TriMesh::gl_draw()
-
 
109
	{
-
 
110
		int old_mat_idx = -1;
-
 
111
		glBegin(GL_TRIANGLES);
-
 
112
		for(int i=0;i<geometry.no_faces();i++) 
-
 
113
			{
-
 
114
				if(mat_idx[i] != old_mat_idx)
-
 
115
					{
-
 
116
						glEnd();
-
 
117
						gl_set_material(mat_idx[i]);
-
 
118
						glBegin(GL_TRIANGLES);
-
 
119
						old_mat_idx = mat_idx[i];
-
 
120
					}
-
 
121
				Vec3i n_face = normals.face(i);
-
 
122
				Vec3i g_face = geometry.face(i);
-
 
123
				Vec3i t_face = texcoords.face(i);
-
 
124
				for(int j=0;j<3;j++) 
-
 
125
					{
-
 
126
						if(n_face != NULL_FACE)
-
 
127
							{
-
 
128
								Vec3f norm = normals.vertex(n_face[j]);
-
 
129
								glNormal3fv(norm.get());
-
 
130
							}
-
 
131
						if(t_face != NULL_FACE)
-
 
132
							{
-
 
133
								Vec3f texc = texcoords.vertex(t_face[j]);
-
 
134
								glTexCoord2fv(texc.get());
-
 
135
							}
-
 
136
						Vec3f vert = geometry.vertex(g_face[j]);
-
 
137
						glVertex3fv(vert.get());
-
 
138
					}
-
 
139
		}
-
 
140
		glEnd();
-
 
141
		glDisable(GL_TEXTURE_2D);
-
 
142
	}
-
 
143
 
70
 
144
  void TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
71
  void TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
145
  {
72
  {
146
    int i;
73
    int i;
147
    p0 = geometry.vertex(0);
74
    p0 = geometry.vertex(0);
148
    p7 = geometry.vertex(0);
75
    p7 = geometry.vertex(0);
149
    for(i=1;i<geometry.no_vertices();i++) 
76
    for(i=1;i<geometry.no_vertices();i++) 
150
      {
77
      {
151
				p0 = v_min(geometry.vertex(i), p0);
78
				p0 = v_min(geometry.vertex(i), p0);
152
				p7 = v_max(geometry.vertex(i), p7);
79
				p7 = v_max(geometry.vertex(i), p7);
153
      }
80
      }
154
  }
81
  }
155
 
82
 
156
  void TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
83
  void TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
157
  {
84
  {
158
    Vec3f p0,p7;
85
    Vec3f p0,p7;
159
    get_bbox(p0, p7);
86
    get_bbox(p0, p7);
160
    Vec3f rad = (p7 - p0)/2.0;
87
    Vec3f rad = (p7 - p0)/2.0;
161
    c = p0 + rad;
88
    c = p0 + rad;
162
    r = rad.length();
89
    r = rad.length();
163
  }
90
  }
164
 
91
 
165
 
92
 
166
}
93
}
167
 
94