public Triangle(Triangle parent, Vector2 tPoint, Vector2 lPoint, Vector2 rPoint, AdaptativeHeightmap map) { //Crea el triángulo this.map = map; int resolution = map.HeightmapData.GetLength(0); this.tInd = (int)(tPoint.X + tPoint.Y * resolution); this.lInd = (int)(lPoint.X + lPoint.Y * resolution); this.rInd = (int)(rPoint.X + rPoint.Y * resolution); this.lPos = new Vector3(map.Position.X + lPoint.X * map.ScaleXZ, map.Position.Y + map.HeightmapData[(int)lPoint.X, (int)lPoint.Y] * map.ScaleY, map.Position.Z + lPoint.Y * map.ScaleXZ); //centro: promedio de cada componente Vector2 center = new Vector2((lPoint.X + rPoint.X) / 2, (lPoint.Y + rPoint.Y) / 2); this.centerPos = new Vector3(map.Position.X + center.X * map.ScaleXZ, map.Position.Y + map.HeightmapData[(int)center.X, (int)center.Y] * map.ScaleY, map.Position.Z + center.Y * map.ScaleXZ); this.parent = parent; //Splitea el triángulo hasta que queden triángulos medianamente chicos //(para dejar el árbol armado con la mayor calidad posible) if (Vector2Distance(lPoint, tPoint) > 1) { this.lChild = new Triangle(this, center, tPoint, lPoint, map); this.rChild = new Triangle(this, center, rPoint, tPoint, map); } }
public void addNeighs(Triangle lNeigh, Triangle rNeigh, Triangle bNeigh) { //Agrega referencias a los vecinos this.lNeigh = lNeigh; this.rNeigh = rNeigh; this.bNeigh = bNeigh; //Encuentra y asigna las referencias a vecinos de sus hijos 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 void loadHeightmap(string heightmapPath, Vector3 center) { var d3dDevice = Gui.I.D3dDevice; //Dispose de vb y ib si había if (vbTerrain != null && !vbTerrain.Disposed) { vbTerrain.Dispose(); } if (ibTerrain != null && !ibTerrain.Disposed) { ibTerrain.Dispose(); } //Cargar heightmap lastHeightmapPath = heightmapPath; HeightmapData = loadHeightmapValues(d3dDevice, heightmapPath); totalVertices = 2 * 3 * (HeightmapData.GetLength(0) - 1) * (HeightmapData.GetLength(1) - 1); //por cada puntito tengo dos triángulos de 3 vértices int width = HeightmapData.GetLength(0); int length = HeightmapData.GetLength(1); //Convertir de centro a esquina this.Position = center - new Vector3(width * ScaleXZ / 2, Position.Y, length * ScaleXZ / 2); //Crear vértices y bajarlos al VertexBuffer updateVertices(); //Crear los dos triángulos root, el piso float terrainSize = width - 1; Triangle leftTriangle = new Triangle(null, new Vector2(0, 0), new Vector2(terrainSize, 0), new Vector2(0, terrainSize), this); Triangle rightTriangle = new Triangle(null, new Vector2(terrainSize, terrainSize), new Vector2(0, terrainSize), new Vector2(terrainSize, 0), this); leftTriangle.addNeighs(null, null, rightTriangle); rightTriangle.addNeighs(null, null, leftTriangle); //Inicializar listas de triángulos e índices this.triangleList = new List<Triangle>(); triangleList.Add(leftTriangle); triangleList.Add(rightTriangle); this.indicesList = new List<int>(); foreach (Triangle t in triangleList) { t.addIndices(ref indicesList); } //Bajar índices al IndexBuffer this.ibTerrain = new IndexBuffer(typeof(int), totalVertices, d3dDevice, Usage.WriteOnly, Pool.Default); ibTerrain.SetData(indicesList.ToArray(), 0, LockFlags.None); }