Fix 2D gui shaders.

This commit is contained in:
UnknownShadow200 2015-04-05 16:16:13 +10:00
parent 4e75d8f071
commit e98f6ac4d8
6 changed files with 52 additions and 53 deletions

View File

@ -28,7 +28,7 @@ namespace ClassicalSharp.Particles {
Graphics.Texturing = true;
Graphics.Bind2DTexture( Window.TerrainAtlasTexId );
Graphics.AlphaTest = true;
Graphics.DrawVertices( DrawMode.Triangles, vertices );
Graphics.DrawVertices( DrawMode.Triangles, vertices, vertices.Length );
Graphics.AlphaTest = false;
Graphics.Texturing = false;
}

View File

@ -189,7 +189,7 @@ namespace ClassicalSharp {
guiShader = new GuiShader();
guiShader.Initialise( ModernGraphics );
}
Shader guiShader;
GuiShader guiShader;
public void SetViewDistance( int distance ) {
ViewDistance = distance;
@ -267,14 +267,16 @@ namespace ClassicalSharp {
SelectedPos = null;
}
//ModernGraphics.UseProgram( guiShader.ProgramId );
Graphics.Mode2D( Width, Height );
ModernGraphics.UseProgram( guiShader.ProgramId );
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 );
ModernGraphics.SetUniform( guiShader.projLoc, ref matrix );
Graphics.Mode2D( guiShader );
fpsScreen.Render( e.Time );
if( activeScreen != null ) {
activeScreen.Render( e.Time );
}
Graphics.Mode3D();
//ModernGraphics.UseProgram( 0 );
ModernGraphics.UseProgram( 0 );
if( screenshotRequested ) {
if( !Directory.Exists( "screenshots" ) ) {

View File

@ -90,18 +90,7 @@ namespace ClassicalSharp.GraphicsAPI {
/// <summary> Whether writing to the depth buffer is enabled. </summary>
public abstract bool DepthWrite { set; }
public virtual void DrawVertices( DrawMode mode, VertexPos3fCol4b[] vertices ) {
DrawVertices( mode, vertices, vertices.Length );
}
public virtual void DrawVertices( DrawMode mode, VertexPos3fTex2f[] vertices ) {
DrawVertices( mode, vertices, vertices.Length );
}
public virtual void DrawVertices( DrawMode mode, VertexPos3fTex2fCol4b[] vertices ) {
DrawVertices( mode, vertices, vertices.Length );
}
// TODO: Kill these forever
public abstract void DrawVertices( DrawMode mode, VertexPos3fCol4b[] vertices, int count );
public abstract void DrawVertices( DrawMode mode, VertexPos3fTex2f[] vertices, int count );
@ -206,48 +195,19 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void OnWindowResize( int newWidth, int newHeight );
public virtual void Draw2DQuad( float x, float y, float width, float height, FastColour col ) {
VertexPos3fCol4b[] vertices = {
new VertexPos3fCol4b( x + width, y, 0, col ),
new VertexPos3fCol4b( x + width, y + height, 0, col ),
new VertexPos3fCol4b( x, y, 0, col ),
new VertexPos3fCol4b( x, y + height, 0, col ),
};
DrawVertices( DrawMode.TriangleStrip, vertices );
}
public abstract void Draw2DQuad( float x, float y, float width, float height, FastColour col );
public virtual void Draw2DTextureVertices( ref Texture tex ) {
float x1 = tex.X1, y1 = tex.Y1, x2 = tex.X2, y2 = tex.Y2;
// Have to order them this way because it's a triangle strip.
VertexPos3fTex2f[] vertices = {
new VertexPos3fTex2f( x2, y1, 0, tex.U2, tex.V1 ),
new VertexPos3fTex2f( x2, y2, 0, tex.U2, tex.V2 ),
new VertexPos3fTex2f( x1, y1, 0, tex.U1, tex.V1 ),
new VertexPos3fTex2f( x1, y2, 0, tex.U1, tex.V2 ),
};
DrawVertices( DrawMode.TriangleStrip, vertices );
}
public abstract void Draw2DTextureVertices( ref Texture tex );
public void Mode2D( float width, float height ) {
SetMatrixMode( MatrixType.Projection );
PushMatrix();
LoadIdentityMatrix();
//GL.Ortho( 0, width, height, 0, 0, 1 );
protected GuiShader shader;
public void Mode2D( GuiShader shader ) {
DepthTest = false;
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 );
LoadMatrix( ref matrix );
SetMatrixMode( MatrixType.Modelview );
PushMatrix();
LoadIdentityMatrix();
AlphaBlending = true;
// Please excuse the awful hackery here
this.shader = shader;
}
public void Mode3D() {
// Get rid of orthographic 2D matrix.
SetMatrixMode( MatrixType.Projection );
PopMatrix();
SetMatrixMode( MatrixType.Modelview );
PopMatrix();
DepthTest = true;
AlphaBlending = false;
}

View File

@ -576,6 +576,43 @@ namespace ClassicalSharp.GraphicsAPI {
}
#endif
public override void Draw2DQuad( float x, float y, float width, float height, FastColour col ) {
VertexPos3fCol4b[] vertices = {
new VertexPos3fCol4b( x + width, y, 0, col ),
new VertexPos3fCol4b( x + width, y + height, 0, col ),
new VertexPos3fCol4b( x, y, 0, col ),
new VertexPos3fCol4b( x, y + height, 0, col ),
};
GL.Begin( BeginMode.TriangleStrip );
for( int i = 0; i < vertices.Length; i++ ) {
VertexPos3fCol4b vertex = vertices[i];
GL.VertexAttrib2( shader.texCoordsLoc, -1f, -1f );
GL.VertexAttrib4( shader.colourLoc,
new Vector4( vertex.R / 255f, vertex.G / 255f, vertex.B / 255f, vertex.A / 255f ) );
GL.VertexAttrib3( shader.positionLoc, vertex.X, vertex.Y, vertex.Z );
}
GL.End();
}
public override void Draw2DTextureVertices( ref Texture tex ) {
float x1 = tex.X1, y1 = tex.Y1, x2 = tex.X2, y2 = tex.Y2;
// Have to order them this way because it's a triangle strip.
VertexPos3fTex2f[] vertices = {
new VertexPos3fTex2f( x2, y1, 0, tex.U2, tex.V1 ),
new VertexPos3fTex2f( x2, y2, 0, tex.U2, tex.V2 ),
new VertexPos3fTex2f( x1, y1, 0, tex.U1, tex.V1 ),
new VertexPos3fTex2f( x1, y2, 0, tex.U1, tex.V2 ),
};
GL.Begin( BeginMode.TriangleStrip );
for( int i = 0; i < vertices.Length; i++ ) {
VertexPos3fTex2f vertex = vertices[i];
GL.VertexAttrib4( shader.colourLoc, new Vector4( 1, 1, 1, 1 ) );
GL.VertexAttrib2( shader.texCoordsLoc, vertex.U, vertex.V );
GL.VertexAttrib3( shader.positionLoc, vertex.X, vertex.Y, vertex.Z );
}
GL.End();
}
public override void PrintApiSpecificInfo() {
Console.WriteLine( "OpenGL vendor: " + GL.GetString( StringName.Vendor ) );
Console.WriteLine( "OpenGL renderer: " + GL.GetString( StringName.Renderer ) );

Binary file not shown.

View File

@ -60,7 +60,7 @@ namespace ClassicalSharp.Renderers {
DrawZPlane( max.Z + offset, max.X + offset, min.Y - offset, max.X - size + offset, max.Y + offset );
DrawZPlane( max.Z + offset, min.X - offset, min.Y - offset, max.X + offset, min.Y + size - offset );
DrawZPlane( max.Z + offset, min.X - offset, max.Y + offset, max.X + offset, max.Y - size + offset );
graphics.DrawVertices( DrawMode.Triangles, vertices );
graphics.DrawVertices( DrawMode.Triangles, vertices, vertices.Length );
}
}