Advertisement

Loading a obj into Directx11 (solved)

Started by June 07, 2017 02:45 AM
-1 comments, last by Myra 7 years, 8 months ago

I saw this tutorial online to load a 3D model into the scene from external file (Rastertek Tutorial 7: 3D Model Rendering)

The way the Tiny Room Demo creates model is to hardcode the coordinates and renders it
TriangleSet walls;
walls.AddSolidColorBox(10.1f, 0.0f, 20.0f, 10.0f, 4.0f, -20.0f, 0xff808080); // Left Wall
walls.AddSolidColorBox(10.0f, -0.1f, 20.1f, -10.0f, 4.0f, 20.0f, 0xff808080); // Back Wall
walls.AddSolidColorBox(-10.0f, -0.1f, 20.0f, -10.1f, 4.0f, -20.0f, 0xff808080); // Right Wall
Add(
new Model(&walls, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1),
new Material(
new Texture(false, 256, 256, Texture::AUTO_WALL)
)
)
);

void AddSolidColorBox(float x1, float y1, float z1, float x2, float y2, float z2, uint32_t c)
{
AddQuad(Vertex(XMFLOAT3(x1, y2, z1), ModifyColor(c, XMFLOAT3(x1, y2, z1)), z1, x1),
Vertex(XMFLOAT3(x2, y2, z1), ModifyColor(c, XMFLOAT3(x2, y2, z1)), z1, x2),
Vertex(XMFLOAT3(x1, y2, z2), ModifyColor(c, XMFLOAT3(x1, y2, z2)), z2, x1),
Vertex(XMFLOAT3(x2, y2, z2), ModifyColor(c, XMFLOAT3(x2, y2, z2)), z2, x2));
...}

AddQuad(Vertex v0, Vertex v1, Vertex v2, Vertex v3) { AddTriangle(v0, v1, v2); AddTriangle(v3, v2, v1); }
void AddTriangle(Vertex v0, Vertex v1, Vertex v2)
{
VALIDATE(numVertices <= (maxBuffer - 3), "Insufficient triangle set");
for (int i = 0; i < 3; i++) Indices[numIndices++] = short(numVertices + i);
Vertices[numVertices++] = v0;
Vertices[numVertices++] = v1;
Vertices[numVertices++] = v2;
}
Tried to load the model into the scene using a function from the tutorial

TriangleSet models;
models.LoadModel("F:\\cube.txt");
Add(
new OBJModel(&models, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1),
new OBJMaterial(
new Texture(false, 256, 256, Texture::AUTO_WHITE)
)
)
); //3D Model

void LoadModel(char* filename)
{
ifstream fin;
char input;

// Open the model file.
fin.open(filename);
// Read up to the value of vertex count.
fin.get(input);
while (input != ':')
{
fin.get(input);
}
// Read in the vertex count.
m_vertexCount = 0;
fin >> m_vertexCount;
// Read up to the beginning of the data.
fin.get(input);
while (input != ':')
{
fin.get(input);
}
fin.get(input);
fin.get(input);
// Read in the vertex data.
for (int i = 0; i<m_vertexCount; i++)
{
Indices[numIndices++] = short( i);
fin >> Vertices[numVertices].Pos.x >> Vertices[numVertices].Pos.y >> Vertices[numVertices].Pos.z;
fin >> Vertices[numVertices].U >> Vertices[numVertices].V;
fin >> Normals[numVertices].Norm.x >> Normals[numVertices].Norm.y >> Normals[numVertices].Norm.z;
Vertices[numVertices].C = ModifyColor(0xffffffff, Vertices[numVertices].Pos);
numVertices+=1;
}
// Close the model file.
fin.close();
}
I did not use the normal as from the tutorial it was meant for the texture of the object. Instead I defined the color to be solid white. Tried to keep the structure of loading the model as similar to Tiny Room Demo as possible.

I have used the same model, material and texture (vertex shader and pixel shader) as Tiny Room Demo does. The file I tried to load was cube.txt. Had some code to convert my obj to a txt file.
Vertex Count: 36
Data:
-1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.0
1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0
-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0
-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0
1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0
1.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.0
1.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0
1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0
1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0
1.0 -1.0 1.0 1.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0
-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0
1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0
1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0
-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0
-1.0 -1.0 1.0 1.0 1.0 0.0 0.0 1.0

Vertex and Index buffer
struct OBJModel
{
XMFLOAT3 Pos;
XMFLOAT4 Rot;
OBJMaterial * Fill;
DataBuffer * VertexBuffer;
DataBuffer * IndexBuffer;
int NumIndices;
OBJModel() : Fill(nullptr), VertexBuffer(nullptr), IndexBuffer(nullptr) {};
void Init(TriangleSet * t)
{
NumIndices = t->numIndices;
VertexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_VERTEX_BUFFER, &t->Vertices[0], t->numVertices * sizeof(Vertex));
IndexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_INDEX_BUFFER, &t->Indices[0], t->numIndices * sizeof(short));
}
...
DIRECTX.Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
...
---------------------------------------------------------------------------------------------------------------------------------
Loading the cube was fine. but when I tried to load other model there seems to be an issue. I kind of understand the problem is that the indices might have issue. but I don't understand why did it work for the cube and not other model... So where could have gone wrong in this case. I have tried to use rasterizer without back culling to draw this object and with but the effect is the same too.
house.txt http://imgur.com/a/M6gQ3
3D Model data:
Vertex Count: 798
Data:
28.3005 0.415886 -45.8282 0.7216 0.720211 0 0 -1
28.3005 -0.809079 -45.8282 0.732222 0.720211 0 0 -1
-27.7441 -0.809079 -45.8282 0.732222 0.847836 0 0 -1
28.3005 0.415886 68.1056 0.459891 0.720286 0 1 -0
28.3005 0.415886 -45.8282 0.719341 0.720286 0 1 -0
-27.7441 0.415886 -45.8282 0.719341 0.847911 0 1 -0
28.3005 -0.809079 68.1056 0.721603 0.720211 0 0 1
28.3005 0.415886 68.1056 0.732225 0.720211 0 0 1
-27.7441 0.415886 68.1056 0.732225 0.847836 0 0 1
28.3005 -0.809079 -45.8282 0.459891 0.720298 0 -1 -0
28.3005 -0.809079 68.1056 0.719341 0.720298 0 -1 -0
-27.7441 -0.809079 68.1056 0.719341 0.847923 0 -1 -0
28.3005 0.415886 68.1056 0.719341 0.70683 1 0 -0
...

UPDATE:

I SOLVED IT :)

This topic is closed to new replies.

Advertisement