- morning panick victory
[The-Artvertiser.git] / artvertiser / glm.h
blob4725b08e2e057cb020b133e2d9c44a6d6fc24dce
1 /*
2 * GLM library. Wavefront .obj file format reader/writer/manipulator.
4 * Written by Nate Robins, 1997.
5 * email: ndr@pobox.com
6 * www: http://www.pobox.com/~ndr
7 */
10 /* includes */
11 #include <GL/glut.h>
14 #ifndef M_PI
15 #define M_PI 3.14159265
16 #endif
19 /* defines */
20 #define GLM_NONE (0) /* render with only vertices */
21 #define GLM_FLAT (1 << 0) /* render with facet normals */
22 #define GLM_SMOOTH (1 << 1) /* render with vertex normals */
23 #define GLM_TEXTURE (1 << 2) /* render with texture coords */
24 #define GLM_COLOR (1 << 3) /* render with colors */
25 #define GLM_MATERIAL (1 << 4) /* render with materials */
28 /* structs */
30 /* GLMmaterial: Structure that defines a material in a model.
32 typedef struct _GLMmaterial
34 char* name; /* name of material */
35 GLfloat diffuse[4]; /* diffuse component */
36 GLfloat ambient[4]; /* ambient component */
37 GLfloat specular[4]; /* specular component */
38 GLfloat emmissive[4]; /* emmissive component */
39 GLfloat shininess; /* specular exponent */
40 } GLMmaterial;
42 /* GLMtriangle: Structure that defines a triangle in a model.
44 typedef struct {
45 GLuint vindices[3]; /* array of triangle vertex indices */
46 GLuint nindices[3]; /* array of triangle normal indices */
47 GLuint tindices[3]; /* array of triangle texcoord indices*/
48 GLuint findex; /* index of triangle facet normal */
49 } GLMtriangle;
51 /* GLMgroup: Structure that defines a group in a model.
53 typedef struct _GLMgroup {
54 char* name; /* name of this group */
55 GLuint numtriangles; /* number of triangles in this group */
56 GLuint* triangles; /* array of triangle indices */
57 GLuint material; /* index to material for group */
58 struct _GLMgroup* next; /* pointer to next group in model */
59 } GLMgroup;
61 /* GLMmodel: Structure that defines a model.
63 typedef struct {
64 char* pathname; /* path to this model */
65 char* mtllibname; /* name of the material library */
67 GLuint numvertices; /* number of vertices in model */
68 GLfloat* vertices; /* array of vertices */
70 GLuint numnormals; /* number of normals in model */
71 GLfloat* normals; /* array of normals */
73 GLuint numtexcoords; /* number of texcoords in model */
74 GLfloat* texcoords; /* array of texture coordinates */
76 GLuint numfacetnorms; /* number of facetnorms in model */
77 GLfloat* facetnorms; /* array of facetnorms */
79 GLuint numtriangles; /* number of triangles in model */
80 GLMtriangle* triangles; /* array of triangles */
82 GLuint nummaterials; /* number of materials in model */
83 GLMmaterial* materials; /* array of materials */
85 GLuint numgroups; /* number of groups in model */
86 GLMgroup* groups; /* linked list of groups */
88 GLfloat position[3]; /* position of the model */
90 } GLMmodel;
93 /* public functions */
95 /* glmUnitize: "unitize" a model by translating it to the origin and
96 * scaling it to fit in a unit cube around the origin. Returns the
97 * scalefactor used.
99 * model - properly initialized GLMmodel structure
101 GLfloat
102 glmUnitize(GLMmodel* model);
104 /* glmDimensions: Calculates the dimensions (width, height, depth) of
105 * a model.
107 * model - initialized GLMmodel structure
108 * dimensions - array of 3 GLfloats (GLfloat dimensions[3])
110 GLvoid
111 glmDimensions(GLMmodel* model, GLfloat* dimensions);
113 /* glmScale: Scales a model by a given amount.
115 * model - properly initialized GLMmodel structure
116 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
118 GLvoid
119 glmScale(GLMmodel* model, GLfloat scale);
121 /* glmReverseWinding: Reverse the polygon winding for all polygons in
122 * this model. Default winding is counter-clockwise. Also changes
123 * the direction of the normals.
125 * model - properly initialized GLMmodel structure
127 GLvoid
128 glmReverseWinding(GLMmodel* model);
130 /* glmFacetNormals: Generates facet normals for a model (by taking the
131 * cross product of the two vectors derived from the sides of each
132 * triangle). Assumes a counter-clockwise winding.
134 * model - initialized GLMmodel structure
136 GLvoid
137 glmFacetNormals(GLMmodel* model);
139 /* glmVertexNormals: Generates smooth vertex normals for a model.
140 * First builds a list of all the triangles each vertex is in. Then
141 * loops through each vertex in the the list averaging all the facet
142 * normals of the triangles each vertex is in. Finally, sets the
143 * normal index in the triangle for the vertex to the generated smooth
144 * normal. If the dot product of a facet normal and the facet normal
145 * associated with the first triangle in the list of triangles the
146 * current vertex is in is greater than the cosine of the angle
147 * parameter to the function, that facet normal is not added into the
148 * average normal calculation and the corresponding vertex is given
149 * the facet normal. This tends to preserve hard edges. The angle to
150 * use depends on the model, but 90 degrees is usually a good start.
152 * model - initialized GLMmodel structure
153 * angle - maximum angle (in degrees) to smooth across
155 GLvoid
156 glmVertexNormals(GLMmodel* model, GLfloat angle);
158 /* glmLinearTexture: Generates texture coordinates according to a
159 * linear projection of the texture map. It generates these by
160 * linearly mapping the vertices onto a square.
162 * model - pointer to initialized GLMmodel structure
164 GLvoid
165 glmLinearTexture(GLMmodel* model);
167 /* glmSpheremapTexture: Generates texture coordinates according to a
168 * spherical projection of the texture map. Sometimes referred to as
169 * spheremap, or reflection map texture coordinates. It generates
170 * these by using the normal to calculate where that vertex would map
171 * onto a sphere. Since it is impossible to map something flat
172 * perfectly onto something spherical, there is distortion at the
173 * poles. This particular implementation causes the poles along the X
174 * axis to be distorted.
176 * model - pointer to initialized GLMmodel structure
178 GLvoid
179 glmSpheremapTexture(GLMmodel* model);
181 /* glmDelete: Deletes a GLMmodel structure.
183 * model - initialized GLMmodel structure
185 GLvoid
186 glmDelete(GLMmodel* model);
188 /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
189 * Returns a pointer to the created object which should be free'd with
190 * glmDelete().
192 * filename - name of the file containing the Wavefront .OBJ format data.
194 GLMmodel*
195 glmReadOBJ(char* filename);
197 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
198 * a file.
200 * model - initialized GLMmodel structure
201 * filename - name of the file to write the Wavefront .OBJ format data to
202 * mode - a bitwise or of values describing what is written to the file
203 * GLM_NONE - write only vertices
204 * GLM_FLAT - write facet normals
205 * GLM_SMOOTH - write vertex normals
206 * GLM_TEXTURE - write texture coords
207 * GLM_FLAT and GLM_SMOOTH should not both be specified.
209 GLvoid
210 glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode);
212 /* glmDraw: Renders the model to the current OpenGL context using the
213 * mode specified.
215 * model - initialized GLMmodel structure
216 * mode - a bitwise OR of values describing what is to be rendered.
217 * GLM_NONE - render with only vertices
218 * GLM_FLAT - render with facet normals
219 * GLM_SMOOTH - render with vertex normals
220 * GLM_TEXTURE - render with texture coords
221 * GLM_FLAT and GLM_SMOOTH should not both be specified.
223 GLvoid
224 glmDraw(GLMmodel* model, GLuint mode);
226 /* glmList: Generates and returns a display list for the model using
227 * the mode specified.
229 * model - initialized GLMmodel structure
230 * mode - a bitwise OR of values describing what is to be rendered.
231 * GLM_NONE - render with only vertices
232 * GLM_FLAT - render with facet normals
233 * GLM_SMOOTH - render with vertex normals
234 * GLM_TEXTURE - render with texture coords
235 * GLM_FLAT and GLM_SMOOTH should not both be specified.
237 GLuint
238 glmList(GLMmodel* model, GLuint mode);
240 /* glmWeld: eliminate (weld) vectors that are within an epsilon of
241 * each other.
243 * model - initialized GLMmodel structure
244 * epsilon - maximum difference between vertices
245 * ( 0.00001 is a good start for a unitized model)
248 GLvoid
249 glmWeld(GLMmodel* model, GLfloat epsilon);