如何从3D软件导入模型,如玛雅转换成OpenGL? [英] How to Import models from 3D software, like Maya into OpenGL?

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

问题描述

如何导入从像MAYA 3D建模软件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.

这样的介绍之后,让我们说你有一个模型,像一个立方体。立方体由六个面,每个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
…

等等,我希望你的想法。每一行形成的顶点载体的。这个向量的格式arbitraty。您可以将这些属性(位置,法等)在一个共同的,交错的数组,或者使用多个阵列在自己的阵列中的每个属性。交错阵列oftenly更有效率,但。

And so on, I hope you get the idea. Each line forms a vertex vector. The format of this vector is arbitraty. 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

加载模型意味着,你写一些程序,从模型文件格式读取数据(preferrably一个记录一个,或一个你想出了自己,写了出口商的模拟程序)。读出的数据,然后放置到内存在如上概述,以便使OpenGL可以利用它的方式。

Loading a model means, you write some program that reads the data from a model file format (preferrably 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.

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

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