diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index e9dbc3384..63ee81d3f 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -18,7 +18,7 @@ namespace ClassicalSharp.GraphicsAPI { Device device; Direct3D d3d; Capabilities caps; - const int texBufferSize = 512, iBufferSize = 1024, vBufferSize = 2048; + const int texBufferSize = 512, iBufferSize = 256, vBufferSize = 2048; D3D.Texture[] textures = new D3D.Texture[texBufferSize]; VertexBuffer[] vBuffers = new VertexBuffer[vBufferSize]; @@ -288,8 +288,7 @@ namespace ClassicalSharp.GraphicsAPI { batchStride = strideSizes[(int)format]; } - public override void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount ) { - device.SetStreamSource( 0, vBuffers[id], 0, batchStride ); + public override void DrawVb( DrawMode mode, int startVertex, int verticesCount ) { device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) ); } @@ -301,7 +300,12 @@ namespace ClassicalSharp.GraphicsAPI { device.SetIndices( iBuffers[ib] ); } - public override void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ) { + public override void DrawIndexedVb( DrawMode mode, int indicesCount, int startIndex ) { + device.DrawIndexedPrimitives( modeMappings[(int)mode], 0, 0, + indicesCount / 6 * 4, startIndex, NumPrimitives( indicesCount, mode ) ); + } + + public override void DrawIndexedVb_T2fC4b( DrawMode mode, int indicesCount, int startVertex, int startIndex ) { device.DrawIndexedPrimitives( modeMappings[(int)mode], startVertex, startVertex, indicesCount / 6 * 4, startIndex, NumPrimitives( indicesCount, mode ) ); } diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index f03657ce2..947281c68 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -124,12 +124,12 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void BeginVbBatch( VertexFormat format ); - public abstract void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount ); + public abstract void DrawVb( DrawMode mode, int startVertex, int verticesCount ); - public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ); + public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startIndex ); /// Optimised version of DrawIndexedVb for VertexFormat.Pos3fTex2fCol4b - //public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ); + public abstract void DrawIndexedVb_T2fC4b( DrawMode mode, int indicesCount, int offsetVertex, int startIndex ); protected static int[] strideSizes = { 20, 16, 24 }; diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index 80320a550..81f4ce8df 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -266,8 +266,7 @@ namespace ClassicalSharp.GraphicsAPI { } } - public override void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount ) { - GL.BindBufferARB( BufferTarget.ArrayBuffer, id ); + public override void DrawVb( DrawMode mode, int startVertex, int verticesCount ) { setupBatchFunc(); GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount ); } @@ -280,8 +279,13 @@ namespace ClassicalSharp.GraphicsAPI { GL.BindBufferARB( BufferTarget.ElementArrayBuffer, ib ); } - const DrawElementsType indexType = DrawElementsType.UnsignedShort; - public override void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ) { + const DrawElementsType indexType = DrawElementsType.UnsignedShort; + public override void DrawIndexedVb( DrawMode mode, int indicesCount, int startIndex ) { + setupBatchFunc(); + GL.DrawElements( modeMappings[(int)mode], indicesCount, indexType, new IntPtr( startIndex * 2 ) ); + } + + public override void DrawIndexedVb_T2fC4b( DrawMode mode, int indicesCount, int startVertex, int startIndex ) { int offset = startVertex * VertexPos3fTex2fCol4b.Size; GL.VertexPointer( 3, PointerType.Float, 24, new IntPtr( offset ) ); GL.ColorPointer( 4, PointerType.UnsignedByte, 24, new IntPtr( offset + 12 ) ); diff --git a/ClassicalSharp/Model/ChickenModel.cs b/ClassicalSharp/Model/ChickenModel.cs index 343e03472..a63e897a1 100644 --- a/ClassicalSharp/Model/ChickenModel.cs +++ b/ClassicalSharp/Model/ChickenModel.cs @@ -47,7 +47,7 @@ namespace ClassicalSharp.Model { const float y1 = 0f, y2 = 0.3125f, z2 = 0.0625f, z1 = -0.125f; YPlane( 32, 0, 3, 3, x2, x1, z1, z2, y1, false ); // bottom feet ZPlane( 36, 3, 1, 5, legX1, legX2, y1, y2, z2, false ); // vertical part of leg - return new ModelPart( index - 12, 2 * 6, graphics ); + return new ModelPart( index - 12, 2 * 6 ); } public override float NameYOffset { @@ -71,7 +71,7 @@ namespace ClassicalSharp.Model { DrawRotate( 0, 0.5625f, -0.1875f, -p.PitchRadians, 0, 0, Head ); DrawRotate( 0, 0.5625f, -0.1875f, -p.PitchRadians, 0, 0, Head2 ); DrawRotate( 0, 0.5625f, -0.1875f, -p.PitchRadians, 0, 0, Head3 ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.3125f, 0.0625f, p.leftLegXRot, 0, 0, LeftLeg ); DrawRotate( 0, 0.3125f, 0.0625f, p.rightLegXRot, 0, 0, RightLeg ); DrawRotate( -0.1875f, 0.6875f, 0, 0, 0, -Math.Abs( p.leftArmXRot ), LeftWing ); diff --git a/ClassicalSharp/Model/CreeperModel.cs b/ClassicalSharp/Model/CreeperModel.cs index a826fdccd..58a8a698e 100644 --- a/ClassicalSharp/Model/CreeperModel.cs +++ b/ClassicalSharp/Model/CreeperModel.cs @@ -51,7 +51,7 @@ namespace ClassicalSharp.Model { graphics.BindTexture( texId ); DrawRotate( 0, 1.125f, 0, -p.PitchRadians, 0, 0, Head ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.375f, -0.125f, p.leftLegXRot, 0, 0, LeftLegFront ); DrawRotate( 0, 0.375f, -0.125f, p.rightLegXRot, 0, 0, RightLegFront ); DrawRotate( 0, 0.375f, 0.125f, p.rightLegXRot, 0, 0, LeftLegBack ); diff --git a/ClassicalSharp/Model/IModel.cs b/ClassicalSharp/Model/IModel.cs index 58cf48139..021fdbc72 100644 --- a/ClassicalSharp/Model/IModel.cs +++ b/ClassicalSharp/Model/IModel.cs @@ -28,6 +28,7 @@ namespace ClassicalSharp.Model { Matrix4 mat = Matrix4.RotateY( -p.YawRadians ) * Matrix4.Translate( p.Position ); graphics.MultiplyMatrix( ref mat ); graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); + graphics.BindVb( vb ); DrawPlayerModel( p ); graphics.PopMatrix(); } @@ -51,7 +52,7 @@ namespace ClassicalSharp.Model { ZPlane( x + sidesW + bodyW + sidesW, y + endsH, bodyW, bodyH, x1, x2, y1, y2, z2, _64x64 ); // back XPlane( x, y + endsH, sidesW, sidesH, z2, z1, y1, y2, x2, _64x64 ); // left XPlane( x + sidesW + bodyW, y + endsH, sidesW, sidesH, z1, z2, y1, y2, x1, _64x64 ); // right - return new ModelPart( index - 36, 6 * 6, graphics ); + return new ModelPart( index - 36, 6 * 6 ); } protected ModelPart MakeRotatedPart( int x, int y, int sidesW, int sidesH, int endsW, int endsH, int bodyW, int bodyH, @@ -70,7 +71,7 @@ namespace ClassicalSharp.Model { vertex.Y = z; vertices[i] = vertex; } - return new ModelPart( index - 36, 6 * 6, graphics ); + return new ModelPart( index - 36, 6 * 6 ); } protected static TextureRectangle SkinTexCoords( int x, int y, int width, int height, float skinWidth, float skinHeight ) { @@ -127,7 +128,7 @@ namespace ClassicalSharp.Model { } mat = Matrix4.Translate( -x, -y, -z ) * mat; graphics.MultiplyMatrix( ref mat ); - part.Render( vb ); + part.Render( graphics ); graphics.PopMatrix(); } } diff --git a/ClassicalSharp/Model/ModelPart.cs b/ClassicalSharp/Model/ModelPart.cs index aa32ce6b0..8b03a77d9 100644 --- a/ClassicalSharp/Model/ModelPart.cs +++ b/ClassicalSharp/Model/ModelPart.cs @@ -7,16 +7,14 @@ namespace ClassicalSharp { public int Offset = 0; public int Count; - public IGraphicsApi Graphics; - public ModelPart( int offset, int count, IGraphicsApi graphics ) { + public ModelPart( int offset, int count ) { Offset = offset; Count = count; - Graphics = graphics; } - public void Render( int vb ) { - Graphics.DrawVb( DrawMode.Triangles, vb, Offset, Count ); + public void Render( IGraphicsApi api ) { + api.DrawVb( DrawMode.Triangles, Offset, Count ); } } diff --git a/ClassicalSharp/Model/PigModel.cs b/ClassicalSharp/Model/PigModel.cs index c264db5ee..66128deed 100644 --- a/ClassicalSharp/Model/PigModel.cs +++ b/ClassicalSharp/Model/PigModel.cs @@ -51,7 +51,7 @@ namespace ClassicalSharp.Model { graphics.BindTexture( texId ); DrawRotate( 0, 0.75f, -0.375f, -p.PitchRadians, 0, 0, Head ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.375f, -0.3125f, p.leftLegXRot, 0, 0, LeftLegFront ); DrawRotate( 0, 0.375f, -0.3125f, p.rightLegXRot, 0, 0, RightLegFront ); DrawRotate( 0, 0.375f, 0.4375f, p.rightLegXRot, 0, 0, LeftLegBack ); diff --git a/ClassicalSharp/Model/PlayerModel.cs b/ClassicalSharp/Model/PlayerModel.cs index 73de252b9..43e288ddb 100644 --- a/ClassicalSharp/Model/PlayerModel.cs +++ b/ClassicalSharp/Model/PlayerModel.cs @@ -98,7 +98,7 @@ namespace ClassicalSharp.Model { else if( skinType == SkinType.Type64x64Slim ) model = Set64x64Slim; DrawRotate( 0, 1.5f, 0, -p.PitchRadians, 0, 0, model.Head ); - model.Torso.Render( vb ); + model.Torso.Render( graphics ); DrawRotate( 0, 0.75f, 0, p.leftLegXRot, 0, 0, model.LeftLeg ); DrawRotate( 0, 0.75f, 0, p.rightLegXRot, 0, 0, model.RightLeg ); DrawRotate( 0, 1.5f, 0, p.leftArmXRot, 0, p.leftArmZRot, model.LeftArm ); diff --git a/ClassicalSharp/Model/SheepModel.cs b/ClassicalSharp/Model/SheepModel.cs index 65a804a7c..a457b3580 100644 --- a/ClassicalSharp/Model/SheepModel.cs +++ b/ClassicalSharp/Model/SheepModel.cs @@ -75,7 +75,7 @@ namespace ClassicalSharp.Model { graphics.BindTexture( texId ); DrawRotate( 0, 1.125f, -0.5f, -p.PitchRadians, 0, 0, Head ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.75f, -0.3125f, p.leftLegXRot, 0, 0, LeftLegFront ); DrawRotate( 0, 0.75f, -0.3125f, p.rightLegXRot, 0, 0, RightLegFront ); DrawRotate( 0, 0.75f, 0.4375f, p.rightLegXRot, 0, 0, LeftLegBack ); @@ -83,7 +83,7 @@ namespace ClassicalSharp.Model { graphics.AlphaTest = true; if( Fur ) { graphics.BindTexture( furTextureId ); - FurTorso.Render( vb ); + FurTorso.Render( graphics ); DrawRotate( 0, 1.125f, -0.5f, -p.PitchRadians, 0, 0, FurHead ); DrawRotate( 0, 0.75f, -0.3125f, p.leftLegXRot, 0, 0, FurLeftLegFront ); DrawRotate( 0, 0.75f, -0.3125f, p.rightLegXRot, 0, 0, FurRightLegFront ); diff --git a/ClassicalSharp/Model/SkeletonModel.cs b/ClassicalSharp/Model/SkeletonModel.cs index 3c982aa89..3b532dd1e 100644 --- a/ClassicalSharp/Model/SkeletonModel.cs +++ b/ClassicalSharp/Model/SkeletonModel.cs @@ -64,7 +64,7 @@ namespace ClassicalSharp.Model { graphics.BindTexture( texId ); DrawRotate( 0, 1.5f, 0, -p.PitchRadians, 0, 0, Head ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.75f, 0, p.leftLegXRot, 0, 0, LeftLeg ); DrawRotate( 0, 0.75f, 0, p.rightLegXRot, 0, 0, RightLeg ); DrawRotate( 0, 1.375f, 0, (float)Math.PI / 2, 0, p.leftArmZRot, LeftArm ); diff --git a/ClassicalSharp/Model/SpiderModel.cs b/ClassicalSharp/Model/SpiderModel.cs index ce86a6bc6..f88d19c21 100644 --- a/ClassicalSharp/Model/SpiderModel.cs +++ b/ClassicalSharp/Model/SpiderModel.cs @@ -57,8 +57,8 @@ namespace ClassicalSharp.Model { graphics.AlphaTest = true; DrawRotate( 0, 0.5f, -0.1875f, -p.PitchRadians, 0, 0, Head ); - Link.Render( vb ); - End.Render( vb ); + Link.Render( graphics ); + End.Render( graphics ); // TODO: leg animations DrawRotate( -0.1875f, 0.5f, 0, 0, quarterPi, eighthPi, LeftLeg ); DrawRotate( -0.1875f, 0.5f, 0, 0, eighthPi, eighthPi, LeftLeg ); diff --git a/ClassicalSharp/Model/ZombieModel.cs b/ClassicalSharp/Model/ZombieModel.cs index 698656a1b..bf2540832 100644 --- a/ClassicalSharp/Model/ZombieModel.cs +++ b/ClassicalSharp/Model/ZombieModel.cs @@ -63,7 +63,7 @@ namespace ClassicalSharp.Model { graphics.BindTexture( texId ); DrawRotate( 0, 1.5f, 0, -p.PitchRadians, 0, 0, Head ); - Torso.Render( vb ); + Torso.Render( graphics ); DrawRotate( 0, 0.75f, 0, p.leftLegXRot, 0, 0, LeftLeg ); DrawRotate( 0, 0.75f, 0, p.rightLegXRot, 0, 0, RightLeg ); DrawRotate( 0, 1.375f, 0, (float)Math.PI / 2, 0, p.leftArmZRot, LeftArm ); diff --git a/ClassicalSharp/Rendering/MapEnvRenderer.cs b/ClassicalSharp/Rendering/MapEnvRenderer.cs index b4a195a42..385c029b1 100644 --- a/ClassicalSharp/Rendering/MapEnvRenderer.cs +++ b/ClassicalSharp/Rendering/MapEnvRenderer.cs @@ -46,7 +46,7 @@ namespace ClassicalSharp { Graphics.BindTexture( sideTexId ); Graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); Graphics.BindVb( sidesVb ); - Graphics.DrawIndexedVb( DrawMode.Triangles, sidesIndices, 0, 0 ); + Graphics.DrawIndexedVb_T2fC4b( DrawMode.Triangles, sidesIndices, 0, 0 ); // Do not draw water when we cannot see it. // Fixes 'depth bleeding through' issues with 16 bit depth buffers on large maps. @@ -54,7 +54,7 @@ namespace ClassicalSharp { Graphics.AlphaBlending = true; Graphics.BindTexture( edgeTexId ); Graphics.BindVb( edgesVb ); - Graphics.DrawIndexedVb( DrawMode.Triangles, edgesIndices, 0, 0 ); + Graphics.DrawIndexedVb_T2fC4b( DrawMode.Triangles, edgesIndices, 0, 0 ); Graphics.AlphaBlending = false; } Graphics.Texturing = false; diff --git a/ClassicalSharp/Rendering/MapRenderer.Rendering.cs b/ClassicalSharp/Rendering/MapRenderer.Rendering.cs index 24168edab..81ab727b9 100644 --- a/ClassicalSharp/Rendering/MapRenderer.Rendering.cs +++ b/ClassicalSharp/Rendering/MapRenderer.Rendering.cs @@ -21,7 +21,7 @@ namespace ClassicalSharp { DrawPart( info, ref part ); if( part.spriteCount > 0 ) - api.DrawIndexedVb( mode, part.spriteCount, 0, 0 ); + api.DrawIndexedVb_T2fC4b( mode, part.spriteCount, 0, 0 ); game.Vertices += part.IndicesCount; } } @@ -60,51 +60,51 @@ namespace ClassicalSharp { if( drawLeft && drawRight ) { api.FaceCulling = true; - api.DrawIndexedVb( mode, part.leftCount + part.rightCount, 0, part.leftIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.leftCount + part.rightCount, 0, part.leftIndex ); api.FaceCulling = false; } else if( drawLeft ) { - api.DrawIndexedVb( mode, part.leftCount, 0, part.leftIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.leftCount, 0, part.leftIndex ); } else if( drawRight ) { - api.DrawIndexedVb( mode, part.rightCount, 0, part.rightIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.rightCount, 0, part.rightIndex ); } if( drawFront && drawBack ) { api.FaceCulling = true; - api.DrawIndexedVb( mode, part.frontCount + part.backCount, 0, part.frontIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.frontCount + part.backCount, 0, part.frontIndex ); api.FaceCulling = false; } else if( drawFront ) { - api.DrawIndexedVb( mode, part.frontCount, 0, part.frontIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.frontCount, 0, part.frontIndex ); } else if( drawBack ) { - api.DrawIndexedVb( mode, part.backCount, 0, part.backIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.backCount, 0, part.backIndex ); } if( drawBottom && drawTop ) { api.FaceCulling = true; if( part.IndicesCount > maxIndices ) { int part1Count = maxIndices - part.bottomIndex; - api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex ); - api.DrawIndexedVb( mode, part.bottomCount + part.topCount - part1Count, maxVertex, 0 ); + api.DrawIndexedVb_T2fC4b( mode, part1Count, 0, part.bottomIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.bottomCount + part.topCount - part1Count, maxVertex, 0 ); } else { - api.DrawIndexedVb( mode, part.bottomCount + part.topCount, 0, part.bottomIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.bottomCount + part.topCount, 0, part.bottomIndex ); } api.FaceCulling = false; } else if( drawBottom ) { int part1Count; if( part.IndicesCount > maxIndices && ( part1Count = maxIndices - part.bottomIndex ) < part.bottomCount ) { - api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex ); - api.DrawIndexedVb( mode, part.bottomCount - part1Count, maxVertex, 0 ); + api.DrawIndexedVb_T2fC4b( mode, part1Count, 0, part.bottomIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.bottomCount - part1Count, maxVertex, 0 ); } else { - api.DrawIndexedVb( mode, part.bottomCount, 0, part.bottomIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.bottomCount, 0, part.bottomIndex ); } } else if( drawTop ) { int part1Count; if( part.IndicesCount > maxIndices && ( part1Count = maxIndices - part.topIndex ) < part.topCount ) { - api.DrawIndexedVb( mode, part1Count, 0, part.topIndex ); - api.DrawIndexedVb( mode, part.topCount - part1Count, maxVertex, 0 ); + api.DrawIndexedVb_T2fC4b( mode, part1Count, 0, part.topIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.topCount - part1Count, maxVertex, 0 ); } else { - api.DrawIndexedVb( mode, part.topCount, 0, part.topIndex ); + api.DrawIndexedVb_T2fC4b( mode, part.topCount, 0, part.topIndex ); } } diff --git a/ClassicalSharp/Rendering/StandardEnvRenderer.cs b/ClassicalSharp/Rendering/StandardEnvRenderer.cs index abb0f18aa..f5264a227 100644 --- a/ClassicalSharp/Rendering/StandardEnvRenderer.cs +++ b/ClassicalSharp/Rendering/StandardEnvRenderer.cs @@ -30,7 +30,7 @@ namespace ClassicalSharp.Renderers { if( pos.Y < Map.Height + skyOffset ) { Graphics.BeginVbBatch( VertexFormat.Pos3fCol4b ); Graphics.BindVb( skyVb ); - Graphics.DrawIndexedVb( DrawMode.Triangles, skyIndices, 0, 0 ); + Graphics.DrawIndexedVb( DrawMode.Triangles, skyIndices, 0 ); } RenderClouds( deltaTime ); ResetFog(); @@ -100,7 +100,7 @@ namespace ClassicalSharp.Renderers { Graphics.BindTexture( cloudTexture ); Graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); Graphics.BindVb( cloudsVb ); - Graphics.DrawIndexedVb( DrawMode.Triangles, cloudsIndices, 0, 0 ); + Graphics.DrawIndexedVb_T2fC4b( DrawMode.Triangles, cloudsIndices, 0, 0 ); Graphics.AlphaTest = false; Graphics.Texturing = false; diff --git a/ClassicalSharp/Selections/SelectionBox.cs b/ClassicalSharp/Selections/SelectionBox.cs index 0b5da8940..36c52f4c1 100644 --- a/ClassicalSharp/Selections/SelectionBox.cs +++ b/ClassicalSharp/Selections/SelectionBox.cs @@ -7,8 +7,8 @@ namespace ClassicalSharp.Selections { public class SelectionBox { public short ID; - public const int VerticesCount = 6 * 6, LineVerticesCount = 12 * 2; - public int Vb, LineVb; + const int VerticesCount = 6 * 4, LineVerticesCount = 12 * 2, IndicesCount = 6 * 6; + public int Vb; public IGraphicsApi Graphics; public Vector3I Min, Max; @@ -19,48 +19,46 @@ namespace ClassicalSharp.Selections { public void Render( double delta ) { Graphics.DepthWrite = false; - Graphics.DrawVb( DrawMode.Triangles, Vb, 0, VerticesCount ); + Graphics.BindVb( Vb ); + Graphics.DrawIndexedVb( DrawMode.Triangles, IndicesCount, 0 ); Graphics.DepthWrite = true; - Graphics.DrawVb( DrawMode.Lines, LineVb, 0, LineVerticesCount ); + Graphics.DrawVb( DrawMode.Lines, VerticesCount, LineVerticesCount ); } - static VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[VerticesCount]; + static VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[VerticesCount + LineVerticesCount]; public SelectionBox( Vector3I start, Vector3I end, FastColour col, IGraphicsApi graphics ) { - Graphics = graphics; - + Graphics = graphics; Min = Vector3I.Min( start, end ); - Max = Vector3I.Max( start, end ); - + Max = Vector3I.Max( start, end ); int index = 0; Vector3 p1 = (Vector3)Min + new Vector3( 0.0625f, 0.0625f, 0.0625f ); Vector3 p2 = (Vector3)Max - new Vector3( 0.0625f, 0.0625f, 0.0625f ); - FastColour lineCol = new FastColour( (byte)~col.R, (byte)~col.G, (byte)~col.B ); - // bottom face - Line( ref index, p1.X, p1.Y, p1.Z, p2.X, p1.Y, p1.Z, lineCol ); - Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p1.Y, p2.Z, lineCol ); - Line( ref index, p2.X, p1.Y, p2.Z, p1.X, p1.Y, p2.Z, lineCol ); - Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p1.Y, p1.Z, lineCol ); - // top face - Line( ref index, p1.X, p2.Y, p1.Z, p2.X, p2.Y, p1.Z, lineCol ); - Line( ref index, p2.X, p2.Y, p1.Z, p2.X, p2.Y, p2.Z, lineCol ); - Line( ref index, p2.X, p2.Y, p2.Z, p1.X, p2.Y, p2.Z, lineCol ); - Line( ref index, p1.X, p2.Y, p2.Z, p1.X, p2.Y, p1.Z, lineCol ); - // side faces - Line( ref index, p1.X, p1.Y, p1.Z, p1.X, p2.Y, p1.Z, lineCol ); - Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p2.Y, p1.Z, lineCol ); - Line( ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z, lineCol ); - Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z, lineCol ); - LineVb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, LineVerticesCount ); - - index = 0; RenderYPlane( ref index, p1.X, p1.Z, p2.X, p2.Z, p1.Y, col ); // bottom RenderYPlane( ref index, p1.X, p1.Z, p2.X, p2.Z, p2.Y, col ); // top RenderXPlane( ref index, p1.X, p2.X, p1.Y, p2.Y, p1.Z, col ); // sides RenderXPlane( ref index, p1.X, p2.X, p1.Y, p2.Y, p2.Z, col ); RenderZPlane( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p1.X, col ); RenderZPlane( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p2.X, col ); - Vb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, VerticesCount ); + + col = new FastColour( (byte)~col.R, (byte)~col.G, (byte)~col.B ); + // bottom face + Line( ref index, p1.X, p1.Y, p1.Z, p2.X, p1.Y, p1.Z, col ); + Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p1.Y, p2.Z, col ); + Line( ref index, p2.X, p1.Y, p2.Z, p1.X, p1.Y, p2.Z, col ); + Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p1.Y, p1.Z, col ); + // top face + Line( ref index, p1.X, p2.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); + Line( ref index, p2.X, p2.Y, p1.Z, p2.X, p2.Y, p2.Z, col ); + Line( ref index, p2.X, p2.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); + Line( ref index, p1.X, p2.Y, p2.Z, p1.X, p2.Y, p1.Z, col ); + // side faces + Line( ref index, p1.X, p1.Y, p1.Z, p1.X, p2.Y, p1.Z, col ); + Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); + Line( ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z, col ); + Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); + + Vb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, VerticesCount + LineVerticesCount ); } void Line( ref int index, float x1, float y1, float z1, float x2, float y2, float z2, FastColour col ) { @@ -71,35 +69,25 @@ namespace ClassicalSharp.Selections { void RenderZPlane( ref int index, float z1, float z2, float y1, float y2, float x, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x, y1, z1, col ); vertices[index++] = new VertexPos3fCol4b( x, y2, z1, col ); - vertices[index++] = new VertexPos3fCol4b( x, y2, z2, col ); - vertices[index++] = new VertexPos3fCol4b( x, y2, z2, col ); vertices[index++] = new VertexPos3fCol4b( x, y1, z2, col ); - vertices[index++] = new VertexPos3fCol4b( x, y1, z1, col ); } void RenderXPlane( ref int index, float x1, float x2, float y1, float y2, float z, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x1, y1, z, col ); vertices[index++] = new VertexPos3fCol4b( x1, y2, z, col ); - vertices[index++] = new VertexPos3fCol4b( x2, y2, z, col ); - vertices[index++] = new VertexPos3fCol4b( x2, y2, z, col ); vertices[index++] = new VertexPos3fCol4b( x2, y1, z, col ); - vertices[index++] = new VertexPos3fCol4b( x1, y1, z, col ); } void RenderYPlane( ref int index, float x1, float z1, float x2, float z2, float y, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x1, y, z1, col ); vertices[index++] = new VertexPos3fCol4b( x1, y, z2, col ); - vertices[index++] = new VertexPos3fCol4b( x2, y, z2, col ); - vertices[index++] = new VertexPos3fCol4b( x2, y, z2, col ); vertices[index++] = new VertexPos3fCol4b( x2, y, z1, col ); - vertices[index++] = new VertexPos3fCol4b( x1, y, z1, col ); } public void Dispose() { - Graphics.DeleteVb( LineVb ); Graphics.DeleteVb( Vb ); } }