Fix 2D gui being affected by fog when in lava, optimise IsometricBlockDrawer by avoiding redundant calculations.

This commit is contained in:
UnknownShadow200 2015-10-08 16:47:53 +11:00
parent 2ea9349a05
commit d084385215
11 changed files with 80 additions and 62 deletions

View File

@ -1,7 +1,7 @@
using System; using System;
using ClassicalSharp.GraphicsAPI; using ClassicalSharp.GraphicsAPI;
using OpenTK;
using ClassicalSharp.Model; using ClassicalSharp.Model;
using OpenTK;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -15,9 +15,15 @@ namespace ClassicalSharp {
static float scale; static float scale;
static FastColour colNormal, colXSide, colZSide, colYBottom; static FastColour colNormal, colXSide, colZSide, colYBottom;
static float cosX, sinX, cosY, sinY;
static IsometricBlockDrawer() { static IsometricBlockDrawer() {
colNormal = FastColour.White; colNormal = FastColour.White;
FastColour.GetShaded( colNormal, ref colXSide, ref colZSide, ref colYBottom ); FastColour.GetShaded( colNormal, ref colXSide, ref colZSide, ref colYBottom );
cosX = (float)Math.Cos( 26.565f * Utils.Deg2Rad );
sinX = (float)Math.Sin( 26.565f * Utils.Deg2Rad );
cosY = (float)Math.Cos( -45f * Utils.Deg2Rad );
sinY = (float)Math.Sin( -45f * Utils.Deg2Rad );
} }
public static void Draw( Game game, byte block, float size, float x, float y ) { public static void Draw( Game game, byte block, float size, float x, float y ) {
@ -28,9 +34,9 @@ namespace ClassicalSharp {
index = 0; index = 0;
scale = size; scale = size;
// screen to isometric coords // screen to isometric coords (cos(-x) = cos(x), sin(-x) = -sin(x))
pos.X = x; pos.Y = y; pos.Z = 0; pos.X = x; pos.Y = y; pos.Z = 0;
pos = Utils.RotateY( Utils.RotateX( pos, -angleX ), -angleY ); pos = Utils.RotateY( Utils.RotateX( pos, cosX, -sinX ), cosY, -sinY );
if( info.IsSprite[block] ) { if( info.IsSprite[block] ) {
DrawXFace( block, 0f, TileSide.Right ); DrawXFace( block, 0f, TileSide.Right );
@ -47,8 +53,6 @@ namespace ClassicalSharp {
cache.vertices, index, index * 6 / 4 ); cache.vertices, index, index * 6 / 4 );
} }
static float angleY = (float)Utils.DegreesToRadians( -45f );
static float angleX = (float)Utils.DegreesToRadians( 26.565f );
static void TransformVertex( ref VertexPos3fTex2fCol4b vertex ) { static void TransformVertex( ref VertexPos3fTex2fCol4b vertex ) {
Vector3 pos = new Vector3( vertex.X, vertex.Y, vertex.Z ); Vector3 pos = new Vector3( vertex.X, vertex.Y, vertex.Z );
#if USE_DX #if USE_DX
@ -56,19 +60,10 @@ namespace ClassicalSharp {
pos.X -= 0.5f; pos.X -= 0.5f;
pos.Y -= 0.5f; pos.Y -= 0.5f;
#endif #endif
pos = Utils.RotateX( Utils.RotateY( pos, angleY ), angleX ); pos = Utils.RotateX( Utils.RotateY( pos, cosY, sinY ), cosX, sinX );
vertex.X = pos.X; vertex.Y = pos.Y; vertex.Z = pos.Z; vertex.X = pos.X; vertex.Y = pos.Y; vertex.Z = pos.Z;
} }
public static void SetupState( IGraphicsApi graphics, bool setFog ) {
if( setFog ) graphics.Fog = false;
graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
}
public static void RestoreState( IGraphicsApi graphics, bool setFog ) {
if( setFog ) graphics.Fog = true;
}
static Vector3 pos = Vector3.Zero; static Vector3 pos = Vector3.Zero;
static void DrawYFace( byte block, float y, int side ) { static void DrawYFace( byte block, float y, int side ) {
int texId = info.GetTextureLoc( block, side ); int texId = info.GetTextureLoc( block, side );

View File

@ -1,6 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using ClassicalSharp.Renderers; using ClassicalSharp.GraphicsAPI;
using OpenTK.Input; using OpenTK.Input;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -27,9 +27,8 @@ namespace ClassicalSharp {
rows * blockSize + 30 + 10, backCol ); rows * blockSize + 30 + 10, backCol );
graphicsApi.Texturing = true; graphicsApi.Texturing = true;
graphicsApi.BindTexture( game.TerrainAtlas.TexId ); graphicsApi.BindTexture( game.TerrainAtlas.TexId );
graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
bool setFog = game.EnvRenderer is StandardEnvRenderer;
IsometricBlockDrawer.SetupState( graphicsApi, setFog );
for( int i = 0; i < blocksTable.Length; i++ ) { for( int i = 0; i < blocksTable.Length; i++ ) {
int x, y; int x, y;
GetCoords( i, out x, out y ); GetCoords( i, out x, out y );
@ -46,7 +45,6 @@ namespace ClassicalSharp {
IsometricBlockDrawer.Draw( game, (byte)blocksTable[selectedIndex], 20, IsometricBlockDrawer.Draw( game, (byte)blocksTable[selectedIndex], 20,
x + blockSize / 2, y + 28 ); x + blockSize / 2, y + 28 );
} }
IsometricBlockDrawer.RestoreState( graphicsApi, setFog );
if( blockInfoTexture.IsValid ) if( blockInfoTexture.IsValid )
blockInfoTexture.Render( graphicsApi ); blockInfoTexture.Render( graphicsApi );

View File

@ -36,14 +36,24 @@ namespace ClassicalSharp {
} }
} }
Font chatFont, chatBoldFont, historyFont, announcementFont; static FastColour backColour = new FastColour( 60, 60, 60, 180 );
public void RenderBackground() {
int height = normalChat.GetUsedHeight();
int y = normalChat.Y + normalChat.Height - height - 5;
int x = normalChat.X - 5;
int width = normalChat.Width + 10;
if( height > 0 )
graphicsApi.Draw2DQuad( x, y, width, height + 10, backColour );
}
Font chatFont, chatInputFont, announcementFont;
public override void Init() { public override void Init() {
chatFont = new Font( "Arial", game.Chat.FontSize ); chatFont = new Font( "Arial", game.Chat.FontSize );
chatBoldFont = new Font( "Arial", game.Chat.FontSize, FontStyle.Bold ); chatInputFont = new Font( "Arial", game.Chat.FontSize, FontStyle.Bold );
announcementFont = new Font( "Arial", 14 ); announcementFont = new Font( "Arial", 14 );
historyFont = new Font( "Arial", 12, FontStyle.Italic );
textInput = new TextInputWidget( game, chatFont, chatBoldFont ); textInput = new TextInputWidget( game, chatFont, chatInputFont );
textInput.ChatInputYOffset = ChatInputYOffset; textInput.ChatInputYOffset = ChatInputYOffset;
status = new TextGroupWidget( game, 3, chatFont ); status = new TextGroupWidget( game, 3, chatFont );
status.VerticalDocking = Docking.LeftOrTop; status.VerticalDocking = Docking.LeftOrTop;
@ -101,8 +111,7 @@ namespace ClassicalSharp {
game.chatInInputBuffer = textInput.chatInputText.ToString(); game.chatInInputBuffer = textInput.chatInputText.ToString();
} }
chatFont.Dispose(); chatFont.Dispose();
chatBoldFont.Dispose(); chatInputFont.Dispose();
historyFont.Dispose();
announcementFont.Dispose(); announcementFont.Dispose();
normalChat.Dispose(); normalChat.Dispose();

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using ClassicalSharp.GraphicsAPI;
using OpenTK.Input; using OpenTK.Input;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -16,9 +17,16 @@ namespace ClassicalSharp {
public override void Render( double delta ) { public override void Render( double delta ) {
if( game.HideGui ) return; if( game.HideGui ) return;
chat.RenderBackground();
graphicsApi.Texturing = true; graphicsApi.Texturing = true;
chat.Render( delta ); chat.Render( delta );
hotbar.Render( delta ); hotbar.Render( delta );
//graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
//graphicsApi.BindTexture( game.TerrainAtlas.TexId );
//IsometricBlockDrawer.Draw( game, (byte)Block.Brick, 30, game.Width - 50, game.Height - 20 );
if( playerList != null ) { if( playerList != null ) {
playerList.Render( delta ); playerList.Render( delta );
// NOTE: Should usually be caught by KeyUp, but just in case. // NOTE: Should usually be caught by KeyUp, but just in case.

View File

@ -1,6 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using ClassicalSharp.Renderers; using ClassicalSharp.GraphicsAPI;
using OpenTK.Input; using OpenTK.Input;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -43,9 +43,8 @@ namespace ClassicalSharp {
background.Render( graphicsApi ); background.Render( graphicsApi );
// TODO: Maybe redesign this so we don't have to bind the whole atlas. Not cheap. // TODO: Maybe redesign this so we don't have to bind the whole atlas. Not cheap.
graphicsApi.BindTexture( game.TerrainAtlas.TexId ); graphicsApi.BindTexture( game.TerrainAtlas.TexId );
graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
bool setFog = game.EnvRenderer is StandardEnvRenderer;
IsometricBlockDrawer.SetupState( graphicsApi, setFog );
for( int i = 0; i < hotbarCount; i++ ) { for( int i = 0; i < hotbarCount; i++ ) {
int x = X + i * blockSize; int x = X + i * blockSize;
IsometricBlockDrawer.Draw( game, (byte)game.Inventory.Hotbar[i], 10, IsometricBlockDrawer.Draw( game, (byte)game.Inventory.Hotbar[i], 10,
@ -54,7 +53,6 @@ namespace ClassicalSharp {
selectedBlock.X1 = x; selectedBlock.X1 = x;
} }
IsometricBlockDrawer.RestoreState( graphicsApi, setFog );
selectedBlock.Render( graphicsApi ); selectedBlock.Render( graphicsApi );
graphicsApi.Texturing = false; graphicsApi.Texturing = false;
} }

View File

@ -77,6 +77,15 @@ namespace ClassicalSharp {
return y; return y;
} }
public int GetUsedHeight() {
int sum = 0;
for( int i = 0; i < textures.Length; i++ ) {
if( textures[i].IsValid )
sum += textures[i].Height;
}
return sum;
}
void UpdateDimensions() { void UpdateDimensions() {
Height = 0; Height = 0;
for( int i = 0; i < textures.Length; i++ ) { for( int i = 0; i < textures.Length; i++ ) {

View File

@ -20,16 +20,14 @@ namespace ClassicalSharp {
public float YawDegrees, PitchDegrees; public float YawDegrees, PitchDegrees;
protected float StepSize; protected float StepSize;
const float deg2Rad = (float)( Math.PI / 180 );
const float rad2Deg = (float)( 180 / Math.PI );
public float YawRadians { public float YawRadians {
get { return YawDegrees * deg2Rad; } get { return YawDegrees * Utils.Deg2Rad; }
set { YawDegrees = value * rad2Deg; } set { YawDegrees = value * Utils.Rad2Deg; }
} }
public float PitchRadians { public float PitchRadians {
get { return PitchDegrees * deg2Rad; } get { return PitchDegrees * Utils.Deg2Rad; }
set { PitchDegrees = value * rad2Deg; } set { PitchDegrees = value * Utils.Rad2Deg; }
} }
public virtual Vector3 CollisionSize { public virtual Vector3 CollisionSize {

View File

@ -229,12 +229,12 @@ namespace ClassicalSharp {
SelectedPos.SetAsInvalid(); SelectedPos.SetAsInvalid();
} }
Graphics.Mode2D( Width, Height ); Graphics.Mode2D( Width, Height, EnvRenderer is StandardEnvRenderer );
fpsScreen.Render( e.Time ); fpsScreen.Render( e.Time );
if( activeScreen != null ) { if( activeScreen != null ) {
activeScreen.Render( e.Time ); activeScreen.Render( e.Time );
} }
Graphics.Mode3D(); Graphics.Mode3D( EnvRenderer is StandardEnvRenderer );
if( screenshotRequested ) if( screenshotRequested )
TakeScreenshot(); TakeScreenshot();

View File

@ -225,7 +225,7 @@ namespace ClassicalSharp.GraphicsAPI {
Draw2DTexture( ref tex, FastColour.White ); Draw2DTexture( ref tex, FastColour.White );
} }
public void Mode2D( float width, float height ) { public void Mode2D( float width, float height, bool setFog ) {
SetMatrixMode( MatrixType.Projection ); SetMatrixMode( MatrixType.Projection );
PushMatrix(); PushMatrix();
DepthTest = false; DepthTest = false;
@ -234,6 +234,7 @@ namespace ClassicalSharp.GraphicsAPI {
PushMatrix(); PushMatrix();
LoadIdentityMatrix(); LoadIdentityMatrix();
AlphaBlending = true; AlphaBlending = true;
if( setFog ) Fog = false;
} }
protected virtual void LoadOrthoMatrix( float width, float height ) { protected virtual void LoadOrthoMatrix( float width, float height ) {
@ -241,7 +242,7 @@ namespace ClassicalSharp.GraphicsAPI {
LoadMatrix( ref matrix ); LoadMatrix( ref matrix );
} }
public void Mode3D() { public void Mode3D( bool setFog ) {
// Get rid of orthographic 2D matrix. // Get rid of orthographic 2D matrix.
SetMatrixMode( MatrixType.Projection ); SetMatrixMode( MatrixType.Projection );
PopMatrix(); PopMatrix();
@ -249,6 +250,7 @@ namespace ClassicalSharp.GraphicsAPI {
PopMatrix(); PopMatrix();
DepthTest = true; DepthTest = true;
AlphaBlending = false; AlphaBlending = false;
if( setFog ) Fog = true;
} }
internal unsafe int MakeDefaultIb() { internal unsafe int MakeDefaultIb() {

View File

@ -36,7 +36,7 @@ namespace ClassicalSharp {
} }
public override Matrix4 GetProjection() { public override Matrix4 GetProjection() {
float fovy = (float)Utils.DegreesToRadians( 70 ); float fovy = 70 * Utils.Deg2Rad;
float aspectRatio = (float)game.Width / game.Height; float aspectRatio = (float)game.Width / game.Height;
float zNear = game.Graphics.MinZNear; float zNear = game.Graphics.MinZNear;
return Matrix4.CreatePerspectiveFieldOfView( fovy, aspectRatio, zNear, game.ViewDistance ); return Matrix4.CreatePerspectiveFieldOfView( fovy, aspectRatio, zNear, game.ViewDistance );
@ -71,12 +71,13 @@ namespace ClassicalSharp {
delta = Point.Empty; delta = Point.Empty;
} }
static readonly float sensiFactor = (float)Utils.RadiansToDegrees( 0.0002f ); static readonly float sensiFactor = 0.0002f * Utils.Rad2Deg;
private void UpdateMouseRotation() { private void UpdateMouseRotation() {
float sensitivity = sensiFactor * game.MouseSensitivity; float sensitivity = sensiFactor * game.MouseSensitivity;
float yaw = player.nextYaw + delta.X * sensitivity; float yaw = player.nextYaw + delta.X * sensitivity;
float pitch = player.nextPitch + delta.Y * sensitivity; float pitch = player.nextPitch + delta.Y * sensitivity;
LocationUpdate update = LocationUpdate.MakeOri( yaw, pitch ); LocationUpdate update = LocationUpdate.MakeOri( yaw, pitch );
// Need to make sure we don't cross the vertical axes, because that gets weird. // Need to make sure we don't cross the vertical axes, because that gets weird.
if( update.Pitch >= 90 && update.Pitch <= 270 ) if( update.Pitch >= 90 && update.Pitch <= 270 )
update.Pitch = player.nextPitch < 180 ? 89.9f : 270.1f; update.Pitch = player.nextPitch < 180 ? 89.9f : 270.1f;

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Text;
using OpenTK; using OpenTK;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -101,13 +100,8 @@ namespace ClassicalSharp {
} }
} }
public static double DegreesToRadians( double degrees ) { public const float Deg2Rad = (float)(Math.PI / 180);
return degrees * Math.PI / 180.0; public const float Rad2Deg = (float)(180 / Math.PI);
}
public static double RadiansToDegrees( double radians ) {
return radians * 180.0 / Math.PI;
}
public static int DegreesToPacked( double degrees, int period ) { public static int DegreesToPacked( double degrees, int period ) {
return (int)(degrees * period / 360.0) % period; return (int)(degrees * period / 360.0) % period;
@ -129,10 +123,16 @@ namespace ClassicalSharp {
return new Vector3( cosA * x - sinA * z, y, sinA * x + cosA * z ); return new Vector3( cosA * x - sinA * z, y, sinA * x + cosA * z );
} }
public static Vector3 RotateX( Vector3 v, float angle ) { public static Vector3 RotateX( Vector3 p, float cosA, float sinA ) {
float cosA = (float)Math.Cos( angle ); return new Vector3( p.X, cosA * p.Y + sinA * p.Z, -sinA * p.Y + cosA * p.Z );
float sinA = (float)Math.Sin( angle ); }
return new Vector3( v.X, cosA * v.Y + sinA * v.Z, -sinA * v.Y + cosA * v.Z );
public static Vector3 RotateY( Vector3 p, float cosA, float sinA ) {
return new Vector3( cosA * p.X - sinA * p.Z, p.Y, sinA * p.X + cosA * p.Z );
}
public static Vector3 RotateZ( Vector3 p, float cosA, float sinA ) {
return new Vector3( cosA * p.X + sinA * p.Y, -sinA * p.X + cosA * p.Y, p.Z );
} }
public static Vector3 RotateX( float x, float y, float z, float cosA, float sinA ) { public static Vector3 RotateX( float x, float y, float z, float cosA, float sinA ) {