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