diff --git a/2D/Screens/BlockSelectScreen.cs b/2D/Screens/BlockSelectScreen.cs index 5aea35f12..239bda138 100644 --- a/2D/Screens/BlockSelectScreen.cs +++ b/2D/Screens/BlockSelectScreen.cs @@ -135,9 +135,9 @@ namespace ClassicalSharp { blocksTable = new BlockDrawInfo[blocksCount]; int tableIndex = 0; - for( int i = 0; i < BlockInfo.BlocksCount; i++ ) { - if( Window.CanPlace[i] || Window.CanDelete[i] ) { - Block block = (Block)i; + for( int tile = 1; tile < BlockInfo.BlocksCount; tile++ ) { + if( Window.CanPlace[tile] || Window.CanDelete[tile] ) { + Block block = (Block)tile; int texId = Window.BlockInfo.GetOptimTextureLoc( (byte)block, TileSide.Left ); TextureRectangle rec = Window.TerrainAtlas.GetTexRec( texId ); int verSize = blockSize; diff --git a/Game/Game.cs b/Game/Game.cs index f8a6701c1..db2f39fab 100644 --- a/Game/Game.cs +++ b/Game/Game.cs @@ -177,7 +177,7 @@ namespace ClassicalSharp { Graphics.DepthTest = true; //Graphics.DepthWrite = true; Graphics.AlphaBlendFunc( BlendFunc.SourceAlpha, BlendFunc.InvSourceAlpha ); - Graphics.AlphaTestFunc( AlphaFunc.Greater, 0.5f ); + Graphics.AlphaTestFunc( CompareFunc.Greater, 0.5f ); RegisterInputHandlers(); Title = Utils.AppName; fpsScreen = new FpsScreen( this ); diff --git a/GraphicsAPI/IGraphicsApi.cs b/GraphicsAPI/IGraphicsApi.cs index 9a36fb274..c989f120e 100644 --- a/GraphicsAPI/IGraphicsApi.cs +++ b/GraphicsAPI/IGraphicsApi.cs @@ -58,7 +58,7 @@ namespace ClassicalSharp.GraphicsAPI { /// Sets the alpha test function that is used when alpha testing is enabled. - public abstract void AlphaTestFunc( AlphaFunc func, float value ); + public abstract void AlphaTestFunc( CompareFunc func, float value ); /// Whether alpha testing is currently enabled. public abstract bool AlphaTest { set; } @@ -80,7 +80,7 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void ColourMask( bool red, bool green, bool blue, bool alpha ); - public abstract void DepthTestFunc( DepthFunc func ); + public abstract void DepthTestFunc( CompareFunc func ); /// Whether depth testing is currently enabled. public abstract bool DepthTest { set; } @@ -124,6 +124,9 @@ namespace ClassicalSharp.GraphicsAPI { public abstract int InitVb( T[] vertices, DrawMode mode, VertexFormat format, int count ) where T : struct; + public abstract IndexedVbInfo InitIndexedVb( T[] vertices, ushort[] indices, DrawMode mode, + VertexFormat format, int verticesCount, int indicesCount ) where T : struct; + public abstract void DeleteVb( int id ); public abstract void DrawVbPos3f( DrawMode mode, int id, int verticesCount ); @@ -267,19 +270,7 @@ namespace ClassicalSharp.GraphicsAPI { ReverseSubtract = 4, } - public enum AlphaFunc { - Always = 0, - NotEqual = 1, - Never = 2, - - Less = 3, - LessEqual = 4, - Equal = 5, - GreaterEqual = 6, - Greater = 7, - } - - public enum DepthFunc { + public enum CompareFunc { Always = 0, NotEqual = 1, Never = 2, @@ -318,4 +309,13 @@ namespace ClassicalSharp.GraphicsAPI { Modelview = 1, Texture = 2, } + + public struct IndexedVbInfo { + public int Vb, Ib; + + public IndexedVbInfo( int vb, int ib ) { + Vb = vb; + Ib = ib; + } + } } \ No newline at end of file diff --git a/GraphicsAPI/OpenGLApi.cs b/GraphicsAPI/OpenGLApi.cs index d7194b30e..6c4bc35be 100644 --- a/GraphicsAPI/OpenGLApi.cs +++ b/GraphicsAPI/OpenGLApi.cs @@ -1,14 +1,14 @@ //#define TRACK_RESOURCES -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.Runtime.InteropServices; -using OpenTK; -using OpenTK.Graphics; -using OpenTK.Graphics.OpenGL; -using BmpPixelFormat = System.Drawing.Imaging.PixelFormat; -using GlPixelFormat = OpenTK.Graphics.OpenGL.PixelFormat; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.Runtime.InteropServices; +using OpenTK; +using OpenTK.Graphics; +using OpenTK.Graphics.OpenGL; +using BmpPixelFormat = System.Drawing.Imaging.PixelFormat; +using GlPixelFormat = OpenTK.Graphics.OpenGL.PixelFormat; namespace ClassicalSharp.GraphicsAPI { @@ -58,7 +58,7 @@ namespace ClassicalSharp.GraphicsAPI { AlphaFunction.Lequal, AlphaFunction.Equal, AlphaFunction.Gequal, AlphaFunction.Greater, }; - public override void AlphaTestFunc( AlphaFunc func, float value ) { + public override void AlphaTestFunc( CompareFunc func, float value ) { GL.AlphaFunc( alphaFuncs[(int)func], value ); } @@ -184,7 +184,7 @@ namespace ClassicalSharp.GraphicsAPI { DepthFunction.Lequal, DepthFunction.Equal, DepthFunction.Gequal, DepthFunction.Greater, }; - public override void DepthTestFunc( DepthFunc func ) { + public override void DepthTestFunc( CompareFunc func ) { GL.DepthFunc( depthFuncs[(int)func] ); } @@ -297,13 +297,56 @@ namespace ClassicalSharp.GraphicsAPI { return id; } - unsafe int CreateDisplayList( T[] vertices, DrawMode mode, VertexFormat format, int count ) where T : struct { + public unsafe override IndexedVbInfo InitIndexedVb( T[] vertices, ushort[] indices, DrawMode mode, + VertexFormat format, int verticesCount, int indicesCount ) { + if( !useVbos ) { + // TODO: Indexed display lists + return default( IndexedVbInfo ); //return CreateDisplayList( vertices, mode, format, count ); + } + int* ids = stackalloc int[2]; + GL.Arb.GenBuffers( 2, ids ); + int sizeInBytes = GetSizeInBytes( verticesCount, format ); + GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, ids[0] ); + GL.Arb.BufferData( BufferTargetArb.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageArb.StaticDraw ); + GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, 0 ); + + sizeInBytes = indicesCount * sizeof( ushort ); + GL.Arb.BindBuffer( BufferTargetArb.ElementArrayBuffer, ids[1] ); + GL.Arb.BufferData( BufferTargetArb.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageArb.StaticDraw ); + GL.Arb.BindBuffer( BufferTargetArb.ElementArrayBuffer, 0 ); + return new IndexedVbInfo( ids[0], ids[1] ); + } + + IndexedVbInfo CreateIndexedDisplayList( T[] vertices, ushort[] indices, DrawMode mode, + VertexFormat format, int verticesCount, int indicesCount ) where T : struct { + GCHandle handle; + int id = SetupDisplayListState( vertices, format, out handle ); + GL.DrawElements( modeMappings[(int)mode], indicesCount, DrawElementsType.UnsignedShort, 0 ); + RestoreDisplayListState( format, ref handle ); + #if TRACK_RESOURCES + vbs.Add( id, Environment.StackTrace ); + #endif + return new IndexedVbInfo( id, id ); + } + + int CreateDisplayList( T[] vertices, DrawMode mode, VertexFormat format, int count ) where T : struct { + GCHandle handle; + int id = SetupDisplayListState( vertices, format, out handle ); + GL.DrawArrays( modeMappings[(int)mode], 0, count ); + RestoreDisplayListState( format, ref handle ); + #if TRACK_RESOURCES + vbs.Add( id, Environment.StackTrace ); + #endif + return id; + } + + unsafe static int SetupDisplayListState( T[] vertices, VertexFormat format, out GCHandle handle ) { int id = GL.GenLists( 1 ); int stride = strideSizes[(int)format]; GL.NewList( id, ListMode.Compile ); GL.EnableClientState( ArrayCap.VertexArray ); - GCHandle handle = GCHandle.Alloc( vertices, GCHandleType.Pinned ); + handle = GCHandle.Alloc( vertices, GCHandleType.Pinned ); IntPtr p = handle.AddrOfPinnedObject(); GL.VertexPointer( 3, VertexPointerType.Float, stride, (IntPtr)( 0 + (byte*)p ) ); @@ -319,7 +362,10 @@ namespace ClassicalSharp.GraphicsAPI { GL.TexCoordPointer( 2, TexCoordPointerType.Float, stride, (IntPtr)( 12 + (byte*)p ) ); GL.ColorPointer( 4, ColorPointerType.UnsignedByte, stride, (IntPtr)( 20 + (byte*)p ) ); } - GL.DrawArrays( modeMappings[(int)mode], 0, count ); + return id; + } + + static void RestoreDisplayListState( VertexFormat format, ref GCHandle handle ) { handle.Free(); GL.DisableClientState( ArrayCap.VertexArray ); @@ -333,10 +379,6 @@ namespace ClassicalSharp.GraphicsAPI { } GL.Color3( 1f, 1f, 1f ); // NOTE: not sure why, but display lists seem to otherwise modify global colour.. GL.EndList(); - #if TRACK_RESOURCES - vbs.Add( id, Environment.StackTrace ); - #endif - return id; } public override void DeleteVb( int id ) { diff --git a/Rendering/MapRenderer.cs b/Rendering/MapRenderer.cs index 0be7cbc5c..66b23dccf 100644 --- a/Rendering/MapRenderer.cs +++ b/Rendering/MapRenderer.cs @@ -237,7 +237,7 @@ namespace ClassicalSharp { Graphics.AlphaBlending = false; // First fill depth buffer - Graphics.DepthTestFunc( DepthFunc.LessEqual ); + Graphics.DepthTestFunc( CompareFunc.LessEqual ); Graphics.ColourMask( false, false, false, false ); for( int batch = 0; batch < _1Dcount; batch++ ) { RenderTranslucentBatchNoAdd( batch ); @@ -250,7 +250,7 @@ namespace ClassicalSharp { Graphics.Bind2DTexture( Window.TerrainAtlas1DTexIds[batch] ); RenderTranslucentBatch( batch ); } - Graphics.DepthTestFunc( DepthFunc.Less ); + Graphics.DepthTestFunc( CompareFunc.Less ); Graphics.AlphaTest = false; Graphics.AlphaBlending = false;