4

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. What exactly are those indices?
  2. How should I display the vertices?

Here is the class that I made:

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

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

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Taigi
  • 143
  • 1
  • 1
  • 4
  • That access violation is just a pointer "pointing" out of it's boundaries, possible like the OS's boundaries etc. I don't know how to fix that, it's best to try to pinpoint that pointer and possible not use a pointer for that access violation thing. Or you can be edgy and import KERNAL32 into your program to possibly fix it. check for any char[MAX+1] that may lead to the problem – Nfagie Yansaneh Aug 13 '17 at 09:09

1 Answers1

13

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]);
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thnx for the tutorial i will check it out ( I know how to display vertexes and such but i didn't really worked with vertexes array or buffers and such ) And about those indices i know that they are a list but i just dont get what do they represent ? ( I mean that lets say we have an array of vertices and a triangle and each points of the triangle is an index to the array of vertices / thats mostly what i understant when i see the word index) – Taigi Dec 30 '12 at 22:13
  • Ok i have read it and so are those the attribute indices? And if that is true That means i need more than 1 pointers ( for bigger 3d models ) right? – Taigi Dec 30 '12 at 22:45
  • @Taigi: The number of pointers has nothing to do with the size of the model. A vertex is not just a position (common misconception) but consists of a number of attributes; position, normal, color, texture coordinate, etc. For each attribute you set a pointer, so that OpenGL knows where to get that attribute from. The indices sort of "pack" raw vertices into something with a structure. – datenwolf Dec 30 '12 at 23:16
  • Ok... I don't really get it darn... (im so stupid) Do you got something like skype or anything so we could talk on easier? – Taigi Dec 30 '12 at 23:42
  • @Taigi: See my answer update. I prefer asynchronous communication (email and such), hence I neither Skype nor VoIP. – datenwolf Dec 31 '12 at 00:24
  • I keep getting an error so i modified the question ( I guess im accesing some bad memory zones with the indices but not sure why and how) – Taigi Dec 31 '12 at 00:45
  • @Taigi: You've hardcoded my count of 36 vertices in the call to glDrawElements. This is for sure not what you want. You wanto to pass `numVertices` there. However (please take no offense by this), the rest of your code looks horrible. Hardcoded array sizes, redundant copies, etc. And the naming of some of the functions (like control points) suggest that the file format may contain something very different. – datenwolf Dec 31 '12 at 00:55
  • Well I'v tried with both versions ( numVertices and 36 ( for that im displaying a cube )) and I know that my code is horrible :( Been doing algorithmics for like 5 years ( so the most complex thing was the...for loop ?) and now im 16 and just starded to do OpenGl for a while and everything else ... What do you mean by redundant copies? And the controlpoints functions is something from the FBX SDK as far as i know... But still why is the error there? Even with numVertices it sais : Unhandled exception at 0x77e215de in A new begging.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd. – Taigi Dec 31 '12 at 01:16
  • Btw when it prints the indices it sais cdcdcdcd so i guess thats the location it can't read... – Taigi Dec 31 '12 at 01:17
  • @Taigi: Well 0xcdcdcdcd doesn't look like a valid pointer to me. I don't know the FBX SDK and it would take me some time (half a day or so) to work into it. But getting this value means, that something there is not quite right. Your problem is now, that you're fighting two battles: Being an OpenGL newb without a solid understanding of its concepts and facing the API of an SDK (hard enough) without even knowing the concepts required. I'd say, get your OpenGL knowledge solid first. Work through the whole tutorial I linked (all the chapters) and only then tackle the FBX files. – datenwolf Dec 31 '12 at 01:23
  • Oh well, Then do you got some other good tutorial ( As you can see im working with the "old" OpenGl not the new one . Iv done all the tutorial from [here](http://www.videotutorialsrock.com) and if i am going to get my knowladge solid i should keep on doing the same OpenGl atleast – Taigi Dec 31 '12 at 01:30
  • But also i gotta make a this importor working so i still have to understand why it doesn't work:( – Taigi Dec 31 '12 at 01:32
  • There is a minor mistake in the answer at _face_indices_ - the face {6,7,2} should be {6,7,3} so that it's complementary to {3,2,6} – Armen Ablak Feb 23 '14 at 18:00
  • What I think he means is that the OP wants to import a .fbx and read all the vertices inside of it like a ASSIMP file, he knows how VBO VAO INDICIES, he is trying to make a class that grabs the data from the .fbx and reads it contents and it's mesh and display it. Just like Unity or Unreal engine – Nfagie Yansaneh Aug 13 '17 at 09:05