Simplify cloud rendering, some other minor simplifications.

This commit is contained in:
UnknownShadow200 2015-01-01 17:04:55 +11:00
parent d0f6fb0360
commit 55aade0f39
5 changed files with 55 additions and 109 deletions

View File

@ -288,17 +288,12 @@ namespace ClassicalSharp.GraphicsAPI {
drawBatchFuncs[3] = (mode, id, count) => DrawVbPos3fTex2fCol4bFast( mode, id, count ); 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>( T[] vertices, DrawMode mode, VertexFormat format, int count ) { public override int InitVb<T>( T[] vertices, DrawMode mode, VertexFormat format, int count ) {
if( !useVbos ) { if( !useVbos ) {
return CreateDisplayList( vertices, mode, format, count ); return CreateDisplayList( vertices, mode, format, count );
} }
int id = GenBufferId(); int id = 0;
GL.Arb.GenBuffers( 1, out id );
int sizeInBytes = GetSizeInBytes( count, format ); int sizeInBytes = GetSizeInBytes( count, format );
GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, id ); GL.Arb.BindBuffer( BufferTargetArb.ArrayBuffer, id );
GL.Arb.BufferData( BufferTargetArb.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageArb.StaticDraw ); GL.Arb.BufferData( BufferTargetArb.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageArb.StaticDraw );
@ -364,6 +359,7 @@ namespace ClassicalSharp.GraphicsAPI {
} }
public override void DeleteVb( int id ) { public override void DeleteVb( int id ) {
if( id <= 0 ) return;
#if TRACK_RESOURCES #if TRACK_RESOURCES
vbs.Remove( id ); vbs.Remove( id );
#endif #endif
@ -371,7 +367,7 @@ namespace ClassicalSharp.GraphicsAPI {
GL.DeleteLists( id, 1 ); GL.DeleteLists( id, 1 );
return; return;
} }
GL.DeleteBuffers( 1, new [] { id } ); GL.DeleteBuffers( 1, ref id );
} }
public override void DrawVbPos3f( DrawMode mode, int id, int verticesCount ) { public override void DrawVbPos3f( DrawMode mode, int id, int verticesCount ) {

View File

@ -104,58 +104,23 @@ namespace ClassicalSharp.Renderers {
} }
void ResetClouds() { void ResetClouds() {
if( cloudsVbo != -1 ) { Graphics.DeleteVb( cloudsVbo );
Graphics.DeleteVb( cloudsVbo );
}
if( Map.IsNotLoaded ) return; 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 extent = Window.ViewDistance;
int cellsCount = 0; int x1 = 0 - extent, x2 = Map.Width + extent;
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 y = Map.Height + 2; int y = Map.Height + 2;
int z1 = 0 - extent, z2 = Map.Length + extent;
FastColour cloudsCol = Map.CloudsCol; FastColour cloudsCol = Map.CloudsCol;
for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { cloudsVertices = CountVertices( x2 - x1, z2 - z1 );
for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices];
int x2 = x + cloudSize; DrawCloudsYPlane( x1, z1, x2, z2, y, cloudsCol, vertices );
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 );
}
}
cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b ); cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b );
} }
void ResetSky() { void ResetSky() {
if( skyVbo != -1 ) { Graphics.DeleteVb( skyVbo );
Graphics.DeleteVb( skyVbo );
}
if( Map.IsNotLoaded ) return; if( Map.IsNotLoaded ) return;
int extent = Window.ViewDistance; int extent = Window.ViewDistance;
@ -166,7 +131,7 @@ namespace ClassicalSharp.Renderers {
skyVertices = CountVertices( x2 - x1, z2 - z1 ); skyVertices = CountVertices( x2 - x1, z2 - z1 );
VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices]; 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 ); skyVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fCol4b );
} }
@ -176,7 +141,7 @@ namespace ClassicalSharp.Renderers {
return cellsAxis1 * cellsAxis2 * 6; 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 width = x2 - x1, endX = x2;
int length = z2 - z1, endZ = z2, startZ = z1; int length = z2 - z1, endZ = z2, startZ = z1;
int index = 0; 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 ) { double blendFactor( int x ) {
//return -0.05 + 0.22 * Math.Log( Math.Pow( x, 0.25 ) ); //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 ) ); double blend = -0.13 + 0.28 * Math.Log( Math.Pow( x, 0.25 ) );

View File

@ -71,9 +71,7 @@ namespace ClassicalSharp {
int sidesVertices, edgeVertices = 0; int sidesVertices, edgeVertices = 0;
static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgeCol = FastColour.White; static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgeCol = FastColour.White;
void RebuildSides() { void RebuildSides() {
if( sidesVboId != -1 ) { Graphics.DeleteVb( sidesVboId );
Graphics.DeleteVb( sidesVboId );
}
int groundLevel = Map.GroundHeight; int groundLevel = Map.GroundHeight;
sidesVertices = 0; sidesVertices = 0;
@ -98,9 +96,7 @@ namespace ClassicalSharp {
} }
void RebuildEdges() { void RebuildEdges() {
if( edgesVboId != -1 ) { Graphics.DeleteVb( edgesVboId );
Graphics.DeleteVb( edgesVboId );
}
int waterLevel = Map.WaterHeight; int waterLevel = Map.WaterHeight;
edgeVertices = 0; edgeVertices = 0;

View File

@ -12,7 +12,7 @@ namespace ClassicalSharp.Renderers {
Map = Window.Map; Map = Window.Map;
} }
int cloudTexture = -1, cloudsVbo = -1, cloudsVertices = 0; int cloudTexture = -1, cloudsVbo = -1, cloudsVertices = 6;
int skyOffset = 10, skyVbo = -1, skyVertices = 6; int skyOffset = 10, skyVbo = -1, skyVertices = 6;
public float CloudsSpeed = 1f; public float CloudsSpeed = 1f;
@ -103,58 +103,27 @@ namespace ClassicalSharp.Renderers {
} }
void ResetClouds() { void ResetClouds() {
if( cloudsVbo != -1 ) { Graphics.DeleteVb( cloudsVbo );
Graphics.DeleteVb( cloudsVbo );
}
if( Map.IsNotLoaded ) return; 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]; 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 y = Map.Height + 2;
int z1 = 0 - extent, z2 = Map.Length + extent;
FastColour cloudsCol = Map.CloudsCol; FastColour cloudsCol = Map.CloudsCol;
for( int x = -extent; x < Map.Width + extent; x += cloudSize ) { vertices[0] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol );
for( int z = -extent; z < Map.Length + extent; z += cloudSize ) { vertices[1] = new VertexPos3fTex2fCol4b( x2, y, z1, x2 / 2048f, z1 / 2048f, cloudsCol );
int x2 = x + cloudSize; vertices[2] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol );
int z2 = z + cloudSize;
if( x2 < -dist || z2 < -dist ) continue; vertices[3] = new VertexPos3fTex2fCol4b( x2, y, z2, x2 / 2048f, z2 / 2048f, cloudsCol );
if( x > Map.Width + dist || z > Map.Length + dist ) continue; vertices[4] = new VertexPos3fTex2fCol4b( x1, y, z2, x1 / 2048f, z2 / 2048f, cloudsCol );
// Clip clouds to visible view distance (avoid overdrawing) vertices[5] = new VertexPos3fTex2fCol4b( x1, y, z1, x1 / 2048f, z1 / 2048f, cloudsCol );
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 );
}
}
cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b ); cloudsVbo = Graphics.InitVb( vertices, DrawMode.Triangles, VertexFormat.VertexPos3fTex2fCol4b );
} }
void ResetSky() { void ResetSky() {
if( skyVbo != -1 ) { Graphics.DeleteVb( skyVbo );
Graphics.DeleteVb( skyVbo );
}
if( Map.IsNotLoaded ) return; if( Map.IsNotLoaded ) return;
VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices]; VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices];
int extent = Window.ViewDistance; int extent = Window.ViewDistance;

View File

@ -69,9 +69,7 @@ namespace ClassicalSharp {
static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgesCol = FastColour.White; static readonly FastColour sidesCol = new FastColour( 128, 128, 128 ), edgesCol = FastColour.White;
void RebuildSides() { void RebuildSides() {
if( sidesVboId != -1 ) { Graphics.DeleteVb( sidesVboId );
Graphics.DeleteVb( sidesVboId );
}
int groundLevel = Map.GroundHeight; int groundLevel = Map.GroundHeight;
VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[sidesVertices]; VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[sidesVertices];
@ -89,9 +87,7 @@ namespace ClassicalSharp {
} }
void RebuildEdges() { void RebuildEdges() {
if( edgesVboId != -1 ) { Graphics.DeleteVb( edgesVboId );
Graphics.DeleteVb( edgesVboId );
}
int waterLevel = Map.WaterHeight; int waterLevel = Map.WaterHeight;
VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[edgesVertices]; VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[edgesVertices];