Subversion Repositories gelsvn

Rev

Rev 79 | Rev 136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
78 jab 1
// bdl, jab, feb 2005
2
// Inspired by Nate Robins Obj loader
3
 
4
#include "TriMesh.h"
5
 
132 jab 6
#include "Graphics/gel_glu.h"
7
 
78 jab 8
#include <CGLA/Vec3f.h>
9
#include <stdio.h>
10
#include <iostream>
11
 
12
using namespace std;
13
using namespace CGLA;
14
 
79 jab 15
namespace Geometry 
78 jab 16
{
17
	int TriMesh::find_material(const string& name) const
18
	{
19
		for(int i=0;i<materials.size(); ++i)
20
			{
21
				if(materials[i].name == name)
22
					return i;
23
			}
24
		return 0;
25
	}
26
 
27
	int TriMesh::find_texmap(const string& name) const
28
	{
29
		for(int 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()
38
	{		
39
		// 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
41
		// copy.
42
		normals = geometry;
43
 
44
		const int NV = normals.no_vertices();
45
		// The normals are initialized to zero.
46
		int i;
47
		for(i=0;i<NV; ++i)
48
			normals.vertex_rw(i) = Vec3f(0);
49
 
50
		// For each face
51
		int NF = geometry.no_faces();
52
		for(i=0;i<NF; ++i)
53
      {
54
				// Compute the normal
55
				const Vec3i& f  = geometry.face(i);
56
				const Vec3f p0 = geometry.vertex(f[0]);
57
				const Vec3f a  = geometry.vertex(f[1]) - p0;
58
				const Vec3f b  = geometry.vertex(f[2]) - p0;
59
				Vec3f face_normal = cross(a,b);
60
				float l = sqr_length(face_normal);
61
				if(l > 0.0f)
62
					face_normal /= sqrt(l);
63
 
64
				// Add the angle weighted normal to each vertex
65
				for(int j=0;j<3; ++j)
66
					{
67
						const Vec3f p0 = geometry.vertex(f[j]);
68
						const Vec3f a = normalize(geometry.vertex(f[(j+1)%3]) - p0);
69
						const Vec3f b = normalize(geometry.vertex(f[(j+2)%3]) - p0);
70
						float d = max(-1.0f, min(1.0f, dot(a,b)));
71
						normals.vertex_rw(f[j]) += face_normal * acos(d);
72
					}
73
      }
74
 
75
		// Normalize all normals
76
    for(i=0;i<NV; ++i)
77
			{
78
				normals.vertex_rw(i).normalize();
79
			}
80
	}
81
 
82
	void TriMesh::gl_init_textures()
83
	{
84
		for(int i=0;i<texmaps.size();++i)
85
			texmaps[i].gl_init();
86
	}
87
 
88
 
89
	void TriMesh::gl_set_material(int 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
 
144
  void TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
145
  {
146
    int i;
147
    p0 = geometry.vertex(0);
148
    p7 = geometry.vertex(0);
149
    for(i=1;i<geometry.no_vertices();i++) 
150
      {
151
				p0 = v_min(geometry.vertex(i), p0);
152
				p7 = v_max(geometry.vertex(i), p7);
153
      }
154
  }
155
 
156
  void TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
157
  {
158
    Vec3f p0,p7;
159
    get_bbox(p0, p7);
160
    Vec3f rad = (p7 - p0)/2.0;
161
    c = p0 + rad;
162
    r = rad.length();
163
  }
164
 
165
 
166
}