Selections shader, started OpenGL api cleanup.

This commit is contained in:
UnknownShadow200 2015-06-20 11:09:08 +10:00
parent 08049fc297
commit 1f10fbf2d1
11 changed files with 83 additions and 186 deletions

View file

@ -140,6 +140,7 @@ namespace ClassicalSharp {
CommandManager = new CommandManager(); CommandManager = new CommandManager();
CommandManager.Init( this ); CommandManager.Init( this );
SelectionManager = new SelectionManager( this ); SelectionManager = new SelectionManager( this );
SelectionManager.Init();
ParticleManager = new ParticleManager( this ); ParticleManager = new ParticleManager( this );
ParticleManager.Init(); ParticleManager.Init();
WeatherRenderer = new WeatherRenderer( this ); WeatherRenderer = new WeatherRenderer( this );
@ -277,7 +278,6 @@ namespace ClassicalSharp {
} }
} }
LocalPlayer.Render( deltaTime, t ); LocalPlayer.Render( deltaTime, t );
Graphics.UseProgram( 0 );
} }
public override void Dispose() { public override void Dispose() {

View file

@ -499,4 +499,52 @@ void main() {
api.DisableVertexAttrib( positionLoc ); api.DisableVertexAttrib( positionLoc );
} }
} }
public sealed class SelectionShader : FogAndMVPShader {
public SelectionShader() {
VertexSource = @"
#version 130
in vec3 in_position;
in vec4 in_colour;
out vec4 out_colour;
uniform mat4 MVP;
void main() {
gl_Position = MVP * vec4(in_position, 1.0);
out_colour = in_colour;
}";
FragmentSource = @"
#version 130
in vec2 out_texcoords;
in vec4 out_colour;
out vec4 final_colour;
--IMPORT fog_uniforms
void main() {
vec4 finalColour = out_colour;
--IMPORT fog_code
final_colour = finalColour;
}";
}
public int positionLoc, colourLoc;
protected override void GetLocations( OpenGLApi api ) {
positionLoc = api.GetAttribLocation( ProgramId, "in_position" );
colourLoc = api.GetAttribLocation( ProgramId, "in_colour" );
base.GetLocations( api );
}
protected override void EnableVertexAttribStates( OpenGLApi api, int stride ) {
api.EnableVertexAttribF( positionLoc, 3, stride, 0 );
api.EnableVertexAttribF( colourLoc, 4, VertexAttribType.UInt8, true, stride, 12 );
}
protected override void DisableVertexAttribStates( OpenGLApi api, int stride ) {
api.DisableVertexAttrib( positionLoc );
api.DisableVertexAttrib( colourLoc );
}
}
} }

View file

