Jump to content
  • Advertisement
Sign in to follow this  
Six222

.OBJ file loader problems

This topic is 2781 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

Hey all, I've been writing a little engine and i'm having a few problems loading in vertex/index data from Wavefront .OBJ files, all the data is getting loaded in but when I try to render that data, it doesn't render... If anyone could take a look at some of my code, that'd be really appreciated. I'm using DirectX 10 with Visual Studio 2010 and Blender.

By the way... i'm aware of the lack of error checking lol, i'm getting round to adding that soon

.OBJ File It should be a cube...

# Blender3D v249 OBJ File:
# www.blender3d.org
mtllib untitled.mtl
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8


My file loader
bool CFileLoader::LoadFile(std::string filepath, std::vector<Vertex>*Verts, std::vector<int>*Index)
{
std::ifstream theFile(filepath.c_str());
int numVerts = 0;
int numIndexes = 0;

std::string line_stream;

while(std::getline(theFile, line_stream))
{
Vertex temp;
std::stringstream str_stream(line_stream);
std::string type_str;
str_stream >> type_str;
if(type_str == "v")
{
str_stream >> temp.pos.x >> temp.pos.y >> temp.pos.z;
temp.colour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
numVerts++;
Verts->push_back(temp);
}
if(type_str == "f")
{
for(int x = 0; x < 4; x++)
{
int temp;
str_stream >> temp;
Index->push_back(temp);
numIndexes++;
}
}

}

return true;
}


That data then gets stored in two vectors in my CModel class and gets mapped and rendered with my CVertexBuffer class...



#include "CVertexBuffer.h"

CVertexBuffer::CVertexBuffer()
{
numVerts = 0;
}

bool CVertexBuffer::CreateBuffer(ID3D10Device *Device, bool Dynamic, UINT Size, D3D10_PASS_DESC PassDesc)
{
D3D10_BUFFER_DESC bd;

if(Dynamic)
bd.Usage = D3D10_USAGE_DYNAMIC;
else
bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = sizeof(Vertex) * Size;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

this->Size = Size;

Device->CreateBuffer(&bd, NULL, &VertexBuffer);

D3D10_INPUT_ELEMENT_DESC pVertexLayout[] =
{
// first input element: position
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT,
D3D10_INPUT_PER_VERTEX_DATA, 0},

// second input element: color
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT,
D3D10_INPUT_PER_VERTEX_DATA, 0}
};

// use the input element descriptions to create the input layout
Device->CreateInputLayout(pVertexLayout,
2,
PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize,
&Layout);
return true;
}

bool CVertexBuffer::CreateIndexBuffer(ID3D10Device *Device, UINT Size)
{
D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(DWORD) * 24;
bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

Device->CreateBuffer(&bd, NULL, &IndexBuffer);

return true;
}

bool CVertexBuffer::MapIndexData(UINT numIndex, void* pData, bool Discard)
{
void* pVoid;
IndexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer
memcpy(pVoid, pData, sizeof(int) * numIndex); // copy the vertices to the buffer
IndexBuffer->Unmap();

this->numIndexes = numIndex;

return true;
}

bool CVertexBuffer::MapData( UINT numVerticies, void* pData, bool Discard)
{
void* pVoid;
VertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer
memcpy(pVoid, pData, sizeof(Vertex) * numVerticies); // copy the vertices to the buffer
VertexBuffer->Unmap();

this->numVerts = numVerticies;

return true;
}


void CVertexBuffer::Render(ID3D10Device *Device, D3D_PRIMITIVE_TOPOLOGY Type, ID3D10EffectPass* Pass)
{
Device->IASetPrimitiveTopology(Type);
UINT stride = sizeof(Vertex);
UINT offset = 0;

Device->IASetInputLayout(Layout);
Device->IASetVertexBuffers(0, 1, &VertexBuffer, &stride, &offset);
Device->IASetIndexBuffer(IndexBuffer, DXGI_FORMAT_R32_UINT, 0);

Pass->Apply(0);


Device->DrawIndexed(numIndexes, 0, 0);


}

void CVertexBuffer::Release()
{
VertexBuffer->Release();
Layout->Release();
}


Render call
void CModel::Render(ID3D10Device *Device)
{
VertBuffer.Render(Device, D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST, Technique->GetPassByIndex(0));
}

EDIT:
I'm pretty sure the problem is the actual index data... i'm not too sure though

Share this post


Link to post
Share on other sites
Advertisement

Here is a link to full source and tutorial on obj loading. It is for dx 10. there is also one for dx11. Hope it helps.


Ah brilliant, thanks! I struggled to find a decent tutorial on how to do it properly, thanks again :)

Share this post


Link to post
Share on other sites

[quote name='ryan mccabe' timestamp='1304443962' post='4806034']
Here is a link to full source and tutorial on obj loading. It is for dx 10. there is also one for dx11. Hope it helps.


Ah brilliant, thanks! I struggled to find a decent tutorial on how to do it properly, thanks again :)
[/quote]
You might want to eliminate duplicate vertices for very large OBJ files, it doesn't store the vertex data in the most friendly way for a GPU. I believe the DX sdk has an example of how to do this bear in mind that the code they give you isn't very fast for big OBJ files though.

Share this post


Link to post
Share on other sites
For the future, may also want to consider efficient collation of vertices: http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html

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!