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