0
\$\begingroup\$

When i draw a cube using indices, the faces of the cube are skewed but only when CullMode is set to cull back. When i switch it to cull none, it draws the cube perfect. Unfortunately i cant post any code at the time because I'm using my phone, but what are some possible explanations for why the faces of my cube are skewed wjile culling the back? I was thinking the order of my indices may be the problem.

https://i.stack.imgur.com/eZa7E.png

VertexType* vertices;
unsigned long* indices;
D3D10_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D10_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;

m_vertexCount = 8;
m_indexCount = 36;

vertices = new VertexType[m_vertexCount];
if(!vertices)
{
    return false;
}

// Create the index array.
indices = new unsigned long[m_indexCount];
if(!indices)
{
    return false;
}

vertices[0].position = D3DXVECTOR3(-1, 1, -1);
vertices[0].color = D3DXVECTOR4(1, 0, 0, 1);

vertices[1].position = D3DXVECTOR3(1, 1, -1);
vertices[1].color = D3DXVECTOR4(1, 1, 0, 1);

vertices[2].position = D3DXVECTOR3(-1, -1, -1);
vertices[2].color = D3DXVECTOR4(1, 1, 1, 1);

vertices[3].position = D3DXVECTOR3(1, -1, -1);
vertices[3].color = D3DXVECTOR4(0, 1, 0, 1);

vertices[4].position = D3DXVECTOR3(-1, 1, 1);
vertices[4].color = D3DXVECTOR4(0, 1, 1, 1);

vertices[5].position = D3DXVECTOR3(1, 1, 1);
vertices[5].color = D3DXVECTOR4(0, 0, 1, 1);

vertices[6].position = D3DXVECTOR3(-1, -1, 1);
vertices[6].color = D3DXVECTOR4(1, 0, 0, 1);

vertices[7].position = D3DXVECTOR3(1, -1, 1);
vertices[7].color = D3DXVECTOR4(1, 0, 0, 1);

//indices = { 2, 0, 3, 3, 1, 0, 3, 1, 7, 7, 5, 1, 6, 4, 2, 2, 0, 4, 7, 5, 6, 6, 4, 5, 0, 4, 1, 1, 5, 4, 6, 2, 7, 7, 3, 2 };

indices[0] = 2;
indices[1] = 0;
indices[2] = 3;
indices[3] = 3;
indices[4] = 1;
indices[5] = 0;
indices[6] = 3;
indices[7] = 1;
indices[8] = 7;
indices[9] = 7;
indices[10] = 5;
indices[11] = 1;
indices[12] = 6;
indices[13] = 4;
indices[14] = 2;
indices[15] = 2;
indices[16] = 0;
indices[17] = 4;
indices[18] = 7;
indices[19] = 5;
indices[20] = 6; 
indices[21] = 6;
indices[22] = 4;
indices[23] = 5;
indices[24] = 0; 
indices[25] = 4;
indices[26] = 1;
indices[27] = 1;
indices[28] = 5;
indices[29] = 4;
indices[30] = 6;
indices[31] = 2;
indices[32] = 7;
indices[33] = 7;
indices[34] = 3;
indices[35] = 2;

vertexBufferDesc.Usage = D3D10_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) *m_vertexCount;
vertexBufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;

vertexData.pSysMem = vertices;

result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if(FAILED(result))
{
    return false;
}

indexBufferDesc.Usage = D3D10_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) *m_indexCount;
indexBufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;

indexData.pSysMem = indices;

result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);

return true;
}

void CubeClass::RenderBuffers(ID3D10Device* device)
{
unsigned int stride;
unsigned int offset;

stride = sizeof(VertexType);
offset = 0;

device->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);

device->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);

device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

device->DrawIndexed(36, 0, 0);

}

\$\endgroup\$
5
  • \$\begingroup\$ I assume you mean skewed as in it's actually culling the front faces and you're seeing the inside? If this is the case then you're vertex winding is wrong. I believe DirectX is counter-clockwise by default, so make sure that your indices represent that. \$\endgroup\$
    – Syntac_
    Mar 31, 2014 at 23:23
  • \$\begingroup\$ I changed tge binding and it still looks wrong. The triangles arr just being drawn wrong or not at all \$\endgroup\$ Mar 31, 2014 at 23:33
  • \$\begingroup\$ It would be better to wait until we can see pictures and code. \$\endgroup\$
    – Syntac_
    Mar 31, 2014 at 23:37
  • \$\begingroup\$ Edited. It looks right to me \$\endgroup\$ Apr 1, 2014 at 1:59
  • \$\begingroup\$ I think you've got a mixture of vertex orders there. Make sure they are all listed CCW. \$\endgroup\$
    – Syntac_
    Apr 1, 2014 at 18:09

2 Answers 2

1
\$\begingroup\$

Your indices produce triangle with mixed winding orders. Have a look at the quad which is formed by your vertices 0, 1, 2, 3. When viewed along the z-axis it looks like this (sorry for the ASCII art):

    ^ y
    |
0   |   1
    |
----+-----------> x
    |
2   |   3

Your indices draw 203 which gives a clockwise winding, and then 310 which gives a counter-clockwise winding. Modify your indices to produce only one order.

\$\endgroup\$
0
\$\begingroup\$

From experience, when I have an object with faces that do not render, it is because my normals are not facing towards the correct direction. Perhaps setting some normals would help, since you plan on going 3D, this would have to be done anyways.

Here is a reference describing how culling works in D3D. It may well be a winding issue.

Directly from msdn: "To improve rendering performance, you can cull out (or remove) a primitive that faces away from the camera. For single-sided primitives, this saves rendering time because a back-face is not visible. To enable culling, you need to know the winding order of the vertices (typically counter-clockwise). This example will remove any primitive whose back face is facing forward (given a counter-clockwise winding order):..."

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .