[D3D11] Newbie Help with DrawIndexed

Started by
4 comments, last by BBeck 7 years, 9 months ago

I have recently gone through all the tutorials at http://www.rastertek.com/tutdx11s2.html to learn DirectX11 pipeline. I have gotten a pretty good grasp of how the pipeline works and what the code is doing. (though I know very little about all the various options chosen in the initialization process, or what possible changes could be made yet). The code is quite long but it basically sets everything up to draw a single textured triangle. I'm having trouble getting this code to draw a 2nd triangle.

I've searched the forums for help but mostly came up with D3D9 code example which using a different function to draw the primitives.

Here is the relevent code:


// Set the number of vertices in the vertex array.
	m_vertexCount = 3;

	// Set the number of indices in the index array.
	m_indexCount = 3;

	// Load the vertex array with data.
	vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f);  // Bottom left.
	vertices[0].texture = XMFLOAT2(0.0f, 0.0f);


	vertices[1].position = XMFLOAT3(1.0f, 1.0f, 0.0f);  // Top middle.
	vertices[1].texture = XMFLOAT2(1.0f, 0.0f);

	vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f);  // Bottom right.
	vertices[2].texture = XMFLOAT2(0.0f, 1.0f);

	// Load the index array with data.
	indices[0] = 0;  // Bottom left.
	indices[1] = 1;  // Top middle.
	indices[2] = 2;  // Bottom right.


        // Along with hundres of lines of initialization and run-time code not shown
        // This line draws the triangle correctly.

        deviceContext->DrawIndexed(indexCount, 0, 0);


Now if I change it to this:


// Set the number of vertices in the vertex array.
m_vertexCount = 4;
// Set the number of indices in the index array.
m_indexCount = 6;
// Load the vertex array with data.
vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); // Bottom left.
vertices[0].texture = XMFLOAT2(0.0f, 0.0f);
vertices[1].position = XMFLOAT3(1.0f, 1.0f, 0.0f); // Top middle.
vertices[1].texture = XMFLOAT2(1.0f, 0.0f);
vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom right.
vertices[2].texture = XMFLOAT2(0.0f, 1.0f);
vertices[3].position = XMFLOAT3(1.0f, -1.0f, 0.0f); // Bottom right.
vertices[3].texture = XMFLOAT2(1.0f, 1.0f);
// Load the index array with data.
indices[0] = 0; // Bottom left.
indices[1] = 1; // Top middle.
indices[2] = 2; // Bottom right.
indices[3] = 1;
indices[4] = 2;
indices[5] = 3;
// Along with hundres of lines of initialization and run-time code not shown
// This line draws the triangle correctly.
deviceContext->DrawIndexed(indexCount, 0, 0);

It still only draws the first traingle, and never draws. the 2nd. I know IndexCount is being set to 6. I also know the index buffer is working correctly with the vertex buffer because I can change: indices[2]=2 to indices[2]=3 and a different triangle gets drawn. I'm curious if anyone familiar with this tutorial series, or just D3DX11 in general might clue me in on how DrawIndexed works, or if there is some obvious setting somehwere to let it know it needs to draw more than 1 triangle?

Advertisement

Just a quick guess, Is backface culling enabled? That might be what is making the second triangle "disappear".

-potential energy is easily made kinetic-

Thank you that was exactly the problem. I switched the CULL_MODE method in the raster from D3D11_CULL_BACK to D3D11_CULL_NONE.

So I guess I'm not understanding the Cull process. Why would the 2nd triangle in my example be culled? The 4 vertices I chose are just form a square (in fact when i changed the culling mode a square shows up perfectly with my square texture perfectly displayed on the 2 triangles). What is it about the 2nd triangle that causes DirectX to determine its backward facing. Though i will say it was consistent. When I tried using CULL_FRONT the first triangle disappeared and only the 2nd triangle was drawn.

Edit: I guess it uses some Normal of the 3 triangles, and when the normal would point directly towards or away from the camera it must be based on the order of the 3 vertex. When I swap the order that the vertices are drawn, it will appear and disappear if culling is set to FRONT or BACK.

Take a look at this: http://learnopengl.com/#!Advanced-OpenGL/Face-culling

-potential energy is easily made kinetic-

Try changing indices to this.


indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;

It's all in whether you define the vertices in clockwise or counterclockwise order. That determines the normal of the face. And that determines which side is which for back-face (or front-face) culling.

Wow, a short post. I didn't know I even could make a short post. ;-)

This topic is closed to new replies.

Advertisement