Jump to content
  • Advertisement
Sign in to follow this  
jeffreyp23

Need help with OBJ loader in DirectX11

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <Windows.h>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

#include <D3D11.h>
#include <D3DX11.h>
#include <D3DX10.h>

#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<D3DXVECTOR3> GetVertexVector(){return verticesSorted;}


private:

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

vector<D3DXVECTOR3> vertices;
vector<D3DXVECTOR3> normals;
vector<D3DXVECTOR2> textures;
vector<D3DXVECTOR3> verticesSorted;
vector<TFace> 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<faces.size();i++)
{
verticesSorted.push_back(vertices[static_cast<int>(faces.vertex[0]) - 1]);
verticesSorted.push_back(vertices[static_cast<int>(faces.vertex[1]) - 1]);
verticesSorted.push_back(vertices[static_cast<int>(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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!