Subversion Repositories gelsvn

Rev

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

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