如何在OpenGL中读取obj? [英] how to read obj in opengl?

查看:111
本文介绍了如何在OpenGL中读取obj?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以obj格式获取顶点和面信息?谢谢,谢谢!

how to get the vertices and face information in obj format ?please tell me,thanks!

推荐答案

您可能希望对此进行一些谷歌搜索,(提示:Lightwave Obj loader)将是一个很好的搜索词.实际上,已经有100多个,1000多个(如果没有100,000多个)这个问题的解决方案,都已经在互联网上等待着.

您需要了解的是:openGL实际上与OBJ文件本身没有任何关系-GL只负责显示该对象.

也就是说,您需要制定一种机制/方案来存储有关几何的所有必需信息,您需要制定一种方案来解析文件并填充数据结构.最后,在将相关信息分发到openGL之前,您需要找到一种选择要显示的多边形的方法.

去年我写了一篇,发现大概只"花了一天左右的时间. (当时不为
You may wish to do some Googling for it, (hint: Lightwave Obj loader) would be a good search term. There are indeed 100''s, 1000''s if not 100,000''s of solutions to this problem, all already out there waiting on the internet.

Something that you need to be aware of is: openGL doesn''t actually have anything much to do with the OBJ file itself - GL is only responsible for the display of that object.

That is to say, you need to work out a mechanism/scheme for storing all of the required information about the geometry, you need to work out a scheme for parsing the file and filling your data structure. Finally, you need to work out a way of selecting polygons for display before dispatching the relevant information over to openGL.

I wrote one last year, finding it probably ''only'' took a day or so. (wasn''t working for


工作)但是,这是在过去十年左右零星地思考问题之后才承认的.

它实际上是一种非常简单的文件格式,仅需数百行代码即可读取.
我还将指出,除非您想两次读取文件,否则应将对象数据存储在许多不同的(类型)std :: vector中.这是因为该格式不包含任何说明要遵循的特定类型项的数量的任何方式,因此您必须(a)对该文件进行两次分析以确定内存需求,或者仅(b)存储适当类型的向量中的每个项目.

这是我将所有数据以及数据类型定义存储在其中的向量.玩得开心!

//我的meshobj类的摘录
at the time) But, this was admittedly after thinking over the problem sporadically for the past decade or so.

It''s actually quite a simple file format and may be read in only a couple of hundred lines of code.
I''ll also point out that unless you want to read the file twice, you should store your object data inside a number of different(type) std::vector. This is because the format doesn''t include any way of telling how many of a particular type of item is to follow, so you have to either (a) parse the file twice to determine memory needs, or you simply (b)store each item in a vector of the appropriate type.

Here''s the vectors I store all of my data in, along with the data type definitions. Have fun!

// excerpt of my meshobj class
vector<vert> vertList;
vector<face> faceList;
vector<tex2d> texCoordList;
vector<vec3> normList;
vector<string> matLibList;



"myTypes.h"的完整代码



Complete code of "myTypes.h"

#ifndef mytypes_h_
 #define mytypes_h_
    #include <gl/gl.h>
    #include <vector>
    #include <math.h>

    //using namespace std;  // NASTY! - especially in a H file
    using std::vector;      // much better

    typedef struct
    {
        GLfloat x, y, z;
    } vert, vec3;

    // = v1-v2
    vec3 vecSub(vec3 v1, vec3 v2);
    float vecLen(vec3 v1);
    float dotProduct(vec3 v1, vec3 v2);
    vec3 normalizeVec(vec3 v1);
    vec3 crossProduct(vec3 v1, vec3 v2);

    typedef struct
    {
        GLfloat u, v;
    } vec2, tex2d;

    typedef struct
    {
        vector<GLint> vertIndexes;
        vector<GLint> texIndexes;
        vector<GLint> normIndexes;
        bool smooth;
        char *materialName;
    } face;

#endif


这篇关于如何在OpenGL中读取obj?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