1
\$\begingroup\$

I've written this down on paper and tried to understand how the triangles of each cube are created, but I can't get my texture drawn on my cube properly. I don't know if it's the texuture coordinates of each face of my cube, or the order in which I create the cube from my vertices, but I only see two sides correctly and the rest are gray.

enter image description here

    private void SetUpFrontFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {

        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, 0), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, 0), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, 0), new Vector2(1, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, 0), new Vector2(1, 1)));
    }

    private void SetUpBackFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, -1), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, -1), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, -1), new Vector2(1, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, -1), new Vector2(1, 1)));
    }

    private void SetUpLeftFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, -1), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, -1), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, 0), new Vector2(1, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, 0), new Vector2(1, 1)));
    }

    private void SetUpRightFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, 0), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, 0), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, -1), new Vector2(1, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, -1), new Vector2(1, 1)));
    }

    private void SetUpTopFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, 0), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, -1), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, -1), new Vector2(1, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, 0), new Vector2(1, 1)));
    }

    private void SetUpBottomFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
    {
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, 0), new Vector2(0, 0)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, -1), new Vector2(0, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, -1), new Vector2(1, 1)));
        vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, 0), new Vector2(1, 0)));
    }

    private void SetUpIndicesAndVertices(float x, float y, float z, List<VertexPositionTexture> vertices, List<short> indices)
    {
        short verticesStart = (short)vertices.Count;

        Vector3 cubeInChunkPosition = new Vector3(x, y, z);

        SetUpFrontFace(cubeInChunkPosition, vertices);
        SetUpBackFace(cubeInChunkPosition, vertices);

        SetUpRightFace(cubeInChunkPosition, vertices);
        SetUpLeftFace(cubeInChunkPosition, vertices);

        SetUpTopFace(cubeInChunkPosition, vertices);
        SetUpBottomFace(cubeInChunkPosition, vertices);

All my vertices being built. I think the texture coordinates are correct. They start from the top left corner rotating clockwise around the whole face.

    indices.Add((short)(verticesStart + 0));
        indices.Add((short)(verticesStart + 2));
        indices.Add((short)(verticesStart + 3));

        indices.Add((short)(verticesStart + 0));
        indices.Add((short)(verticesStart + 1));
        indices.Add((short)(verticesStart + 2));

        indices.Add((short)(verticesStart + 1));
        indices.Add((short)(verticesStart + 5));
        indices.Add((short)(verticesStart + 6));

        indices.Add((short)(verticesStart + 1));
        indices.Add((short)(verticesStart + 6));
        indices.Add((short)(verticesStart + 2));

        indices.Add((short)(verticesStart + 2));
        indices.Add((short)(verticesStart + 6));
        indices.Add((short)(verticesStart + 7));

        indices.Add((short)(verticesStart + 2));
        indices.Add((short)(verticesStart + 7));
        indices.Add((short)(verticesStart + 3));

        indices.Add((short)(verticesStart + 4));
        indices.Add((short)(verticesStart + 7));
        indices.Add((short)(verticesStart + 6));

        indices.Add((short)(verticesStart + 4));
        indices.Add((short)(verticesStart + 6));
        indices.Add((short)(verticesStart + 5));

        indices.Add((short)(verticesStart + 1));
        indices.Add((short)(verticesStart + 4));
        indices.Add((short)(verticesStart + 5));

        indices.Add((short)(verticesStart + 1));
        indices.Add((short)(verticesStart + 0));
        indices.Add((short)(verticesStart + 4));

        indices.Add((short)(verticesStart + 0));
        indices.Add((short)(verticesStart + 7));
        indices.Add((short)(verticesStart + 4));

        indices.Add((short)(verticesStart + 0));
        indices.Add((short)(verticesStart + 3));
        indices.Add((short)(verticesStart + 7));

The indices. I don't know why this is happening. I've messed around with the Texture values and have gotten textures to show on all sides, but they always come out skewed.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your texture coordinates are being specified anti-clockwise. They should match the x and y coords of your first face.

private void SetUpFrontFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
{

    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, 0), new Vector2(0, 0)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, 0), new Vector2(0, 1)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, 0), new Vector2(1, 1)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, 0), new Vector2(1, 0)));
}

