如何将模型从 Maya 等 3D 软件导入 OpenGL? [英] How to Import models from 3D software, like Maya into OpenGL?

查看:64
本文介绍了如何将模型从 Maya 等 3D 软件导入 OpenGL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 3D 模型从 MAYA 等 3D 建模软件导入 OpenGL,特别是用于 PC 而不是 Iphone 开发?

How can I import 3D models from 3D modelling software like MAYA into OpenGL especially for PC rather than for Iphone development ?

推荐答案

也许这让您感到惊讶,但 OpenGL 没有模型"这样的东西.OpenGL 的作用是,它接受一堆数据,您需要让 OpenGL 访问这些数据,以便它对这些数据执行的操作会在屏幕上产生漂亮的图像.

Maybe this surprises you, but OpenGL has no such thing qualifying as "models". What OpenGL does is, it accepts a bunch of data and it is upon you to make OpenGL access this data so that the operations it performs on that data will result in a nice image on the screen.

所以在介绍之后,假设你有一个模型,比如一个立方体.一个立方体由 6 个面组成,每个面有 4 个角.虽然每个角位置由 3 个面共享,但面部方向,即所谓的法线,在该角上有很大不同,具体取决于您正在看哪个面.位置、法线和其他一些属性构成了所谓的顶点.因此,对于一个立方体,我们有 6 * 4 = 24 个顶点(大写位置,下法线)

So after that introduction, let's say you got a model, like a cube. A cube consists of 6 faces with 4 corners each. While each corner positions is shared by 3 faces each, the face direction, the so called Normal, is very different on that corner depending which face you're looking at. Position, Normal and a few other attributes form what is known as a vertex. Thus for a cube we have 6 * 4 = 24 vertices (capital position, lower normal)

X,   Y,  Z,  x,  y,  z
-1, -1, -1, -1,  0,  0
-1, -1,  1, -1,  0,  0
-1,  1,  1, -1,  0,  0
-1,  1, -1, -1,  0,  0
 1, -1, -1,  1,  0,  0
 1, -1,  1,  1,  0,  0
 1,  1,  1,  1,  0,  0
 1,  1, -1,  1,  0,  0
…

等等,我希望你能明白.每条线形成一个顶点向量.这个向量的格式是任意的.您可以将这些属性(Position、Normal 等)放在一个公共的、交错 数组中,或者您可以在其自己的数组中为每个属性使用多个数组.不过,交错数组通常更有效.

And so on, I hope you get the idea. Each line forms a vertex vector. The format of this vector is arbitrary. You can place those attributes (Position, Normal, etc.) in one common, interleaved array, or you use multiple arrays for each attribute in its own array. Interleaved arrays oftenly are more efficient though.

与顶点列表一起,您还有一个面列表,即顶点索引元组,对于面,因此在立方体的情况下,这些元组指定四边形

Together with the list of vertices you also have a list of faces, i.e. tuple of vertex indices, which for a face, so in the case of a cube those tuples, designating quadrilaterals would be

0, 1, 2, 3
4, 5, 6, 7
...
20, 21, 22, 23

加载模型意味着,您编写一些程序来从模型文件格式(最好是有文档的文件格式,或者您自己想出并为您的建模程序编写导出器)中读取数据.然后以上述方式将读取的数据放入内存中,以便 OpenGL 可以使用它.

Loading a model means, you write some program that reads the data from a model file format (preferably a documented one, or one you came up with yourself and wrote an exporter for your modelling program for). The read data is then placed into memory in such a way as outlined above so that OpenGL can make use of it.

然后您将这些数据提供给 OpenGL.有两种选择:将数据保存在程序的进程地址空间(客户端顶点数组)中,或者将数据上传到 OpenGL 缓冲区对象(顶点缓冲区对象)中.

You then supply this data to OpenGL. There are two options: You keep the data in your program's process address space (Client Side Vertex Arrays), or you upload the data into a OpenGL buffer object (Vertex Buffer Object).

然后您告诉 OpenGL 获取其几何数据.

Then you tell OpenGL to fetch its geometry data.

在客户端顶点数组的情况下,这是通过以下方式完成的

In the case of Client Side Vertex Arrays this is done in the following way

GLenum primitive_mode;
GLfloat *vertex_data;
GLushort *indices_data;
int face_count;


void load_model(...)
{
    /* ... */
    read_vertex_data_from_file(&vertex_data, 
                               &indices_data, 
                               &face_count, 
                               &primitive_mode, ...);

}

/*...*/

void draw_model()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(
                    3, /* we're supplying vectors of which the 3 first elements are to be used for vertex position */
                    GL_FLOAT, /* the type of each vector element is GLfloat */
                    sizeof(GLfloat)*6, /* the beginnings of vectors in vertex_data are 6 * sizeof(GLfloat) apart */
                    vertex_data + 0 /* the pointer to the data, positions are offset 0 */
    );

    glNormalPointer( /* normals always have 3 elements, no size given */
                    GL_FLOAT, /* the type of each vector element is GLfloat */
                    sizeof(GLfloat)*6, /* the beginnings of vectors in vertex_data are 6 * sizeof(GLfloat) apart */
                    vertex_data + 3 /* the pointer to the data, normals are offset 3 */
    );

    glDrawElements(primitive_mode, face_count, GL_UNSIGNED_SHORT, indices_data);

}

您可以自己实现模型加载器,或者使用一些第三方库来完成这项任务.

It is upon you to either implement the model loader yourself, or use some third party library for this task.

这篇关于如何将模型从 Maya 等 3D 软件导入 OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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