# Imported OBJ Looks Funky... :(

## Recommended Posts

Below is a screen shot of my imported OBJ inside my editor. I've been looking at several tutorials and for now - I just want to get a simple OBJ cube loaded with a color shader file. I am think I'm confused on the whole Index section, because I think this is where it's going haywire. I'll try to chainge the topology to trianglelist because now I have it set to trianglestrip.

Inside the debug console it writes the OBJ structure. 8 vertices and 12 faces. the OBJ import reverse the Index backwards also. It has to be the part where it loads the vertex and the index. Whenever ever someone has time - could they at least if I'm doing something wrong in codewise.

[code]

// Read in the vertices, texture coordinates, and normals into the data structures.
// Important: Also convert to left hand coordinate system since Maya uses right hand coordinate system.
fin.get(input);
while(!fin.eof())
{
if(input == 'v')
{
fin.get(input);

if(input == ' ')
{
fin >> vertices[vertexIndex].x >> vertices[vertexIndex].y >> vertices[vertexIndex].z;

// Invert the Z vertex to change to left hand system.
vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f;
vertexIndex++;
}

// Read in the texture uv coordinates.
if(input == 't')
{
fin >> texcoords[texcoordIndex].x >> texcoords[texcoordIndex].y;

// Invert the V texture coordinates to left hand system.
texcoords[texcoordIndex].y = 1.0f - texcoords[texcoordIndex].y;
texcoordIndex++;
}

if(input == 'n')
{
fin >> normals[normalIndex].x >> normals[normalIndex].y >> normals[normalIndex].z;

// Invert the Z normal to change to left hand system.
normals[normalIndex].z = normals[normalIndex].z * -1.0f;
normalIndex++;
}
}

if(input == 'f')
{
fin.get(input);
if(input == ' ')
{
// Read the face data in backwards to convert it to a left hand system from right hand system.
fin >> faces[faceIndex].vIndex3 >> input2 >> faces[faceIndex].tIndex3 >> input2 >> faces[faceIndex].nIndex3
>> faces[faceIndex].vIndex2 >> input2 >> faces[faceIndex].tIndex2 >> input2 >> faces[faceIndex].nIndex2
>> faces[faceIndex].vIndex1 >> input2 >> faces[faceIndex].tIndex1 >> input2 >> faces[faceIndex].nIndex1;
faceIndex++;
}
}

// Read in the remainder of the line.
while(input != '\n')
{
fin.get(input);
}

// Start reading the beginning of the next line.
fin.get(input);
}

// Close the file.
fin.close();
[/code]

this part gathers the stored index count and literates the the file again - grabbing vertex data and such.

[code]
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );

vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * vCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
///////////////**************new**************////////////////////

D3D11_SUBRESOURCE_DATA vertexBufferData;

ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
vertexBufferData.pSysMem = &vertices[0];
HR( dev->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &objVertexBuffer));

//Set the vertex buffer
UINT stride = sizeof(VertexType);
UINT offset = 0;
devcon->IASetVertexBuffers( 0, 1, &objVertexBuffer, &stride, &offset );

D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(FaceType) * 3 * 12;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA iinitData;

iinitData.pSysMem = &faces[0];
HR(dev->CreateBuffer(&indexBufferDesc, &iinitData, &objIndexBuffer));

devcon->IASetIndexBuffer( objIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

Debug::WriteLine("Index Buffer from OBJ loaded Successfully!");

[/code]

then when it's drawn - it looks like what's below. Additional, here's a wireframe shot within the editor.
[attachment=10401:OBJGoneWrong.PNG]

Thanks for the help guys! Much appreciated!

##### Share on other sites
would you suggest I create my own format or look into another format like FBX? Since FBX is popular but I read on google there's some problems - so I wasn't sure if I should go ahead with importing FBX files. Thanks for the response BitMaster.

##### Share on other sites
BitMaster    8651
The huge advantage of .obj files is that they are human readable (a real blessing for anything you need to debug) and relatively simple (usually, they can contain some weird stuff but you usually never encounter those bits in 'normal' files).
Unless you would rather use a library (like Assimp) I would strongly advise sticking with .obj files until loading and display works as intended. When that is working, consider a different format.

That said, I would advise against writing your own loaders (unless that is the point of the exercise). It is a rather lengthy and not very interesting work that has already been solved a lot of times.

##### Share on other sites
powly k    657
No, he suggests that you substract one (1) from your indices, because .obj lists them as 1,2,3... and everyone else lists them 0,1,2...

Which was my thought too, just based on the topic headline. Didn't dig into your code yet, though.

##### Share on other sites
scniton    252
In addition to what has been said about the indexing base, you code makes several assumptions about the content of obj file, in particular:[list]
[*]Triangles only (i.e. always 3 sets of indices for faces, no more, no less)
[*]Texture AND normal data is always present (i.e. face indices are always of the form: #/#/# )
[/list]
I would check the file by hand to make sure these assumptions are not violated.

Also, I'm not sure what you mean by changing the topology to triangle list, given your above assumptions the trivial way of rendering the parsed obj data is as a triangle list. Edited by scniton

##### Share on other sites
Alright, I'm gotta study more about setting up Index Buffers a bit more. If I look back now, it doesn't really make sense in Index Buffer's mind because it's like, "You want me to draw what again? Okay cool, I'll draw this but you'll not like it!" So, this reminds me to do more studing before tackling the fun stuff.

##### Share on other sites
OH YEAH BABY! A successful Obj Import without texture just pretty hot red! Okay, so the code now just picks out the vertices and the indices! Check it out - wireframe and solid.

[attachment=10409:SuccessfulOBJImport-SOLID.PNG][attachment=10410:SuccessfulOBJImport-WIRE.PNG]

You guy's da BEST!

##### Share on other sites
Okay, a cube - so what right? Well, I did some revisions to the code to automatically calculate the vertices and indices. In the drawIndex section I did the same calculation again and this is the final result of a imported obj sphere. Looks like a egg but eggs are good too!

[attachment=10411:Successful_OBJ_Sphere_Import-SOLID.PNG][attachment=10412:Successful_OBJ_Sphere_Import-Wire.PNG]

##### Share on other sites
BitMaster was right that I had to substract fromt he Index Collection in order to draw this on screen. Scniton was correct at the code's assumption. With this in mind - I came to the conclusion...if it's reading the index and collecting the Index Verteices and the normal Indexes and the Texture Indexes. I better filter the one's I just need for now then improve upon. I justed wanted to challenge myself to get a simple obj file imported. The sphere looks squashed so I'm working on this and addiotnally I'm working on the importing side. I got so excited I forgot to thank everyone.