EDIT:

Also your vertices in SetUpBackFace and SetUpBottomFace are being supplied in anti-clockwise.

private void SetUpBackFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
{
     vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, -1), new Vector2(0, 0)));
     vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, -1), new Vector2(0, 1)));
     vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 1, -1), new Vector2(1, 1)));
     vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 1, -1), new Vector2(1, 0)));
}

private void SetUpBottomFace(Vector3 cubeInChunkPosition, List<VertexPositionTexture> vertices)
{
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, 0), new Vector2(0, 0)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, 0), new Vector2(0, 1)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(1, 0, -1), new Vector2(1, 1)));
    vertices.Add(new VertexPositionTexture(cubeInChunkPosition + new Vector3(0, 0, -1), new Vector2(1, 0)));
}

EDIT 2:

You need to re-evaluate your index list:

    // Front face
    indices.Add((short)(verticesStart + 0));
    indices.Add((short)(verticesStart + 2));
    indices.Add((short)(verticesStart + 3));

    indices.Add((short)(verticesStart + 0));
    indices.Add((short)(verticesStart + 1));
    indices.Add((short)(verticesStart + 2));

    // Back face
    indices.Add((short)(verticesStart + 4));
    indices.Add((short)(verticesStart + 6));
    indices.Add((short)(verticesStart + 7));

    indices.Add((short)(verticesStart + 4));
    indices.Add((short)(verticesStart + 5));
    indices.Add((short)(verticesStart + 6));

    // Left face
    indices.Add((short)(verticesStart + 8));
    indices.Add((short)(verticesStart + 10));
    indices.Add((short)(verticesStart + 11));

    indices.Add((short)(verticesStart + 8));
    indices.Add((short)(verticesStart + 9));
    indices.Add((short)(verticesStart + 10));

    // Right face
    indices.Add((short)(verticesStart + 12));
    indices.Add((short)(verticesStart + 14));
    indices.Add((short)(verticesStart + 15));

    indices.Add((short)(verticesStart + 12));
    indices.Add((short)(verticesStart + 13));
    indices.Add((short)(verticesStart + 14));

    // Top face
    indices.Add((short)(verticesStart + 16));
    indices.Add((short)(verticesStart + 18));
    indices.Add((short)(verticesStart + 19));

    indices.Add((short)(verticesStart + 16));
    indices.Add((short)(verticesStart + 17));
    indices.Add((short)(verticesStart + 18));

    // Bottom face
    indices.Add((short)(verticesStart + 20));
    indices.Add((short)(verticesStart + 22));
    indices.Add((short)(verticesStart + 23));

    indices.Add((short)(verticesStart + 20));
    indices.Add((short)(verticesStart + 21));
    indices.Add((short)(verticesStart + 22));
\$\endgroup\$
9
  • \$\begingroup\$ That worked for the most part. 2 faces are still completely gray \$\endgroup\$ Jul 14, 2013 at 22:28
  • \$\begingroup\$ I figured out that its in my SetUpBackFace method. If I change the texture values, everything turns gray except 2 sides \$\endgroup\$ Jul 14, 2013 at 22:30
  • \$\begingroup\$ Yes you're right, I've edited the answer. \$\endgroup\$
    – Syntac_
    Jul 14, 2013 at 22:46
  • \$\begingroup\$ That actually made things worse for some reason. Now the actual sides of the cube are wacky \$\endgroup\$ Jul 14, 2013 at 22:55
  • \$\begingroup\$ Yes, your index list was very bizarre. Edited. \$\endgroup\$
    – Syntac_
    Jul 14, 2013 at 23:20

You must log in to answer this question.

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