00001 /* $Id: glm.h,v 1.1.1.1 2002/07/01 14:50:51 cvs Exp $ */ 00002 /* glm.h Nate Robins, 1997 ndr@pobox.com, http://www.pobox.com/~ndr/ Wavefront OBJ model file format reader/writer/manipulator. Includes routines for generating smooth normals with preservation of edges, welding redundant vertices & texture coordinate generation (spheremap and planar projections) + more. */ 00003 00004 #ifndef __GLM_H 00005 #define __GLM_H 00006 00007 #ifdef WIN32 00008 #include <wtypes.h> 00009 #endif 00010 00011 #include <GL/gl.h> 00012 00013 #ifndef M_PI 00014 #define M_PI 3.14159265 00015 #endif 00016 00017 #define GLM_NONE (0) /* render with only vertices */ 00018 #define GLM_FLAT (1 << 0) /* render with facet normals */ 00019 #define GLM_SMOOTH (1 << 1) /* render with vertex normals */ 00020 #define GLM_TEXTURE (1 << 2) /* render with texture coords */ 00021 #define GLM_COLOR (1 << 3) /* render with colors */ 00022 #define GLM_MATERIAL (1 << 4) /* render with materials */ 00023 00024 #ifdef __cplusplus 00025 extern "C" { 00026 #endif /* __cplusplus */ 00027 00028 /* GLMmaterial: Structure that defines a material in a model. */ 00029 typedef struct _GLMmaterial { 00030 char* name; /* name of material */ 00031 GLfloat diffuse[4]; /* diffuse component */ 00032 GLfloat ambient[4]; /* ambient component */ 00033 GLfloat specular[4]; /* specular component */ 00034 GLfloat emmissive[4]; /* emmissive component */ 00035 GLfloat shininess; /* specular exponent */ 00036 } GLMmaterial; 00037 00038 /* GLMtriangle: Structure that defines a triangle in a model. 00039 */ 00040 typedef struct _GLMtriangle { 00041 GLuint vindices[3]; /* array of triangle vertex indices */ 00042 GLuint nindices[3]; /* array of triangle normal indices */ 00043 GLuint tindices[3]; /* array of triangle texcoord indices*/ 00044 GLuint findex; /* index of triangle facet normal */ 00045 } GLMtriangle; 00046 00047 /* GLMgroup: Structure that defines a group in a model. 00048 */ 00049 typedef struct _GLMgroup { 00050 char* name; /* name of this group */ 00051 GLuint numtriangles; /* number of triangles in this group */ 00052 GLuint* triangles; /* array of triangle indices */ 00053 GLuint material; /* index to material for group */ 00054 struct _GLMgroup* next; /* pointer to next group in model */ 00055 } GLMgroup; 00056 00057 /* GLMmodel: Structure that defines a model. 00058 */ 00059 typedef struct _GLMmodel { 00060 char* pathname; /* path to this model */ 00061 char* mtllibname; /* name of the material library */ 00062 00063 GLuint numvertices; /* number of vertices in model */ 00064 GLfloat* vertices; /* array of vertices */ 00065 00066 GLuint numnormals; /* number of normals in model */ 00067 GLfloat* normals; /* array of normals */ 00068 00069 GLuint numtexcoords; /* number of texcoords in model */ 00070 GLfloat* texcoords; /* array of texture coordinates */ 00071 GLuint numfacetnorms; /* number of facetnorms in model */ 00072 GLfloat* facetnorms; /* array of facetnorms */ 00073 GLuint numtriangles; /* number of triangles in model */ 00074 GLMtriangle* triangles; /* array of triangles */ 00075 GLuint nummaterials; /* number of materials in model */ 00076 GLMmaterial* materials; /* array of materials */ 00077 GLuint numgroups; /* number of groups in model */ 00078 GLMgroup* groups; /* linked list of groups */ 00079 GLfloat position[3]; /* position of the model */ 00080 } GLMmodel; 00081 00082 /* glmUnitize: "unitize" a model by translating it to the origin and 00083 * scaling it to fit in a unit cube around the origin. Returns the 00084 * scalefactor used. 00085 * 00086 * model - properly initialized GLMmodel structure 00087 */ 00088 GLfloat 00089 glmUnitize(GLMmodel* model); 00090 00091 /* glmDimensions: Calculates the dimensions (width, height, depth) of 00092 * a model. 00093 * 00094 * model - initialized GLMmodel structure 00095 * dimensions - array of 3 GLfloats (GLfloat dimensions[3]) 00096 */ 00097 GLvoid 00098 glmDimensions(GLMmodel* model, GLfloat* dimensions); 00099 00100 /* glmScale: Scales a model by a given amount. 00101 * 00102 * model - properly initialized GLMmodel structure 00103 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large) 00104 */ 00105 GLvoid 00106 glmScale(GLMmodel* model, GLfloat scale); 00107 00108 /* glmReverseWinding: Reverse the polygon winding for all polygons in 00109 * this model. Default winding is counter-clockwise. Also changes 00110 * the direction of the normals. 00111 * 00112 * model - properly initialized GLMmodel structure 00113 */ 00114 GLvoid glmReverseWinding(GLMmodel* model); 00115 00116 /* glmFacetNormals: Generates facet normals for a model (by taking the 00117 * cross product of the two vectors derived from the sides of each 00118 * triangle). Assumes a counter-clockwise winding. 00119 * 00120 * model - initialized GLMmodel structure 00121 */ 00122 GLvoid glmFacetNormals(GLMmodel* model); 00123 00124 /* glmVertexNormals: Generates smooth vertex normals for a model. 00125 * First builds a list of all the triangles each vertex is in. Then 00126 * loops through each vertex in the the list averaging all the facet 00127 * normals of the triangles each vertex is in. Finally, sets the 00128 * normal index in the triangle for the vertex to the generated smooth 00129 * normal. If the dot product of a facet normal and the facet normal 00130 * associated with the first triangle in the list of triangles the 00131 * current vertex is in is greater than the cosine of the angle 00132 * parameter to the function, that facet normal is not added into the 00133 * average normal calculation and the corresponding vertex is given 00134 * the facet normal. This tends to preserve hard edges. The angle to 00135 * use depends on the model, but 90 degrees is usually a good start. 00136 * 00137 * model - initialized GLMmodel structure 00138 * angle - maximum angle (in degrees) to smooth across 00139 */ 00140 GLvoid 00141 glmVertexNormals(GLMmodel* model, GLfloat angle); 00142 00143 /* glmLinearTexture: Generates texture coordinates according to a 00144 * linear projection of the texture map. It generates these by 00145 * linearly mapping the vertices onto a square. 00146 * 00147 * model - pointer to initialized GLMmodel structure 00148 */ 00149 GLvoid 00150 glmLinearTexture(GLMmodel* model); 00151 00152 /* glmSpheremapTexture: Generates texture coordinates according to a 00153 * spherical projection of the texture map. Sometimes referred to as 00154 * spheremap, or reflection map texture coordinates. It generates 00155 * these by using the normal to calculate where that vertex would map 00156 * onto a sphere. Since it is impossible to map something flat 00157 * perfectly onto something spherical, there is distortion at the 00158 * poles. This particular implementation causes the poles along the X 00159 * axis to be distorted. 00160 * 00161 * model - pointer to initialized GLMmodel structure 00162 */ 00163 GLvoid 00164 glmSpheremapTexture(GLMmodel* model); 00165 00166 /* glmDelete: Deletes a GLMmodel structure. 00167 * 00168 * model - initialized GLMmodel structure 00169 */ 00170 GLvoid 00171 glmDelete(GLMmodel* model); 00172 00173 /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file. 00174 * Returns a pointer to the created object which should be free'd with 00175 * glmDelete(). 00176 * 00177 * filename - name of the file containing the Wavefront .OBJ format data. 00178 */ 00179 GLMmodel* 00180 glmReadOBJ(const char* filename); 00181 00182 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to 00183 * a file. 00184 * 00185 * model - initialized GLMmodel structure 00186 * filename - name of the file to write the Wavefront .OBJ format data to 00187 * mode - a bitwise or of values describing what is written to the file 00188 * GLM_NONE - write only vertices 00189 * GLM_FLAT - write facet normals 00190 * GLM_SMOOTH - write vertex normals 00191 * GLM_TEXTURE - write texture coords 00192 * GLM_FLAT and GLM_SMOOTH should not both be specified. 00193 */ 00194 GLvoid 00195 glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode); 00196 00197 /* glmDraw: Renders the model to the current OpenGL context using the 00198 * mode specified. 00199 * 00200 * model - initialized GLMmodel structure 00201 * mode - a bitwise OR of values describing what is to be rendered. 00202 * GLM_NONE - render with only vertices 00203 * GLM_FLAT - render with facet normals 00204 * GLM_SMOOTH - render with vertex normals 00205 * GLM_TEXTURE - render with texture coords 00206 * GLM_FLAT and GLM_SMOOTH should not both be specified. 00207 */ 00208 GLvoid 00209 glmDraw(GLMmodel* model, GLuint mode); 00210 00211 /* glmList: Generates and returns a display list for the model using 00212 * the mode specified. 00213 * 00214 * model - initialized GLMmodel structure 00215 * mode - a bitwise OR of values describing what is to be rendered. 00216 * GLM_NONE - render with only vertices 00217 * GLM_FLAT - render with facet normals 00218 * GLM_SMOOTH - render with vertex normals 00219 * GLM_TEXTURE - render with texture coords 00220 * GLM_FLAT and GLM_SMOOTH should not both be specified. 00221 */ 00222 GLuint 00223 glmList(GLMmodel* model, GLuint mode); 00224 00225 /* glmWeld: eliminate (weld) vectors that are within an epsilon of 00226 * each other. 00227 * 00228 * model - initialized GLMmodel structure 00229 * epsilon - maximum difference between vertices 00230 * ( 0.00001 is a good start for a unitized model) 00231 * 00232 */ 00233 GLvoid 00234 glmWeld(GLMmodel* model, GLfloat epsilon); 00235 00236 00237 00238 #ifdef __cplusplus 00239 } 00240 #endif /* __cplusplus */ 00241 00242 #endif /* __GLM_H */