Fermat
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
glm.h
1 /*
2  * GLM library. Wavefront .obj file format reader/writer/manipulator.
3  *
4  * Written by Nate Robins, 1997.
5  * email: ndr@pobox.com
6  * www: http://www.pobox.com/~ndr
7  */
8 
9 #pragma once
10 
11 /* includes */
12 
13 /* defines */
14 #if 0
15 #define GLM_NONE (0) /* render with only vertices */
16 #define GLM_FLAT (1 << 0) /* render with facet normals */
17 #define GLM_SMOOTH (1 << 1) /* render with vertex normals */
18 #define GLM_TEXTURE (1 << 2) /* render with texture coords */
19 #define GLM_COLOR (1 << 3) /* render with colors */
20 #define GLM_MATERIAL (1 << 4) /* render with materials */
21 #endif
22 
23 enum {
24  GLM_NONE = (0),
25  GLM_FLAT = (1 << 0),
26  GLM_SMOOTH = (1 << 1),
27  GLM_TEXTURE = (1 << 2),
28  GLM_COLOR = (1 << 3),
29  GLM_MATERIAL = (1 << 4),
30  GLM_FLAT_SHADE = (1 << 5),
31  GLM_SPECULAR_SHADE = (1 << 6)
32 };
33 
34 enum Sizes {
35  MaxStringLength = 128
36 };
37 
38 /* structs */
39 
40 /* GLMmaterial: Structure that defines a material in a model.
41 */
42 typedef struct _GLMmaterial
43 {
44  char* name; /* name of material */
45  float diffuse[4]; // Kd diffuse component
46  float ambient[4]; // Ka ambient component
47  float specular[4]; // Ks specular component
48  float emissive[4]; // emissive component
49  float shininess; // Ns specular exponent
50  float refraction; // Tr
51  float alpha; // d
52  float reflectivity; // reflection
53  int shader; // illum
54 
55  // Texture maps, zero length if not specified.
56  char ambient_map [MaxStringLength]; // map_Ka
57  char diffuse_map [MaxStringLength]; // map_Kd
58  char specular_map[MaxStringLength]; // map_Ks
59  char dissolve_map[MaxStringLength]; // map_D
60 
61  // Scaling for texture maps (initialized to 0 for not set)
62  float ambient_map_scaling[2];
63  float diffuse_map_scaling[2];
64  float specular_map_scaling[2];
65  float dissolve_map_scaling[2];
66 
67 } GLMmaterial;
68 
69 /* GLMtriangle: Structure that defines a triangle in a model.
70 */
71 typedef struct {
72  unsigned int vindices[3]; /* array of triangle vertex indices */
73  unsigned int nindices[3]; /* array of triangle normal indices */
74  unsigned int tindices[3]; /* array of triangle texcoord indices*/
75  unsigned int findex; /* index of triangle facet normal */
76 } GLMtriangle;
77 
78 /* GLMgroup: Structure that defines a group in a model.
79 */
80 typedef struct _GLMgroup {
81  char* name; /* name of this group */
82  unsigned int numtriangles; /* number of triangles in this group */
83  unsigned int* triangles; /* array of triangle indices */
84  unsigned int material; /* index to material for group */
85  char* mtlname; /*name of the material for this group*/
86  struct _GLMgroup* next; /* pointer to next group in model */
87 } GLMgroup;
88 
89 /* GLMmodel: Structure that defines a model.
90 */
91 typedef struct {
92  char* pathname; /* path to this model */
93  char* mtllibname; /* name of the material library */
94 
95  unsigned int numvertices; /* number of vertices in model */
96  float* vertices; /* array of vertices
97  [x1,y1,z1,x2,y2,z2...] */
98  unsigned char* vertexColors; /* array of vertex colors */
99 
100  unsigned int numnormals; /* number of normals in model */
101  float* normals; /* array of normals */
102 
103  unsigned int numtexcoords; /* number of texcoords in model */
104  float* texcoords; /* array of texture coordinates */
105 
106  unsigned int numfacetnorms; /* number of facetnorms in model */
107  float* facetnorms; /* array of facetnorms */
108 
109  unsigned int numtriangles; /* number of triangles in model */
110  GLMtriangle* triangles; /* array of triangles */
111 
112  unsigned int nummaterials; /* number of materials in model */
113  GLMmaterial* materials; /* array of materials */
114 
115  /* This is the thing you will want to iterate over. Each group has
116  an associated list of triangles, material, and name. */
117  unsigned int numgroups; /* number of groups in model */
118  GLMgroup* groups; /* linked list of groups */
119 
120  float position[3]; /* position of the model */
121 
122  bool usePerVertexColors; /* Are there per vertex colors? */
123 
124 } GLMmodel;
125 
126 
127 /* public functions */
128 
129 /* glmUnitize: "unitize" a model by translating it to the origin and
130  * scaling it to fit in a unit cube around the origin. Returns the
131  * scalefactor used.
132  *
133  * model - properly initialized GLMmodel structure
134  */
135 float
136 glmUnitize(GLMmodel* model);
137 
138 /*
139  * glmBoundingBox: Calculates the min/max positions of the model
140  */
141 void
142 glmBoundingBox(GLMmodel *model, float *minpos, float *maxpos);
143 
144 
145 /* glmDimensions: Calculates the dimensions (width, height, depth) of
146  * a model.
147  *
148  * model - initialized GLMmodel structure
149  * dimensions - array of 3 floats (float dimensions[3])
150  */
151 void
152 glmDimensions(GLMmodel* model, float* dimensions);
153 
154 /* glmScale: Scales a model by a given amount.
155  *
156  * model - properly initialized GLMmodel structure
157  * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
158  */
159 void
160 glmScale(GLMmodel* model, float scale);
161 
162 /* glmReverseWinding: Reverse the polygon winding for all polygons in
163  * this model. Default winding is counter-clockwise. Also changes
164  * the direction of the normals.
165  *
166  * model - properly initialized GLMmodel structure
167  */
168 void
169 glmReverseWinding(GLMmodel* model);
170 
171 /* glmFacetNormals: Generates facet normals for a model (by taking the
172  * cross product of the two vectors derived from the sides of each
173  * triangle). Assumes a counter-clockwise winding.
174  *
175  * model - initialized GLMmodel structure
176  */
177 void
178 glmFacetNormals(GLMmodel* model);
179 
180 /* glmVertexNormals: Generates smooth vertex normals for a model.
181  * First builds a list of all the triangles each vertex is in. Then
182  * loops through each vertex in the the list averaging all the facet
183  * normals of the triangles each vertex is in. Finally, sets the
184  * normal index in the triangle for the vertex to the generated smooth
185  * normal. If the dot product of a facet normal and the facet normal
186  * associated with the first triangle in the list of triangles the
187  * current vertex is in is greater than the cosine of the angle
188  * parameter to the function, that facet normal is not added into the
189  * average normal calculation and the corresponding vertex is given
190  * the facet normal. This tends to preserve hard edges. The angle to
191  * use depends on the model, but 90 degrees is usually a good start.
192  *
193  * model - initialized GLMmodel structure
194  * angle - maximum angle (in degrees) to smooth across
195  */
196 void
197 glmVertexNormals(GLMmodel* model, float angle);
198 
199 /* glmLinearTexture: Generates texture coordinates according to a
200  * linear projection of the texture map. It generates these by
201  * linearly mapping the vertices onto a square.
202  *
203  * model - pointer to initialized GLMmodel structure
204  */
205 void
206 glmLinearTexture(GLMmodel* model);
207 
208 /* glmSpheremapTexture: Generates texture coordinates according to a
209  * spherical projection of the texture map. Sometimes referred to as
210  * spheremap, or reflection map texture coordinates. It generates
211  * these by using the normal to calculate where that vertex would map
212  * onto a sphere. Since it is impossible to map something flat
213  * perfectly onto something spherical, there is distortion at the
214  * poles. This particular implementation causes the poles along the X
215  * axis to be distorted.
216  *
217  * model - pointer to initialized GLMmodel structure
218  */
219 void
220 glmSpheremapTexture(GLMmodel* model);
221 
222 /* glmDelete: Deletes a GLMmodel structure.
223  *
224  * model - initialized GLMmodel structure
225  */
226 void glmDelete(GLMmodel* model);
227 
228 /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
229  * Returns a pointer to the created object which should be free'd with
230  * glmDelete().
231  *
232  * filename - name of the file containing the Wavefront .OBJ format data.
233  *
234  * returns 0 if there was a problem reading the file.
235  */
236 GLMmodel* glmReadOBJ(const char* filename);
237 
238 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
239  * a file.
240  *
241  * model - initialized GLMmodel structure
242  * filename - name of the file to write the Wavefront .OBJ format data to
243  * mode - a bitwise or of values describing what is written to the file
244  * GLM_NONE - write only vertices
245  * GLM_FLAT - write facet normals
246  * GLM_SMOOTH - write vertex normals
247  * GLM_TEXTURE - write texture coords
248  * GLM_FLAT and GLM_SMOOTH should not both be specified.
249  *
250  * returns 1 if there was error, 0 otherwise.
251  *
252  */
253 int
254 glmWriteOBJ(GLMmodel* model, char* filename, unsigned int mode);
255 
256 /* glmWeld: eliminate (weld) vectors that are within an epsilon of
257  * each other.
258  *
259  * model - initialized GLMmodel structure
260  * epsilon - maximum difference between vertices
261  * ( 0.00001 is a good start for a unitized model)
262  *
263  */
264 void
265 glmWeld(GLMmodel* model, float epsilon);
266 
Definition: glm.h:42
Definition: glm.h:91
Definition: glm.h:71
Definition: glm.h:80
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > scale(const Vector< T, 3 > &vec)
build a 3d scaling matrix
Definition: matrix_inline.h:539