位置: 编程技术 - 正文

OpenGL - obj文件的导入(opengl读取obj文件)

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

推荐整理分享OpenGL - obj文件的导入(opengl读取obj文件),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:opengl obj模型,opengl 文档,opengl文件格式,opengl 文档,opengl obj模型,opengl obj模型,opengl读取obj文件,opengl导入obj能动起来吗,内容如对您有帮助,希望把文章链接给更多的朋友!

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

OBJ文件导出

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

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

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

接下来创建一个猴脸!

Add -> Mesh -> Monkey

File -> Export -> .obj

下面的选项一定要注意!

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

用gedit打开看一下:

# Blender v2. (sub 0) OBJ File: 'quit.blend'# www.blender.orgo Monkeyv 0. 0. -0.v -0. 0. -0.v 0. 0. -0.v -0. 0. -0.

... ...

vn 0. 0. 0.vn -0. 0. 0.vn -0. 0. 0.... ...

f // // //f // // //f // // //f // // //

#表示注释

v :顶点的坐标

OpenGL - obj文件的导入(opengl读取obj文件)

vn :顶点法向量

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

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

简单的Parser

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

[cpp] view plaincopybool loadOBJ( const char * path, std::vector<glm::vec3> & out_vertices, std::vector<glm::vec2> & out_uvs, std::vector<glm::vec3> & out_normals ){ printf("Loading OBJ file %s...n", path); std::vector<unsigned int> vertexIndices, uvIndices, normalIndices; std::vector<glm::vec3> temp_vertices; std::vector<glm::vec2> temp_uvs; std::vector<glm::vec3> temp_normals; FILE * file = fopen(path, "r"); if( file == NULL ){ printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for detailsn"); return false; } while( 1 ){ char lineHeader[]; // read the first word of the line int res = fscanf(file, "%s", lineHeader); if (res == EOF) break; // EOF = End Of File. Quit the loop. // else : parse lineHeader if ( strcmp( lineHeader, "v" ) == 0 ){ cout<<"Get v"<<endl; glm::vec3 vertex; fscanf(file, "%f %f %fn", &vertex.x, &vertex.y, &vertex.z ); temp_vertices.push_back(vertex); }else if ( strcmp( lineHeader, "vt" ) == 0 ){ cout<<"Get vt"<<endl; glm::vec2 uv; fscanf(file, "%f %fn", &uv.x, &uv.y ); 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. temp_uvs.push_back(uv); }else if ( strcmp( lineHeader, "vn" ) == 0 ){ cout<<"Get vn"<<endl; glm::vec3 normal; fscanf(file, "%f %f %fn", &normal.x, &normal.y, &normal.z ); temp_normals.push_back(normal); }else if ( strcmp( lineHeader, "f" ) == 0 ){ cout<<"Get f"<<endl; std::string vertex1, vertex2, vertex3; unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; int matches = fscanf(file, "%d//%d %d//%d %d//%dn", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]); if (matches != 6){ printf("File can't be read by our simple parser :-( Try exporting with other optionsn"); return false; } vertexIndices.push_back(vertexIndex[0]); vertexIndices.push_back(vertexIndex[1]); vertexIndices.push_back(vertexIndex[2]); normalIndices.push_back(normalIndex[0]); normalIndices.push_back(normalIndex[1]); normalIndices.push_back(normalIndex[2]); }else{ // Probably a comment, eat up the rest of the line char stupidBuffer[]; fgets(stupidBuffer, , file); } } // For each vertex of each triangle for( unsigned int i=0; i<vertexIndices.size(); i&#;&#; ){ // Get the indices of its attributes unsigned int vertexIndex = vertexIndices[i]; unsigned int normalIndex = normalIndices[i]; // Get the attributes thanks to the index glm::vec3 vertex = temp_vertices[ vertexIndex-1 ]; glm::vec3 normal = temp_normals[ normalIndex-1 ]; // Put the attributes in buffers out_vertices.push_back(vertex); out_normals .push_back(normal); } return true; }

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

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

[cpp] view plaincopy// Read our .obj file vector<glm::vec3> vertices; vector<glm::vec2> uvs; vector<glm::vec3> normals; bool res = loadOBJ("monkey.obj", vertices, uvs, normals); cout<<vertices.size()<<endl; for(int i=0; i<vertices.size(); i&#;&#;) { cout << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z<<endl; } 运行效果就想这样;

说明&#;式解析正确了。

安装glm库

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

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

可以去官网( 目录下面就可以使用了。

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

封装代码

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

首先来看一下项目结构:

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

cgl.h

[cpp] view plaincopy#ifndef CGL_H #define CGL_H #include <GL/glew.h> #include <GL/gl.h> #include <GL/glu.h> #include <SDL/SDL.h> #include "util.h" class CGL { public: CGL(); CGL(int _width, int _height); virtual ~CGL(); bool initGL(); bool resizeGL(int width,int height); bool renderGL(); protected: private: int width; int height; }; #endif // CGL_H

csdl.h

[cpp] view plaincopy#ifndef SDL_H #define SDL_H #include <SDL/SDL.h> #include <GL/glew.h> #include <GL/gl.h> #include <GL/glu.h> class CSDL { public: CSDL(); CSDL(int width, int height, int bpp, int flags); virtual ~CSDL(); void init(Uint flags); void quit(int code); void toggle_fullscreen(); void handleKeyEvent( SDL_keysym* keysym ); void handleEvent(); void setCaption(char *); protected: private: int screen_width; int screen_height; int screen_bpp; SDL_Surface *screen; //Whether the window is windowed or not bool windowed; //Whether the window is fine bool windowOK; }; #endif // SDL_H util.h[cpp] view plaincopy#ifndef UTIL_H #define UTIL_H #include <glm/glm.hpp> #include <vector> #include <string> #include <cstring> #include <iostream> using namespace std; class Util { public: Util(); virtual ~Util(); bool loadOBJ(const char * path,std::vector<glm::vec3> & out_vertices,std::vector<glm::vec2> & out_uvs,std::vector<glm::vec3> & out_normals); char *textFileRead(char *fn); protected: private: }; #endif // UTIL_H cpp就不贴了,太长。

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

[cpp] view plaincopy#include <iostream> #include "csdl.h" #include "cgl.h" using namespace std; const int SCREEN_WIDTH = ; const int SCREEN_HEIGHT =; const int SCREEN_BPP = ; int main() { CGL *gl = new CGL(SCREEN_WIDTH, SCREEN_HEIGHT); // Color depth in bits of our window. int flags= SDL_OPENGL|SDL_RESIZABLE; CSDL *sdl = new CSDL(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,flags, gl); sdl->setCaption("Load obj in OpenGL"); gl->initGL(); while(true) { sdl->handleEvent(gl); gl->renderGL(); } return 0;}

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

[cpp] view plaincopybool CGL::initGL() { float ratio = width / height; // Our shading model--Gouraud (smooth). glShadeModel( GL_SMOOTH ); // Set the clear color. glClearColor( 0, 0, 0, 0 ); // Setup our viewport. glViewport( 0, 0, width, height ); glEnable(GL_DEPTH_TEST); //Change to the projection matrix and set our viewing volume. glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( .0, ratio, 1.0, .0 ); GLfloat light_position[] = {.0, .0, .0, 0.0}; GLfloat white_light[] = {1.0, 0.0, 0.0, 1.0}; GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0}; glLightfv(GL_LIGHT0,GL_POSITION,light_position); glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light); glLightfv(GL_LIGHT0,GL_SPECULAR,white_light); GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess[] = {.0}; glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); Util util; //Read .obj file and store the infos bool res = util.loadOBJ("monkey.obj", vertices, uvs, normals); return( true ); } 接下来就可以进行渲染了:[cpp] view plaincopyglBegin(GL_TRIANGLES); for(i = 0; i<nodesSize; i&#;&#;) { glNormal3f(normals[i].x, normals[i].y, normals[i].z); glVertex3f( vertices[i].x, vertices[i].y, vertices[i].z); } glEnd(); 实际就是不断地绘制三角形...当然,你也可以去一些网站下载一些模型来载入~搜 Blender model

代码下载

SDL C&#;&#; 编程框架 - - - loading -

版权声明:本文为博主原创文章,未经博主允许不得转载。

freeglut 使用笔记 1.介绍GLUT代表OpenGL应用工具包,英文全称为OpenGLUtilityToolkit,是一个和窗口系统无关的软件包,可以跨平台使用,而freeglut是GLUT的一个完全开源替代库,

cocos2d-x 3.6 渲染绘制流程 Director::mainLoop()调用Director::drawSceneDirector::drawScene()调用Scene::visitScene::visit去遍历所有儿子节点,在儿子节点的visit函数中调用虚函数draw实现绘制精灵类

OpenGL入门学习 详细内容请点击下面链接

标签: opengl读取obj文件

本文链接地址:https://www.jiuchutong.com/biancheng/368442.html 转载请保留说明!

上一篇:VS2013 OpenGL MFC 编程问题

下一篇:freeglut 使用笔记(free怎么使用)

  • 非金融企业利息收入计入什么科目
  • 漏报了残保金如何调整?
  • 残保金滞纳金可以税前扣除吗
  • 提现的现金流量代码是什么
  • 企业会计准则季报报送哪几个报表
  • 政府补贴在企业怎么申请
  • 固定资产转让开票金额是净值
  • 其他收益在报表哪个位置
  • 会计中预付款余额是什么
  • 代扣个税手续费返还怎么申请
  • 预缴增值税为什么记借方
  • 工程承建方给予的工期奖励如何做账?
  • 被代持股份怎么退出
  • 出售固定资产的税率是多少
  • 园林设计属于什么
  • 公司为职工购买社保只购买三险可以吗
  • 业务招待住宿进什么科目
  • mac怎么安装字体文件
  • 股权转让的分录怎么做
  • 应交税费年末账务处理
  • 企业所得税汇算表
  • w10引导修复工具
  • php面向对象的三大特征
  • 强化税收风险意识
  • 买房 印花税
  • upload靶场搭建
  • 巴伐利亚森林国家公园钍自然真正自然
  • 存货成本在哪个表里
  • php curl设置cookie
  • PHP使用http_build_query()构造URL字符串的方法
  • AI:DeepSpeed Chat(一款帮用户训练自己模型的工具且简单/低成本/快 RLHF 训练类ChatGPT高质量大模型)的简介、安装、使用方法之详细攻略
  • php接口技术实例分析
  • 润滑脂开票属于成品油吗
  • 使用二氧化碳灭火器时人应该站在什么位置
  • echarts中国地图代码
  • 第十届蓝桥杯大赛个人赛省赛结果
  • ChatGPT 能自己跑代码了!
  • 嵌入式软件开票要求
  • 工伤保险赔偿范围和企业承担范围
  • 建筑业先预缴还是先扣税
  • 生产部门使用的各种机器设备属于什么会计科目
  • 一般纳税人只有销项没有进项
  • 织梦怎么建站
  • 土地被政府收回会计分录
  • 周转材料二级科目有哪些
  • mysql深入理解
  • 普通动产和特殊动产登记的区别
  • 生产辅助
  • 商誉需要交税吗
  • 销售收入未及时入账
  • sql查看日志
  • 资本公积的用途有哪些
  • 进口环节缴纳关税计入什么科目
  • 租赁房屋怎么做账
  • 营业执照变更法人需要本人去吗
  • 公司员工食堂买菜没发票怎么办
  • 收入准则适用范围
  • 厂商租赁公司
  • 可供出售金融资产属于什么科目
  • 行政单位的财务报表包括哪些
  • 私营公司固定资产怎么查
  • ubuntu系统怎么设置不锁屏
  • windows10x预览版
  • blg是什么文件
  • mac不同账户如何共享文件
  • 一开机弹出个微软重新设定
  • win1020h2版好不好
  • android开发流程
  • js一维数组转树结构
  • 狗刨好学吗
  • jquery 动态绑定
  • jquery插件使用教程
  • jquery.validate提示错误信息位置方法
  • unity接入安卓sdk
  • 轻轻松松学习JavaScript
  • 票种核定和税种核定的区别
  • 河南税务报到
  • 湖北税务登不上去是怎么了
  • 上海国家税务局官网发票查验平台
  • 票种核定在电子税务哪里查
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设