Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glm.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifndef GLM_H
  9. #define GLM_H
  10. typedef unsigned int uint;
  11. #ifndef M_PI
  12. #define M_PI 3.14159265
  13. #endif
  14. /* defines */
  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. /* structs */
  22. /* GLMmaterial: Structure that defines a material in a model.
  23. */
  24. typedef struct _GLMmaterial
  25. {
  26. char* name; /* name of material */
  27. float diffuse[4]; /* diffuse component */
  28. float ambient[4]; /* ambient component */
  29. float specular[4]; /* specular component */
  30. float emmissive[4]; /* emmissive component */
  31. float shininess; /* specular exponent */
  32. char *map_kd; /* diffuse texture map file */
  33. uint texture_kd; /* diffuse texture map */
  34. uint texture_ks; /* specular texture map */
  35. int uDiffuse, uAmbient, uSpecular, uShininess, uDiffTex, uSpecTex;
  36. uint prog;
  37. } GLMmaterial;
  38. /* GLMtriangle: Structure that defines a triangle in a model.
  39. */
  40. typedef struct {
  41. uint vindices[3]; /* array of triangle vertex indices */
  42. uint nindices[3]; /* array of triangle normal indices */
  43. uint tindices[3]; /* array of triangle texcoord indices*/
  44. uint findex; /* index of triangle facet normal */
  45. } GLMtriangle;
  46. /* GLMgroup: Structure that defines a group in a model.
  47. */
  48. typedef struct _GLMgroup {
  49. char* name; /* name of this group */
  50. uint numtriangles; /* number of triangles in this group */
  51. uint* triangles; /* array of triangle indices */
  52. uint material; /* index to material for group */
  53. uint * triIndexes;
  54. uint minIndex, maxIndex;
  55. struct _GLMgroup* next; /* pointer to next group in model */
  56. } GLMgroup;
  57. /* GLMmodel: Structure that defines a model.
  58. */
  59. typedef struct {
  60. char* pathname; /* path to this model */
  61. char* mtllibname; /* name of the material library */
  62. uint numvertices; /* number of vertices in model */
  63. float* vertices; /* array of vertices */
  64. uint numnormals; /* number of normals in model */
  65. float* normals; /* array of normals */
  66. uint numtexcoords; /* number of texcoords in model */
  67. float* texcoords; /* array of texture coordinates */
  68. uint numfacetnorms; /* number of facetnorms in model */
  69. float* facetnorms; /* array of facetnorms */
  70. uint numtriangles; /* number of triangles in model */
  71. GLMtriangle* triangles; /* array of triangles */
  72. uint nummaterials; /* number of materials in model */
  73. GLMmaterial* materials; /* array of materials */
  74. uint numgroups; /* number of groups in model */
  75. GLMgroup* groups; /* linked list of groups */
  76. float position[3]; /* position of the model */
  77. float scale;
  78. uint vbo; /* OpenGL VBO for vertex data */
  79. uint vertexSize; /* number of floats per vertex */
  80. uint posOffset; /* offset of position within vertex, in bytes */
  81. uint normOffset; /* offset of normal within vertex, in bytes */
  82. uint texOffset; /* offset of texcoord within vertex, in bytes */
  83. } GLMmodel;
  84. /* public functions */
  85. /* glmUnitize: "unitize" a model by translating it to the origin and
  86. * scaling it to fit in a unit cube around the origin. Returns the
  87. * scalefactor used.
  88. *
  89. * model - properly initialized GLMmodel structure
  90. */
  91. float
  92. glmUnitize(GLMmodel* model);
  93. /* glmDimensions: Calculates the dimensions (width, height, depth) of
  94. * a model.
  95. *
  96. * model - initialized GLMmodel structure
  97. * dimensions - array of 3 floats (float dimensions[3])
  98. */
  99. void
  100. glmDimensions(GLMmodel* model, float* dimensions);
  101. /* glmScale: Scales a model by a given amount.
  102. *
  103. * model - properly initialized GLMmodel structure
  104. * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
  105. */
  106. void
  107. glmScale(GLMmodel* model, float scale);
  108. /* glmReverseWinding: Reverse the polygon winding for all polygons in
  109. * this model. Default winding is counter-clockwise. Also changes
  110. * the direction of the normals.
  111. *
  112. * model - properly initialized GLMmodel structure
  113. */
  114. void
  115. glmReverseWinding(GLMmodel* model);
  116. /* glmFacetNormals: Generates facet normals for a model (by taking the
  117. * cross product of the two vectors derived from the sides of each
  118. * triangle). Assumes a counter-clockwise winding.
  119. *
  120. * model - initialized GLMmodel structure
  121. */
  122. void
  123. glmFacetNormals(GLMmodel* model);
  124. /* glmVertexNormals: Generates smooth vertex normals for a model.
  125. * First builds a list of all the triangles each vertex is in. Then
  126. * loops through each vertex in the the list averaging all the facet
  127. * normals of the triangles each vertex is in. Finally, sets the
  128. * normal index in the triangle for the vertex to the generated smooth
  129. * normal. If the dot product of a facet normal and the facet normal
  130. * associated with the first triangle in the list of triangles the
  131. * current vertex is in is greater than the cosine of the angle
  132. * parameter to the function, that facet normal is not added into the
  133. * average normal calculation and the corresponding vertex is given
  134. * the facet normal. This tends to preserve hard edges. The angle to
  135. * use depends on the model, but 90 degrees is usually a good start.
  136. *
  137. * model - initialized GLMmodel structure
  138. * angle - maximum angle (in degrees) to smooth across
  139. */
  140. void
  141. glmVertexNormals(GLMmodel* model, float angle);
  142. /* glmLinearTexture: Generates texture coordinates according to a
  143. * linear projection of the texture map. It generates these by
  144. * linearly mapping the vertices onto a square.
  145. *
  146. * model - pointer to initialized GLMmodel structure
  147. */
  148. void
  149. glmLinearTexture(GLMmodel* model);
  150. /* glmSpheremapTexture: Generates texture coordinates according to a
  151. * spherical projection of the texture map. Sometimes referred to as
  152. * spheremap, or reflection map texture coordinates. It generates
  153. * these by using the normal to calculate where that vertex would map
  154. * onto a sphere. Since it is impossible to map something flat
  155. * perfectly onto something spherical, there is distortion at the
  156. * poles. This particular implementation causes the poles along the X
  157. * axis to be distorted.
  158. *
  159. * model - pointer to initialized GLMmodel structure
  160. */
  161. void
  162. glmSpheremapTexture(GLMmodel* model);
  163. /* glmDelete: Deletes a GLMmodel structure.
  164. *
  165. * model - initialized GLMmodel structure
  166. */
  167. void
  168. glmDelete(GLMmodel* model);
  169. /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
  170. * Returns a pointer to the created object which should be free'd with
  171. * glmDelete().
  172. *
  173. * filename - name of the file containing the Wavefront .OBJ format data.
  174. */
  175. GLMmodel*
  176. glmReadOBJ(char* filename);
  177. /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
  178. * a file.
  179. *
  180. * model - initialized GLMmodel structure
  181. * filename - name of the file to write the Wavefront .OBJ format data to
  182. * mode - a bitwise or of values describing what is written to the file
  183. * GLM_NONE - write only vertices
  184. * GLM_FLAT - write facet normals
  185. * GLM_SMOOTH - write vertex normals
  186. * GLM_TEXTURE - write texture coords
  187. * GLM_FLAT and GLM_SMOOTH should not both be specified.
  188. */
  189. void
  190. glmWriteOBJ(GLMmodel* model, char* filename, uint mode);
  191. /* glmDraw: Renders the model to the current OpenGL context using the
  192. * mode specified.
  193. *
  194. * model - initialized GLMmodel structure
  195. * mode - a bitwise OR of values describing what is to be rendered.
  196. * GLM_NONE - render with only vertices
  197. * GLM_FLAT - render with facet normals
  198. * GLM_SMOOTH - render with vertex normals
  199. * GLM_TEXTURE - render with texture coords
  200. * GLM_FLAT and GLM_SMOOTH should not both be specified.
  201. */
  202. void
  203. glmDraw(GLMmodel* model, uint mode);
  204. /* glmList: Generates and returns a display list for the model using
  205. * the mode specified.
  206. *
  207. * model - initialized GLMmodel structure
  208. * mode - a bitwise OR of values describing what is to be rendered.
  209. * GLM_NONE - render with only vertices
  210. * GLM_FLAT - render with facet normals
  211. * GLM_SMOOTH - render with vertex normals
  212. * GLM_TEXTURE - render with texture coords
  213. * GLM_FLAT and GLM_SMOOTH should not both be specified.
  214. */
  215. uint
  216. glmList(GLMmodel* model, uint mode);
  217. /* glmWeld: eliminate (weld) vectors that are within an epsilon of
  218. * each other.
  219. *
  220. * model - initialized GLMmodel structure
  221. * epsilon - maximum difference between vertices
  222. * ( 0.00001 is a good start for a unitized model)
  223. *
  224. */
  225. void
  226. glmWeld(GLMmodel* model, float epsilon);
  227. void
  228. glmReIndex(GLMmodel *model);
  229. void
  230. glmMakeVBOs(GLMmodel *model);
  231. void
  232. glmDrawVBO(GLMmodel *model);
  233. void
  234. glmPrint(const GLMmodel *model);
  235. void
  236. glmShaderMaterial(GLMmaterial *mat);
  237. void
  238. glmLoadTextures(GLMmodel *model);
  239. void
  240. glmSpecularTexture(GLMmodel *model, uint cubeTex);
  241. #endif /* GLM_H */