在OpenGl中导入和显示.fbx文件 [英] Importing and Displaying .fbx files in OpenGl

查看:2112
本文介绍了在OpenGl中导入和显示.fbx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 it. The questions:


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

  2. 我应该如何展示顶点?

这是我所做的课程:

3dModelBasicStructs.h

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

struct texturecoords
{
float a,b;
};

struct poligon
{
int a,b,c;
};

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

型号.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);
}

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

Sadly , When i try to use drawelements i get this error: Unhandled exception at 0x77e215de in A new begging.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

推荐答案


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

2) How should I display the vertices?

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

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

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

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)这些指数究竟是什么?

1) What exactly are those indices ?

您有一个顶点列表。顶点的索引是它在该列表中的位置。您可以使用 glDrawElements

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

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

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

我在程序中的数组中有它们,这形成了他们的立场列表。你从一个文件加载它们,我正在写一个C数组:

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

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

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:

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

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}
};

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

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

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

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

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]);

这篇关于在OpenGl中导入和显示.fbx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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