Sun Qiqung accompany you to learn--opengl loading the obj file library glm.c and glm.h

Source: Internet
Author: User
GLM implements the common use of the C language for obj files, which should suffice for simple 3D game programming. 3DS Max,maya can export the model in obj file format. The obj file contains data such as vertices, polygons, triangles, normal, and texture coordinates of the model, but does not contain textures and materials. The material can be placed in the obj file to specify a material library file, and when used, the material library file is typically put together with the obj file. Texture graphics will have to find a way.
Model data structure:
struct glmmodel{
char* pathname; Path to model files and material libraries
char* Mtllibname; Material library name Gluint numvertices; Top points
glfloat* vertices; Vector for storing vertices

Gluint numnormals; The Model normal number
glfloat* normals; Storing normal vector gluint numtexcoords; Number of texture coordinates
glfloat* texcoords; The vector of storing texture coordinates gluint numfacetnorms; Number of rule planes
glfloat* facetnorms; The vector of the storage rule plane gluint numtriangles; Number of triangles
glmtriangle* triangles; The vector of the storage triangle Gluint nummaterials; Number of materials
glmmaterial* materials; Vector for storing materials

Gluint numgroups;//number of primitive groups
glmgroup* groups;//the location of the vector glfloat position[3];//model of the stored entity. This model data structure is mainly used for its own expansion operations, and the following are the related actions that GLM provides://unit model and return scaling factor
The unit is to limit the model to a 3-D coordinate system centered on the midpoint of a cube by panning and scaling transformations within the square area
Glfloat glmunitize (glmmodel* model); Calculates the width, height, and depth of the model, and the result is stored in an array of 3 elements referred to by dimensions
Glvoid glmdimensions (glmmodel* model, glfloat* dimensions); Scale the model by proportional parameter, parameter greater than 1 magnification, greater than 0 less than 1 Zoom out, less than 0 reflection, equals 0 reduced to 0
Glvoid Glmscale (glmmodel* model, glfloat scale); Reverses the order of the polygon vertices of the model while reversing the normal vector, the default polygon vertex order is counterclockwise.
Glvoid glmreversewinding (glmmodel* model); Calculates the normal direction of the model face (assuming that the polygon vertex order is counterclockwise)
Glvoid glmfacetnormals (glmmodel* model); Calculates the smooth vertex normal direction of the model, the angle parameter is the maximum angle of the smoothed intersection (angle system)
Glvoid glmvertexnormals (glmmodel* model, glfloat angle); produces texture coordinates by linear projection, which maps vertices linearly to rectangles
Glvoid glmlineartexture (glmmodel* model); Create texture coordinates by spherical mapping
Glvoid glmspheremaptexture (glmmodel* model); Releasing the model from memory
Glvoid Glmdelete (glmmodel* model); Read the model from the Wavefront company standard. obj file
glmmodel* glmreadobj (char* filename); Press the model Wavefront. OBJ file format is written to the file, and the filename is specified by the filename parameter
mode specifies the Write method, which is the fetch or ("|" ) is a bitwise UNION:
Glm_none-processing by vertices only
Glm_flat-by Surface calculation method
Glm_smooth-According to vertex calculation method
Glm_texture-Contains texture coordinates
Glm_color-Contains only color information (solid color material)
Glm_material-Contains material information
where Glm_flat and Glm_smooth cannot be specified simultaneously
Glm_color and glm_material cannot be specified at the same time.
Glvoid Glmwriteobj (glmmodel* model, char* filename, gluint mode); Specify pattern by mode use current OpenGL draw context (context) to draw model
Mode parameter in the same glmreadobj
Glvoid Glmdraw (glmmodel* model, gluint mode); Generate OpenGL display list from model and return display list index number
Where the mode parameter is the same as the mode parameter in Glmwriteobj and Glmdraw
Gluint glmlist (glmmodel* model, gluint mode); Merge vertices with very small differences in the model, the epsilon parameter specifies the maximum gap between vertices to merge
Recommended Epsilon parameter 0.00001 is the starting point for the weld model
Glvoid Glmweld (glmmodel* model, Glfloat Epsilon); Reading a graphics file in PPM format
Returns a 24-bit color image pointer (used for OpenGL texture mapping and image drawing functions)
At the same time, the image size is stored in the address specified by the width,height pointer parameter
GLMREADPPM (char* filename, int* width, int* height);

2. Use of GLM.C

Below we learn how to use GLM.C.

First add the GLM.C to the project, and in the main file
#include "glm.h".

Load the file with the following code:

char* G_MODEL_FN = "Data\\al.obj";
glmmodel* G_model = NULL;

G_model = Glmreadobj (G_MODEL_FN);
if (!g_model) exit (0);
Glmunitize (G_model);
Glmscale (g_model,1.8);
Glmfacetnormals (G_model);
Glmvertexnormals (G_model, 90.0);

G_MODEL_FN is the file name.

G_model is a pointer to the GLMMODEL structure.

Glmreadobj loads an obj file and generates a GL
Mmodel the structure and returns its pointer. Later on this
This pointer is required as an identifier for the model operation.

Glmunitize model Normalization to one ( -0.5,-0.5,-0.5)-
(0.5,0.5,0.5) inside the box. The maximum size of this model
is 1, center at the origin point.

Glmscale the model as a telescopic transformation (al height 1.8-meter).

Glmfacetnormals for each polygon (facet) generation method of the model
to the vector.

Glmvertexnormals generates a normal for each vertex of the model
Vector. The normal vector of vertices is the normal vectors of adjacent faces.
The average value, so it needs to be pre-called
Glmfacetnormals. The second parameter represents a boundary,
If the angle between an adjacent polygon and the first adjacent polygon exceeds
This boundary, then the surface does not participate in the average. This will protect
Make a big transition without leaving the whole model in the corner.

The display model uses the following code:

Vector3 his_position (0,0.9,-2);

Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Gldisable (gl_texture_2d);

Gltranslatef (HIS_POSITION.X,HIS_POSITION.Y,HIS_POSITION.Z);
Glmdraw (G_model,glm_smooth | Glm_material);

His_position is a global variable that records the location of al. Al's
The position is represented by his center of gravity coordinates, so the y coordinate is 0.9.
(Y-coordinate vertical upward).

First, the local coordinate system is set to the bit of al using Gltranslatef.
Reset

Glmdraw Displays the model. The second parameter indicates how it is displayed.
Glm_smooth means smooth coloring, glm_material
Represents the material that comes with the model. In addition, there are other
Some ways: Glm_none only show vertices, Glm_flat
Using non-glossy shading), glm_texture
Use texture coordinates.

The model should also be deleted at the end of the program:

if (G_model) glmdelete (G_model);

GLM also includes some other functions:

Glvoid glmdimensions (glmmodel* model, glfloat* dimensions);
Get the model's length and width height
Glvoid glmreversewinding (glmmodel* model);
Reverse the vertex order of a polygon
Glvoid glmlineartexture (glmmodel* model);
Creating texture coordinates using projection-to-plane methods
Glvoid glmspheremaptexture (glmmodel* model);
Create texture coordinates by projecting to a sphere
Glvoid Glmwriteobj (glmmodel* model, char* filename, gluint mode);
To save a model as an obj file
Gluint glmlist (glmmodel* model, gluint mode);
Generating the display list for the model
Glvoid Glmweld (glmmodel* model, Glfloat Epsilon);
Merge vertices with distances less than epsilon
glubyte* glmreadppm (const char* filename, int* width, int* height);
Loading a PPM file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.