1/*
2 *  GLM library.  Wavefront .obj file format reader/writer/manipulator.
3 *
4 *  Written by Nate Robins, 1997.
5 *  email: ndr@pobox.com
6 *  www: http://www.pobox.com/~ndr
7 */
8
9#ifndef GLM_H
10#define GLM_H
11
12
13typedef unsigned int uint;
14
15
16#ifndef M_PI
17#define M_PI 3.14159265
18#endif
19
20
21/* defines */
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 */
28
29
30/* structs */
31
32/* GLMmaterial: Structure that defines a material in a model.
33 */
34typedef struct _GLMmaterial
35{
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;
46  uint prog;
47} GLMmaterial;
48
49/* GLMtriangle: Structure that defines a triangle in a model.
50 */
51typedef struct {
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 */
56} GLMtriangle;
57
58/* GLMgroup: Structure that defines a group in a model.
59 */
60typedef 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 */
65  uint *          triIndexes;
66  uint            minIndex, maxIndex;
67  uint            indexVboOffset;       /* offset into index VBO for elements */
68  struct _GLMgroup* next;		/* pointer to next group in model */
69} GLMgroup;
70
71/* GLMmodel: Structure that defines a model.
72 */
73typedef struct {
74  char*    pathname;			/* path to this model */
75  char*    mtllibname;			/* name of the material library */
76
77  uint   numvertices;			/* number of vertices in model */
78  float* vertices;			/* array of vertices  */
79
80  uint   numnormals;			/* number of normals in model */
81  float* normals;			/* array of normals */
82
83  uint   numtexcoords;		/* number of texcoords in model */
84  float* texcoords;			/* array of texture coordinates */
85
86  uint   numfacetnorms;		/* number of facetnorms in model */
87  float* facetnorms;			/* array of facetnorms */
88
89  uint       numtriangles;		/* number of triangles in model */
90  GLMtriangle* triangles;		/* array of triangles */
91
92  uint       nummaterials;		/* number of materials in model */
93  GLMmaterial* materials;		/* array of materials */
94
95  uint       numgroups;		/* number of groups in model */
96  GLMgroup*    groups;			/* linked list of groups */
97
98  float position[3];			/* position of the model */
99  float scale;
100
101  uint vbo;  /* OpenGL VBO for vertex data */
102  uint index_vbo;   /* VBO for index data */
103  uint vertexSize;  /* number of floats per vertex */
104  uint posOffset;   /* offset of position within vertex, in bytes */
105  uint normOffset;   /* offset of normal within vertex, in bytes */
106  uint texOffset;   /* offset of texcoord within vertex, in bytes */
107} GLMmodel;
108
109
110/* public functions */
111
112/* glmUnitize: "unitize" a model by translating it to the origin and
113 * scaling it to fit in a unit cube around the origin.  Returns the
114 * scalefactor used.
115 *
116 * model - properly initialized GLMmodel structure
117 */
118float
119glmUnitize(GLMmodel* model);
120
121/* glmDimensions: Calculates the dimensions (width, height, depth) of
122 * a model.
123 *
124 * model      - initialized GLMmodel structure
125 * dimensions - array of 3 floats (float dimensions[3])
126 */
127void
128glmDimensions(GLMmodel* model, float* dimensions);
129
130/* glmScale: Scales a model by a given amount.
131 *
132 * model - properly initialized GLMmodel structure
133 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
134 */
135void
136glmScale(GLMmodel* model, float scale);
137
138/* glmReverseWinding: Reverse the polygon winding for all polygons in
139 * this model.  Default winding is counter-clockwise.  Also changes
140 * the direction of the normals.
141 *
142 * model - properly initialized GLMmodel structure
143 */
144void
145glmReverseWinding(GLMmodel* model);
146
147/* glmFacetNormals: Generates facet normals for a model (by taking the
148 * cross product of the two vectors derived from the sides of each
149 * triangle).  Assumes a counter-clockwise winding.
150 *
151 * model - initialized GLMmodel structure
152 */
153void
154glmFacetNormals(GLMmodel* model);
155
156/* glmVertexNormals: Generates smooth vertex normals for a model.
157 * First builds a list of all the triangles each vertex is in.  Then
158 * loops through each vertex in the list averaging all the facet
159 * normals of the triangles each vertex is in.  Finally, sets the
160 * normal index in the triangle for the vertex to the generated smooth
161 * normal.  If the dot product of a facet normal and the facet normal
162 * associated with the first triangle in the list of triangles the
163 * current vertex is in is greater than the cosine of the angle
164 * parameter to the function, that facet normal is not added into the
165 * average normal calculation and the corresponding vertex is given
166 * the facet normal.  This tends to preserve hard edges.  The angle to
167 * use depends on the model, but 90 degrees is usually a good start.
168 *
169 * model - initialized GLMmodel structure
170 * angle - maximum angle (in degrees) to smooth across
171 */
172void
173glmVertexNormals(GLMmodel* model, float angle);
174
175/* glmLinearTexture: Generates texture coordinates according to a
176 * linear projection of the texture map.  It generates these by
177 * linearly mapping the vertices onto a square.
178 *
179 * model - pointer to initialized GLMmodel structure
180 */
181void
182glmLinearTexture(GLMmodel* model);
183
184/* glmSpheremapTexture: Generates texture coordinates according to a
185 * spherical projection of the texture map.  Sometimes referred to as
186 * spheremap, or reflection map texture coordinates.  It generates
187 * these by using the normal to calculate where that vertex would map
188 * onto a sphere.  Since it is impossible to map something flat
189 * perfectly onto something spherical, there is distortion at the
190 * poles.  This particular implementation causes the poles along the X
191 * axis to be distorted.
192 *
193 * model - pointer to initialized GLMmodel structure
194 */
195void
196glmSpheremapTexture(GLMmodel* model);
197
198/* glmDelete: Deletes a GLMmodel structure.
199 *
200 * model - initialized GLMmodel structure
201 */
202void
203glmDelete(GLMmodel* model);
204
205/* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
206 * Returns a pointer to the created object which should be free'd with
207 * glmDelete().
208 *
209 * filename - name of the file containing the Wavefront .OBJ format data.
210 */
211GLMmodel*
212glmReadOBJ(char* filename);
213
214/* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
215 * a file.
216 *
217 * model    - initialized GLMmodel structure
218 * filename - name of the file to write the Wavefront .OBJ format data to
219 * mode     - a bitwise or of values describing what is written to the file
220 *            GLM_NONE    -  write only vertices
221 *            GLM_FLAT    -  write facet normals
222 *            GLM_SMOOTH  -  write vertex normals
223 *            GLM_TEXTURE -  write texture coords
224 *            GLM_FLAT and GLM_SMOOTH should not both be specified.
225 */
226void
227glmWriteOBJ(GLMmodel* model, char* filename, uint mode);
228
229/* glmDraw: Renders the model to the current OpenGL context using the
230 * mode specified.
231 *
232 * model    - initialized GLMmodel structure
233 * mode     - a bitwise OR of values describing what is to be rendered.
234 *            GLM_NONE    -  render with only vertices
235 *            GLM_FLAT    -  render with facet normals
236 *            GLM_SMOOTH  -  render with vertex normals
237 *            GLM_TEXTURE -  render with texture coords
238 *            GLM_FLAT and GLM_SMOOTH should not both be specified.
239 */
240void
241glmDraw(GLMmodel* model, uint mode);
242
243/* glmList: Generates and returns a display list for the model using
244 * the mode specified.
245 *
246 * model    - initialized GLMmodel structure
247 * mode     - a bitwise OR of values describing what is to be rendered.
248 *            GLM_NONE    -  render with only vertices
249 *            GLM_FLAT    -  render with facet normals
250 *            GLM_SMOOTH  -  render with vertex normals
251 *            GLM_TEXTURE -  render with texture coords
252 *            GLM_FLAT and GLM_SMOOTH should not both be specified.
253 */
254uint
255glmList(GLMmodel* model, uint mode);
256
257/* glmWeld: eliminate (weld) vectors that are within an epsilon of
258 * each other.
259 *
260 * model      - initialized GLMmodel structure
261 * epsilon    - maximum difference between vertices
262 *              ( 0.00001 is a good start for a unitized model)
263 *
264 */
265void
266glmWeld(GLMmodel* model, float epsilon);
267
268void
269glmReIndex(GLMmodel *model);
270
271void
272glmMakeVBOs(GLMmodel *model);
273
274void
275glmDrawVBO(GLMmodel *model);
276
277void
278glmPrint(const GLMmodel *model);
279
280void
281glmShaderMaterial(GLMmaterial *mat);
282
283void
284glmLoadTextures(GLMmodel *model);
285
286void
287glmSpecularTexture(GLMmodel *model, uint cubeTex);
288
289#endif /* GLM_H */
290