progs/objviewer: Wavefront .obj file loader/viewer demo
[mesa.git] / progs / objviewer / glm.h
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
13 typedef 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 */
34 typedef 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 */
51 typedef 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 */
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 */
65 uint * triIndexes;
66 uint minIndex, maxIndex;
67 struct _GLMgroup* next; /* pointer to next group in model */
68 } GLMgroup;
69
70 /* GLMmodel: Structure that defines a model.
71 */
72 typedef struct {
73 char* pathname; /* path to this model */
74 char* mtllibname; /* name of the material library */
75
76 uint numvertices; /* number of vertices in model */
77 float* vertices; /* array of vertices */
78
79 uint numnormals; /* number of normals in model */
80 float* normals; /* array of normals */
81
82 uint numtexcoords; /* number of texcoords in model */
83 float* texcoords; /* array of texture coordinates */
84
85 uint numfacetnorms; /* number of facetnorms in model */
86 float* facetnorms; /* array of facetnorms */
87
88 uint numtriangles; /* number of triangles in model */
89 GLMtriangle* triangles; /* array of triangles */
90
91 uint nummaterials; /* number of materials in model */
92 GLMmaterial* materials; /* array of materials */
93
94 uint numgroups; /* number of groups in model */
95 GLMgroup* groups; /* linked list of groups */
96
97 float position[3]; /* position of the model */
98 float scale;
99
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 */
105 } GLMmodel;
106
107
108 /* public functions */
109
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
112 * scalefactor used.
113 *
114 * model - properly initialized GLMmodel structure
115 */
116 float
117 glmUnitize(GLMmodel* model);
118
119 /* glmDimensions: Calculates the dimensions (width, height, depth) of
120 * a model.
121 *
122 * model - initialized GLMmodel structure
123 * dimensions - array of 3 floats (float dimensions[3])
124 */
125 void
126 glmDimensions(GLMmodel* model, float* dimensions);
127
128 /* glmScale: Scales a model by a given amount.
129 *
130 * model - properly initialized GLMmodel structure
131 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
132 */
133 void
134 glmScale(GLMmodel* model, float scale);
135
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.
139 *
140 * model - properly initialized GLMmodel structure
141 */
142 void
143 glmReverseWinding(GLMmodel* model);
144
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.
148 *
149 * model - initialized GLMmodel structure
150 */
151 void
152 glmFacetNormals(GLMmodel* model);
153
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.
166 *
167 * model - initialized GLMmodel structure
168 * angle - maximum angle (in degrees) to smooth across
169 */
170 void
171 glmVertexNormals(GLMmodel* model, float angle);
172
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.
176 *
177 * model - pointer to initialized GLMmodel structure
178 */
179 void
180 glmLinearTexture(GLMmodel* model);
181
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.
190 *
191 * model - pointer to initialized GLMmodel structure
192 */
193 void
194 glmSpheremapTexture(GLMmodel* model);
195
196 /* glmDelete: Deletes a GLMmodel structure.
197 *
198 * model - initialized GLMmodel structure
199 */
200 void
201 glmDelete(GLMmodel* model);
202
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
205 * glmDelete().
206 *
207 * filename - name of the file containing the Wavefront .OBJ format data.
208 */
209 GLMmodel*
210 glmReadOBJ(char* filename);
211
212 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
213 * a file.
214 *
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.
223 */
224 void
225 glmWriteOBJ(GLMmodel* model, char* filename, uint mode);
226
227 /* glmDraw: Renders the model to the current OpenGL context using the
228 * mode specified.
229 *
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.
237 */
238 void
239 glmDraw(GLMmodel* model, uint mode);
240
241 /* glmList: Generates and returns a display list for the model using
242 * the mode specified.
243 *
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.
251 */
252 uint
253 glmList(GLMmodel* model, uint mode);
254
255 /* glmWeld: eliminate (weld) vectors that are within an epsilon of
256 * each other.
257 *
258 * model - initialized GLMmodel structure
259 * epsilon - maximum difference between vertices
260 * ( 0.00001 is a good start for a unitized model)
261 *
262 */
263 void
264 glmWeld(GLMmodel* model, float epsilon);
265
266 void
267 glmReIndex(GLMmodel *model);
268
269 void
270 glmMakeVBOs(GLMmodel *model);
271
272 void
273 glmDrawVBO(GLMmodel *model);
274
275 void
276 glmPrint(const GLMmodel *model);
277
278 void
279 glmShaderMaterial(GLMmaterial *mat);
280
281 void
282 glmLoadTextures(GLMmodel *model);
283
284 void
285 glmSpecularTexture(GLMmodel *model, uint cubeTex);
286
287 #endif /* GLM_H */