Skip to main content
Fixed casting bug
Source Link
Andrew Russell
  • 21.3k
  • 7
  • 57
  • 103

Your code for SetupVertices is not taking into account the position of each cube in the chunk. The vertex buffer is for the whole chunk - and individual vertices must be stored in "chunk" space (relative to to the position of the chunk). Right now you're just creating a bunch of cubes at the origin. You need to translate them (just use +) to the correct position.

Then your SetupIndices method is not taking into account the location in the vertex buffer where each cube starts. So, effectively, each time you're adding a cube, you're really telling the GPU to render the first cube in the buffer - and the later cubes are just being ignored.

Here is a modified version of your code. I haven't tested it or checked your numbers. This is just to illustrate how you must do the offsetting for both vertices and indices. This code replaces SetupVertices and SetupIndices with a single method that takes the coordinates of each cube relative to the chunk.

private void SetUpVerticesAndIndicies(int x, int y, int z)
{
    intshort verticesStart = (short)vertices.Count;
    Vector3 cubePosition = new Vector3(x, y, z);
    
    // Create the appropriate vertices for the cube:
    
    //front left bottom corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 0, 0), new Vector2(1, 0)));
    //front left upper corner
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 1, 0), new Vector2(1, 1)));
    //front right upper corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(1, 1, 0), new Vector2(0, 1)));
    // ... etc ...

    // Create the appropriate indices for the cube:
    // (these are indexes into the vertices list)
    
    indices.Add(verticesStart + 0);
    indices.Add(verticesStart + 2);
    indices.Add(verticesStart + 3);

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

    // ... etc ...
}

Hopefully you can see how this would translate to a method to add individual faces, as we discussed in your previous questions.

Your code for SetupVertices is not taking into account the position of each cube in the chunk. The vertex buffer is for the whole chunk - and individual vertices must be stored in "chunk" space (relative to to the position of the chunk). Right now you're just creating a bunch of cubes at the origin. You need to translate them (just use +) to the correct position.

Then your SetupIndices method is not taking into account the location in the vertex buffer where each cube starts. So, effectively, each time you're adding a cube, you're really telling the GPU to render the first cube in the buffer - and the later cubes are just being ignored.

Here is a modified version of your code. I haven't tested it or checked your numbers. This is just to illustrate how you must do the offsetting for both vertices and indices. This code replaces SetupVertices and SetupIndices with a single method that takes the coordinates of each cube relative to the chunk.

private void SetUpVerticesAndIndicies(int x, int y, int z)
{
    int verticesStart = vertices.Count;
    Vector3 cubePosition = new Vector3(x, y, z);
    
    // Create the appropriate vertices for the cube:
    
    //front left bottom corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 0, 0), new Vector2(1, 0)));
    //front left upper corner
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 1, 0), new Vector2(1, 1)));
    //front right upper corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(1, 1, 0), new Vector2(0, 1)));
    // ... etc ...

    // Create the appropriate indices for the cube:
    // (these are indexes into the vertices list)
    
    indices.Add(verticesStart + 0);
    indices.Add(verticesStart + 2);
    indices.Add(verticesStart + 3);

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

    // ... etc ...
}

Hopefully you can see how this would translate to a method to add individual faces, as we discussed in your previous questions.

Your code for SetupVertices is not taking into account the position of each cube in the chunk. The vertex buffer is for the whole chunk - and individual vertices must be stored in "chunk" space (relative to to the position of the chunk). Right now you're just creating a bunch of cubes at the origin. You need to translate them (just use +) to the correct position.

Then your SetupIndices method is not taking into account the location in the vertex buffer where each cube starts. So, effectively, each time you're adding a cube, you're really telling the GPU to render the first cube in the buffer - and the later cubes are just being ignored.

Here is a modified version of your code. I haven't tested it or checked your numbers. This is just to illustrate how you must do the offsetting for both vertices and indices. This code replaces SetupVertices and SetupIndices with a single method that takes the coordinates of each cube relative to the chunk.

private void SetUpVerticesAndIndicies(int x, int y, int z)
{
    short verticesStart = (short)vertices.Count;
    Vector3 cubePosition = new Vector3(x, y, z);
    
    // Create the appropriate vertices for the cube:
    
    //front left bottom corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 0, 0), new Vector2(1, 0)));
    //front left upper corner
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 1, 0), new Vector2(1, 1)));
    //front right upper corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(1, 1, 0), new Vector2(0, 1)));
    // ... etc ...

    // Create the appropriate indices for the cube:
    // (these are indexes into the vertices list)
    
    indices.Add(verticesStart + 0);
    indices.Add(verticesStart + 2);
    indices.Add(verticesStart + 3);

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

    // ... etc ...
}

Hopefully you can see how this would translate to a method to add individual faces, as we discussed in your previous questions.

Source Link
Andrew Russell
  • 21.3k
  • 7
  • 57
  • 103

Your code for SetupVertices is not taking into account the position of each cube in the chunk. The vertex buffer is for the whole chunk - and individual vertices must be stored in "chunk" space (relative to to the position of the chunk). Right now you're just creating a bunch of cubes at the origin. You need to translate them (just use +) to the correct position.

Then your SetupIndices method is not taking into account the location in the vertex buffer where each cube starts. So, effectively, each time you're adding a cube, you're really telling the GPU to render the first cube in the buffer - and the later cubes are just being ignored.

Here is a modified version of your code. I haven't tested it or checked your numbers. This is just to illustrate how you must do the offsetting for both vertices and indices. This code replaces SetupVertices and SetupIndices with a single method that takes the coordinates of each cube relative to the chunk.

private void SetUpVerticesAndIndicies(int x, int y, int z)
{
    int verticesStart = vertices.Count;
    Vector3 cubePosition = new Vector3(x, y, z);
    
    // Create the appropriate vertices for the cube:
    
    //front left bottom corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 0, 0), new Vector2(1, 0)));
    //front left upper corner
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(0, 1, 0), new Vector2(1, 1)));
    //front right upper corner ok
    vertices.Add(new VertexPositionTexture(cubePosition + new Vector3(1, 1, 0), new Vector2(0, 1)));
    // ... etc ...

    // Create the appropriate indices for the cube:
    // (these are indexes into the vertices list)
    
    indices.Add(verticesStart + 0);
    indices.Add(verticesStart + 2);
    indices.Add(verticesStart + 3);

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

    // ... etc ...
}

Hopefully you can see how this would translate to a method to add individual faces, as we discussed in your previous questions.