Fix entity shadows on Direct3D9 backend. (Thanks goodlyay)

This commit is contained in:
UnknownShadow200 2016-02-18 23:13:19 +11:00
parent f78af8e815
commit 17b695845a
7 changed files with 58 additions and 62 deletions

View file

@ -143,9 +143,17 @@ namespace ClassicalSharp {
public void DrawShadows() {
if( ShadowMode == EntityShadow.None ) return;
Player.boundShadowTex = false;
Players[255].DrawShadow( ShadowMode );
if( ShadowMode != EntityShadow.CircleAll ) return;
game.Graphics.AlphaTest = false;
game.Graphics.AlphaArgBlend = true;
Players[255].DrawShadow( ShadowMode );
if( ShadowMode != EntityShadow.CircleAll )
DrawOtherShadows();
game.Graphics.AlphaTest = true;
game.Graphics.AlphaArgBlend = false;
}
void DrawOtherShadows() {
for( int i = 0; i < 255; i++) {
if( Players[i] == null ) continue;
Players[i].DrawShadow( ShadowMode );

View file

@ -9,7 +9,6 @@ namespace ClassicalSharp {
internal static bool boundShadowTex = false;
internal unsafe void DrawShadow( EntityShadow shadow ) {
if( shadow == EntityShadow.None ) return;
float posX = Position.X, posZ = Position.Z;
int posY = Math.Min( (int)Position.Y, game.Map.Height - 1 );
float y; byte alpha;
@ -62,13 +61,12 @@ namespace ClassicalSharp {
if( index == 0 ) return;
CheckShadowTexture();
game.Graphics.AlphaTest = false;
if( !boundShadowTex ) {
game.Graphics.BindTexture( shadowTex );
boundShadowTex = true;
}
game.Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, vb, verts, index, index * 6 / 4 );
game.Graphics.AlphaTest = true;
}
unsafe bool CalculateShadow( Vector3I* coords, ref int coordsCount, float x, float z, int posY, out float y, out byte alpha ) {
@ -91,7 +89,7 @@ namespace ClassicalSharp {
}
coords[coordsCount] = p; coordsCount++;
if( (Position.Y - posY) <= 6 ) { y += 1/64f; alpha = (byte)(255 - 255 * (Position.Y - posY) / 6); }
if( (Position.Y - posY) <= 6 ) { y += 1/64f; alpha = (byte)(220 - 220 * (Position.Y - posY) / 6); }
else if( (Position.Y - posY) <= 16 ) { y += 1/64f; alpha = 0; }
else if( (Position.Y - posY) <= 32 ) { y += 1/16f; alpha = 0; }
else if( (Position.Y - posY) <= 96 ) { y += 1/8f; alpha = 0; }

View file

@ -213,6 +213,13 @@ namespace ClassicalSharp.GraphicsAPI {
set { depthWrite = value; device.SetRenderState( RenderState.ZWriteEnable, value ); }
}
public override bool AlphaArgBlend {
set {
TextureOp op = value ? TextureOp.Modulate : TextureOp.SelectArg1;
device.SetTextureStageState( 0, TextureStage.AlphaOperation, (int)op );
}
}
#region Vertex buffers
public override int CreateDynamicVb( VertexFormat format, int maxVertices ) {

View file

@ -120,6 +120,9 @@ namespace ClassicalSharp.GraphicsAPI {
/// <summary> Whether writing to the depth buffer is enabled. </summary>
public abstract bool DepthWrite { set; }
/// <summary> Whether blending between the alpha components of the texture and colour are performed. </summary>
public abstract bool AlphaArgBlend { set; }
/// <summary> Creates a vertex buffer that can have its data dynamically updated. </summary>
public abstract int CreateDynamicVb( VertexFormat format, int maxVertices );

View file

@ -18,7 +18,7 @@ namespace ClassicalSharp.GraphicsAPI {
InitFields();
int texDims;
GL.GetIntegerv( GetPName.MaxTextureSize, &texDims );
textureDimensions = texDims;
texDims = texDims;
CheckVboSupport();
base.InitDynamicBuffers();
@ -46,13 +46,9 @@ namespace ClassicalSharp.GraphicsAPI {
GL.UseArbVboAddresses();
}
public override bool AlphaTest {
set { ToggleCap( EnableCap.AlphaTest, value ); }
}
public override bool AlphaTest { set { Toggle( EnableCap.AlphaTest, value ); } }
public override bool AlphaBlending {
set { ToggleCap( EnableCap.Blend, value ); }
}
public override bool AlphaBlending { set { Toggle( EnableCap.Blend, value ); } }
Compare[] compareFuncs;
public override void AlphaTestFunc( CompareFunc func, float value ) {
@ -64,9 +60,7 @@ namespace ClassicalSharp.GraphicsAPI {
GL.BlendFunc( blendFuncs[(int)srcFunc], blendFuncs[(int)dstFunc] );
}
public override bool Fog {
set { ToggleCap( EnableCap.Fog, value ); }
}
public override bool Fog { set { Toggle( EnableCap.Fog, value ); } }
FastColour lastFogCol = FastColour.Black;
public override void SetFogColour( FastColour col ) {
@ -125,31 +119,24 @@ namespace ClassicalSharp.GraphicsAPI {
}
}
public override bool ColourWrite {
set { GL.ColorMask( value, value, value, value ); }
}
public override bool ColourWrite { set { GL.ColorMask( value, value, value, value ); } }
public override void DepthTestFunc( CompareFunc func ) {
GL.DepthFunc( compareFuncs[(int)func] );
}
public override bool DepthTest {
set { ToggleCap( EnableCap.DepthTest, value ); }
}
public override bool DepthTest { set { Toggle( EnableCap.DepthTest, value ); } }
public override bool DepthWrite {
set { GL.DepthMask( value ); }
}
public override bool DepthWrite { set { GL.DepthMask( value ); } }
public override bool AlphaArgBlend { set { } }
#region Texturing
int textureDimensions;
public override int MaxTextureDimensions {
get { return textureDimensions; }
}
public override bool Texturing {
set { ToggleCap( EnableCap.Texture2D, value ); }
}
int texDimensions;
public override int MaxTextureDimensions { get { return texDimensions; } }
public override bool Texturing { set { Toggle( EnableCap.Texture2D, value ); } }
public override int CreateTexture( int width, int height, IntPtr scan0 ) {
if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) )
@ -423,7 +410,7 @@ namespace ClassicalSharp.GraphicsAPI {
GL.Viewport( 0, 0, game.Width, game.Height );
}
static void ToggleCap( EnableCap cap, bool value ) {
static void Toggle( EnableCap cap, bool value ) {
if( value ) GL.Enable( cap );
else GL.Disable( cap );
}

View file

@ -16,7 +16,7 @@ namespace ClassicalSharp.GraphicsAPI {
InitFields();
int texDims;
GL.GetInteger( All.MaxTextureSize, &texDims );
textureDimensions = texDims;
textureDims = texDims;
base.InitDynamicBuffers();
setupBatchFuncCol4b = SetupVbPos3fCol4b;
@ -25,13 +25,9 @@ namespace ClassicalSharp.GraphicsAPI {
GL.EnableClientState( All.ColorArray );
}
public override bool AlphaTest {
set { ToggleCap( All.AlphaTest, value ); }
}
public override bool AlphaTest { set { Toggle( All.AlphaTest, value ); } }
public override bool AlphaBlending {
set { ToggleCap( All.Blend, value ); }
}
public override bool AlphaBlending { set { Toggle( All.Blend, value ); } }
All[] compareFuncs;
public override void AlphaTestFunc( CompareFunc func, float value ) {
@ -43,9 +39,7 @@ namespace ClassicalSharp.GraphicsAPI {
GL.BlendFunc( blendFuncs[(int)srcFunc], blendFuncs[(int)dstFunc] );
}
public override bool Fog {
set { ToggleCap( All.Fog, value ); }
}
public override bool Fog { set { Toggle( All.Fog, value ); } }
FastColour lastFogCol = FastColour.Black;
public override void SetFogColour( FastColour col ) {
@ -104,31 +98,24 @@ namespace ClassicalSharp.GraphicsAPI {
}
}
public override bool ColourWrite {
set { GL.ColorMask( value, value, value, value ); }
public override bool ColourWrite { set { GL.ColorMask( value, value, value, value ); } }
public override void DepthTestFunc( CompareFunc func ) {
GL.DepthFunc( compareFuncs[(int)func] );
}
public override void DepthTestFunc( CompareFunc func ) {
GL.DepthFunc( compareFuncs[(int)func] );
}
public override bool DepthTest { set { Toggle( All.DepthTest, value ); } }
public override bool DepthTest {
set { ToggleCap( All.DepthTest, value ); }
}
public override bool DepthWrite { set { GL.DepthMask( value ); } }
public override bool DepthWrite {
set { GL.DepthMask( value ); }
}
public override bool AlphaArgBlend { set { } }
#region Texturing
int textureDimensions;
public override int MaxTextureDimensions {
get { return textureDimensions; }
}
public override bool Texturing {
set { ToggleCap( All.Texture2D, value ); }
}
int textureDims;
public override int MaxTextureDimensions { get { return textureDims; } }
public override bool Texturing { set { Toggle( All.Texture2D, value ); } }
public override int CreateTexture( int width, int height, IntPtr scan0 ) {
if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) )
@ -411,7 +398,7 @@ namespace ClassicalSharp.GraphicsAPI {
GL.Viewport( 0, 0, game.Width, game.Height );
}
static void ToggleCap( All cap, bool value ) {
static void Toggle( All cap, bool value ) {
if( value ) GL.Enable( cap );
else GL.Disable( cap );
}

View file

@ -414,9 +414,15 @@ namespace SharpDX.Direct3D9 {
}
public enum TextureStage : int {
AlphaOperation = 4,
TextureTransformFlags = 24,
}
public enum TextureOp : int {
SelectArg1 = 2,
Modulate = 4,
}
[Flags]
public enum TextureTransform : int {
Disable = 0,