This article describes how to create a 3D object from the obj file format, and we use the Nate Miller's obj format loading class.
This would is very useful to create large Virtual Reality applications as we could make use of the readily available 3D mo del files or make use of modeling tools to create models and load them instead of creating them. The. obj format is a very simple and popular format and files of the other types such 3D Studio (. 3ds) can are exported to this The format or converted using tools such as 3D exploration. this. obj loading code cannot read textures, it can only also read. mtl files in addition to the. obj file and thus make u SE of material data too.
1, Nate Miller's obj file loading class, its complete source code can be downloaded from the Http://www.pobox.com/~ndr.
GLM header File
/*
* wavefront. obj file format reader.
*
* Author:nate Robins
* email:ndr@pobox.com
* Www:http://www.pobox.com/~ndr
*/
* Includes * *
#include <GL/glut.h>
#ifndef M_PI
#define M_PI 3.14159265
#endif
* Defines * *
#define GLM_NONE (0)/* Render with only vertices *
#define GLM_FLAT (1 << 0)/* Render with facet normals * *
#define GLM_SMOOTH (1 << 1)/* Render with vertex normals * *
#define GLM_TEXTURE (1 << 2)/* Render with texture coords * *
#define GLM_COLOR (1 << 3)/* Render with colors * *
#define GLM_MATERIAL (1 << 4)/* Render with materials * *
* Structs * *
/* Glmmaterial:structure that defines a material in a model.
*/
typedef struct _GLMMATERIAL
{
char* name; /* Name of material * *
Glfloat Diffuse[4]; /* Diffuse Component * *
Glfloat Ambient[4]; /* Ambient Component * *
Glfloat Specular[4]; /* Specular Component * *
Glfloat Emmissive[4]; /* emmissive Component * *
Glfloat shininess; /* Specular exponent * *
} glmmaterial;
/* Glmtriangle:structure that defines a triangle in a model.
*/
typedef struct {
Gluint Vindices[3]; /* Array of triangle vertex indices * *
Gluint Nindices[3]; /* Array of triangle normal indices * *
Gluint Tindices[3]; /* Array of triangle Texcoord indices*/
Gluint Findex; /* Index of triangle facet normal */
} Glmtriangle;
/* Glmgroup:structure that defines a group in a model.
*/
typedef struct _GLMGROUP {
char* name; /* Name of this group * *
Gluint Numtriangles; /* Number of triangles in this group * *
gluint* triangles; /* Array of triangle indices * *
Gluint material; /* Index to material for group */
struct _glmgroup* next; /* Pointer to next group in model * *
} Glmgroup;
/* Glmmodel:structure that defines a model.
*/
typedef struct {
char* pathname; /* Path to this model * *
char* Mtllibname; /* Name of the material library * *
Gluint numvertices; /* Number of vertices in model * *
glfloat* vertices; /* Array of vertices * *
Gluint numnormals; /* Number of normals in model * *
glfloat* normals; /* Array of normals * *
Gluint numtexcoords; /* Number of Texcoords in model * *
glfloat* texcoords; /* array of texture coordinates * *
Gluint numfacetnorms; /* Number of facetnorms in model * *
glfloat* facetnorms; /* Array of facetnorms * *
Gluint Numtriangles; /* Number of triangles in model * *
glmtriangle* triangles; /* Array of triangles * *
Gluint nummaterials; /* Number of materials in model * *
glmmaterial* materials; /* Array of materials * *
Gluint numgroups; /* Number of groups in model * *
glmgroup* groups; /* Linked List of groups * *
Glfloat Position[3]; /* Position of the model * *
} Glmmodel;
/* Public Functions * *
/* Glmunitize: "Unitize" a model by translating it to the origin and
* Scaling it to fit in a unit cube around the origin. Returns the
* Scalefactor used.
*
* Model-properly initialized Glmmodel structure
*/
Glfloat
Glmunitize (glmmodel* model);
/* Glmdimensions:calculates The dimensions (width, height, depth) of
* A model.
*
* Model-initialized Glmmodel structure
* Dimensions-array of 3 glfloats (Glfloat dimensions[3])
*/
Glvoid
Glmdimensions (glmmodel* model, glfloat* dimensions);
/* Glmscale:scales a model by a given amount.
*
* Model-properly initialized Glmmodel structure
* Scale-scalefactor (0.5 = half as large, 2.0 = twice as Large)
*/
Glvoid
Glmscale (glmmodel* model, glfloat scale);
/* Glmreversewinding:reverse The polygon winding for all polygons in
* This model. Default Winding is counter-clockwise. Also changes
* The direction of the normals.
*
* Model-properly initialized Glmmodel structure
*/
Glvoid
Glmreversewinding (glmmodel* model);
/* glmfacetnormals:generates facet normals for a-model by taking the
* Cross product of the two vectors derived from the sides of each
* triangle). Assumes a counter-clockwise winding.
*
* Model-initialized Glmmodel structure
*/
Glvoid
Glmfacetnormals (glmmodel* model);
/* Glmvertexnormals:generates smooth vertex normals for a model.
* Builds a list of all the triangles each vertex are in. Then
* Loops through each vertex in the ' list averaging all facet
* Normals of the triangles each vertex are in. Finally, sets the
* Normal index in the triangle for the vertex to the generated smooth
* Normal. If the dot product of a facet normal and the facet normal
* Associated with the ' the ' Triangle in the list of triangles the
* Current vertex are in greater than the cosine of the angle
* parameter to the function, which is facet normal isn't added into the
* Average normal calculation and the corresponding vertex is given
* the facet normal. This is tends to preserve hard edges. The angle to
* Use depends in the model, but degrees is usually a good start.
*
* Model-initialized Glmmodel structure
* Angle-maximum angle (in degrees) to smooth across
*/
Glvoid
Glmvertexnormals (glmmodel* model, glfloat angle);
/* glmlineartexture:generates texture coordinates according to a
* Linear projection of the texture map. It generates by
* Linearly mapping the vertices onto a square.
*
* Model-pointer to initialized Glmmodel structure
*/
Glvoid
Glmlineartexture (glmmodel* model);
/* glmspheremaptexture:generates texture coordinates according to a
* Spherical projection of the texture map. Sometimes referred to as
* SphereMap, or reflection map texture coordinates. It generates
* These are using the normal to calculate where that vertex would map
* onto a sphere. Since It is impossible to map something flat
* Perfectly onto something spherical, there is distortion at the
* Poles. This particular implementation causes the poles along the X
* axis to be distorted.
*
* Model-pointer to initialized Glmmodel structure
*/
Glvoid
Glmspheremaptexture (glmmodel* model);
/* Glmdelete:deletes a Glmmodel structure.
*
* Model-initialized Glmmodel structure
*/
Glvoid
Glmdelete (glmmodel* model);
/* Glmreadobj:reads A model description from a wavefront. OBJ file.
* Returns A pointer to the created object which should is free ' d with
* Glmdelete ().
*
* Filename-name of the file containing the wavefront. OBJ format data.
*/
glmmodel*
Glmreadobj (char* filename);
/* Glmwriteobj:writes A model description in Wavefront. OBJ format to
* a file.
*
* Model-initialized Glmmodel structure
* Filename-name of the file to write the wavefront. OBJ Format data to
* Mode-a bitwise OR of values describing what is written to the file
* Glm_none-write only vertices
* Glm_flat-write facet Normals
* Glm_smooth-write Vertex normals
* Glm_texture-write Texture coords
* Glm_flat and Glm_smooth should not both to be specified.
*/
Glvoid
Glmwriteobj (glmmodel* model, char* filename, gluint mode);
/* Glmdraw:renders the model to the current OpenGL context using the
* Mode specified.
*
* Model-initialized Glmmodel structure
* Mode-a bitwise OR of values describing what is to be rendered.
* Glm_none-render with vertices
* Glm_flat-render with facet normals
* Glm_smooth-render with vertex normals
* Glm_texture-render with texture coords
* Glm_flat and Glm_smooth should not both to be specified.
*/
Glvoid
Glmdraw (glmmodel* model, gluint mode);
/* Glmlist:generates and returns a display list for the model using
* the mode specified.
*
* Model-initialized Glmmodel structure
* Mode-a bitwise OR of values describing what is to be rendered.
* Glm_none-render with vertices
* Glm_flat-render with facet normals
* Glm_smooth-render with vertex normals
* Glm_texture-render with texture coords
* Glm_flat and Glm_smooth should not both to be specified.
*/
Gluint
Glmlist (glmmodel* model, gluint mode);
/* glmweld:eliminate (weld) vectors that are within a epsilon of
* each other.
*
* Model-initialized Glmmodel structure
* Epsilon-maximum difference between vertices
* (0.00001 is a good the start for a unitized model)
*
*/
Glvoid
Glmweld (glmmodel* model, Glfloat Epsilon);