public void AddNeighs(Triangle lNeigh, Triangle rNeigh, Triangle bNeigh) { this.lNeigh = lNeigh; this.rNeigh = rNeigh; this.bNeigh = bNeigh; if (lChild != null) { Triangle bNeighRightChild = null; Triangle bNeighLeftChild = null; Triangle lNeighRightChild = null; Triangle rNeighLeftChild = null; if (bNeigh != null) { bNeighLeftChild = bNeigh.lChild; bNeighRightChild = bNeigh.rChild; } if (lNeigh != null) lNeighRightChild = lNeigh.rChild; if (rNeigh != null) rNeighLeftChild = rNeigh.lChild; lChild.AddNeighs(rChild, bNeighRightChild, lNeighRightChild); rChild.AddNeighs(bNeighLeftChild, lChild, rNeighLeftChild); } }
public Triangle(Triangle parent, Vector2 tPoint, Vector2 lPoint, Vector2 rPoint, float[,] heightData) { int resolution = heightData.GetLength(0); tInd = (int)(tPoint.X + tPoint.Y * resolution); lInd = (int)(lPoint.X + lPoint.Y * resolution); rInd = (int)(rPoint.X + rPoint.Y * resolution); lPos = new Vector3(lPoint.X, heightData[(int)lPoint.X, (int)lPoint.Y], lPoint.Y); Vector2 center = (lPoint + rPoint) / 2; centerPos = new Vector3(center.X, heightData[(int)center.X, (int)center.Y], center.Y); this.parent = parent; if (Vector2.Distance(lPoint, tPoint) > 1) { lChild = new Triangle(this, center, tPoint, lPoint, heightData); rChild = new Triangle(this, center, rPoint, tPoint, heightData); } }
protected override void LoadContent() { //Loading textures and effects from content terrainMap = Game.Content.Load<Texture2D>("Models/Terrain/Heightmaps/heightmap4"); grassTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/grass"); sandTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/sand"); snowTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/snow"); rockTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/rock"); TerrainUtils.LoadHeightData(terrainMap, ref heightData, ref width, ref height); TerrainUtils.SetUpVertices(heightData, ref vertices, width, height); indices = TerrainUtils.CreateTerrainIndices(width, height); vertices = TerrainUtils.GenerateNormalsForTriangleStrip(vertices, indices); VertexMultiTextured[,] vertexArray = TerrainUtils.Reshape1DTo2D<VertexMultiTextured>(vertices, width, height); rootNode = new QTNode(vertexArray, device, grassTexture, 64, effect, rockTexture, snowTexture, sandTexture); TerrainUtils.CopyToBuffer(ref vertexBuffer, ref indexBuffer, vertices, indices, device); int terrainSize = 32; Triangle leftTriangle = new Triangle(null, new Vector2(0, 0), new Vector2(terrainSize, 0), new Vector2(0, terrainSize), heightData); Triangle rightTriangle = new Triangle(null, new Vector2(terrainSize, terrainSize), new Vector2(0, terrainSize), new Vector2(terrainSize, 0), heightData); leftTriangle.AddNeighs(null, null, rightTriangle); rightTriangle.AddNeighs(null, null, leftTriangle); triangleList = new List<Triangle>(); triangleList.Add(leftTriangle); triangleList.Add(rightTriangle); indicesList = new List<int>(); foreach (Triangle t in triangleList) t.AddIndices(ref indicesList); dynamicIndexBuffer = new DynamicIndexBuffer(device, typeof(int), indicesList.Count, BufferUsage.WriteOnly); dynamicIndexBuffer.SetData(indicesList.ToArray(), 0, indicesList.Count, SetDataOptions.Discard); dynamicIndexBuffer.ContentLost += dynamicIndexBuffer_ContentLost; }