Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need help with OBJ loader in DirectX11

Started by jeffreyp23 Sep 20, 2011 at 3:08 PM 1 replies 6.7k views
Original Post
jeffreyp23
jeffreyp23
Hi guys,

For a few days now im trying to get the OBJ model on the screen which im loading.
But i cant make it happen, so i thought why not ask if someone can take a look at my code?
If i replace the vertices/indices which i load for vertices/indices i make myself then everything works,
so it there must be something wrong how i load the OBJ model.
(BTW im not using the normals/texture coords)
Here's the code :

Header :
[source]
#ifndef TOBJECTLOADER_H
#define TOBJECTLOADER_H

#include
#include
#include
#include
#include

#include
#include
#include

#include "TDebug.h"
#include "TStructures.h"

using namespace std;


class TObjectLoader
{
public:
TObjectLoader();

HRESULT LoadFromFile(std::string path);

int GetFaceCount(){return FaceCount;}
int GetVertexCount(){return verticesSorted.size();}
int GetNormalCount(){return NormalCount;}

vector GetVertexVector(){return verticesSorted;}


private:

TDebug debugger;
int FaceCount;
int VertexCount;
int NormalCount;
int TextureCount;

vector vertices;
vector normals;
vector textures;
vector verticesSorted;
vector faces;

void SortAll();


};



#endif

[/source]

Source file :
[source]
#include "TObjectLoader.h"

TObjectLoader::TObjectLoader()
{

}

HRESULT TObjectLoader::LoadFromFile(std::string filename)
{
ifstream file;
file.open(filename.c_str());
if(!file.is_open())
return E_FAIL;

string stream_line;
while(getline(file, stream_line)){
stringstream str_stream(stream_line);
string type_str;
str_stream >> type_str;

if(type_str == "v"){
D3DXVECTOR3 vec;
float x, y, z;
str_stream >> x >> y >> z;
vec.x = x;
vec.y = y;
vec.z = z * -1.0f;
vertices.push_back(vec);
}
else if(type_str == "vn"){
D3DXVECTOR3 vec2;
str_stream >> vec2.x >> vec2.y >> vec2.z;
vec2.z *= -1.0f;
normals.push_back(vec2);
}
else if(type_str == "vt"){
D3DXVECTOR2 vec3;
str_stream >> vec3.x >> vec3.y;
textures.push_back(vec3);
}
else if (type_str == "f"){
TFace f;
char skip;
str_stream >> f.vertex[2] >> skip
>> f.texture[0] >> skip
>> f.normal[2] >> f.vertex[1] >> skip
>> f.texture[1] >> skip
>> f.normal[1] >> f.vertex[0] >> skip
>> f.texture[0] >> skip
>> f.normal[0];

faces.push_back(f);

}

}

SortAll();

return S_OK;
}

void TObjectLoader::SortAll()
{

for(DWORD i=0;i {
verticesSorted.push_back(vertices[static_cast(faces.vertex[0]) - 1]);
verticesSorted.push_back(vertices[static_cast(faces.vertex[1]) - 1]);
verticesSorted.push_back(vertices[static_cast(faces.vertex[2]) - 1]);
}

}

[/source]

And this is the value of the vertex / index buffer :
[source]
Debugger : log voor object : cube.obj

Vertex lijst : // Vertex list
V : -2.9273 0 5.3928
V : -28.8989 0 5.3928
V : -28.8989 0 -17.7732
V : -28.8989 0 -17.7732
V : -2.9273 0 -17.7732
V : -2.9273 0 5.3928
V : -2.9273 19.1794 5.3928
V : -2.9273 19.1794 -17.7732
V : -28.8989 19.1794 -17.7732
V : -28.8989 19.1794 -17.7732
V : -28.8989 19.1794 5.3928
V : -2.9273 19.1794 5.3928
V : -2.9273 19.1794 -17.7732
V : -2.9273 0 -17.7732
V : -28.8989 0 -17.7732
V : -28.8989 0 -17.7732
V : -28.8989 19.1794 -17.7732
V : -2.9273 19.1794 -17.7732
V : -2.9273 19.1794 5.3928
V : -2.9273 0 5.3928
V : -2.9273 0 -17.7732
V : -2.9273 0 -17.7732
V : -2.9273 19.1794 -17.7732
V : -2.9273 19.1794 5.3928
V : -28.8989 19.1794 5.3928
V : -28.8989 0 5.3928
V : -2.9273 0 5.3928
V : -2.9273 0 5.3928
V : -2.9273 19.1794 5.3928
V : -28.8989 19.1794 5.3928
V : -28.8989 19.1794 -17.7732
V : -28.8989 0 -17.7732
V : -28.8989 0 5.3928
V : -28.8989 0 5.3928
V : -28.8989 19.1794 5.3928
V : -28.8989 19.1794 -17.7732

Index lijst : // Index list
I : 0
I : 1
I : 2
I : 3
I : 4
I : 5
I : 6
I : 7
I : 8
I : 9
I : 10
I : 11
I : 12
I : 13
I : 14
I : 15
I : 16
I : 17
I : 18
I : 19
I : 20
I : 21
I : 22
I : 23
I : 24
I : 25
I : 26
I : 27
I : 28
I : 29
I : 30
I : 31
I : 32
I : 33
I : 34
I : 35

Debugger : eind log voor object : cube.obj

[/source]

Thanks in advance
FriedPuffPearl
FriedPuffPearl
quite a time ago...In case anybody needs it...
may you take a look at that http://www.rastertek.../dx11tut08.html .
an obj file parser is written there, which exports it to an .txt file with the vertices set up.
all you then have to do is read in the vertices, from the txt file and setup the indexbuffer. the .obj files can be exportet from Maya e.g., but be sure to triangulate them first.
One issue with this approach would of course be the duplicate vertices as well as the indexing, but i think its good to start with.
MarkS_
MarkS_
When I wrote my loader, I found the best way to debug it was to load an object, and then using cout, print the vertices and faces in the format of an obj file. This let me compare the file I was loading with what was loaded. It solved a great many errors.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.