本文将介绍如何从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处下载。
Glm头文件
2, 在第17篇的基础上,CCY457OpenGLView类中加入下述变量,用来表示不同物体类型
GLuint m_MonitorList; //显示器
GLuint m_ChairList; //椅子
GLuint m_PotList; //花瓶
GLuint m_ComputerList; //计算机
int m_nObjectNo;
2, 在InitializeOpenGL函数中加入对LoadModelsFromFiles的调用
3, 绘制函数修改如下:
复制代码
void CCY457OpenGLView::RenderScene ()
{//绘制函数
//Position Camera
gluLookAt(m_PosX,m_PosY,m_PosZ,m_DirX,m_DirY,m_DirZ,0.0f,1.0f,0.0f);
//Draw the Scene
//Draw the floor
// Draw the ground, we do manual shading to a darker green
// in the background to give the illusion of depth
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_Texture[3]);
glBegin(GL_POLYGON);
glColor3ub(0,255,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-2.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(2.0f,0.0f, 0.0f);
glColor3ub(0,100,0);    
glTexCoord2f(1.0f, 1.0f);
glVertex3f(2.0f, 0.0f, -2.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-2.0f,0.0f, -2.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//Draw the Cube
// Save the matrix state and do the rotations
glPushMatrix();
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);
DrawCube(FALSE);
// Restore original matrix state
glPopMatrix();    
// Get ready to draw the shadow and the ground
// First disable lighting and save the projection state
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glPushMatrix();
// 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
DrawCube(TRUE);    
// Restore the projection to normal
glPopMatrix();
// Restore lighting state variables
glEnable(GL_DEPTH_TEST);
// Draw the light source
glPushMatrix();
glTranslatef(1.5f,1.5f,-1.0f);
glColor3ub(255,255,0);
glutSolidSphere(0.01f,10,10);
glPopMatrix();
glEnable(GL_LIGHTING);
}
void CCY457OpenGLView::DrawCube (BOOL bShadow)
{
// Set material color, note we only have to set to black
// for the shadow once
if(!bShadow)
{
switch (m_nObjectNo)
{
case 0: glCallList(m_ChairList);
break;
case 1: glCallList(m_PotList);
break;
case 2: glCallList(m_ComputerList);
break;
case 3: glCallList(m_MonitorList);
break;
case 4: DrawCubeTex();
break;
}
}
else
{
glColor3ub(0,0,0);
switch (m_nObjectNo)
{
case 0: glCallList(m_ChairList);
break;
case 1: glCallList(m_PotList);
break;
case 2: glCallList(m_ComputerList);
break;
case 3: glCallList(m_MonitorList);
break;
case 4: DrawCubeNoTex();
break;    
}
}
}
复制代码
4, 加载Obj文件的具体实现代码:
复制代码
//Load all the Models from the Files of type .obj
void CCY457OpenGLView::LoadModelsFromFiles()
{
GLfloat scalefactor = 0.0;
//Load Computer from file
GLMmodel *object1;
object1 = glmReadOBJ("models/computer.obj");
if(!scalefactor) 
{
scalefactor = glmUnitize(object1);
else 
{
glmScale(object1, scalefactor);
}
glmScale(object1, 2.5);    
/* build a display list */
m_ComputerList = glmList(object1, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
glmDelete(object1);    
//Load Chair From File
GLMmodel *object2;
scalefactor = 0.0;
object2 = glmReadOBJ("models/chair04.obj");
if(!scalefactor) 
{
scalefactor = glmUnitize(object2);
else 
{
glmScale(object2, scalefactor);
}
glmScale(object2, 5.0);    
/* build a display list */
m_ChairList = glmList(object2, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
glmDelete(object2);    
//Load Monitor from file
GLMmodel *object5;
scalefactor = 0.0;
object5 = glmReadOBJ("models/samsung.obj");
if(!scalefactor) 
{
scalefactor = glmUnitize(object5);
else 
{
glmScale(object5, scalefactor);
}
glmScale(object5, 0.5);    
/* build a display list */
m_MonitorList = glmList(object5, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
glmDelete(object5);    
//Load Phone Object from file
GLMmodel *object6;
scalefactor = 0.0;
object6 = glmReadOBJ("models/plant2.obj");
if(!scalefactor) 
{
scalefactor = glmUnitize(object6);
else 
{
glmScale(object6, scalefactor);
}
glmScale(object6, 0.5);    
/* build a display list */
m_PotList = glmList(object6, GLM_SMOOTH);
/* nuke it, we don't need it anymore */
glmDelete(object6);        
}
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/07/1328957.html,如需转载请自行联系原作者

《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format相关推荐

  1. 【转】《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows(1)

    本文源代码下载:OpenGL_ch2.rar WGL – Windows 的 OpenGL 扩展层 The WGL extension consists of a set of functions ( ...

  2. 《基于MFC的OpenGL编程》Part 1 A Primer

    3D图形学基本概念 Perspective Perspective refers to the angles between the lines that lend the illusion of t ...

  3. 《基于MFC的OpenGL编程》Part 14 Quadrics

    本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码: Quadrics Every quadric has a few settings associated with it. We have ...

  4. 《基于MFC的OpenGL编程》Part 8 Colors

         OpenGL支持两种颜色模式:RGBA和颜色索引模式,本文关注于前者. Smooth Shading and Flat Shading When Smooth Shading is spec ...

  5. 基于MFC的socket编程(异步非阻塞通信)

    对于许多初学者来说,网络通信程序的开发,普遍的一个现象就是觉得难以入手.许多概念,诸如:同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等,初学者往往迷惑不清,只知其 ...

  6. 计算机图形绘制三棱柱源代码,基于MFC用OpenGL画得一个三棱柱

    [实例简介] 改程序是我自学OpenGL时仿照代码敲得一个例子,觉得OpenGL真是一个神奇的工具,画得3D图形太强了 [实例截图] [核心代码] SdiOpenGL └── SdiOpenGL ├─ ...

  7. OpenGL编程设置

    OpenGL编程设置 13小时前 一.环境配置 由于微软公司为了推销自己的产品DirectX,击败OpenGL,因此现在的windows操作系统只支持OpenGL1.1版本,而现在的OpengGL版本 ...

  8. 网络编程-基于MFC的仿QQ聊天室-2020

    基于MFC的仿QQ聊天室(2020) 有幸学习过网络编程的一些知识,出于对编程的热爱,把曾经的一次简单实践编程作业进行了自定义的完成. 编程所需: 编程工具为VS 2010,需要掌握MFC的基本操作以 ...

  9. MFC——OpenGL编程

    目标 (1) 绘制三个立方体,位置如图所示.要求绘制出坐标系的 3 个坐标轴. (2)三个立方体一起沿着坐标轴(x 或 y 或 z)旋转起来.说 明:旋转可以通过设置时钟事件来实现,提供的程序里 已经 ...

最新文章

  1. SAP 关于EWM的WT增强简介
  2. Android 知识点梳理
  3. java 资料大全-转
  4. 第十章 优先级队列 (b3)完全二叉堆:删除与下滤
  5. clnt_create: RPC: Program not registered
  6. vue项目配置eslint(附visio studio code配置)
  7. python做大数据的框架_Python+大数据计算平台,PyODPS架构手把手教你搭建
  8. 小程序功能模块-优客娱乐视频1.0.5源码
  9. 四舍五入算法 php,3种PHP实现四舍五入的方法
  10. 五月数据库技术通讯丨Oracle 12c因新特性引发异常Library Cache Lock等待
  11. python处理数据集并制作词云图
  12. 第五篇:明确拒绝不想编译器自动生成的拷贝构造函数和赋值运算符重载函数...
  13. 网站Webshell大马密码极速暴力爆破工具-cheetah
  14. 十大顶级大数据可视化工具
  15. python3修改文件的编码格式_python批量修改文件编码格式的方法
  16. 产品发布会快闪创意动画PPT模板
  17. word文档docx转为pdf文件,在Linux操作系统上也能正常显示中文
  18. linux 桌面对比,七大顶级Linux桌面比较
  19. ue4/5玻璃材质制作(简单易懂上手)
  20. 万万没想到,“红孩儿”竟然做了程序员,还是CTO!

热门文章

  1. RabbitMQ入门(2)--工作队列
  2. 将DataRow转换为DataTable
  3. web前端网站推荐(后续继续补充)
  4. JavaScript中Ajax
  5. 成功网络管理员必备“软件”素质
  6. DB2 一个汉字的Byte数,太操蛋了
  7. ps aux 查看进程
  8. [Angularjs]视图和路由(四)
  9. 基于windows PE文件的恶意代码分析;使用SystemInternal工具与内核调试器研究windows用户空间与内核空间...
  10. 软件过程评估和软件能力评价之间的差异