Subversion Repositories gelsvn

Rev

Rev 136 | Rev 295 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 136 Rev 178
Line 1... Line 1...
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
{
Line 21... Line 19...
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.
Line 77... Line 65...
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);