@ -45,21 +45,6 @@ namespace ClassicalSharp.GraphicsAPI {
protected static int[] strideSizes = { 20, 16, 24 }; protected static int[] strideSizes = { 20, 16, 24 };
public void Translate( float x, float y, float z ) {
Matrix4 matrix = Matrix4.CreateTranslation( x, y, z );
MultiplyMatrix( ref matrix );
}
public void RotateY( float degrees ) {
Matrix4 matrix = Matrix4.CreateRotationY( degrees * 0.01745329251f );
MultiplyMatrix( ref matrix );
}
public virtual void Scale( float x, float y, float z ) {
Matrix4 matrix = Matrix4.Scale( x, y, z );
MultiplyMatrix( ref matrix );
}
public void CheckResources() { public void CheckResources() {
} }

View file

@ -1,5 +1,4 @@
//#define TRACK_RESOURCES using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
@ -22,10 +21,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.GetInteger( GetPName.MaxTextureSize, &texDims ); GL.GetInteger( GetPName.MaxTextureSize, &texDims );
textureDimensions = texDims; textureDimensions = texDims;
InitDynamicBuffers(); InitDynamicBuffers();
drawBatchFuncCol4b = DrawVbPos3fCol4bFast;
drawBatchFuncTex2f = DrawVbPos3fTex2fFast;
drawBatchFuncTex2fCol4b = DrawVbPos3fTex2fCol4bFast;
} }
public int MaxTextureDimensions { public int MaxTextureDimensions {
@ -112,11 +107,6 @@ namespace ClassicalSharp.GraphicsAPI {
set { ToggleCap( EnableCap.CullFace, value ); } set { ToggleCap( EnableCap.CullFace, value ); }
} }
#if TRACK_RESOURCES
Dictionary<int, string> textures = new Dictionary<int, string>();
#endif
public unsafe int LoadTexture( int width, int height, IntPtr scan0 ) { public unsafe int LoadTexture( int width, int height, IntPtr scan0 ) {
if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) ) if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) )
Utils.LogWarning( "Creating a non power of two texture." ); Utils.LogWarning( "Creating a non power of two texture." );
@ -131,9 +121,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0,
GlPixelFormat.Bgra, PixelType.UnsignedByte, scan0 ); GlPixelFormat.Bgra, PixelType.UnsignedByte, scan0 );
GL.Disable( EnableCap.Texture2D ); GL.Disable( EnableCap.Texture2D );
#if TRACK_RESOURCES
textures.Add( texId, Environment.StackTrace );
#endif
return texId; return texId;
} }
@ -143,9 +130,6 @@ namespace ClassicalSharp.GraphicsAPI {
public unsafe void DeleteTexture( ref int texId ) { public unsafe void DeleteTexture( ref int texId ) {
if( texId <= 0 ) return; if( texId <= 0 ) return;
#if TRACK_RESOURCES
textures.Remove( texId );
#endif
int id = texId; int id = texId;
GL.DeleteTextures( 1, &id ); GL.DeleteTextures( 1, &id );
texId = -1; texId = -1;
@ -198,14 +182,6 @@ namespace ClassicalSharp.GraphicsAPI {
#region Vertex buffers #region Vertex buffers
#if TRACK_RESOURCES
Dictionary<int, string> vbs = new Dictionary<int, string>();
#endif
Action<DrawMode, int, int, int> drawBatchFunc;
Action<DrawMode, int, int, int> drawBatchFuncTex2f;
Action<DrawMode, int, int, int> drawBatchFuncCol4b;
Action<DrawMode, int, int, int> drawBatchFuncTex2fCol4b;
public unsafe int CreateDynamicVb( VertexFormat format, int maxVertices ) { public unsafe int CreateDynamicVb( VertexFormat format, int maxVertices ) {
int id = 0; int id = 0;
GL.GenBuffers( 1, &id ); GL.GenBuffers( 1, &id );
@ -223,9 +199,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.BindBuffer( BufferTarget.ArrayBuffer, id ); GL.BindBuffer( BufferTarget.ArrayBuffer, id );
GL.BufferData( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageHint.StaticDraw ); GL.BufferData( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageHint.StaticDraw );
GL.BindBuffer( BufferTarget.ArrayBuffer, 0 ); GL.BindBuffer( BufferTarget.ArrayBuffer, 0 );
#if TRACK_RESOURCES
vbs.Add( id, Environment.StackTrace );
#endif
return id; return id;
} }
@ -241,16 +214,6 @@ namespace ClassicalSharp.GraphicsAPI {
return id; return id;
} }
public void DrawDynamicVb<T>( DrawMode mode, int vb, T[] vertices, VertexFormat format, int count ) where T : struct {
int sizeInBytes = count * strideSizes[(int)format];
GL.BindBuffer( BufferTarget.ArrayBuffer, vb );
GL.BufferSubData( BufferTarget.ArrayBuffer, IntPtr.Zero, new IntPtr( sizeInBytes ), vertices );
BeginVbBatch( format );
DrawVbBatch( mode, vb, 0, count );
EndVbBatch();
}
public unsafe void DeleteDynamicVb( int id ) { public unsafe void DeleteDynamicVb( int id ) {
if( id <= 0 ) return; if( id <= 0 ) return;
GL.DeleteBuffers( 1, &id ); GL.DeleteBuffers( 1, &id );
@ -258,9 +221,6 @@ namespace ClassicalSharp.GraphicsAPI {
public unsafe void DeleteVb( int id ) { public unsafe void DeleteVb( int id ) {
if( id <= 0 ) return; if( id <= 0 ) return;
#if TRACK_RESOURCES
vbs.Remove( id );
#endif
GL.DeleteBuffers( 1, &id ); GL.DeleteBuffers( 1, &id );
} }
@ -277,71 +237,6 @@ namespace ClassicalSharp.GraphicsAPI {
return GL.IsBuffer( ib ); return GL.IsBuffer( ib );
} }
public void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) {
BeginVbBatch( format );
DrawVbBatch( mode, id, startVertex, verticesCount );
EndVbBatch();
}
VertexFormat batchFormat = (VertexFormat)999;
public void BeginVbBatch( VertexFormat format ) {
batchFormat = format;
GL.EnableClientState( ArrayCap.VertexArray );
if( format == VertexFormat.Pos3fTex2fCol4b ) {
GL.EnableClientState( ArrayCap.ColorArray );
GL.EnableClientState( ArrayCap.TextureCoordArray );
drawBatchFunc = drawBatchFuncTex2fCol4b;
} else if( format == VertexFormat.Pos3fTex2f ) {
GL.EnableClientState( ArrayCap.TextureCoordArray );
drawBatchFunc = drawBatchFuncTex2f;
} else if( format == VertexFormat.Pos3fCol4b ) {
GL.EnableClientState( ArrayCap.ColorArray );
drawBatchFunc = drawBatchFuncCol4b;
}
}
public void BeginIndexedVbBatch() {
BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
}
public void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ) {
drawBatchFunc( mode, id, startVertex, verticesCount );
}
public void EndVbBatch() {
GL.BindBuffer( BufferTarget.ArrayBuffer, 0 );
GL.DisableClientState( ArrayCap.VertexArray );
if( batchFormat == VertexFormat.Pos3fTex2fCol4b ) {
GL.DisableClientState( ArrayCap.ColorArray );
GL.DisableClientState( ArrayCap.TextureCoordArray );
} else if( batchFormat == VertexFormat.Pos3fTex2f ) {
GL.DisableClientState( ArrayCap.TextureCoordArray );
} else if( batchFormat == VertexFormat.Pos3fCol4b ) {
GL.DisableClientState( ArrayCap.ColorArray );
}
}
void DrawVbPos3fTex2fFast( DrawMode mode, int id, int offset, int verticesCount ) {
GL.BindBuffer( BufferTarget.ArrayBuffer, id );
GL.VertexPointer( 3, VertexPointerType.Float, 20, new IntPtr( 0 ) );
GL.TexCoordPointer( 2, TexCoordPointerType.Float, 20, new IntPtr( 12 ) );
GL.DrawArrays( modeMappings[(int)mode], offset, verticesCount );
}
void DrawVbPos3fCol4bFast( DrawMode mode, int id, int offset, int verticesCount ) {
GL.BindBuffer( BufferTarget.ArrayBuffer, id );
GL.VertexPointer( 3, VertexPointerType.Float, 16, new IntPtr( 0 ) );
GL.ColorPointer( 4, ColorPointerType.UnsignedByte, 16, new IntPtr( 12 ) );
GL.DrawArrays( modeMappings[(int)mode], offset, verticesCount );
}
void DrawVbPos3fTex2fCol4bFast( DrawMode mode, int id, int offset, int verticesCount ) {
GL.BindBuffer( BufferTarget.ArrayBuffer, id );
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.DrawArrays( modeMappings[(int)mode], offset, verticesCount );
}
#endregion #endregion
@ -362,48 +257,8 @@ namespace ClassicalSharp.GraphicsAPI {
GL.LoadMatrix( ptr ); GL.LoadMatrix( ptr );
} }
public void LoadIdentityMatrix() {
GL.LoadIdentity();
}
public void PushMatrix() {
GL.PushMatrix();
}
public void PopMatrix() {
GL.PopMatrix();
}
public unsafe void MultiplyMatrix( ref Matrix4 matrix ) {
fixed( Single* ptr = &matrix.Row0.X )
GL.MultMatrix( ptr );
}
#endregion #endregion
#if TRACK_RESOURCES
public void CheckResources() {
if( textures.Count > 0 ) {
foreach( var pair in textures ) {
Console.WriteLine( pair.Value );
Console.WriteLine( "for tex id " + pair.Key );
Console.WriteLine( "===========" );
}
}
if( vbs.Count > 0 ) {
foreach( var pair in vbs ) {
Console.WriteLine( pair.Value );
Console.WriteLine( "for vb id " + pair.Key );
Console.WriteLine( "===========" );
}
}
Console.WriteLine( "tex " + textures.Count + ", vb" + vbs.Count );
if( textures.Count > 0 || vbs.Count > 0 ) {
System.Diagnostics.Debugger.Break();
}
}
#endif
public void BeginFrame( Game game ) { public void BeginFrame( Game game ) {
} }

View file

@ -41,7 +41,6 @@ namespace ClassicalSharp.Model {
curMVPMatrix = Matrix4.RotateY( -yaw ) * Matrix4.Translation( pos ) * window.MVP; curMVPMatrix = Matrix4.RotateY( -yaw ) * Matrix4.Translation( pos ) * window.MVP;
graphics.SetUniform( shader.mvpLoc, ref curMVPMatrix ); graphics.SetUniform( shader.mvpLoc, ref curMVPMatrix );
DrawPlayerModel( player, renderer ); DrawPlayerModel( player, renderer );
graphics.PopMatrix();
} }
protected abstract void DrawPlayerModel( Player player, PlayerRenderer renderer ); protected abstract void DrawPlayerModel( Player player, PlayerRenderer renderer );

View file

@ -219,7 +219,7 @@ namespace ClassicalSharp {
api.Bind2DTexture( texIds[batch] ); api.Bind2DTexture( texIds[batch] );
RenderSpriteBatch( batch ); RenderSpriteBatch( batch );
} }
api.UseProgram( 0 );
Window.MapEnvRenderer.RenderMapSides( deltaTime ); Window.MapEnvRenderer.RenderMapSides( deltaTime );
Window.MapEnvRenderer.RenderMapEdges( deltaTime ); Window.MapEnvRenderer.RenderMapEdges( deltaTime );
@ -244,7 +244,6 @@ namespace ClassicalSharp {
} }
//Graphics.DepthWrite = true; //Graphics.DepthWrite = true;
api.AlphaBlending = false; api.AlphaBlending = false;
api.UseProgram( 0 );
} }
int[] distances; int[] distances;

