2 * GLM library. Wavefront .obj file format reader/writer/manipulator.
4 * Written by Nate Robins, 1997.
6 * www: http://www.pobox.com/~ndr
13 typedef unsigned int uint
;
17 #define M_PI 3.14159265
22 #define GLM_NONE (0) /* render with only vertices */
23 #define GLM_FLAT (1 << 0) /* render with facet normals */
24 #define GLM_SMOOTH (1 << 1) /* render with vertex normals */
25 #define GLM_TEXTURE (1 << 2) /* render with texture coords */
26 #define GLM_COLOR (1 << 3) /* render with colors */
27 #define GLM_MATERIAL (1 << 4) /* render with materials */
32 /* GLMmaterial: Structure that defines a material in a model.
34 typedef struct _GLMmaterial
36 char* name
; /* name of material */
37 float diffuse
[4]; /* diffuse component */
38 float ambient
[4]; /* ambient component */
39 float specular
[4]; /* specular component */
40 float emmissive
[4]; /* emmissive component */
41 float shininess
; /* specular exponent */
42 char *map_kd
; /* diffuse texture map file */
43 uint texture_kd
; /* diffuse texture map */
44 uint texture_ks
; /* specular texture map */
45 int uDiffuse
, uAmbient
, uSpecular
, uShininess
, uDiffTex
, uSpecTex
;
49 /* GLMtriangle: Structure that defines a triangle in a model.
52 uint vindices
[3]; /* array of triangle vertex indices */
53 uint nindices
[3]; /* array of triangle normal indices */
54 uint tindices
[3]; /* array of triangle texcoord indices*/
55 uint findex
; /* index of triangle facet normal */
58 /* GLMgroup: Structure that defines a group in a model.
60 typedef struct _GLMgroup
{
61 char* name
; /* name of this group */
62 uint numtriangles
; /* number of triangles in this group */
63 uint
* triangles
; /* array of triangle indices */
64 uint material
; /* index to material for group */
66 uint minIndex
, maxIndex
;
67 struct _GLMgroup
* next
; /* pointer to next group in model */
70 /* GLMmodel: Structure that defines a model.
73 char* pathname
; /* path to this model */
74 char* mtllibname
; /* name of the material library */
76 uint numvertices
; /* number of vertices in model */
77 float* vertices
; /* array of vertices */
79 uint numnormals
; /* number of normals in model */
80 float* normals
; /* array of normals */
82 uint numtexcoords
; /* number of texcoords in model */
83 float* texcoords
; /* array of texture coordinates */
85 uint numfacetnorms
; /* number of facetnorms in model */
86 float* facetnorms
; /* array of facetnorms */
88 uint numtriangles
; /* number of triangles in model */
89 GLMtriangle
* triangles
; /* array of triangles */
91 uint nummaterials
; /* number of materials in model */
92 GLMmaterial
* materials
; /* array of materials */
94 uint numgroups
; /* number of groups in model */
95 GLMgroup
* groups
; /* linked list of groups */
97 float position
[3]; /* position of the model */
100 uint vbo
; /* OpenGL VBO for vertex data */
101 uint vertexSize
; /* number of floats per vertex */
102 uint posOffset
; /* offset of position within vertex, in bytes */
103 uint normOffset
; /* offset of normal within vertex, in bytes */
104 uint texOffset
; /* offset of texcoord within vertex, in bytes */
108 /* public functions */
110 /* glmUnitize: "unitize" a model by translating it to the origin and
111 * scaling it to fit in a unit cube around the origin. Returns the
114 * model - properly initialized GLMmodel structure
117 glmUnitize(GLMmodel
* model
);
119 /* glmDimensions: Calculates the dimensions (width, height, depth) of
122 * model - initialized GLMmodel structure
123 * dimensions - array of 3 floats (float dimensions[3])
126 glmDimensions(GLMmodel
* model
, float* dimensions
);
128 /* glmScale: Scales a model by a given amount.
130 * model - properly initialized GLMmodel structure
131 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
134 glmScale(GLMmodel
* model
, float scale
);
136 /* glmReverseWinding: Reverse the polygon winding for all polygons in
137 * this model. Default winding is counter-clockwise. Also changes
138 * the direction of the normals.
140 * model - properly initialized GLMmodel structure
143 glmReverseWinding(GLMmodel
* model
);
145 /* glmFacetNormals: Generates facet normals for a model (by taking the
146 * cross product of the two vectors derived from the sides of each
147 * triangle). Assumes a counter-clockwise winding.
149 * model - initialized GLMmodel structure
152 glmFacetNormals(GLMmodel
* model
);
154 /* glmVertexNormals: Generates smooth vertex normals for a model.
155 * First builds a list of all the triangles each vertex is in. Then
156 * loops through each vertex in the the list averaging all the facet
157 * normals of the triangles each vertex is in. Finally, sets the
158 * normal index in the triangle for the vertex to the generated smooth
159 * normal. If the dot product of a facet normal and the facet normal
160 * associated with the first triangle in the list of triangles the
161 * current vertex is in is greater than the cosine of the angle
162 * parameter to the function, that facet normal is not added into the
163 * average normal calculation and the corresponding vertex is given
164 * the facet normal. This tends to preserve hard edges. The angle to
165 * use depends on the model, but 90 degrees is usually a good start.
167 * model - initialized GLMmodel structure
168 * angle - maximum angle (in degrees) to smooth across
171 glmVertexNormals(GLMmodel
* model
, float angle
);
173 /* glmLinearTexture: Generates texture coordinates according to a
174 * linear projection of the texture map. It generates these by
175 * linearly mapping the vertices onto a square.
177 * model - pointer to initialized GLMmodel structure
180 glmLinearTexture(GLMmodel
* model
);
182 /* glmSpheremapTexture: Generates texture coordinates according to a
183 * spherical projection of the texture map. Sometimes referred to as
184 * spheremap, or reflection map texture coordinates. It generates
185 * these by using the normal to calculate where that vertex would map
186 * onto a sphere. Since it is impossible to map something flat
187 * perfectly onto something spherical, there is distortion at the
188 * poles. This particular implementation causes the poles along the X
189 * axis to be distorted.
191 * model - pointer to initialized GLMmodel structure
194 glmSpheremapTexture(GLMmodel
* model
);
196 /* glmDelete: Deletes a GLMmodel structure.
198 * model - initialized GLMmodel structure
201 glmDelete(GLMmodel
* model
);
203 /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
204 * Returns a pointer to the created object which should be free'd with
207 * filename - name of the file containing the Wavefront .OBJ format data.
210 glmReadOBJ(char* filename
);
212 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
215 * model - initialized GLMmodel structure
216 * filename - name of the file to write the Wavefront .OBJ format data to
217 * mode - a bitwise or of values describing what is written to the file
218 * GLM_NONE - write only vertices
219 * GLM_FLAT - write facet normals
220 * GLM_SMOOTH - write vertex normals
221 * GLM_TEXTURE - write texture coords
222 * GLM_FLAT and GLM_SMOOTH should not both be specified.
225 glmWriteOBJ(GLMmodel
* model
, char* filename
, uint mode
);
227 /* glmDraw: Renders the model to the current OpenGL context using the
230 * model - initialized GLMmodel structure
231 * mode - a bitwise OR of values describing what is to be rendered.
232 * GLM_NONE - render with only vertices
233 * GLM_FLAT - render with facet normals
234 * GLM_SMOOTH - render with vertex normals
235 * GLM_TEXTURE - render with texture coords
236 * GLM_FLAT and GLM_SMOOTH should not both be specified.
239 glmDraw(GLMmodel
* model
, uint mode
);
241 /* glmList: Generates and returns a display list for the model using
242 * the mode specified.
244 * model - initialized GLMmodel structure
245 * mode - a bitwise OR of values describing what is to be rendered.
246 * GLM_NONE - render with only vertices
247 * GLM_FLAT - render with facet normals
248 * GLM_SMOOTH - render with vertex normals
249 * GLM_TEXTURE - render with texture coords
250 * GLM_FLAT and GLM_SMOOTH should not both be specified.
253 glmList(GLMmodel
* model
, uint mode
);
255 /* glmWeld: eliminate (weld) vectors that are within an epsilon of
258 * model - initialized GLMmodel structure
259 * epsilon - maximum difference between vertices
260 * ( 0.00001 is a good start for a unitized model)
264 glmWeld(GLMmodel
* model
, float epsilon
);
267 glmReIndex(GLMmodel
*model
);
270 glmMakeVBOs(GLMmodel
*model
);
273 glmDrawVBO(GLMmodel
*model
);
276 glmPrint(const GLMmodel
*model
);
279 glmShaderMaterial(GLMmaterial
*mat
);
282 glmLoadTextures(GLMmodel
*model
);
285 glmSpecularTexture(GLMmodel
*model
, uint cubeTex
);