Importing and Displaying .fbx files in OpenGl

前端 未结 1 1279
孤独总比滥情好
孤独总比滥情好 2021-02-10 18:22

I have been trying to import and display an fbx file using the FBX SDK.Untill. I managed to load in the file, but I got stuck at the part where I have to display i

1条回答
  •  臣服心动
    2021-02-10 18:57

    2) How should I display the vertices?

    Questions like these indicate, that you should work through some OpenGL tutorials. Those are the basics and you need to know them.

    This is a good start regarding your problem, but you'll need to work through the whole tutorial http://opengl.datenwolf.net/gltut/html/Basics/Tut01%20Following%20the%20Data.html

    1) What exactly are those indices ?

    You have a list of vertices. The index of a vertex is the position at which it is in that list. You can draw vertex arrays by its indices using glDrawElements

    Update due to comment

    Say you have a cube with shared vertices (uncommon in OpenGL, but I'm too lazy for writing down 24 vertices).

    Cube Vertices

    I have them in my program in an array, that forms a list of their positions. You load them from a file, I'm writing them a C array:

    GLfloat vertices[3][] = {
        {-1,-1, 1},
        { 1,-1, 1},
        { 1, 1, 1},
        {-1, 1, 1},
        {-1,-1,-1},
        { 1,-1,-1},
        { 1, 1,-1},
        {-1, 1,-1},
    };
    

    This gives the vertices indices (position in the array), in the picture it looks like

    Cube Vertices with Indices

    To draw a cube we have to tell OpenGL in which vertices, in which order make a face. So let's have a look at the faces:

    Cube with face edges

    We're going to build that cube out of triangles. 3 consecutive indices make up a triangle. For the cube this is

    GLuint face_indices[3][] = {
        {0,1,2},{2,3,0},
        {1,5,6},{6,2,1},
        {5,4,7},{7,6,5},
        {4,0,3},{3,7,4},
        {3,2,6},{6,7,2},
        {4,5,0},{1,0,5}
    };
    

    You can draw this then by pointing OpenGL to the vertex array

    glVertexPointer(3, GL_FLOAT, 0, &vertices[0][0]);
    

    and issuing a batches call on the array with vertices. There are 6*2 = 12 triangles, each triangle consisting of 3 vertices, which makes a list of 36 indices.

    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, &face_indices[0][0]);
    

    0 讨论(0)
提交回复
热议问题