public override void RenderTiles(TerrainMap map, ref int totalTriangles) { if (fullVboId == -1) { Console.WriteLine("Generating with textures!"); fullVboId = GLUtils.GenBuffer(); var sw = Stopwatch.StartNew(); VertexPos3Tex2[] vertices = BuildTerrain(map, ref triangles); long elapsed = sw.ElapsedMilliseconds; sw.Stop(); Console.WriteLine("Took " + elapsed + " ms to generate the map."); int size = vertices.Length * VertexPos3Tex2.Stride; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, fullVboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), vertices, BufferUsageArb.StaticDraw); fullVerticesCount = vertices.Length; } GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, fullVboId); GL.VertexPointer(3, VertexPointerType.Float, VertexPos3Tex2.Stride, new IntPtr(0)); GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexPos3Tex2.Stride, new IntPtr(12)); GL.DrawArrays(BeginMode.Quads, 0, fullVerticesCount); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.DisableClientState(ArrayCap.VertexArray); totalTriangles = triangles; }
public override void RenderTiles(TerrainMap map, ref int totalTriangles) { if (vboId == -1) { Console.WriteLine("Generating with colours!"); vboId = GLUtils.GenBuffer(); var sw = Stopwatch.StartNew(); Vertex[] vertices = BuildTerrain(map, ref triangles); long elapsed = sw.ElapsedMilliseconds; sw.Stop(); Console.WriteLine("Took " + elapsed + " ms to generate the map."); int size = vertices.Length * 16; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, vboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), vertices, BufferUsageArb.StaticDraw); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); RenderColoursInfo(map); } GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.ColorArray); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, vboId); GL.VertexPointer(3, VertexPointerType.Float, 16, new IntPtr(0)); GL.ColorPointer(4, ColorPointerType.UnsignedByte, 16, new IntPtr(12)); GL.DrawArrays(BeginMode.Triangles, 0, triangles * 3); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.ColorArray); totalTriangles = triangles; }
protected override void BuildTerrainVbo(TerrainMap map) { fullVboId = GLUtils.GenBuffer(); Vertex[] fullVertices = new Vertex[map.Width * map.Length * PrimitiveElementSize]; vertices = fullVertices; BuildTerrain(map, ref triangles); fullVerticesCount = verticesCount; int size = fullVerticesCount * 16; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, fullVboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), fullVertices, BufferUsageArb.StaticDraw); if (linesEnabled) { linesVboId = GLUtils.GenBuffer(); lineMode = true; Vertex[] lineVertices = new Vertex[map.Width * map.Length * PrimitiveElementSize]; vertices = lineVertices; BuildTerrain(map, ref triangles); size = verticesCount * 16; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, linesVboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), lineVertices, BufferUsageArb.StaticDraw); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); GL.LineWidth(2); } }
public override void RenderTiles(TerrainMap map, ref int totalTriangles) { if (fullVboId == -1) { Console.WriteLine("Generating with textures!"); fullVboId = GLUtils.GenBuffer(); linesVboId = GLUtils.GenBuffer(); var sw = Stopwatch.StartNew(); VertexPos3Tex2[] vertices = BuildTerrain(map, ref triangles); VertexPos3Col4[] lineVertices = BuildTerrainBorders(map, ref triangles); long elapsed = sw.ElapsedMilliseconds; sw.Stop(); Console.WriteLine("Took " + elapsed + " ms to generate the map."); int size = vertices.Length * VertexPos3Tex2.Stride; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, fullVboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), vertices, BufferUsageArb.StaticDraw); size = lineVertices.Length * VertexPos3Col4.Stride; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, linesVboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), lineVertices, BufferUsageArb.StaticDraw); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); fullVerticesCount = vertices.Length; lineVerticesCount = lineVertices.Length; GL.LineWidth(5); } GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, fullVboId); GL.VertexPointer(3, VertexPointerType.Float, VertexPos3Tex2.Stride, new IntPtr(0)); GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexPos3Tex2.Stride, new IntPtr(12)); GL.DrawArrays(BeginMode.Quads, 0, fullVerticesCount); GL.DisableClientState(ArrayCap.TextureCoordArray); /*GL.EnableClientState( ArrayCap.ColorArray ); * * GL.PolygonMode( MaterialFace.FrontAndBack, PolygonMode.Line ); * GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, linesVboId ); * GL.VertexPointer( 3, VertexPointerType.Float, VertexPos3Col4.Stride, new IntPtr( 0 ) ); * GL.ColorPointer( 4, ColorPointerType.UnsignedByte, VertexPos3Col4.Stride, new IntPtr( 12 ) ); * GL.DrawArrays( BeginMode.Quads, 0, lineVerticesCount ); * GL.PolygonMode( MaterialFace.FrontAndBack, PolygonMode.Fill ); * * GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, 0 ); * GL.DisableClientState( ArrayCap.ColorArray );*/ GL.DisableClientState(ArrayCap.VertexArray); totalTriangles = triangles; }
protected override void BuildTerrainVbo(TerrainMap map) { vboId = GLUtils.GenBuffer(); vertices = new Vertex[map.Width * map.Length * PrimitiveElementSize]; BuildTerrain(map, ref triangles); Console.WriteLine("Triangles:" + triangles + "," + verticesCount); Console.WriteLine(GC.GetTotalMemory(false) / 1024 / 1024); Console.WriteLine(vertices.Length * Marshal.SizeOf(default(Vertex)) / 1024 / 1024); int size = verticesCount * 16; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, vboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), vertices, BufferUsageArb.StaticDraw); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); // Yes this is not nice having to force manual garbage collection. // TODO: Consider using unmanaged memory for this process. // ptr = Marshal.AllocHGlobal( width * length * 6 * Vertex.Stride ) }
BufferInfo BuildTerrainPart(TerrainMap map, int x1, int y1, int width, int length) { TileDrawInfo drawInfo = new TileDrawInfo(); BufferInfo info = new BufferInfo(); int vboId = GLUtils.GenBuffer(); int lengthX = map.Width; int lengthY = map.Length; Vector3[] vertices = new Vector3[width * length * 6]; int index = 0; int x2 = x1 + width; int y2 = y1 + length; float zMax = 0; for (int x = x1; x < x2; x++) { for (int y = y1; y < y2; y++) { Tile tile = map[x, y]; if (tile.Height > zMax) { zMax = tile.Height; } drawInfo.CurrentTile = tile; bool leftExists = x > 0; bool behindExists = y > 0; bool rightExists = x < lengthX - 1; bool frontExists = y < lengthY - 1; if (leftExists && behindExists) { drawInfo.LeftBehind = map[x - 1, y - 1]; } if (leftExists && frontExists) { drawInfo.LeftFront = map[x - 1, y + 1]; } if (rightExists && behindExists) { drawInfo.RightBehind = map[x + 1, y - 1]; } if (rightExists && frontExists) { drawInfo.RightFront = map[x + 1, y + 1]; } if (leftExists) { drawInfo.Left = map[x - 1, y]; } if (behindExists) { drawInfo.Behind = map[x, y - 1]; } if (rightExists) { drawInfo.Right = map[x + 1, y]; } if (frontExists) { drawInfo.Front = map[x, y + 1]; } drawInfo.MapX = x; drawInfo.MapY = y; RenderTile(drawInfo, vertices, index); index += 6; } } // end for int size = vertices.Length * 12; GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, vboId); GL.Arb.BufferData(BufferTargetArb.ArrayBuffer, new IntPtr(size), vertices, BufferUsageArb.StaticDraw); GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, 0); info.Centre = new Vector3(x1 + width / 2f, zMax / 2f, y1 + length / 2f); info.Radius = length; info.VboId = vboId; info.VerticesCount = drawInfo.TotalTriangles * 3; return(info); }