View file

@ -72,7 +72,6 @@ namespace ClassicalSharp.Renderers {
DrawZPlane( p2.Z, p1.X, p2.Y, p2.X, p2.Y - size ); DrawZPlane( p2.Z, p1.X, p2.Y, p2.X, p2.Y - size );
shader.DrawDynamic( graphics, DrawMode.Triangles, VertexPos3fCol4b.Size, vb, vertices, verticesCount ); shader.DrawDynamic( graphics, DrawMode.Triangles, VertexPos3fCol4b.Size, vb, vertices, verticesCount );
graphics.UseProgram( 0 );
} }
} }

View file

@ -39,7 +39,6 @@ namespace ClassicalSharp.Renderers {
} }
RenderClouds( deltaTime ); RenderClouds( deltaTime );
ResetFog(); ResetFog();
Graphics.UseProgram( 0 );
} }
public void SetSkyOffset( int offset ) { public void SetSkyOffset( int offset ) {

View file

@ -27,6 +27,7 @@ namespace ClassicalSharp {
public void Render( double deltaTime ) { public void Render( double deltaTime ) {
Weather weather = map.Weather; Weather weather = map.Weather;
if( weather == Weather.Sunny ) return; if( weather == Weather.Sunny ) return;
api.UseProgram( shader.ProgramId ); api.UseProgram( shader.ProgramId );
api.SetUniform( shader.mvpLoc, ref Window.MVP ); api.SetUniform( shader.mvpLoc, ref Window.MVP );
api.SetUniform( shader.fogColLoc, ref api.modernFogCol ); api.SetUniform( shader.fogColLoc, ref api.modernFogCol );

View file

@ -8,8 +8,8 @@ namespace ClassicalSharp.Selections {
public short ID; public short ID;
public FastColour Colour, LineColour; public FastColour Colour, LineColour;
public int VboId, VerticesCount; public int Vb, VerticesCount;
public int LineVboId, LineVerticesCount; public int LineVb, LineVerticesCount;
public OpenGLApi Graphics; public OpenGLApi Graphics;
public Vector3I Min, Max; public Vector3I Min, Max;
@ -25,11 +25,11 @@ namespace ClassicalSharp.Selections {
} }
} }
public void Render( double delta ) { public void Render( SelectionShader shader ) {
Graphics.DepthWrite = false; Graphics.DepthWrite = false;
Graphics.DrawVbBatch( DrawMode.Triangles, VboId, 0, VerticesCount ); shader.Draw( Graphics, DrawMode.Triangles, VertexPos3fCol4b.Size, Vb, 0, VerticesCount );
Graphics.DepthWrite = true; Graphics.DepthWrite = true;
Graphics.DrawVbBatch( DrawMode.Lines, LineVboId, 0, LineVerticesCount ); shader.Draw( Graphics, DrawMode.Lines, VertexPos3fCol4b.Size, LineVb, 0, LineVerticesCount );
} }
public SelectionBox( Vector3I start, Vector3I end, FastColour col, OpenGLApi graphics ) { public SelectionBox( Vector3I start, Vector3I end, FastColour col, OpenGLApi graphics ) {
@ -59,7 +59,7 @@ namespace ClassicalSharp.Selections {
Line( vertices, ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z ); Line( vertices, ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z );
Line( vertices, ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z ); Line( vertices, ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z );
LineVerticesCount = vertices.Length; LineVerticesCount = vertices.Length;
LineVboId = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b ); LineVb = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b );
vertices = new VertexPos3fCol4b[6 * 6]; vertices = new VertexPos3fCol4b[6 * 6];
index = 0; index = 0;
@ -71,7 +71,7 @@ namespace ClassicalSharp.Selections {
RenderZPlane( vertices, ref index, p1.Z, p2.Z, p1.Y, p2.Y, p1.X ); RenderZPlane( vertices, ref index, p1.Z, p2.Z, p1.Y, p2.Y, p1.X );
RenderZPlane( vertices, ref index, p1.Z, p2.Z, p1.Y, p2.Y, p2.X ); RenderZPlane( vertices, ref index, p1.Z, p2.Z, p1.Y, p2.Y, p2.X );
VerticesCount = vertices.Length; VerticesCount = vertices.Length;
VboId = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b ); Vb = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b );
} }
void Line( VertexPos3fCol4b[] vertices, ref int index, float x1, float y1, float z1, float x2, float y2, float z2 ) { void Line( VertexPos3fCol4b[] vertices, ref int index, float x1, float y1, float z1, float x2, float y2, float z2 ) {
@ -110,8 +110,8 @@ namespace ClassicalSharp.Selections {
} }
public void Dispose() { public void Dispose() {
Graphics.DeleteVb( LineVboId ); Graphics.DeleteVb( LineVb );
Graphics.DeleteVb( VboId ); Graphics.DeleteVb( Vb );
} }
} }
} }

