OpenGL进阶(十) - obj文件的导入

来源:互联网 发布:hex激活码 淘宝 编辑:程序博客网 时间:2024/05/14 07:53

http://blog.csdn.net/silangquan/article/details/9707347

Obj文件简介

        OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模型之间的互导,也可以通过Maya读写。比如你在3dsMax或LightWave中建了一个模型,想把它调到Maya里面渲染或动画,导出OBJ文件就是一种很好的选择。目前几乎所有知名的3D软件都支持OBJ文件的读写,不过其中很多需要通过插件才能实现。

          有一款开源的3D编辑,渲染软件,叫Blender,通过Blender建模,OpenGL导入,就可以更好的发挥想象,通过Linux+Blender+OpenGL这一套完全的开源平台,创造属于自己的3D世界。


OBJ文件导出

       首先需要在系统中安装Blender,软件中心搜一下就可以找到了,2.62版本。也可以到官网下载最新的2.68版本。

       安装好之后打开软件,已经建好了一个cubic,鼠标右键可以选定并且拖动模型,中键用于变换视角,滚轮用于放大和缩小视图。

       选中方块之后按delete把它删掉,因为这个模型太简单了,之所以要用三维建模软件,就是为了应付复杂的模型,如果只是一个cubic,直接画一下就出来了。

        接下来创建一个猴脸!

        Add -> Mesh -> Monkey



File -> Export -> .obj




 下面的选项一定要注意!



改一下名字,命名为monkey.obj, 回车~。


用gedit打开看一下:

# Blender v2.62 (sub 0) OBJ File: 'quit.blend'
# www.blender.org
o Monkey
v 0.576885 0.756494 -0.239630
v -0.298115 0.756494 -0.239630
v 0.639385 0.678369 -0.169318
v -0.360615 0.678369 -0.169318

... ...

vn 0.223706 0.722743 0.653910
vn -0.223706 0.722743 0.653910
vn -0.153610 0.967743 0.199693
... ...

f 96//81 152//81 150//81
f 153//82 99//82 97//82
f 153//82 97//82 151//82
f 98//83 100//83 154//83


#表示注释

v    :顶点的坐标

vn :顶点法向量

f    :面,96//81 152//81 150//81 指的是面的三个顶点,96指面的第一个顶点指的是前面定义的第96号顶点,81指的是第81个法向量,后面的两个顶点类推。


注:老版本的blender导出来的obj文件会有些许的不同,最好查看好obj的文件内容之后再进行解析!

简单的Parser

obj文件格式非常简单,用c++的vector可以很好的处理变长数组的问题。

[cpp] view plaincopy
  1. bool loadOBJ(  
  2.         const char * path,  
  3.         std::vector<glm::vec3> & out_vertices,  
  4.         std::vector<glm::vec2> & out_uvs,  
  5.         std::vector<glm::vec3> & out_normals  
  6. ){  
  7.         printf("Loading OBJ file %s...\n", path);  
  8.   
  9.         std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;  
  10.         std::vector<glm::vec3> temp_vertices;  
  11.         std::vector<glm::vec2> temp_uvs;  
  12.         std::vector<glm::vec3> temp_normals;  
  13.   
  14.   
  15.         FILE * file = fopen(path, "r");  
  16.         if( file == NULL ){  
  17.                 printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for details\n");  
  18.                 return false;  
  19.         }  
  20.   
  21.         while( 1 ){  
  22.   
  23.                 char lineHeader[128];  
  24.                 // read the first word of the line  
  25.                 int res = fscanf(file, "%s", lineHeader);  
  26.                 if (res == EOF)  
  27.                         break// EOF = End Of File. Quit the loop.  
  28.   
  29.                 // else : parse lineHeader  
  30.   
  31.                 if ( strcmp( lineHeader, "v" ) == 0 ){  
  32.                     cout<<"Get v"<<endl;  
  33.                         glm::vec3 vertex;  
  34.                         fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );  
  35.                         temp_vertices.push_back(vertex);  
  36.                 }else if ( strcmp( lineHeader, "vt" ) == 0 ){  
  37.                     cout<<"Get vt"<<endl;  
  38.                         glm::vec2 uv;  
  39.                         fscanf(file, "%f %f\n", &uv.x, &uv.y );  
  40.                         uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.  
  41.                         temp_uvs.push_back(uv);  
  42.                 }else if ( strcmp( lineHeader, "vn" ) == 0 ){  
  43.                     cout<<"Get vn"<<endl;  
  44.                         glm::vec3 normal;  
  45.                         fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );  
  46.                         temp_normals.push_back(normal);  
  47.                 }else if ( strcmp( lineHeader, "f" ) == 0 ){  
  48.                     cout<<"Get f"<<endl;  
  49.                         std::string vertex1, vertex2, vertex3;  
  50.                         unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];  
  51.                         int matches = fscanf(file, "%d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]);  
  52.                         if (matches != 6){  
  53.                                 printf("File can't be read by our simple parser :-( Try exporting with other options\n");  
  54.                                 return false;  
  55.                         }  
  56.                         vertexIndices.push_back(vertexIndex[0]);  
  57.                         vertexIndices.push_back(vertexIndex[1]);  
  58.                         vertexIndices.push_back(vertexIndex[2]);  
  59.                         normalIndices.push_back(normalIndex[0]);  
  60.                         normalIndices.push_back(normalIndex[1]);  
  61.                         normalIndices.push_back(normalIndex[2]);  
  62.                 }else{  
  63.                         // Probably a comment, eat up the rest of the line  
  64.                         char stupidBuffer[1000];  
  65.                         fgets(stupidBuffer, 1000, file);  
  66.                 }  
  67.   
  68.   
  69.         }  
  70.   
  71.         // For each vertex of each triangle  
  72.         for( unsigned int i=0; i<vertexIndices.size(); i++ ){  
  73.   
  74.                 // Get the indices of its attributes  
  75.                 unsigned int vertexIndex = vertexIndices[i];  
  76.                 unsigned int normalIndex = normalIndices[i];  
  77.   
  78.                 // Get the attributes thanks to the index  
  79.                 glm::vec3 vertex = temp_vertices[ vertexIndex-1 ];  
  80.                 glm::vec3 normal = temp_normals[ normalIndex-1 ];  
  81.   
  82.                 // Put the attributes in buffers  
  83.                 out_vertices.push_back(vertex);  
  84.                 out_normals .push_back(normal);  
  85.   
  86.         }  
  87.   
  88.         return true;  
  89. }  


