root/OpenSceneGraph/trunk/src/osgPlugins/obj/glm.h
@
2441
| Revision 2441, 9.8 kB (checked in by robert, 10 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* |
| 2 | * Wavefront .obj file format reader. |
| 3 | * |
| 4 | * author: Nate Robins |
| 5 | * email: ndr@pobox.com |
| 6 | * www: http://www.pobox.com/~ndr |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | /* includes */ |
| 11 | /* #include "glut.h" */ |
| 12 | //#include <GL/glu.h> |
| 13 | //#include<GL/glut.h> |
| 14 | |
| 15 | // Replace about glu/glut calls with the more x-platform friendly osg version, |
| 16 | // also neither glu or glut were requied?! Only gl.h... even this I think |
| 17 | // should be removed since all the GLfloat etc could just as easily be floats. |
| 18 | // Will leave for now as it works... Robert Osfield, June 5th 2001. |
| 19 | #include <osg/GL> |
| 20 | #include <osg/UByte4> |
| 21 | |
| 22 | #ifndef M_PI |
| 23 | #define M_PI 3.14159265 |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | /* defines */ |
| 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 | /* structs */ |
| 37 | |
| 38 | /* GLMmaterial: Structure that defines a material in a model. |
| 39 | */ |
| 40 | struct GLMmaterial |
| 41 | { |
| 42 | char* name; /* name of material */ |
| 43 | GLfloat diffuse[4]; /* diffuse component */ |
| 44 | GLfloat ambient[4]; /* ambient component */ |
| 45 | GLfloat specular[4]; /* specular component */ |
| 46 | GLfloat emmissive[4]; /* emmissive component */ |
| 47 | GLfloat shininess; /* specular exponent */ |
| 48 | char* textureName; /* name of any attached texture, add by RO */ |
| 49 | bool textureReflection; /* true if texture is a reflection map */ |
| 50 | float alpha; /* alpha */ |
| 51 | float textureUScale; /* Scaling along U */ |
| 52 | float textureVScale; /* Scaling along V */ |
| 53 | float textureUOffset; /* Offset along U */ |
| 54 | float textureVOffset; /* Offset along V */ |
| 55 | |
| 56 | void init() |
| 57 | { |
| 58 | name = NULL; |
| 59 | diffuse[0] = diffuse[1] = diffuse[2] = 0.8f; diffuse[3] = 1.0f; |
| 60 | ambient[0] = ambient[1] = ambient[2] = 0.2f; ambient[3] = 1.0f; |
| 61 | specular[0] = specular[1] = specular[2] = 0.0f; specular[3] = 1.0f; |
| 62 | emmissive[0] = emmissive[1] = emmissive[2] = 0.0f; emmissive[3] = 1.0f; |
| 63 | shininess = 0.0f; |
| 64 | textureName = NULL; |
| 65 | textureReflection = false; |
| 66 | alpha = 1.0f; |
| 67 | } |
| 68 | |
| 69 | }; |
| 70 | |
| 71 | /* GLMtriangle: Structure that defines a triangle in a model. |
| 72 | */ |
| 73 | struct GLMtriangle { |
| 74 | unsigned int vindices[3]; /* array of triangle vertex indices */ |
| 75 | unsigned int nindices[3]; /* array of triangle normal indices */ |
| 76 | unsigned int tindices[3]; /* array of triangle texcoord indices*/ |
| 77 | unsigned int findex; /* index of triangle facet normal */ |
| 78 | void init() |
| 79 | { |
| 80 | vindices[0] = vindices[2] = vindices[2] = 0 ; |
| 81 | nindices[0] = nindices[2] = nindices[2] = 0 ; |
| 82 | tindices[0] = tindices[2] = tindices[2] = 0 ; |
| 83 | findex=0; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | /* GLMgroup: Structure that defines a group in a model. |
| 88 | */ |
| 89 | struct GLMgroup { |
| 90 | char* name; /* name of this group */ |
| 91 | unsigned int numtriangles; /* number of triangles in this group */ |
| 92 | unsigned int* triangles; /* array of triangle indices */ |
| 93 | unsigned int material; /* index to material for group */ |
| 94 | bool hastexcoords; /* set to true if triangles have texture coords */ |
| 95 | struct GLMgroup* next; /* pointer to next group in model */ |
| 96 | |
| 97 | void init() |
| 98 | { |
| 99 | name = NULL; |
| 100 | numtriangles = 0; |
| 101 | triangles = NULL; |
| 102 | material = 0; |
| 103 | hastexcoords = false; |
| 104 | next = NULL; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | /* GLMmodel: Structure that defines a model. |
| 109 | */ |
| 110 | struct GLMmodel { |
| 111 | char* pathname; /* path to this model */ |
| 112 | char* mtllibname; /* name of the material library */ |
| 113 | |
| 114 | unsigned int numvertices; /* number of vertices in model */ |
| 115 | GLfloat* vertices; /* array of vertices */ |
| 116 | |
| 117 | bool useColors; /* true if per vertex colors are present.*/ |
| 118 | osg::UByte4* colors; /* array of per vertex colors */ |
| 119 | |
| 120 | unsigned int numnormals; /* number of normals in model */ |
| 121 | GLfloat* normals; /* array of normals */ |
| 122 | |
| 123 | unsigned int numtexcoords; /* number of texcoords in model */ |
| 124 | GLfloat* texcoords; /* array of texture coordinates */ |
| 125 | |
| 126 | unsigned int numfacetnorms; /* number of facetnorms in model */ |
| 127 | GLfloat* facetnorms; /* array of facetnorms */ |
| 128 | |
| 129 | unsigned int numtriangles; /* number of triangles in model */ |
| 130 | GLMtriangle* triangles; /* array of triangles */ |
| 131 | |
| 132 | unsigned int nummaterials; /* number of materials in model */ |
| 133 | GLMmaterial* materials; /* array of materials */ |
| 134 | |
| 135 | unsigned int numgroups; /* number of groups in model */ |
| 136 | GLMgroup* groups; /* linked list of groups */ |
| 137 | |
| 138 | GLfloat position[3]; /* position of the model */ |
| 139 | |
| 140 | |
| 141 | void init() |
| 142 | { |
| 143 | pathname = NULL; |
| 144 | mtllibname = NULL; |
| 145 | |
| 146 | numvertices = 0; |
| 147 | vertices = NULL; |
| 148 | |
| 149 | useColors = false; |
| 150 | colors = NULL; |
| 151 | |
| 152 | numnormals = 0; |
| 153 | normals = NULL; |
| 154 | |
| 155 | numtexcoords = 0; |
| 156 | texcoords = NULL; |
| 157 | |
| 158 | numfacetnorms = 0; |
| 159 | facetnorms = NULL; |
| 160 | |
| 161 | numtriangles = 0; |
| 162 | triangles = NULL; |
| 163 | |
| 164 | nummaterials = 0; |
| 165 | materials = NULL; |
| 166 | |
| 167 | numgroups = 0; |
| 168 | groups = NULL; |
| 169 | |
| 170 | position[0] = position[1] = position[2] = 0.0f; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /* public functions */ |
| 178 | |
| 179 | /* glmUnitize: "unitize" a model by translating it to the origin and |
| 180 | * scaling it to fit in a unit cube around the origin. Returns the |
| 181 | * scalefactor used. |
| 182 | * |
| 183 | * model - properly initialized GLMmodel structure |
| 184 | */ |
| 185 | GLfloat |
| 186 | glmUnitize(GLMmodel* model); |
| 187 | |
| 188 | /* glmDimensions: Calculates the dimensions (width, height, depth) of |
| 189 | * a model. |
| 190 | * |
| 191 | * model - initialized GLMmodel structure |
| 192 | * dimensions - array of 3 GLfloats (GLfloat dimensions[3]) |
| 193 | */ |
| 194 | GLvoid |
| 195 | glmDimensions(GLMmodel* model, GLfloat* dimensions); |
| 196 | |
| 197 | /* glmScale: Scales a model by a given amount. |
| 198 | * |
| 199 | * model - properly initialized GLMmodel structure |
| 200 | * scale - scalefactor (0.5 = half as large, 2.0 = twice as large) |
| 201 | */ |
| 202 | GLvoid |
| 203 | glmScale(GLMmodel* model, GLfloat scale); |
| 204 | |
| 205 | /* glmReverseWinding: Reverse the polygon winding for all polygons in |
| 206 | * this model. Default winding is counter-clockwise. Also changes |
| 207 | * the direction of the normals. |
| 208 | * |
| 209 | * model - properly initialized GLMmodel structure |
| 210 | */ |
| 211 | GLvoid |
| 212 | glmReverseWinding(GLMmodel* model); |
| 213 | |
| 214 | /* glmFacetNormals: Generates facet normals for a model (by taking the |
| 215 | * cross product of the two vectors derived from the sides of each |
| 216 | * triangle). Assumes a counter-clockwise winding. |
| 217 | * |
| 218 | * model - initialized GLMmodel structure |
| 219 | */ |
| 220 | GLvoid |
| 221 | glmFacetNormals(GLMmodel* model); |
| 222 | |
| 223 | /* glmVertexNormals: Generates smooth vertex normals for a model. |
| 224 | * First builds a list of all the triangles each vertex is in. Then |
| 225 | * loops through each vertex in the the list averaging all the facet |
| 226 | * normals of the triangles each vertex is in. Finally, sets the |
| 227 | * normal index in the triangle for the vertex to the generated smooth |
| 228 | * normal. If the dot product of a facet normal and the facet normal |
| 229 | * associated with the first triangle in the list of triangles the |
| 230 | * current vertex is in is greater than the cosine of the angle |
| 231 | * parameter to the function, that facet normal is not added into the |
| 232 | * average normal calculation and the corresponding vertex is given |
| 233 | * the facet normal. This tends to preserve hard edges. The angle to |
| 234 | * use depends on the model, but 90 degrees is usually a good start. |
| 235 | * |
| 236 | * model - initialized GLMmodel structure |
| 237 | * angle - maximum angle (in degrees) to smooth across |
| 238 | */ |
| 239 | GLvoid |
| 240 | glmVertexNormals(GLMmodel* model, GLfloat angle); |
| 241 | |
| 242 | /* glmLinearTexture: Generates texture coordinates according to a |
| 243 | * linear projection of the texture map. It generates these by |
| 244 | * linearly mapping the vertices onto a square. |
| 245 | * |
| 246 | * model - pointer to initialized GLMmodel structure |
| 247 | */ |
| 248 | GLvoid |
| 249 | glmLinearTexture(GLMmodel* model); |
| 250 | |
| 251 | /* glmSpheremapTexture: Generates texture coordinates according to a |
| 252 | * spherical projection of the texture map. Sometimes referred to as |
| 253 | * spheremap, or reflection map texture coordinates. It generates |
| 254 | * these by using the normal to calculate where that vertex would map |
| 255 | * onto a sphere. Since it is impossible to map something flat |
| 256 | * perfectly onto something spherical, there is distortion at the |
| 257 | * poles. This particular implementation causes the poles along the X |
| 258 | * axis to be distorted. |
| 259 | * |
| 260 | * model - pointer to initialized GLMmodel structure |
| 261 | */ |
| 262 | GLvoid |
| 263 | glmSpheremapTexture(GLMmodel* model); |
| 264 | |
| 265 | /* glmDelete: Deletes a GLMmodel structure. |
| 266 | * |
| 267 | * model - initialized GLMmodel structure |
| 268 | */ |
| 269 | GLvoid |
| 270 | glmDelete(GLMmodel* model); |
| 271 | |
| 272 | /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file. |
| 273 | * Returns a pointer to the created object which should be free'd with |
| 274 | * glmDelete(). |
| 275 | * |
| 276 | * filename - name of the file containing the Wavefront .OBJ format data. |
| 277 | */ |
| 278 | GLMmodel* |
| 279 | glmReadOBJ(char* filename); |
| 280 | |
| 281 | /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to |
| 282 | * a file. |
| 283 | * |
| 284 | * model - initialized GLMmodel structure |
| 285 | * filename - name of the file to write the Wavefront .OBJ format data to |
| 286 | * mode - a bitwise or of values describing what is written to the file |
| 287 | * GLM_NONE - write only vertices |
| 288 | * GLM_FLAT - write facet normals |
| 289 | * GLM_SMOOTH - write vertex normals |
| 290 | * GLM_TEXTURE - write texture coords |
| 291 | * GLM_FLAT and GLM_SMOOTH should not both be specified. |
| 292 | */ |
| 293 | GLvoid |
| 294 | glmWriteOBJ(GLMmodel* model, char* filename, unsigned int mode); |
| 295 | |
| 296 | |
| 297 | /* glmWeld: eliminate (weld) vectors that are within an epsilon of |
| 298 | * each other. |
| 299 | * |
| 300 | * model - initialized GLMmodel structure |
| 301 | * epsilon - maximum difference between vertices |
| 302 | * ( 0.00001 is a good start for a unitized model) |
| 303 | * |
| 304 | */ |
| 305 | GLvoid |
| 306 | glmWeld(GLMmodel* model, GLfloat epsilon); |
Note: See TracBrowser
for help on using the browser.
