本文将介绍如何从Obj文件格式中创建3D对象,我们使用的是Nate Miller的obj格式加载类。
This would be very useful to create large Virtual Reality applications as we could make use of the readily available 3D model files or make use of modeling tools to create these models and load them instead of creating them programatically. The .obj format is a very simple and popular format and files of other types such 3D Studio (.3ds) can be exported to this 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 use of material data too.
1, Nate Miller的obj文件加载类,其完整源代码可以从http://www.pobox.com/~ndr处下载。
2, 在第17篇的基础上,CCY457OpenGLView类中加入下述变量,用来表示不同物体类型
GLuint m_MonitorList; //显示器
GLuint m_ComputerList; //计算机
2, 在InitializeOpenGL函数中加入对LoadModelsFromFiles的调用
void CCY457OpenGLView::RenderScene ()
gluLookAt(m_PosX,m_PosY,m_PosZ,m_DirX,m_DirY,m_DirZ,0.0f,1.0f,0.0f);
// Draw the ground, we do manual shading to a darker green
// in the background to give the illusion of depth
glBindTexture(GL_TEXTURE_2D, m_Texture[3]);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-2.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(2.0f,0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(2.0f, 0.0f, -2.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-2.0f,0.0f, -2.0f);
glDisable(GL_TEXTURE_2D);
// Save the matrix state and do the rotations
glTranslatef(-1.0f,0.6f,-1.0f);
// Draw jet at new orientation, put light in correct position
// before rotating the jet
glRotatef(m_xRot,1.0f,0.0f,0.0f);
glRotatef(m_yRot,0.0f,1.0f,0.0f);
// Restore original matrix state
// Get ready to draw the shadow and the ground
// First disable lighting and save the projection state
glDisable(GL_DEPTH_TEST);
// Multiply by shadow projection matrix
glMultMatrixf((GLfloat *)m_ShadowMat);
glTranslatef(-1.0f,0.6f,-1.0f);
glRotatef(m_xRot,1.0f,0.0f,0.0f);
glRotatef(m_yRot,0.0f,1.0f,0.0f);
// Pass true to indicate drawing shadow
// Restore the projection to normal
// Restore lighting state variables
glTranslatef(1.5f,1.5f,-1.0f);
glutSolidSphere(0.01f,10,10);
void CCY457OpenGLView::DrawCube (BOOL bShadow)
// Set material color, note we only have to set to black
case 0: glCallList(m_ChairList);
case 1: glCallList(m_PotList);
case 2: glCallList(m_ComputerList);
case 3: glCallList(m_MonitorList);
case 0: glCallList(m_ChairList);
case 1: glCallList(m_PotList);
case 2: glCallList(m_ComputerList);
case 3: glCallList(m_MonitorList);
//Load all the Models from the Files of type .obj
void CCY457OpenGLView::LoadModelsFromFiles()
GLfloat scalefactor = 0.0;
//Load Computer from file
object1 = glmReadOBJ("models/computer.obj");
scalefactor = glmUnitize(object1);
glmScale(object1, scalefactor);
/* build a display list */
m_ComputerList = glmList(object1, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
object2 = glmReadOBJ("models/chair04.obj");
scalefactor = glmUnitize(object2);
glmScale(object2, scalefactor);
/* build a display list */
m_ChairList = glmList(object2, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
object5 = glmReadOBJ("models/samsung.obj");
scalefactor = glmUnitize(object5);
glmScale(object5, scalefactor);
/* build a display list */
m_MonitorList = glmList(object5, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
//Load Phone Object from file
object6 = glmReadOBJ("models/plant2.obj");
scalefactor = glmUnitize(object6);
glmScale(object6, scalefactor);
/* build a display list */
m_PotList = glmList(object6, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/07/1328957.html,如需转载请自行联系原作者