diff --git a/GraphicsAPI/DirectXApi.cs b/GraphicsAPI/DirectXApi.cs index 52ba59f10..fc4964294 100644 --- a/GraphicsAPI/DirectXApi.cs +++ b/GraphicsAPI/DirectXApi.cs @@ -257,10 +257,10 @@ namespace ClassicalSharp.GraphicsAPI { return IsValid( iBuffers, ib ); } - public override void DrawVb( DrawMode mode, VertexFormat format, int id, int offset, int verticesCount ) { + public override void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) { device.SetStreamSource( 0, vBuffers[id], 0, strideSizes[(int)format] ); device.VertexFormat = formatMapping[(int)format]; - device.DrawPrimitives( modeMappings[(int)mode], offset, NumPrimitives( verticesCount, mode ) ); + device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) ); } int batchStride; @@ -269,9 +269,9 @@ namespace ClassicalSharp.GraphicsAPI { batchStride = strideSizes[(int)format]; } - public override void DrawVbBatch( DrawMode mode, int id, int offset, int verticesCount ) { + public override void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ) { device.SetStreamSource( 0, vBuffers[id], 0, batchStride ); - device.DrawPrimitives( modeMappings[(int)mode], offset, NumPrimitives( verticesCount, mode ) ); + device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) ); } public override void EndVbBatch() { @@ -282,11 +282,12 @@ namespace ClassicalSharp.GraphicsAPI { batchStride = VertexPos3fTex2fCol4b.Size; } - public override void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount ) { + public override void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount, + int startVertex, int startIndex ) { device.Indices = iBuffers[ib]; device.SetStreamSource( 0, vBuffers[vb], 0, batchStride ); - device.DrawIndexedPrimitives( modeMappings[(int)mode], 0, 0, - indicesCount / 6 * 4, 0, NumPrimitives( indicesCount, mode ) ); + device.DrawIndexedPrimitives( modeMappings[(int)mode], startVertex, startVertex, + indicesCount / 6 * 4, startIndex, NumPrimitives( indicesCount, mode ) ); } public override void EndIndexedVbBatch() { diff --git a/GraphicsAPI/IGraphicsApi.cs b/GraphicsAPI/IGraphicsApi.cs index 31701e4e1..10fe77065 100644 --- a/GraphicsAPI/IGraphicsApi.cs +++ b/GraphicsAPI/IGraphicsApi.cs @@ -122,17 +122,18 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void DeleteIb( int ib ); - public abstract void DrawVb( DrawMode mode, VertexFormat format, int id, int offset, int verticesCount ); + public abstract void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ); public abstract void BeginVbBatch( VertexFormat format ); - public abstract void DrawVbBatch( DrawMode mode, int id, int offset, int verticesCount ); + public abstract void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ); public abstract void EndVbBatch(); public abstract void BeginIndexedVbBatch(); - public abstract void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount ); + public abstract void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount, + int startVertex, int startIndex ); public abstract void EndIndexedVbBatch(); diff --git a/GraphicsAPI/OpenGLApi.cs b/GraphicsAPI/OpenGLApi.cs index 7ccc5d76b..eace514f7 100644 --- a/GraphicsAPI/OpenGLApi.cs +++ b/GraphicsAPI/OpenGLApi.cs @@ -283,9 +283,9 @@ namespace ClassicalSharp.GraphicsAPI { return GL.Arb.IsBuffer( ib ); } - public override void DrawVb( DrawMode mode, VertexFormat format, int id, int offset, int verticesCount ) { + public override void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) { BeginVbBatch( format ); - DrawVbBatch( mode, id, offset, verticesCount ); + DrawVbBatch( mode, id, startVertex, verticesCount ); EndVbBatch(); } @@ -320,17 +320,21 @@ namespace ClassicalSharp.GraphicsAPI { BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); } - public override void DrawVbBatch( DrawMode mode, int id, int offset, int verticesCount ) { - drawBatchFunc( mode, id, offset, verticesCount ); + public override void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ) { + drawBatchFunc( mode, id, startVertex, verticesCount ); } - public override void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount ) { + const DrawElementsType indexType = DrawElementsType.UnsignedShort; + public override void DrawIndexedVbBatch( DrawMode mode, int vb, int ib, int indicesCount, + int startVertex, int startIndex ) { GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, vb ); GL.Arb.BindBuffer( BufferTargetArb.ElementArrayBuffer, ib ); - GL.VertexPointer( 3, VertexPointerType.Float, 24, new IntPtr( 0 ) ); - GL.ColorPointer( 4, ColorPointerType.UnsignedByte, 24, new IntPtr( 12 ) ); - GL.TexCoordPointer( 2, TexCoordPointerType.Float, 24, new IntPtr( 16 ) ); - GL.DrawElements( modeMappings[(int)mode], indicesCount, DrawElementsType.UnsignedShort, IntPtr.Zero ); + + int offset = startVertex * VertexPos3fTex2fCol4b.Size; + GL.VertexPointer( 3, VertexPointerType.Float, 24, new IntPtr( offset ) ); + GL.ColorPointer( 4, ColorPointerType.UnsignedByte, 24, new IntPtr( offset + 12 ) ); + GL.TexCoordPointer( 2, TexCoordPointerType.Float, 24, new IntPtr( offset + 16 ) ); + GL.DrawElements( modeMappings[(int)mode], indicesCount, indexType, new IntPtr( startIndex * 2 ) ); } public override void EndVbBatch() { diff --git a/Map/ChunkMeshBuilder.cs b/Map/ChunkMeshBuilder.cs index 78eccf929..f3cff74ca 100644 --- a/Map/ChunkMeshBuilder.cs +++ b/Map/ChunkMeshBuilder.cs @@ -337,16 +337,12 @@ namespace ClassicalSharp { public struct ChunkPartInfo { public int VbId, IbId; - public int VbId2, IbId2; - public int IndicesCount, IndicesCount2; + public int IndicesCount; public ChunkPartInfo( int vb, int ib, int indices ) { VbId = vb; IbId = ib; IndicesCount = indices; - VbId2 = 0; - IbId2 = 0; - IndicesCount2 = 0; } } } \ No newline at end of file diff --git a/Map/ChunkMeshBuilderTex2Col4.cs b/Map/ChunkMeshBuilderTex2Col4.cs index 472189fc2..4f8aee8fd 100644 --- a/Map/ChunkMeshBuilderTex2Col4.cs +++ b/Map/ChunkMeshBuilderTex2Col4.cs @@ -8,7 +8,6 @@ namespace ClassicalSharp { DrawInfo1D[] drawInfoBuffer; TerrainAtlas1D atlas; int arraysCount = 0; - const int maxIndices = 65536; void TerrainAtlasChanged( object sender, EventArgs e ) { int newArraysCount = Window.TerrainAtlas1D.TexIds.Length; @@ -28,45 +27,28 @@ namespace ClassicalSharp { } class DrawInfo1DPart { - public VertexPos3fTex2fCol4b[] vertices1, vertices2, vertices; - public ushort[] indices1, indices2, indices; + public VertexPos3fTex2fCol4b[] vertices; + public ushort[] indices; public int vIndex, vCount; public int iIndex, iCount; - public int vCount1, vCount2; - public int iCount1, iCount2; public DrawInfo1DPart() { - vertices1 = new VertexPos3fTex2fCol4b[0]; - indices1 = new ushort[0]; - vertices2 = new VertexPos3fTex2fCol4b[0]; - indices2 = new ushort[0]; + vertices = new VertexPos3fTex2fCol4b[0]; + indices = new ushort[0]; } public void ExpandToCapacity() { vCount = ( iCount / 6 ) * 4; - - vCount1 = Math.Min( vCount, maxIndices ); - iCount1 = ( vCount1 / 4 ) * 6; - if( vCount1 > vertices1.Length ) { - vertices1 = new VertexPos3fTex2fCol4b[vCount1]; - indices1 = new ushort[iCount1]; + + if( vCount > vertices.Length ) { + vertices = new VertexPos3fTex2fCol4b[vCount]; + indices = new ushort[iCount]; } - - vCount2 = Math.Max( 0, vCount - maxIndices ); - iCount2 = ( vCount2 / 4 ) * 6; - if( vCount2 > vertices2.Length ) { - vertices2 = new VertexPos3fTex2fCol4b[vCount2]; - indices2 = new ushort[iCount2]; - } - vertices = vertices1; - indices = indices1; } public void ResetState() { vIndex = iIndex = 0; vCount = iCount = 0; - vCount1 = vCount2 = 0; - iCount1 = iCount2 = 0; } } @@ -138,18 +120,13 @@ namespace ClassicalSharp { } void SetPartInfo( DrawInfo1DPart part, int i, ref ChunkPartInfo[] parts ) { - if( part.iCount1 == 0 ) return; + if( part.iCount == 0 ) return; ChunkPartInfo info = default( ChunkPartInfo ); - info.VbId = Graphics.InitVb( part.vertices1, VertexFormat.Pos3fTex2fCol4b, part.vCount1 ); - info.IbId = Graphics.InitIb( part.indices1, part.iCount1 ); - info.IndicesCount = part.iCount1; + info.VbId = Graphics.InitVb( part.vertices, VertexFormat.Pos3fTex2fCol4b, part.vCount ); + info.IbId = Graphics.InitIb( part.indices, part.iCount ); + info.IndicesCount = part.iCount; - if( part.iCount2 > 0 ) { - info.VbId2 = Graphics.InitVb( part.vertices2, VertexFormat.Pos3fTex2fCol4b, part.vCount2 ); - info.IbId2 = Graphics.InitIb( part.indices2, part.iCount2 ); - info.IndicesCount2 = part.iCount2; - } // Lazy initalize part arrays so we can save time in MapRenderer for chunks that only contain 1 or 2 part types. if( parts == null ) parts = new ChunkPartInfo[arraysCount]; @@ -363,13 +340,6 @@ namespace ClassicalSharp { void AddIndices( DrawInfo1DPart part ) { int element = part.vIndex; - if( element == maxIndices ) { - part.indices = part.indices2; - part.vertices = part.vertices2; - part.iIndex = 0; - part.vIndex = 0; - element = 0; - } part.indices[part.iIndex++] = (ushort)( element + 0 ); part.indices[part.iIndex++] = (ushort)( element + 1 ); part.indices[part.iIndex++] = (ushort)( element + 2 ); diff --git a/Rendering/MapRenderer.cs b/Rendering/MapRenderer.cs index 75f322dea..41e430215 100644 --- a/Rendering/MapRenderer.cs +++ b/Rendering/MapRenderer.cs @@ -280,19 +280,22 @@ namespace ClassicalSharp { } const DrawMode mode = DrawMode.Triangles; + const int maxVertex = 65536; + const int maxIndices = maxVertex / 4 * 6; void RenderSolidBatch( int batch ) { for( int i = 0; i < chunks.Length; i++ ) { ChunkInfo info = chunks[i]; if( info.SolidParts == null || !info.Visible ) continue; - ChunkPartInfo drawInfo = info.SolidParts[batch]; - if( drawInfo.IndicesCount > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId, drawInfo.IbId, drawInfo.IndicesCount ); - Window.Vertices += drawInfo.IndicesCount; - if( drawInfo.IndicesCount2 > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId2, drawInfo.IbId2, drawInfo.IndicesCount2 ); - Window.Vertices += drawInfo.IndicesCount2; + ChunkPartInfo part = info.SolidParts[batch]; + if( part.IndicesCount > 0 ) { + if( part.IndicesCount > maxIndices ) { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, maxIndices, 0, 0 ); + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount - maxIndices, maxVertex, maxIndices ); + } else { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount, 0, 0 ); } + Window.Vertices += part.IndicesCount; } } } @@ -302,10 +305,10 @@ namespace ClassicalSharp { ChunkInfo info = chunks[i]; if( info.SpriteParts == null || !info.Visible ) continue; - ChunkPartInfo drawInfo = info.SpriteParts[batch]; - if( drawInfo.IndicesCount > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId, drawInfo.IbId, drawInfo.IndicesCount ); - Window.Vertices += drawInfo.IndicesCount; + ChunkPartInfo part = info.SpriteParts[batch]; + if( part.IndicesCount > 0 ) { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount, 0, 0 ); + Window.Vertices += part.IndicesCount; } } } @@ -315,14 +318,15 @@ namespace ClassicalSharp { ChunkInfo info = chunks[i]; if( info.TranslucentParts == null || !info.Visible ) continue; - ChunkPartInfo drawInfo = info.TranslucentParts[batch]; - if( drawInfo.IndicesCount > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId, drawInfo.IbId, drawInfo.IndicesCount ); - Window.Vertices += drawInfo.IndicesCount; - if( drawInfo.IndicesCount2 > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId2, drawInfo.IbId2, drawInfo.IndicesCount2 ); - Window.Vertices += drawInfo.IndicesCount2; + ChunkPartInfo part = info.TranslucentParts[batch]; + if( part.IndicesCount > 0 ) { + if( part.IndicesCount > maxIndices ) { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, maxIndices, 0, 0 ); + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount - maxIndices, maxVertex, maxIndices ); + } else { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount, 0, 0 ); } + Window.Vertices += part.IndicesCount; } } } @@ -332,11 +336,13 @@ namespace ClassicalSharp { ChunkInfo info = chunks[i]; if( info.TranslucentParts == null || !info.Visible ) continue; - ChunkPartInfo drawInfo = info.TranslucentParts[batch]; - if( drawInfo.IndicesCount > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId, drawInfo.IbId, drawInfo.IndicesCount ); - if( drawInfo.IndicesCount2 > 0 ) { - Graphics.DrawIndexedVbBatch( mode, drawInfo.VbId2, drawInfo.IbId2, drawInfo.IndicesCount2 ); + ChunkPartInfo part = info.TranslucentParts[batch]; + if( part.IndicesCount > 0 ) { + if( part.IndicesCount > maxIndices ) { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, maxIndices, 0, 0 ); + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount - maxIndices, maxVertex, maxIndices ); + } else { + Graphics.DrawIndexedVbBatch( mode, part.VbId, part.IbId, part.IndicesCount, 0, 0 ); } } }