glm.h revision 32001f49
132001f49Smrg/*
232001f49Smrg *  GLM library.  Wavefront .obj file format reader/writer/manipulator.
332001f49Smrg *
432001f49Smrg *  Written by Nate Robins, 1997.
532001f49Smrg *  email: ndr@pobox.com
632001f49Smrg *  www: http://www.pobox.com/~ndr
732001f49Smrg */
832001f49Smrg
932001f49Smrg#ifndef GLM_H
1032001f49Smrg#define GLM_H
1132001f49Smrg
1232001f49Smrg
1332001f49Smrgtypedef unsigned int uint;
1432001f49Smrg
1532001f49Smrg
1632001f49Smrg#ifndef M_PI
1732001f49Smrg#define M_PI 3.14159265
1832001f49Smrg#endif
1932001f49Smrg
2032001f49Smrg
2132001f49Smrg/* defines */
2232001f49Smrg#define GLM_NONE     (0)		/* render with only vertices */
2332001f49Smrg#define GLM_FLAT     (1 << 0)		/* render with facet normals */
2432001f49Smrg#define GLM_SMOOTH   (1 << 1)		/* render with vertex normals */
2532001f49Smrg#define GLM_TEXTURE  (1 << 2)		/* render with texture coords */
2632001f49Smrg#define GLM_COLOR    (1 << 3)		/* render with colors */
2732001f49Smrg#define GLM_MATERIAL (1 << 4)		/* render with materials */
2832001f49Smrg
2932001f49Smrg
3032001f49Smrg/* structs */
3132001f49Smrg
3232001f49Smrg/* GLMmaterial: Structure that defines a material in a model.
3332001f49Smrg */
3432001f49Smrgtypedef struct _GLMmaterial
3532001f49Smrg{
3632001f49Smrg  char* name;				/* name of material */
3732001f49Smrg  float diffuse[4];			/* diffuse component */
3832001f49Smrg  float ambient[4];			/* ambient component */
3932001f49Smrg  float specular[4];			/* specular component */
4032001f49Smrg  float emmissive[4];			/* emmissive component */
4132001f49Smrg  float shininess;			/* specular exponent */
4232001f49Smrg  char *map_kd;                         /* diffuse texture map file */
4332001f49Smrg  uint texture_kd;                      /* diffuse texture map */
4432001f49Smrg  uint texture_ks;                      /* specular texture map */
4532001f49Smrg  int uDiffuse, uAmbient, uSpecular, uShininess, uDiffTex, uSpecTex;
4632001f49Smrg  uint prog;
4732001f49Smrg} GLMmaterial;
4832001f49Smrg
4932001f49Smrg/* GLMtriangle: Structure that defines a triangle in a model.
5032001f49Smrg */
5132001f49Smrgtypedef struct {
5232001f49Smrg  uint vindices[3];			/* array of triangle vertex indices */
5332001f49Smrg  uint nindices[3];			/* array of triangle normal indices */
5432001f49Smrg  uint tindices[3];			/* array of triangle texcoord indices*/
5532001f49Smrg  uint findex;			/* index of triangle facet normal */
5632001f49Smrg} GLMtriangle;
5732001f49Smrg
5832001f49Smrg/* GLMgroup: Structure that defines a group in a model.
5932001f49Smrg */
6032001f49Smrgtypedef struct _GLMgroup {
6132001f49Smrg  char*             name;		/* name of this group */
6232001f49Smrg  uint            numtriangles;	/* number of triangles in this group */
6332001f49Smrg  uint*           triangles;		/* array of triangle indices */
6432001f49Smrg  uint            material;           /* index to material for group */
6532001f49Smrg  uint *          triIndexes;
6632001f49Smrg  uint            minIndex, maxIndex;
6732001f49Smrg  uint            indexVboOffset;       /* offset into index VBO for elements */
6832001f49Smrg  struct _GLMgroup* next;		/* pointer to next group in model */
6932001f49Smrg} GLMgroup;
7032001f49Smrg
7132001f49Smrg/* GLMmodel: Structure that defines a model.
7232001f49Smrg */
7332001f49Smrgtypedef struct {
7432001f49Smrg  char*    pathname;			/* path to this model */
7532001f49Smrg  char*    mtllibname;			/* name of the material library */
7632001f49Smrg
7732001f49Smrg  uint   numvertices;			/* number of vertices in model */
7832001f49Smrg  float* vertices;			/* array of vertices  */
7932001f49Smrg
8032001f49Smrg  uint   numnormals;			/* number of normals in model */
8132001f49Smrg  float* normals;			/* array of normals */
8232001f49Smrg
8332001f49Smrg  uint   numtexcoords;		/* number of texcoords in model */
8432001f49Smrg  float* texcoords;			/* array of texture coordinates */
8532001f49Smrg
8632001f49Smrg  uint   numfacetnorms;		/* number of facetnorms in model */
8732001f49Smrg  float* facetnorms;			/* array of facetnorms */
8832001f49Smrg
8932001f49Smrg  uint       numtriangles;		/* number of triangles in model */
9032001f49Smrg  GLMtriangle* triangles;		/* array of triangles */
9132001f49Smrg
9232001f49Smrg  uint       nummaterials;		/* number of materials in model */
9332001f49Smrg  GLMmaterial* materials;		/* array of materials */
9432001f49Smrg
9532001f49Smrg  uint       numgroups;		/* number of groups in model */
9632001f49Smrg  GLMgroup*    groups;			/* linked list of groups */
9732001f49Smrg
9832001f49Smrg  float position[3];			/* position of the model */
9932001f49Smrg  float scale;
10032001f49Smrg
10132001f49Smrg  uint vbo;  /* OpenGL VBO for vertex data */
10232001f49Smrg  uint index_vbo;   /* VBO for index data */
10332001f49Smrg  uint vertexSize;  /* number of floats per vertex */
10432001f49Smrg  uint posOffset;   /* offset of position within vertex, in bytes */
10532001f49Smrg  uint normOffset;   /* offset of normal within vertex, in bytes */
10632001f49Smrg  uint texOffset;   /* offset of texcoord within vertex, in bytes */
10732001f49Smrg} GLMmodel;
10832001f49Smrg
10932001f49Smrg
11032001f49Smrg/* public functions */
11132001f49Smrg
11232001f49Smrg/* glmUnitize: "unitize" a model by translating it to the origin and
11332001f49Smrg * scaling it to fit in a unit cube around the origin.  Returns the
11432001f49Smrg * scalefactor used.
11532001f49Smrg *
11632001f49Smrg * model - properly initialized GLMmodel structure
11732001f49Smrg */
11832001f49Smrgfloat
11932001f49SmrgglmUnitize(GLMmodel* model);
12032001f49Smrg
12132001f49Smrg/* glmDimensions: Calculates the dimensions (width, height, depth) of
12232001f49Smrg * a model.
12332001f49Smrg *
12432001f49Smrg * model      - initialized GLMmodel structure
12532001f49Smrg * dimensions - array of 3 floats (float dimensions[3])
12632001f49Smrg */
12732001f49Smrgvoid
12832001f49SmrgglmDimensions(GLMmodel* model, float* dimensions);
12932001f49Smrg
13032001f49Smrg/* glmScale: Scales a model by a given amount.
13132001f49Smrg *
13232001f49Smrg * model - properly initialized GLMmodel structure
13332001f49Smrg * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
13432001f49Smrg */
13532001f49Smrgvoid
13632001f49SmrgglmScale(GLMmodel* model, float scale);
13732001f49Smrg
13832001f49Smrg/* glmReverseWinding: Reverse the polygon winding for all polygons in
13932001f49Smrg * this model.  Default winding is counter-clockwise.  Also changes
14032001f49Smrg * the direction of the normals.
14132001f49Smrg *
14232001f49Smrg * model - properly initialized GLMmodel structure
14332001f49Smrg */
14432001f49Smrgvoid
14532001f49SmrgglmReverseWinding(GLMmodel* model);
14632001f49Smrg
14732001f49Smrg/* glmFacetNormals: Generates facet normals for a model (by taking the
14832001f49Smrg * cross product of the two vectors derived from the sides of each
14932001f49Smrg * triangle).  Assumes a counter-clockwise winding.
15032001f49Smrg *
15132001f49Smrg * model - initialized GLMmodel structure
15232001f49Smrg */
15332001f49Smrgvoid
15432001f49SmrgglmFacetNormals(GLMmodel* model);
15532001f49Smrg
15632001f49Smrg/* glmVertexNormals: Generates smooth vertex normals for a model.
15732001f49Smrg * First builds a list of all the triangles each vertex is in.  Then
15832001f49Smrg * loops through each vertex in the list averaging all the facet
15932001f49Smrg * normals of the triangles each vertex is in.  Finally, sets the
16032001f49Smrg * normal index in the triangle for the vertex to the generated smooth
16132001f49Smrg * normal.  If the dot product of a facet normal and the facet normal
16232001f49Smrg * associated with the first triangle in the list of triangles the
16332001f49Smrg * current vertex is in is greater than the cosine of the angle
16432001f49Smrg * parameter to the function, that facet normal is not added into the
16532001f49Smrg * average normal calculation and the corresponding vertex is given
16632001f49Smrg * the facet normal.  This tends to preserve hard edges.  The angle to
16732001f49Smrg * use depends on the model, but 90 degrees is usually a good start.
16832001f49Smrg *
16932001f49Smrg * model - initialized GLMmodel structure
17032001f49Smrg * angle - maximum angle (in degrees) to smooth across
17132001f49Smrg */
17232001f49Smrgvoid
17332001f49SmrgglmVertexNormals(GLMmodel* model, float angle);
17432001f49Smrg
17532001f49Smrg/* glmLinearTexture: Generates texture coordinates according to a
17632001f49Smrg * linear projection of the texture map.  It generates these by
17732001f49Smrg * linearly mapping the vertices onto a square.
17832001f49Smrg *
17932001f49Smrg * model - pointer to initialized GLMmodel structure
18032001f49Smrg */
18132001f49Smrgvoid
18232001f49SmrgglmLinearTexture(GLMmodel* model);
18332001f49Smrg
18432001f49Smrg/* glmSpheremapTexture: Generates texture coordinates according to a
18532001f49Smrg * spherical projection of the texture map.  Sometimes referred to as
18632001f49Smrg * spheremap, or reflection map texture coordinates.  It generates
18732001f49Smrg * these by using the normal to calculate where that vertex would map
18832001f49Smrg * onto a sphere.  Since it is impossible to map something flat
18932001f49Smrg * perfectly onto something spherical, there is distortion at the
19032001f49Smrg * poles.  This particular implementation causes the poles along the X
19132001f49Smrg * axis to be distorted.
19232001f49Smrg *
19332001f49Smrg * model - pointer to initialized GLMmodel structure
19432001f49Smrg */
19532001f49Smrgvoid
19632001f49SmrgglmSpheremapTexture(GLMmodel* model);
19732001f49Smrg
19832001f49Smrg/* glmDelete: Deletes a GLMmodel structure.
19932001f49Smrg *
20032001f49Smrg * model - initialized GLMmodel structure
20132001f49Smrg */
20232001f49Smrgvoid
20332001f49SmrgglmDelete(GLMmodel* model);
20432001f49Smrg
20532001f49Smrg/* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
20632001f49Smrg * Returns a pointer to the created object which should be free'd with
20732001f49Smrg * glmDelete().
20832001f49Smrg *
20932001f49Smrg * filename - name of the file containing the Wavefront .OBJ format data.
21032001f49Smrg */
21132001f49SmrgGLMmodel*
21232001f49SmrgglmReadOBJ(char* filename);
21332001f49Smrg
21432001f49Smrg/* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
21532001f49Smrg * a file.
21632001f49Smrg *
21732001f49Smrg * model    - initialized GLMmodel structure
21832001f49Smrg * filename - name of the file to write the Wavefront .OBJ format data to
21932001f49Smrg * mode     - a bitwise or of values describing what is written to the file
22032001f49Smrg *            GLM_NONE    -  write only vertices
22132001f49Smrg *            GLM_FLAT    -  write facet normals
22232001f49Smrg *            GLM_SMOOTH  -  write vertex normals
22332001f49Smrg *            GLM_TEXTURE -  write texture coords
22432001f49Smrg *            GLM_FLAT and GLM_SMOOTH should not both be specified.
22532001f49Smrg */
22632001f49Smrgvoid
22732001f49SmrgglmWriteOBJ(GLMmodel* model, char* filename, uint mode);
22832001f49Smrg
22932001f49Smrg/* glmDraw: Renders the model to the current OpenGL context using the
23032001f49Smrg * mode specified.
23132001f49Smrg *
23232001f49Smrg * model    - initialized GLMmodel structure
23332001f49Smrg * mode     - a bitwise OR of values describing what is to be rendered.
23432001f49Smrg *            GLM_NONE    -  render with only vertices
23532001f49Smrg *            GLM_FLAT    -  render with facet normals
23632001f49Smrg *            GLM_SMOOTH  -  render with vertex normals
23732001f49Smrg *            GLM_TEXTURE -  render with texture coords
23832001f49Smrg *            GLM_FLAT and GLM_SMOOTH should not both be specified.
23932001f49Smrg */
24032001f49Smrgvoid
24132001f49SmrgglmDraw(GLMmodel* model, uint mode);
24232001f49Smrg
24332001f49Smrg/* glmList: Generates and returns a display list for the model using
24432001f49Smrg * the mode specified.
24532001f49Smrg *
24632001f49Smrg * model    - initialized GLMmodel structure
24732001f49Smrg * mode     - a bitwise OR of values describing what is to be rendered.
24832001f49Smrg *            GLM_NONE    -  render with only vertices
24932001f49Smrg *            GLM_FLAT    -  render with facet normals
25032001f49Smrg *            GLM_SMOOTH  -  render with vertex normals
25132001f49Smrg *            GLM_TEXTURE -  render with texture coords
25232001f49Smrg *            GLM_FLAT and GLM_SMOOTH should not both be specified.
25332001f49Smrg */
25432001f49Smrguint
25532001f49SmrgglmList(GLMmodel* model, uint mode);
25632001f49Smrg
25732001f49Smrg/* glmWeld: eliminate (weld) vectors that are within an epsilon of
25832001f49Smrg * each other.
25932001f49Smrg *
26032001f49Smrg * model      - initialized GLMmodel structure
26132001f49Smrg * epsilon    - maximum difference between vertices
26232001f49Smrg *              ( 0.00001 is a good start for a unitized model)
26332001f49Smrg *
26432001f49Smrg */
26532001f49Smrgvoid
26632001f49SmrgglmWeld(GLMmodel* model, float epsilon);
26732001f49Smrg
26832001f49Smrgvoid
26932001f49SmrgglmReIndex(GLMmodel *model);
27032001f49Smrg
27132001f49Smrgvoid
27232001f49SmrgglmMakeVBOs(GLMmodel *model);
27332001f49Smrg
27432001f49Smrgvoid
27532001f49SmrgglmDrawVBO(GLMmodel *model);
27632001f49Smrg
27732001f49Smrgvoid
27832001f49SmrgglmPrint(const GLMmodel *model);
27932001f49Smrg
28032001f49Smrgvoid
28132001f49SmrgglmShaderMaterial(GLMmaterial *mat);
28232001f49Smrg
28332001f49Smrgvoid
28432001f49SmrgglmLoadTextures(GLMmodel *model);
28532001f49Smrg
28632001f49Smrgvoid
28732001f49SmrgglmSpecularTexture(GLMmodel *model, uint cubeTex);
28832001f49Smrg
28932001f49Smrg#endif /* GLM_H */
290