diff --git a/GraphicsAPI/OpenGLApi.cs b/GraphicsAPI/OpenGLApi.cs index 386fdb4cb..a268c648b 100644 --- a/GraphicsAPI/OpenGLApi.cs +++ b/GraphicsAPI/OpenGLApi.cs @@ -288,17 +288,12 @@ namespace ClassicalSharp.GraphicsAPI { drawBatchFuncs[3] = (mode, id, count) => DrawVbPos3fTex2fCol4bFast( mode, id, count ); } - int GenBufferId() { - int[] ids = new int[1]; - GL.Arb.GenBuffers( 1, ids ); - return ids[0]; - } - public override int InitVb( T[] vertices, DrawMode mode, VertexFormat format, int count ) { if( !useVbos ) { return CreateDisplayList( vertices, mode, format, count ); } - int id = GenBufferId(); + int id = 0; + GL.Arb.GenBuffers( 1, out id ); int sizeInBytes = GetSizeInBytes( count, format ); GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, id ); GL.Arb.BufferData( BufferTargetArb.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageArb.StaticDraw ); @@ -364,6 +359,7 @@ namespace ClassicalSharp.GraphicsAPI { } public override void DeleteVb( int id ) { + if( id <= 0 ) return; #if TRACK_RESOURCES vbs.Remove( id ); #endif @@ -371,7 +367,7 @@ namespace ClassicalSharp.GraphicsAPI { GL.DeleteLists( id, 1 ); return; } - GL.DeleteBuffers( 1, new [] { id } ); + GL.DeleteBuffers( 1, ref id ); } public override void DrawVbPos3f( DrawMode mode, int id, int verticesCount ) { diff --git a/Rendering/LegacyEnvRenderer.cs b/Rendering/LegacyEnvRenderer.cs index df6c93d1d..2d8ed6d62 100644 --- a/Rendering/LegacyEnvRenderer.cs +++ b/Rendering/LegacyEnvRenderer.cs @@ -104,58 +104,23 @@ namespace ClassicalSharp.Renderers { } void ResetClouds() { - if( cloudsVbo != -1 ) { - Graphics.DeleteVb( cloudsVbo ); - } + Graphics.DeleteVb( cloudsVbo ); if( Map.IsNotLoaded ) return; - // Calculate the number of possible visible 'cloud grid cells'. - int dist = Window.ViewDistance; - const int cloudSize = 128; - int extent = cloudSize * ( dist / cloudSize + ( dist % cloudSize != 0 ? 1 : 0 ) ); // ceiling division - // Calculate how many 'cloud grid cells' are actually visible. - int cellsCount = 0; - for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { - for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { - if( x + cloudSize < -dist || z + cloudSize < -dist ) continue; - if( x > Map.Width + dist || z > Map.Length + dist ) continue; - cellsCount++; - } - } - cloudsVertices = cellsCount * 6; - VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices]; - int index = 0; + int extent = Window.ViewDistance; + int x1 = 0 - extent, x2 = Map.Width + extent; int y = Map.Height + 2; + int z1 = 0 - extent, z2 = Map.Length + extent; FastColour cloudsCol = Map.CloudsCol; - for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { - for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { - int x2 = x + cloudSize; - int z2 = z + cloudSize; - if( x2 < -dist || z2 < -dist ) continue; - if( x > Map.Width + dist || z > Map.Length + dist ) continue; - // Clip clouds to visible view distance (avoid overdrawing) - x2 = Math.Min( x2, Map.Width + dist ); - z2 = Math.Min( z2, Map.Length + dist ); - int x1 = Math.Max( x, -dist ); - int z1 = Math.Max( z, -dist ); - - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z1, x2 / 2048f, z1 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); - - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z2, x1 / 2048f, z2 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); - } - } + cloudsVertices = CountVertices( x2 - x1, z2 - z1 ); + VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices]; + DrawCloudsYPlane( x1, z1, x2, z2, y, cloudsCol, vertices ); cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b ); } void ResetSky() { - if( skyVbo != -1 ) { - Graphics.DeleteVb( skyVbo ); - } + Graphics.DeleteVb( skyVbo ); if( Map.IsNotLoaded ) return; int extent = Window.ViewDistance; @@ -166,7 +131,7 @@ namespace ClassicalSharp.Renderers { skyVertices = CountVertices( x2 - x1, z2 - z1 ); VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices]; - DrawYPlane( x1, z1, x2, z2, y, skyCol, vertices ); + DrawSkyYPlane( x1, z1, x2, z2, y, skyCol, vertices ); skyVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fCol4b ); } @@ -176,7 +141,7 @@ namespace ClassicalSharp.Renderers { return cellsAxis1 * cellsAxis2 * 6; } - void DrawYPlane( int x1, int z1, int x2, int z2, int y, FastColour col, VertexPos3fCol4b[] vertices ) { + void DrawSkyYPlane( int x1, int z1, int x2, int z2, int y, FastColour col, VertexPos3fCol4b[] vertices ) { int width = x2 - x1, endX = x2; int length = z2 - z1, endZ = z2, startZ = z1; int index = 0; @@ -200,6 +165,30 @@ namespace ClassicalSharp.Renderers { } } + void DrawCloudsYPlane( int x1, int z1, int x2, int z2, int y, FastColour col, VertexPos3fTex2fCol4b[] vertices ) { + int width = x2 - x1, endX = x2; + int length = z2 - z1, endZ = z2, startZ = z1; + int index = 0; + + for( ; x1 < endX; x1 += 128 ) { + x2 = x1 + 128; + if( x2 > endX ) x2 = endX; + z1 = startZ; + for( ; z1 < endZ; z1 += 128 ) { + z2 = z1 + 128; + if( z2 > endZ ) z2 = endZ; + + vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, col ); + vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z2, x1 / 2048f, z2 / 2048f, col ); + vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, col ); + + vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, col ); + vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z1, x2 / 2048f, z1 / 2048f, col ); + vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, col ); + } + } + } + double blendFactor( int x ) { //return -0.05 + 0.22 * Math.Log( Math.Pow( x, 0.25 ) ); double blend = -0.13 + 0.28 * Math.Log( Math.Pow( x, 0.25 ) ); diff --git a/Rendering/LegacyMapEnvRenderer.cs b/Rendering/LegacyMapEnvRenderer.cs index 73c8c9c2d..496f52445 100644 --- a/Rendering/LegacyMapEnvRenderer.cs +++ b/Rendering/LegacyMapEnvRenderer.cs @@ -71,9 +71,7 @@ namespace ClassicalSharp { int sidesVertices, edgeVertices = 0; static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgeCol = FastColour.White; void RebuildSides() { - if( sidesVboId != -1 ) { - Graphics.DeleteVb( sidesVboId ); - } + Graphics.DeleteVb( sidesVboId ); int groundLevel = Map.GroundHeight; sidesVertices = 0; @@ -98,9 +96,7 @@ namespace ClassicalSharp { } void RebuildEdges() { - if( edgesVboId != -1 ) { - Graphics.DeleteVb( edgesVboId ); - } + Graphics.DeleteVb( edgesVboId ); int waterLevel = Map.WaterHeight; edgeVertices = 0; diff --git a/Rendering/NormalEnvRenderer.cs b/Rendering/NormalEnvRenderer.cs index ecefa0278..0fbc6273d 100644 --- a/Rendering/NormalEnvRenderer.cs +++ b/Rendering/NormalEnvRenderer.cs @@ -12,7 +12,7 @@ namespace ClassicalSharp.Renderers { Map = Window.Map; } - int cloudTexture = -1, cloudsVbo = -1, cloudsVertices = 0; + int cloudTexture = -1, cloudsVbo = -1, cloudsVertices = 6; int skyOffset = 10, skyVbo = -1, skyVertices = 6; public float CloudsSpeed = 1f; @@ -103,58 +103,27 @@ namespace ClassicalSharp.Renderers { } void ResetClouds() { - if( cloudsVbo != -1 ) { - Graphics.DeleteVb( cloudsVbo ); - } + Graphics.DeleteVb( cloudsVbo ); if( Map.IsNotLoaded ) return; - // Calculate the number of possible visible 'cloud grid cells'. - int dist = Window.ViewDistance; - const int cloudSize = 512; - int extent = cloudSize * ( dist / cloudSize + ( dist % cloudSize != 0 ? 1 : 0 ) ); // ceiling division - - // Calculate how many 'cloud grid cells' are actually visible. - int cellsCount = 0; - for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { - for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { - if( x + cloudSize < -dist || z + cloudSize < -dist ) continue; - if( x > Map.Width + dist || z > Map.Length + dist ) continue; - cellsCount++; - } - } - cloudsVertices = cellsCount * 6; VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices]; - int index = 0; + int extent = Window.ViewDistance; + int x1 = 0 - extent, x2 = Map.Width + extent; int y = Map.Height + 2; + int z1 = 0 - extent, z2 = Map.Length + extent; FastColour cloudsCol = Map.CloudsCol; - for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { - for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { - int x2 = x + cloudSize; - int z2 = z + cloudSize; - if( x2 < -dist || z2 < -dist ) continue; - if( x > Map.Width + dist || z > Map.Length + dist ) continue; - // Clip clouds to visible view distance (avoid overdrawing) - x2 = Math.Min( x2, Map.Width + dist ); - z2 = Math.Min( z2, Map.Length + dist ); - int x1 = Math.Max( x, -dist ); - int z1 = Math.Max( z, -dist ); - - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z1, x2 / 2048f, z1 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); - - vertices[index++] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z2, x1 / 2048f, z2 / 2048f, cloudsCol ); - vertices[index++] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); - } - } + vertices[0] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); + vertices[1] = new VertexPos3fTex2fCol4b( x2, y, z1, x2 / 2048f, z1 / 2048f, cloudsCol ); + vertices[2] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); + + vertices[3] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol ); + vertices[4] = new VertexPos3fTex2fCol4b( x1, y, z2, x1 / 2048f, z2 / 2048f, cloudsCol ); + vertices[5] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol ); cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b ); } void ResetSky() { - if( skyVbo != -1 ) { - Graphics.DeleteVb( skyVbo ); - } + Graphics.DeleteVb( skyVbo ); if( Map.IsNotLoaded ) return; VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices]; int extent = Window.ViewDistance; diff --git a/Rendering/NormalMapEnvRenderer.cs b/Rendering/NormalMapEnvRenderer.cs index 5ee1e98e8..a6673bae4 100644 --- a/Rendering/NormalMapEnvRenderer.cs +++ b/Rendering/NormalMapEnvRenderer.cs @@ -69,9 +69,7 @@ namespace ClassicalSharp { static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgesCol = FastColour.White; void RebuildSides() { - if( sidesVboId != -1 ) { - Graphics.DeleteVb( sidesVboId ); - } + Graphics.DeleteVb( sidesVboId ); int groundLevel = Map.GroundHeight; VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[sidesVertices]; @@ -89,9 +87,7 @@ namespace ClassicalSharp { } void RebuildEdges() { - if( edgesVboId != -1 ) { - Graphics.DeleteVb( edgesVboId ); - } + Graphics.DeleteVb( edgesVboId ); int waterLevel = Map.WaterHeight; VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[edgesVertices];