View file

@ -8,16 +8,17 @@ namespace ClassicalSharp.Selections {
public class SelectionManager : IDisposable { public class SelectionManager : IDisposable {
public Game Window; public Game Window;
public OpenGLApi Graphics; public OpenGLApi api;
SelectionShader shader;
public SelectionManager( Game window ) { public SelectionManager( Game window ) {
Window = window; Window = window;
Graphics = window.Graphics; api = window.Graphics;
} }
List<SelectionBox> selections = new List<SelectionBox>( 256 ); List<SelectionBox> selections = new List<SelectionBox>( 256 );
public void AddSelection( byte id, Vector3I p1, Vector3I p2, FastColour col ) { public void AddSelection( byte id, Vector3I p1, Vector3I p2, FastColour col ) {
SelectionBox selection = new SelectionBox( p1, p2, col, Graphics ); SelectionBox selection = new SelectionBox( p1, p2, col, api );
selection.ID = id; selection.ID = id;
selections.Add( selection ); selections.Add( selection );
} }
@ -66,20 +67,31 @@ namespace ClassicalSharp.Selections {
return maxDistA == maxDistB ? minDistA.CompareTo( minDistB ) : maxDistA.CompareTo( maxDistB ); return maxDistA == maxDistB ? minDistA.CompareTo( minDistB ) : maxDistA.CompareTo( maxDistB );
} }
public void Init() {
shader = new SelectionShader();
shader.Init( api );
}
public void Render( double delta ) { public void Render( double delta ) {
if( selections.Count == 0 ) return;
api.UseProgram( shader.ProgramId );
api.SetUniform( shader.mvpLoc, ref Window.MVP );
api.SetUniform( shader.fogColLoc, ref api.modernFogCol );
api.SetUniform( shader.fogDensityLoc, api.modernFogDensity );
api.SetUniform( shader.fogEndLoc, api.modernFogEnd );
api.SetUniform( shader.fogModeLoc, api.modernFogMode );
Player player = Window.LocalPlayer; Player player = Window.LocalPlayer;
pos = player.Position; pos = player.Position;
if( selections.Count == 0 ) return;
selections.Sort( (a, b) => Compare( a, b ) ); selections.Sort( (a, b) => Compare( a, b ) );
Graphics.BeginVbBatch( VertexFormat.Pos3fCol4b ); api.AlphaBlending = true;
Graphics.AlphaBlending = true;
for( int i = 0 ; i < selections.Count; i++ ) { for( int i = 0 ; i < selections.Count; i++ ) {
SelectionBox box = selections[i]; SelectionBox box = selections[i];
box.Render( delta ); box.Render( shader );
} }
Graphics.AlphaBlending = false; api.AlphaBlending = false;
Graphics.EndVbBatch();
} }
public void Dispose() { public void Dispose() {