在OpenGl中导入和显示.fbx文件

Tai*_*gi 5 c++ opengl import fbx

我一直在尝试使用导入和显示fbx文件FBX SDK.Untill.我设法加载文件,但我被困在我必须显示它的部分.问题:

  1. 究竟是什么指数?
  2. 我该如何显示顶点?

这是我做的课程:

3dModelBasicStructs.h

struct vertex
{
float x,y,z;
};

struct texturecoords
{
float a,b;
};

struct poligon
{
int a,b,c;
};
Run Code Online (Sandbox Code Playgroud)

Model.h

#ifndef MODEL_H
#define MODEL_H
#define FBXSDK_NEW_API

#define MAX_VERTICES 80000
#define MAX_POLIGONS 80000


#include <fbxsdk.h>
#include "3dModelBasicStructs.h"
#include <iostream>
#include <GL/glut.h>
using namespace std;

class Model
{

     public:

         Model(char*);
         ~Model();

         void ShowDetails();

         char* GetModelName();
         void  SetModelName( char* );
         void  GetFbxInfo( FbxNode* );
         void  RenderModel();
                     void  InitializeVertexBuffer( vertex* );

      private:

          char Name[25];

          vertex vertices[MAX_VERTICES];
          poligon poligons[MAX_POLIGONS];

          int *indices;
          int numIndices;

          int numVertices;


};


#endif
Run Code Online (Sandbox Code Playgroud)

Model.cpp

#include "Model.h"






Model::Model(char *filename)
{
cout<<"\nA model has been built!";

numVertices=0;
numIndices=0;

FbxManager *manager = FbxManager::Create();

FbxIOSettings *ioSettings = FbxIOSettings::Create(manager, IOSROOT);
manager->SetIOSettings(ioSettings);

FbxImporter *importer=FbxImporter::Create(manager,"");
importer->Initialize(filename,-1,manager->GetIOSettings());

FbxScene *scene = FbxScene::Create(manager,"tempName");

importer->Import(scene);
importer->Destroy();

FbxNode* rootNode = scene->GetRootNode();
this->SetModelName(filename);
if(rootNode) { this->GetFbxInfo(rootNode); }

}

Model::~Model()
{
cout<<"\nA model has been destroied!";
}


void Model::ShowDetails()
{
cout<<"\nName:"<<Name;
cout<<"\nVertices Number:"<<numVertices;
cout<<"\nIndices which i never get:"<<indices;

}

char* Model::GetModelName()
{
return Name;
}

void Model::SetModelName(char *x)
{
strcpy(Name,x);
}

void Model::GetFbxInfo( FbxNode* Node )
{

int numKids = Node->GetChildCount();
FbxNode *childNode = 0;

for ( int i=0 ; i<numKids ; i++)
{
    childNode = Node->GetChild(i);
    FbxMesh *mesh = childNode->GetMesh();

    if ( mesh != NULL)
    {
//================= Get Vertices ====================================
        int numVerts = mesh->GetControlPointsCount();

        for ( int j=0; j<numVerts; j++)
        {
            FbxVector4 vert = mesh->GetControlPointAt(j);
            vertices[numVertices].x=(float)vert.mData[0];
            vertices[numVertices].y=(float)vert.mData[1];
            vertices[numVertices++].z=(float)vert.mData[2];
            cout<<"\n"<<vertices[numVertices-1].x<<" "<<vertices[numVertices-    1].y<<" "<<vertices[numVertices-1].z;
this->InitializeVertexBuffer(vertices);
        }
//================= Get Indices ====================================
        int *indices = mesh->GetPolygonVertices();
        numIndices+=mesh->GetPolygonVertexCount();
    }
    this->GetFbxInfo(childNode);
}
}

void Model::RenderModel()
{
glDrawElements(GL_TRIANGLES,36,GL_INT,indices);
}
void Model::InitializeVertexBuffer(vertex *vertices)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
//glDrawArrays(GL_TRIANGLES,0,36);
}
Run Code Online (Sandbox Code Playgroud)

可悲的是,当我尝试使用drawelements时,我收到此错误:在新的begging.exe中0x77e215de处的未处理异常:0xC0000005:访问冲突读取位置0xcdcdcdcd.

dat*_*olf 14

2)我应该如何显示顶点?

这些问题表明,您应该学习一些OpenGL教程.这些是基础知识,你需要了解它们.

这是一个关于你的问题的良好开端,但你需要完成整个教程 http://opengl.datenwolf.net/gltut/html/Basics/Tut01%20Following%20the%20Data.html

1)这些指数究竟是什么?

你有一个顶点列表.顶点的索引是它在该列表中的位置.您可以使用索引绘制顶点数组glDrawElements

由于评论而更新

假设你有一个带有共享顶点的立方体(在OpenGL中不常见,但我懒得写下24个顶点).

立方体顶点

我把它们放在我的程序中,在一个数组中,形成一个他们的位置列表.你从文件中加载它们,我正在写一个C数组:

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},
};
Run Code Online (Sandbox Code Playgroud)

这给出了它看起来像的顶点索引(数组中的位置)

带指数的立方体顶点

要绘制一个立方体,我们必须告诉OpenGL哪个顶点,以哪个顺序构成一个面.那么让我们来看看面孔:

具有脸部边缘的立方体

我们要用三角形构建那个立方体.连续3个指数组成一个三角形.对于立方体,这是

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}
};
Run Code Online (Sandbox Code Playgroud)

您可以通过将OpenGL指向顶点数组来绘制它

glVertexPointer(3, GL_FLOAT, 0, &vertices[0][0]);
Run Code Online (Sandbox Code Playgroud)

并使用顶点在数组上发出批处理调用.有6*2 = 12个三角形,每个三角形由3个顶点组成,它们列出了36个索引.

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, &face_indices[0][0]);
Run Code Online (Sandbox Code Playgroud)