File: model.h

package info (click to toggle)
bzflag 2.0.16.20100405
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,508 kB
  • ctags: 31,338
  • sloc: cpp: 140,118; ansic: 14,429; sh: 10,922; makefile: 2,459; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (258 lines) | stat: -rw-r--r-- 4,210 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

//
// modeltool.h : defintions of the model classes
//

#ifndef _MODEL_H_
#define _MODEL_H_

#include <string>
#include <vector>
#include <map>

extern std::string texdir;
extern std::string groupName;
extern bool useMaterials;
extern bool useAmbient;
extern bool useDiffuse;
extern bool useSpecular;
extern bool useShininess;
extern bool useEmission;
extern bool useNormals;
extern bool useTexcoords ;
extern bool flipYZ;
extern bool useSmoothBounce;
extern float shineFactor;
extern float globalScale;
extern float globalShift[3];

extern float maxShineExponent; // OpenGL minimum shininess

extern std::vector<std::string> bspMaterialSkips; // materials to skip in a bsp map

typedef std::vector<int> tvIndexList;

typedef enum
{
	eXAxis,
	eYAxis,
	eZAxis
}teModelAxis;

class CTexCoord
{
public:
	CTexCoord(){u = v = 0;}
	~CTexCoord(){};
	float u,v;

	bool same ( const CTexCoord &c )
	{
		return u == c.u && v ==c.v;
	}
};

typedef std::vector<CTexCoord> tvTexCoordList;

class CVertex
{
public:
	CVertex(){x = y = z = 0;}
	~CVertex(){};
	float x,y,z;

	float get ( teModelAxis axis )
	{
		switch (axis)
		{
		case eXAxis:
			return x;
		case eYAxis:
			return y;
		default:
			return z;
		}
	}

	void translate ( float val, teModelAxis axis )
	{
		switch (axis)
		{
		case eXAxis:
			x += val;
			return;
		case eYAxis:
			y += val;
			return;
		case eZAxis:
			z += val;
		}
	}

	bool same ( const CVertex &v )
	{
		return x == v.x && y == v.y && z == v.z;
	}
};

typedef std::vector<CVertex> tvVertList;

class CFace
{
public:
	CFace(){};
	~CFace(){};

	std::string material;
	tvIndexList verts;
	tvIndexList	normals;
	tvIndexList	texCoords;

	void clear ( void ) {verts.clear();normals.clear();texCoords.clear();}
};
typedef std::vector<CFace> tvFaceList;


class CMaterial
{
public:
	CMaterial(){clear();}
	~CMaterial(){};

	std::string texture;
	float		ambient[4];
	float		diffuse[4];
	float		specular[4];
	float		emission[4];
	float		shine;

	void clear ( void )
	{
		texture = "";
		ambient[0] = ambient[1] = ambient[2] = 0.2f;
		ambient[3] = 1.0f;
		diffuse[0] = diffuse[1] = diffuse[2] = 1.0f;
		diffuse[3] = 1.0f;
		specular[0] = specular[1] = specular[2] = 0.0f;
		specular[3] = 1.0f;
		emission[0] = emission[1] = emission[2] = 0.0f;
		emission[3] = 1.0f;
		shine = 0.0f;
	}
};

typedef std::map<std::string,CMaterial> tmMaterialMap;

class CMesh
{
public:
	CMesh(){};
	~CMesh(){};

	tvVertList		verts;
	tvVertList		normals;
	tvTexCoordList	texCoords;

	std::string name;
	tvFaceList	faces;

	float getMaxAxisValue ( teModelAxis axis )
	{
		if (!valid())
			return 0.0f;

		float pt = verts[0].get(axis);

		for ( unsigned int i = 0; i < verts.size(); i++ )
			if ( verts[i].get(axis) > pt)
				pt = verts[i].get(axis);

		return pt;
	}

	float getMinAxisValue ( teModelAxis axis )
	{
		if (!valid())
			return 0.0f;

		float pt = verts[0].get(axis);

		for ( unsigned int i = 0; i < verts.size(); i++ )
			if ( verts[i].get(axis) < pt)
				pt = verts[i].get(axis);

		return pt;
	}

	void translate ( float value, teModelAxis axis )
	{
		for ( unsigned int i = 0; i < verts.size(); i++ )
			verts[i].translate(value,axis);
	}

	bool valid ( void )
	{
		return faces.size() != 0;
	}

	void clear ( void )
	{
		faces.clear();
		verts.clear();
		normals.clear();
		texCoords.clear();
		name = "";
	}
	void reindex ( void );
};

typedef std::vector<CMesh> tvMeshList;


class CCustomObject
{
public:
	std::string name;
	std::vector<std::string> params;

	void clear ( void ) {params.clear();name="";}
};

typedef std::vector<CCustomObject> tvCustomObjectList;

class CModel
{
public:
	CModel(){};
	~CModel(){};

	tmMaterialMap	materials;
	tvMeshList		meshes;
	tvCustomObjectList	customObjects;

	void pushAboveAxis ( teModelAxis axis )
	{
		if (!meshes.size())
			return;

		float minValue = meshes[0].getMinAxisValue(axis);

		for ( unsigned int i = 0; i < meshes.size(); i++ )
			if ( minValue > meshes[i].getMinAxisValue(axis))
				minValue = meshes[i].getMinAxisValue(axis);

		for ( unsigned int i = 0; i < meshes.size(); i++ )
			meshes[i].translate(-minValue,axis);
	}

	void clear ( void )
	{
		meshes.clear();
		materials.clear();
		customObjects.clear();
	}
};


#endif // _MODEL_H_