这里只处理顶点和法向量,uv坐标再议。

下面的代码可以用来加载monkey.obj , 并且打印坐标信息。

[cpp] view plaincopy
  1. // Read our .obj file  
  2. vector<glm::vec3> vertices;  
  3. vector<glm::vec2> uvs;  
  4. vector<glm::vec3> normals;  
  5. bool res = loadOBJ("monkey.obj", vertices, uvs, normals);  
  6. cout<<vertices.size()<<endl;  
  7. for(int i=0; i<vertices.size(); i++)  
  8. {  
  9.     cout << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z<<endl;  
  10. }  

运行效果就想这样;


 说明格式解析正确了。


安装glm库

      由于C/C++标准库中没有几何数学库,这样造成在开发一个三维系统之初往往都需要自行实现一个实用的几何数学库,这样太费时费力了。Boost中有几何数学库,不过就这几行代码要用boost似乎有点牛刀小用了....GLM的出现可以很好的解决这个问题。
      GLM设计上遵照OpenGL Shading Language风格,使用开放的MIT授权协议。会GLSL的人可以很快上手。因采用了数据结构与函数方法分离的方式,可以很容易扩充函数方法而不改变原文件(增加新的头文件即可,不过得在不同的头文件中找函数方法比较费力)。

       顺便提一句,glm还可以很好的集成到cuda中去。

     可以去官网( http://glm.g-truc.net/)下载最新的源码,无需编译,解压之后直接将里面的glm文件夹拷贝到/usr/local/include 目录下面就可以使用了。

     官网可能被墙掉了,我自己上传了一份到csdn,点我下载。


封装代码

   之前的一些代码都是用C来完成,因为代码的没几行,但现在我觉得需要花点时间来重构一下代码了,改用面向对象,我用的是CodeBlocks来构建项目的,觉得不想用IDE的可以用CMake的。

首先来看一下项目结构:



cgl 是对OpenGL的封装,csdl是对SDL的封装,util则是一个工具类。简单看一下头文件。

cgl.h

[cpp] view plaincopy
  1. #ifndef CGL_H  
  2. #define CGL_H  
  3. #include <GL/glew.h>  
  4. #include <GL/gl.h>  
  5. #include <GL/glu.h>  
  6. #include <SDL/SDL.h>  
  7. #include "util.h"  
  8. class CGL  
  9. {  
  10.     public:  
  11.         CGL();  
  12.         CGL(int _width, int _height);  
  13.         virtual ~CGL();  
  14.         bool initGL();  
  15.         bool resizeGL(int width,int height);  
  16.         bool renderGL();  
  17.     protected:  
  18.     private:  
  19.         int width;  
  20.         int height;  
  21. };  
  22.   
  23. #endif // CGL_H  


csdl.h

[cpp] view plaincopy
  1. #ifndef SDL_H  
  2. #define SDL_H  
  3. #include <SDL/SDL.h>  
  4. #include <GL/glew.h>  
  5. #include <GL/gl.h>  
  6. #include <GL/glu.h>  
  7. class CSDL  
  8. {  
  9.     public:  
  10.         CSDL();  
  11.         CSDL(int width, int height, int bpp, int flags);  
  12.         virtual ~CSDL();  
  13.         void init(Uint32 flags);  
  14.         void quit(int code);  
  15.         void toggle_fullscreen();  
  16.         void handleKeyEvent( SDL_keysym* keysym );  
  17.         void handleEvent();  
  18.         void setCaption(char *);  
  19.     protected:  
  20.     private:  
  21.         int screen_width;  
  22.         int screen_height;  
  23.         int screen_bpp;  
  24.         SDL_Surface *screen;  
  25.         //Whether the window is windowed or not  
  26.         bool windowed;  
  27.         //Whether the window is fine  
  28.         bool windowOK;  
  29. };  
  30.   
  31. #endif // SDL_H  

util.h

[cpp] view plaincopy
  1. #ifndef UTIL_H  
  2. #define UTIL_H  
  3. #include <glm/glm.hpp>  
  4. #include <vector>  
  5. #include <string>  
  6. #include <cstring>  
  7. #include <iostream>  
  8. using namespace std;  
  9.   
  10. class Util  
  11. {  
  12.     public:  
  13.         Util();  
  14.         virtual ~Util();  
  15.         bool loadOBJ(const char * path,std::vector<glm::vec3> & out_vertices,std::vector<glm::vec2> & out_uvs,std::vector<glm::vec3> & out_normals);  
  16.         char *textFileRead(char *fn);  
  17.     protected:  
  18.     private:  
  19. };  
  20.   
  21. #endif // UTIL_H  

cpp就不贴了,太长。

这样,main.cpp就非常简洁了~

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include "csdl.h"  
  3. #include "cgl.h"  
  4.   
  5. using namespace std;  
  6. const int SCREEN_WIDTH = 800;  
  7. const int SCREEN_HEIGHT =800;  
  8. const int SCREEN_BPP = 32;  
  9. int main()  
  10. {  
  11. CGL *gl = new CGL(SCREEN_WIDTH, SCREEN_HEIGHT);  
  12. // Color depth in bits of our window.  
  13. int flags= SDL_OPENGL|SDL_RESIZABLE;  
  14. CSDL *sdl = new CSDL(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,flags, gl);  
  15. sdl->setCaption("Load obj in OpenGL");  
  16. gl->initGL();  
  17.  while(true) {   
  18.    sdl->handleEvent(gl);  
  19.    gl->renderGL();   
  20.    }   
  21. return 0;}  


需要修改渲染内容的话,直接修改cgl.cpp中的代码就可以了,要添加功能函数就在util中添加,而sdl类基本就不用修改了。
花了一个晚上了用面向对象的方式重构代码,这个时间是值得的——代码结构变得非常清晰,健壮性和可读性都有很大的提升,调试起来也更加方便。 
接下来就要在OpenGL中导入obj文件了。
首先修改一下initGL函数,设置一下灯光和材质,同时导入obj文件。

[cpp] view plaincopy
  1. bool CGL::initGL()  
  2. {  
  3.   
  4.     float ratio = width / height;  
  5.     // Our shading model--Gouraud (smooth).  
  6.     glShadeModel( GL_SMOOTH );  
  7.     // Set the clear color.  
  8.     glClearColor( 0, 0, 0, 0 );  
  9.     // Setup our viewport.  
  10.     glViewport( 0, 0, width, height );  
  11.     glEnable(GL_DEPTH_TEST);  
  12.     //Change to the projection matrix and set our viewing volume.  
  13.     glMatrixMode( GL_PROJECTION );  
  14.     glLoadIdentity();  
  15.     gluPerspective( 60.0, ratio, 1.0, 100.0 );  
  16.   
  17.     GLfloat light_position[] = {13.0, 13.0, 13.0, 0.0};  
  18.     GLfloat white_light[] = {1.0, 0.0, 0.0, 1.0};  
  19.     GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};  
  20.     glLightfv(GL_LIGHT0,GL_POSITION,light_position);  
  21.     glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);  
  22.     glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);  
  23.   
  24.     GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};  
  25.     GLfloat mat_shininess[] = {50.0};  
  26.     glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);  
  27.     glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);  
  28.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);  
  29.   
  30.     glEnable(GL_LIGHTING);  
  31.     glEnable(GL_LIGHT0);  
  32.   
  33.     Util util;  
  34.     //Read .obj file and store the infos  
  35.     bool res = util.loadOBJ("monkey.obj", vertices, uvs, normals);  
  36.     returntrue );  
  37. }  




接下来就可以进行渲染了:
[cpp] view plaincopy
  1. glBegin(GL_TRIANGLES);  
  2. for(i = 0; i<nodesSize; i++)  
  3. {  
  4. glNormal3f(normals[i].x, normals[i].y, normals[i].z);  
  5. glVertex3f( vertices[i].x, vertices[i].y, vertices[i].z);  
  6. }  
  7. glEnd();  



实际就是不断地绘制三角形...




当然,你也可以去一些网站下载一些模型来载入~搜 Blender model



代码下载

SDL C++ 编程框架 - http://download.csdn.net/detail/qp120291570/5856783

工程代码 - http://download.csdn.net/detail/qp120291570/5856935

参考:

《OpenGL编程指南》示例笔记(1)--渲染光照球体 - http://blog.csdn.net/norains/article/details/5684011
Model loading - http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading/
0 0
原创粉丝点击