source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/glm.h @ 983

Revision 983, 10.0 KB checked in by gumbau, 18 years ago (diff)
Line 
1#ifndef __glm_h_
2#define __glm_h_
3
4/*   
5      glm.h
6      Nate Robins, 1997, 2000
7      nate@pobox.com, http://www.pobox.com/~nate
8 
9      Wavefront OBJ model file format reader/writer/manipulator.
10
11      Includes routines for generating smooth normals with
12      preservation of edges, welding redundant vertices & texture
13      coordinate generation (spheremap and planar projections) + more.
14
15 */
16
17
18#include "GL/glut.h"
19
20
21#ifndef M_PI
22#define M_PI 3.14159265f
23#endif
24
25#define GLM_NONE     (0)            /* render with only vertices */
26#define GLM_FLAT     (1 << 0)       /* render with facet normals */
27#define GLM_SMOOTH   (1 << 1)       /* render with vertex normals */
28#define GLM_TEXTURE  (1 << 2)       /* render with texture coords */
29#define GLM_COLOR    (1 << 3)       /* render with colors */
30#define GLM_MATERIAL (1 << 4)       /* render with materials */
31
32
33/* GLMmaterial: Structure that defines a material in a model.
34 */
35typedef struct _GLMmaterial
36{
37  char* name;                   /* name of material */
38  GLfloat diffuse[4];           /* diffuse component */
39  GLfloat ambient[4];           /* ambient component */
40  GLfloat specular[4];          /* specular component */
41  GLfloat emmissive[4];         /* emmissive component */
42  GLfloat shininess;            /* specular exponent */
43} GLMmaterial;
44
45/* GLMtriangle: Structure that defines a triangle in a model.
46 */
47typedef struct _GLMtriangle {
48  GLuint vindices[3];           /* array of triangle vertex indices */
49  GLuint nindices[3];           /* array of triangle normal indices */
50  GLuint tindices[3];           /* array of triangle texcoord indices*/
51  GLuint findex;                /* index of triangle facet normal */
52} GLMtriangle;
53
54/* GLMgroup: Structure that defines a group in a model.
55 */
56typedef struct _GLMgroup {
57  char*             name;           /* name of this group */
58  GLuint            numtriangles;   /* number of triangles in this group */
59  GLuint*           triangles;      /* array of triangle indices */
60  GLuint            material;       /* index to material for group */
61  struct _GLMgroup* next;           /* pointer to next group in model */
62} GLMgroup;
63
64/* GLMmodel: Structure that defines a model.
65 */
66typedef struct _GLMmodel {
67  char*    pathname;            /* path to this model */
68  char*    mtllibname;          /* name of the material library */
69
70  GLuint   numvertices;         /* number of vertices in model */
71  GLfloat* vertices;            /* array of vertices  */
72
73  GLuint   numnormals;          /* number of normals in model */
74  GLfloat* normals;             /* array of normals */
75
76  GLuint   numtexcoords;        /* number of texcoords in model */
77  GLfloat* texcoords;           /* array of texture coordinates */
78
79  GLuint   numfacetnorms;       /* number of facetnorms in model */
80  GLfloat* facetnorms;          /* array of facetnorms */
81
82  GLuint       numtriangles;    /* number of triangles in model */
83  GLMtriangle* triangles;       /* array of triangles */
84
85  GLuint       nummaterials;    /* number of materials in model */
86  GLMmaterial* materials;       /* array of materials */
87
88  GLuint       numgroups;       /* number of groups in model */
89  GLMgroup*    groups;          /* linked list of groups */
90
91  GLfloat position[3];          /* position of the model */
92
93} GLMmodel;
94
95#ifdef __cplusplus
96extern "C" {
97#endif
98/* glmUnitize: "unitize" a model by translating it to the origin and
99 * scaling it to fit in a unit cube around the origin.  Returns the
100 * scalefactor used.
101 *
102 * model - properly initialized GLMmodel structure
103 */
104GLfloat
105glmUnitize(GLMmodel* model);
106
107/* glmDimensions: Calculates the dimensions (width, height, depth) of
108 * a model.
109 *
110 * model      - initialized GLMmodel structure
111 * dimensions - array of 3 GLfloats (GLfloat dimensions[3])
112 */
113GLvoid
114glmDimensions(GLMmodel* model, GLfloat* dimensions);
115
116/* glmScale: Scales a model by a given amount.
117 *
118 * model - properly initialized GLMmodel structure
119 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
120 */
121GLvoid
122glmScale(GLMmodel* model, GLfloat scale);
123
124/* glmReverseWinding: Reverse the polygon winding for all polygons in
125 * this model.  Default winding is counter-clockwise.  Also changes
126 * the direction of the normals.
127 *
128 * model - properly initialized GLMmodel structure
129 */
130GLvoid
131glmReverseWinding(GLMmodel* model);
132
133/* glmFacetNormals: Generates facet normals for a model (by taking the
134 * cross product of the two vectors derived from the sides of each
135 * triangle).  Assumes a counter-clockwise winding.
136 *
137 * model - initialized GLMmodel structure
138 */
139GLvoid
140glmFacetNormals(GLMmodel* model);
141
142/* glmVertexNormals: Generates smooth vertex normals for a model.
143 * First builds a list of all the triangles each vertex is in.  Then
144 * loops through each vertex in the the list averaging all the facet
145 * normals of the triangles each vertex is in.  Finally, sets the
146 * normal index in the triangle for the vertex to the generated smooth
147 * normal.  If the dot product of a facet normal and the facet normal
148 * associated with the first triangle in the list of triangles the
149 * current vertex is in is greater than the cosine of the angle
150 * parameter to the function, that facet normal is not added into the
151 * average normal calculation and the corresponding vertex is given
152 * the facet normal.  This tends to preserve hard edges.  The angle to
153 * use depends on the model, but 90 degrees is usually a good start.
154 *
155 * model - initialized GLMmodel structure
156 * angle - maximum angle (in degrees) to smooth across
157 */
158GLvoid
159glmVertexNormals(GLMmodel* model, GLfloat angle);
160
161/* glmLinearTexture: Generates texture coordinates according to a
162 * linear projection of the texture map.  It generates these by
163 * linearly mapping the vertices onto a square.
164 *
165 * model - pointer to initialized GLMmodel structure
166 */
167GLvoid
168glmLinearTexture(GLMmodel* model);
169
170/* glmSpheremapTexture: Generates texture coordinates according to a
171 * spherical projection of the texture map.  Sometimes referred to as
172 * spheremap, or reflection map texture coordinates.  It generates
173 * these by using the normal to calculate where that vertex would map
174 * onto a sphere.  Since it is impossible to map something flat
175 * perfectly onto something spherical, there is distortion at the
176 * poles.  This particular implementation causes the poles along the X
177 * axis to be distorted.
178 *
179 * model - pointer to initialized GLMmodel structure
180 */
181GLvoid
182glmSpheremapTexture(GLMmodel* model);
183
184/* glmDelete: Deletes a GLMmodel structure.
185 *
186 * model - initialized GLMmodel structure
187 */
188GLvoid
189glmDelete(GLMmodel* model);
190
191/* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
192 * Returns a pointer to the created object which should be free'd with
193 * glmDelete().
194 *
195 * filename - name of the file containing the Wavefront .OBJ format data. 
196 */
197GLMmodel*
198glmReadOBJ(char* filename);
199
200/* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
201 * a file.
202 *
203 * model    - initialized GLMmodel structure
204 * filename - name of the file to write the Wavefront .OBJ format data to
205 * mode     - a bitwise or of values describing what is written to the file
206 *            GLM_NONE    -  write only vertices
207 *            GLM_FLAT    -  write facet normals
208 *            GLM_SMOOTH  -  write vertex normals
209 *            GLM_TEXTURE -  write texture coords
210 *            GLM_FLAT and GLM_SMOOTH should not both be specified.
211 */
212GLvoid
213glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode);
214
215/* glmDraw: Renders the model to the current OpenGL context using the
216 * mode specified.
217 *
218 * model    - initialized GLMmodel structure
219 * mode     - a bitwise OR of values describing what is to be rendered.
220 *            GLM_NONE    -  render with only vertices
221 *            GLM_FLAT    -  render with facet normals
222 *            GLM_SMOOTH  -  render with vertex normals
223 *            GLM_TEXTURE -  render with texture coords
224 *            GLM_FLAT and GLM_SMOOTH should not both be specified.
225 */
226GLvoid
227glmDraw(GLMmodel* model, GLuint mode);
228
229/* glmList: Generates and returns a display list for the model using
230 * the 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 */
240GLuint
241glmList(GLMmodel* model, GLuint mode);
242
243/* glmWeld: eliminate (weld) vectors that are within an epsilon of
244 * each other.
245 *
246 * model      - initialized GLMmodel structure
247 * epsilon    - maximum difference between vertices
248 *              ( 0.00001 is a good start for a unitized model)
249 *
250 */
251GLvoid
252glmWeld(GLMmodel* model, GLfloat epsilon);
253
254/* glmReadPPM: read a PPM raw (type P6) file.  The PPM file has a header
255 * that should look something like:
256 *
257 *    P6
258 *    # comment
259 *    width height max_value
260 *    rgbrgbrgb...
261 *
262 * where "P6" is the magic cookie which identifies the file type and
263 * should be the only characters on the first line followed by a
264 * carriage return.  Any line starting with a # mark will be treated
265 * as a comment and discarded.   After the magic cookie, three integer
266 * values are expected: width, height of the image and the maximum
267 * value for a pixel (max_value must be < 256 for PPM raw files).  The
268 * data section consists of width*height rgb triplets (one byte each)
269 * in binary format (i.e., such as that written with fwrite() or
270 * equivalent).
271 *
272 * The rgb data is returned as an array of unsigned chars (packed
273 * rgb).  The malloc()'d memory should be free()'d by the caller.  If
274 * an error occurs, an error message is sent to stderr and NULL is
275 * returned.
276 *
277 * filename   - name of the .ppm file.
278 * width      - will contain the width of the image on return.
279 * height     - will contain the height of the image on return.
280 *
281 */
282GLubyte*
283glmReadPPM(char* filename, int* width, int* height);
284
285GLfloat
286glmDot(GLfloat* u, GLfloat* v);
287
288#ifdef __cplusplus
289}
290#endif
291#endif
Note: See TracBrowser for help on using the repository browser.