高度地图呈现颠倒和/或倒退

按照这里find的教程: http : //www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/VertexBuffer_and_IndexBuffer.php

这个页面底部的代码是我正在使用的。 (我已经改变了一些事情,试图解决这个问题,所以我的代码和这不完全相同)。

我注意到,高度贴图实际上是颠倒的。 例如,如果我在高度图上写了“hello”,绘制的时候字母就会颠倒过来。 经过一段时间的摆弄之后,我看不出问题所在,而且这些字母总是会出现倒退或倒置,或者两者兼而有之。

基本上我只是试图让高度图的顶部绘制在北方(+ Z或-Z,最后的方向对我来说并不重要)。

private void LoadHeightData(Texture2D heightMap) { terrainWidth = heightMap.Width; terrainHeight = heightMap.Height; Color[] heightMapColors = new Color[terrainWidth * terrainHeight]; heightMap.GetData(heightMapColors); heightData = new float[terrainWidth * terrainHeight]; for (int i = 0; i < heightData.Length; i++) heightData[i] = heightMapColors[i].R; } private void SetUpVertices() { vertices = new VertexPositionColorNormal[terrainWidth * terrainHeight]; //for (int y = terrainHeight - 1; y > -1; y--) for (int z = 0; z < terrainHeight; z++) { //for (int x = terrainWidth - 1; x > -1; x--) for (int x = 0; x < terrainWidth; x++) { vertices[x + z * terrainWidth].Position = new Vector3(x, heightData[x + z * terrainWidth], -z); vertices[x + z * terrainWidth].Color = Color.White; } } } private void SetUpIndices() { indices = new short[(terrainWidth - 1) * (terrainHeight - 1) * 6]; int counter = 0; for (int y = 0; y < terrainHeight - 1; y++) { for (int x = 0; x < terrainWidth - 1; x++) { int lowerLeft = x + (y * terrainWidth); int lowerRight = (x + 1) + (y * terrainWidth); int topLeft = x + ((y + 1) * terrainWidth); int topRight = (x + 1) + ((y + 1) * terrainWidth); indices[counter++] = (short)topLeft; indices[counter++] = (short)lowerRight; indices[counter++] = (short)lowerLeft; indices[counter++] = (short)topLeft; indices[counter++] = (short)topRight; indices[counter++] = (short)lowerRight; } } } private void CalculateNormals() { for (int i = 0; i < vertices.Length; i++) vertices[i].Normal = new Vector3(0, 0, 0); for (int i = 0; i < indices.Length / 3; i++) { int index1 = indices[i * 3]; int index2 = indices[i * 3 + 1]; int index3 = indices[i * 3 + 2]; Vector3 side1 = vertices[index1].Position - vertices[index3].Position; Vector3 side2 = vertices[index1].Position - vertices[index2].Position; Vector3 normal = Vector3.Cross(side1, side2); vertices[index1].Normal += normal; vertices[index2].Normal += normal; vertices[index3].Normal += normal; } for (int i = 0; i < vertices.Length; i++) vertices[i].Normal.Normalize(); } public void Draw(GraphicsDevice device, Effect effect) { foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration); } } 

用这个代码: 在这里输入图像说明

任何想法的错误可能是什么? 我总是可以发布更多的代码,但除了相机,这是一切。 感谢您的时间!

弄清楚了。

 int lowerLeft = x + (y * terrainWidth); int lowerRight = (x + 1) + (y * terrainWidth); int topLeft = x + ((y + 1) * terrainWidth); int topRight = (x + 1) + ((y + 1) * terrainWidth); 

上面的代码创建了相对于高度图的颠倒三角形。 (在纸上画出来后很容易看到)。这个代码应该变成:

 int topLeft = x + (y * terrainWidth); // was lower int topRight = (x + 1) + (y * terrainWidth); //was lower int lowerLeft = x + ((y + 1) * terrainWidth); // was top int lowerRight = (x + 1) + ((y + 1) * terrainWidth); //was top 

从而按照它们应该出现的方式绘制三角形。 由于左下方被标记为左上方等,所以三角形正在倒置。