I can't figure out how DirectX11 understands which values in the vertex buffer are Vertices, which ones are Normals, and which ones are Texcoords
For example:
The following code works, but it draws the model as all white. But the normals and vertices are drawn correctly
std::vector<float> vertex_buffer;
for (int i = 0, j = 0; i < num_vertices; i+=3, j+=2)
{
vertex_buffer.push_back(attrib.vertices[i + 0]);
vertex_buffer.push_back(attrib.vertices[i + 1]);
vertex_buffer.push_back(attrib.vertices[i + 2]);
vertex_buffer.push_back(attrib.normals[i + 0]);
vertex_buffer.push_back(attrib.normals[i + 1]);
vertex_buffer.push_back(attrib.normals[i + 2]);
vertex_buffer.push_back(0.0F);
vertex_buffer.push_back(0.0F);
vertex_buffer.push_back(0.0F);
}
std::vector<UINT> index_buffer;
for (int i = 0, j = 0; i < num_indices; i+=3, j+=2)
{
index_buffer.push_back(shapes[0].mesh.indices[i + 0].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 1].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 2].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 0].normal_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 1].normal_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 2].normal_index);
index_buffer.push_back(0);
index_buffer.push_back(0);
index_buffer.push_back(0);
}
For example, the code above produces this:
But if I start changing anything in the index buffer in the last 3 values of the 9 values, the model draws the vertices incorrectly.
Here I modify it to use texcoords (I use TinyObjLoader for importing obj files and I have no idea why it has 3 texcoords per vertex, instead of 2)
std::vector<float> vertex_buffer;
for (int i = 0, j = 0; i < num_vertices; i += 3, j += 2)
{
vertex_buffer.push_back(attrib.vertices[i + 0]);
vertex_buffer.push_back(attrib.vertices[i + 1]);
vertex_buffer.push_back(attrib.vertices[i + 2]);
vertex_buffer.push_back(attrib.normals[i + 0]);
vertex_buffer.push_back(attrib.normals[i + 1]);
vertex_buffer.push_back(attrib.normals[i + 2]);
vertex_buffer.push_back(attrib.texcoords[i + 0]);
vertex_buffer.push_back(attrib.texcoords[i + 1]);
vertex_buffer.push_back(attrib.texcoords[i + 2]);
}
std::vector<UINT> index_buffer;
for (int i = 0, j = 0; i < num_indices; i += 3, j += 2)
{
index_buffer.push_back(shapes[0].mesh.indices[i + 0].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 1].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 2].vertex_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 0].normal_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 1].normal_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 2].normal_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 0].texcoord_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 1].texcoord_index);
index_buffer.push_back(shapes[0].mesh.indices[i + 2].texcoord_index);
}
I get this result:
Clearly, here its not just the textures that are affected, the vertex order is messed up. But Im only changing the fields in the buffer which are supposed to be for texcoords. Why does it affect vertices. Why do vertex and normal coordinates/values work, but texcoords do not.
How does DirectX know which indeces in IndexBuffer refer to Vertices, which ones refer to Normals, and which ones refer to Texcoords
Also, for this model I had to use a Vertex Stride of 36, when I moved the entries from 9 to 8 and changed vetex stride to 32, it was even worse.
Does DirectX automatically assign the first 3 values within stride to vertex, next3 to normal, and next 2 to texcoordinates? Is this how it works?
Thanks,