使用索引和顶点的纹理

我已经写在纸上,并试图了解如何创建每个立方体的三角形,但我无法正确地在我的立方体上绘制我的纹理。 我不知道这是我的立方体的每个面的纹理坐标,还是从我的顶点创建立方体的顺序,但是我只能看到正确的两面,其余的都是灰色的。

在这里输入图像说明

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); 

我所有的顶点正在建造中。 我认为纹理坐标是正确的。 他们从左上角开始顺时针旋转整个脸部。

  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)); 

指数。 我不知道为什么会这样。 我已经搞乱了纹理值,并且已经获得了纹理来显示四面八方,但是它们总是出现偏斜。

您的纹理坐标被指定为逆时针。 他们应该匹配你的第一张脸的x和y坐标。

 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))); } 

编辑:

另外你的顶点SetUpBackFaceSetUpBottomFace被逆时针提供。

 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))); } 

编辑2:

您需要重新评估您的索引列表:

  // 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));