UltimateZoneBuilder/Source/Rendering/Display2DShader.cs

128 lines
3.7 KiB
C#
Raw Normal View History

2007-10-19 17:05:21 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.ComponentModel;
using CodeImp.DoomBuilder.Map;
using SlimDX.Direct3D9;
using SlimDX;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing.Imaging;
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
2008-01-02 21:49:43 +00:00
internal sealed class Display2DShader : D3DShader
2007-10-19 17:05:21 +00:00
{
#region ================== Variables
// Property handlers
private EffectHandle texture1;
2008-05-18 11:43:28 +00:00
private EffectHandle rendersettings;
private EffectHandle transformsettings;
private EffectHandle filtersettings;
2007-10-19 17:05:21 +00:00
#endregion
#region ================== Properties
public Texture Texture1 { set { if(manager.Enabled) effect.SetTexture(texture1, value); } }
2007-10-19 17:05:21 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public Display2DShader(ShaderManager manager) : base(manager)
2007-10-19 17:05:21 +00:00
{
// Load effect from file
effect = LoadEffect("display2d.fx");
2007-10-19 17:05:21 +00:00
// Get the property handlers from effect
if(effect != null)
{
texture1 = effect.GetParameter(null, "texture1");
2008-05-18 11:43:28 +00:00
rendersettings = effect.GetParameter(null, "rendersettings");
transformsettings = effect.GetParameter(null, "transformsettings");
filtersettings = effect.GetParameter(null, "filtersettings");
2007-10-19 17:05:21 +00:00
}
// Initialize world vertex declaration
VertexElement[] elements = new VertexElement[]
{
2008-05-18 11:43:28 +00:00
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
new VertexElement(0, 16, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
2007-10-19 17:05:21 +00:00
VertexElement.VertexDeclarationEnd
};
vertexdecl = new VertexDeclaration(General.Map.Graphics.Device, elements);
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-10-19 17:05:21 +00:00
public override void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
if(texture1 != null) texture1.Dispose();
2008-05-18 11:43:28 +00:00
if(rendersettings != null) rendersettings.Dispose();
if(transformsettings != null) transformsettings.Dispose();
if(filtersettings != null) filtersettings.Dispose();
2007-10-19 17:05:21 +00:00
// Done
base.Dispose();
}
}
#endregion
#region ================== Methods
// This sets the settings
public void SetSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear)
2007-10-19 17:05:21 +00:00
{
2008-05-18 11:43:28 +00:00
if(manager.Enabled)
{
Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha);
effect.SetValue(rendersettings, values);
Matrix world = manager.D3DDevice.Device.GetTransform(TransformState.World);
Matrix view = manager.D3DDevice.Device.GetTransform(TransformState.View);
effect.SetValue(transformsettings, Matrix.Multiply(world, view));
TextureFilter filter = TextureFilter.Point;
if(bilinear) filter = TextureFilter.Linear;
effect.SetValue<int>(filtersettings, (int)filter);
2008-05-18 11:43:28 +00:00
}
2007-10-19 17:05:21 +00:00
}
#endregion
}
}