public StaticDecal(TerrainInfo map_info, Material mat, Vector3 pos, float size, int resolution, float rotation) { material = mat; map_info.GetHeight(pos, ref pos.Y); bounding = new BoundingSphere(pos, size); float x1 = -size * 0.5f; float z1 = -size * 0.5f; vertices = new VertexTextureColor[(resolution + 1) * (resolution + 1) * 3 * 2]; int amount = 0; Vector2 texD1 = new Vector2(0, 0), texD2 = new Vector2(1, 0), texD3 = new Vector2(0, 1), texD4 = new Vector2(1, 1); Color4 color = material.color; for (float i = 0; i < resolution; i++) { for (float j = 0; j < resolution; j++) { Vector3 position = new Vector3(x1, 0, z1); Vector3 local_position; local_position = rotatePoint(pos, position + new Vector3(0, 0, 0), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2(i / resolution, j / resolution), color); local_position = rotatePoint(pos, position + new Vector3(size / resolution, 0, size / resolution), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2((i + 1) / resolution, (j + 1) / resolution), color); local_position = rotatePoint(pos, position + new Vector3(0, 0, size / resolution), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2(i / resolution, (j + 1) / resolution), color); local_position = rotatePoint(pos, position + new Vector3(0, 0, 0), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2(i / resolution, j / resolution), color); local_position = rotatePoint(pos, position + new Vector3(size / resolution, 0, 0), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2((i + 1) / resolution, j / resolution), color); local_position = rotatePoint(pos, position + new Vector3(size / resolution, 0, size / resolution), rotation); map_info.GetHeight(local_position, ref local_position.Y); vertices[amount++] = new VertexTextureColor(local_position, new Vector2((i + 1) / resolution, (j + 1) / resolution), color); z1 += size / resolution; } x1 += size / resolution; z1 = -size * 0.5f; } vertexBuffer = Buffer.Create <VertexTextureColor>(Display.device, vertices, new BufferDescription(vertices.Length * VertexTextureColor.SizeInBytes, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); }
public void RebuildTerrain(int buffer, float quality) { int resX = (int)Math.Floor(info.width / quality); int resY = (int)Math.Floor(info.height / quality); float tileX = (float)info.width / resX; float tileY = (float)info.height / resY; /********* calc vertices *************/ vertices = new VertexTexture[resX * resY]; int pos = 0; for (int x = 0; x < resX; x++) { for (int y = 0; y < resY; y++) { pos = x + y * resX; vertices[pos].Position = new Vector3(tileX * x, 0, tileY * y); info.GetHeight(vertices[pos].Position, ref vertices[pos].Position.Y); vertices[pos].TexCoord.X = (float)x / resX; vertices[pos].TexCoord.Y = (float)y / resY; if (x == resX - 1) { vertices[pos].Position.X = info.width; vertices[pos].TexCoord.X = 1; } if (y == resY - 1) { vertices[pos].Position.Z = info.height; vertices[pos].TexCoord.Y = 1; } } } //*********** calc indexes ******************** indices = new int[(resX - 1) * (resY - 1) * 6]; int counter = 0; for (uint y = 0; y < resY - 1; y++) { for (uint x = 0; x < resX - 1; x++) { int topLeft = (int)(x + y * resX); int topRight = (int)((x + 1) + y * resX); int lowerLeft = (int)(x + (y + 1) * resX); int lowerRight = (int)((x + 1) + (y + 1) * resX); indices[counter++] = topLeft; indices[counter++] = topRight; indices[counter++] = lowerLeft; indices[counter++] = topRight; indices[counter++] = lowerRight; indices[counter++] = lowerLeft; } } IndicesLength[buffer] = indices.Length; indexBuffer[buffer] = new Buffer(Display.device, DataStream.Create(indices, false, false), new BufferDescription(indices.Length * 4, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); vertexBuffer[buffer] = Buffer.Create <VertexTexture>(Display.device, vertices, new BufferDescription(vertices.Length * VertexTexture.SizeInBytes, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); }