changed things rendering, updated slimdx

This commit is contained in:
codeimp 2007-10-23 05:08:02 +00:00
parent 827ce5993b
commit 5f74778193
18 changed files with 1930 additions and 1831 deletions

View file

@ -21,7 +21,7 @@ colors
color2 = -1;
color3 = -4259937;
color4 = -3750145;
color5 = -24576;
color5 = -21504;
color6 = -49152;
color7 = -128;
color8 = -11645362;
@ -42,7 +42,7 @@ colors
color23 = -14634326;
color24 = -5103070;
color25 = -7077677;
color26 = -2448096;
color26 = -4684277;
color27 = -4144960;
color28 = -8355712;
color29 = -16728065;
@ -55,7 +55,7 @@ colors
color36 = -18751;
color37 = -29696;
color38 = -4343957;
color39 = -4684277;
color39 = -2448096;
}

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 660 B

After

Width:  |  Height:  |  Size: 618 B

View file

@ -167,7 +167,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rendering\Base2DShader.cs" />
<Compile Include="Rendering\Display2DShader.cs" />
<Compile Include="Rendering\ColorCollection.cs" />
<Compile Include="Rendering\ColorSetting.cs" />
<Compile Include="Rendering\D3DGraphics.cs" />
@ -184,7 +184,7 @@
<Compile Include="Rendering\Things2DShader.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="SlimDX, Version=1.0.2847.35002, Culture=neutral, processorArchitecture=x86" />
<Reference Include="SlimDX, Version=1.0.2851.40166, Culture=neutral, processorArchitecture=x86" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
@ -253,7 +253,7 @@
<DependentUpon>ResourceListEditor.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Actions.cfg" />
<EmbeddedResource Include="Resources\base2d.fx" />
<EmbeddedResource Include="Resources\display2d.fx" />
<EmbeddedResource Include="Resources\Thing2D.png" />
<EmbeddedResource Include="Resources\things2d.fx" />
<None Include="Resources\Splash2.png" />

View file

@ -99,7 +99,7 @@ namespace CodeImp.DoomBuilder.Editing
public unsafe override void RedrawDisplay()
{
// Start with a clear display
if(renderer.StartRendering(true))
if(renderer.StartRendering(true, true))
{
// Render things
renderer.SetThingsRenderOrder(false);
@ -124,7 +124,7 @@ namespace CodeImp.DoomBuilder.Editing
protected void Highlight(Linedef l)
{
// Update display
if(renderer.StartRendering(false))
if(renderer.StartRendering(false, false))
{
// Undraw previous highlight
if(highlighted != null)

View file

@ -97,7 +97,7 @@ namespace CodeImp.DoomBuilder.Editing
public unsafe override void RedrawDisplay()
{
// Start with a clear display
if(renderer.StartRendering(true))
if(renderer.StartRendering(true, true))
{
// Render things
renderer.SetThingsRenderOrder(false);
@ -120,7 +120,7 @@ namespace CodeImp.DoomBuilder.Editing
protected void Highlight(Sector s)
{
// Update display
if(renderer.StartRendering(false))
if(renderer.StartRendering(false, false))
{
// Undraw previous highlight
if(highlighted != null)

View file

@ -99,7 +99,7 @@ namespace CodeImp.DoomBuilder.Editing
public unsafe override void RedrawDisplay()
{
// Start with a clear display
if(renderer.StartRendering(true))
if(renderer.StartRendering(true, true))
{
// Render lines and vertices
renderer.RenderLinedefSet(General.Map.Map.Linedefs);
@ -122,7 +122,7 @@ namespace CodeImp.DoomBuilder.Editing
protected void Highlight(Thing t)
{
// Update display
if(renderer.StartRendering(false))
if(renderer.StartRendering(false, false))
{
// Undraw previous highlight
if(highlighted != null)
@ -146,10 +146,10 @@ namespace CodeImp.DoomBuilder.Editing
base.MouseMove(e);
// Find the nearest vertex within highlight range
//Thing t = General.Map.Map.Nearestt(mousemappos, VERTEX_HIGHLIGHT_RANGE / renderer.Scale);
Thing t = General.Map.Map.NearestThingSquareRange(mousemappos, THING_HIGHLIGHT_RANGE / renderer.Scale);
// Highlight if not the same
//if(t != highlighted) Highlight(v);
if(t != highlighted) Highlight(t);
}
// Mouse leaves

View file

@ -99,7 +99,7 @@ namespace CodeImp.DoomBuilder.Editing
public unsafe override void RedrawDisplay()
{
// Start with a clear display
if(renderer.StartRendering(true))
if(renderer.StartRendering(true, true))
{
// Render things
renderer.SetThingsRenderOrder(false);
@ -122,7 +122,7 @@ namespace CodeImp.DoomBuilder.Editing
protected void Highlight(Vertex v)
{
// Update display
if(renderer.StartRendering(false))
if(renderer.StartRendering(false, false))
{
// Undraw previous highlight
if(highlighted != null)

View file

@ -418,6 +418,38 @@ namespace CodeImp.DoomBuilder.Map
// Return result
return closest;
}
// This finds the thing closest to the specified position
public static Thing NearestThingSquareRange(ICollection<Thing> selection, Vector2D pos, float maxrange)
{
RectangleF range = RectangleF.FromLTRB(pos.x - maxrange, pos.y - maxrange, pos.x + maxrange, pos.y + maxrange);
Thing closest = null;
float distance = float.MaxValue;
float d;
// Go for all vertices in selection
foreach(Thing t in selection)
{
// Within range?
if((t.Position.x >= (range.Left - t.Size)) && (t.Position.x <= (range.Right + t.Size)))
{
if((t.Position.y >= (range.Top - t.Size)) && (t.Position.y <= (range.Bottom + t.Size)))
{
// Close than previous find?
d = Math.Abs(t.Position.x - pos.x) + Math.Abs(t.Position.y - pos.y);
if(d < distance)
{
// This one is closer
closest = t;
distance = d;
}
}
}
}
// Return result
return closest;
}
#endregion
@ -435,6 +467,9 @@ namespace CodeImp.DoomBuilder.Map
// This finds the vertex closest to the specified position
public Vertex NearestVertexSquareRange(Vector2D pos, float maxrange) { return MapSet.NearestVertexSquareRange(vertices, pos, maxrange); }
// This finds the thing closest to the specified position
public Thing NearestThingSquareRange(Vector2D pos, float maxrange) { return MapSet.NearestThingSquareRange(things, pos, maxrange); }
// This performs sidedefs compression
public void CompressSidedefs()
{

View file

@ -153,7 +153,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(colors[THINGCOLOR03].ToInt() == 0) colors[THINGCOLOR03] = PixelColor.FromColor(Color.LightSeaGreen);
if(colors[THINGCOLOR04].ToInt() == 0) colors[THINGCOLOR04] = PixelColor.FromColor(Color.Firebrick);
if(colors[THINGCOLOR05].ToInt() == 0) colors[THINGCOLOR05] = PixelColor.FromColor(Color.DarkViolet);
if(colors[THINGCOLOR06].ToInt() == 0) colors[THINGCOLOR06] = PixelColor.FromColor(Color.Goldenrod);
if(colors[THINGCOLOR06].ToInt() == 0) colors[THINGCOLOR06] = PixelColor.FromColor(Color.DarkGoldenrod);
if(colors[THINGCOLOR07].ToInt() == 0) colors[THINGCOLOR07] = PixelColor.FromColor(Color.Silver);
if(colors[THINGCOLOR08].ToInt() == 0) colors[THINGCOLOR08] = PixelColor.FromColor(Color.Gray);
if(colors[THINGCOLOR09].ToInt() == 0) colors[THINGCOLOR09] = PixelColor.FromColor(Color.DeepSkyBlue);
@ -166,7 +166,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(colors[THINGCOLOR16].ToInt() == 0) colors[THINGCOLOR16] = PixelColor.FromColor(Color.LightPink);
if(colors[THINGCOLOR17].ToInt() == 0) colors[THINGCOLOR17] = PixelColor.FromColor(Color.DarkOrange);
if(colors[THINGCOLOR18].ToInt() == 0) colors[THINGCOLOR18] = PixelColor.FromColor(Color.DarkKhaki);
if(colors[THINGCOLOR19].ToInt() == 0) colors[THINGCOLOR19] = PixelColor.FromColor(Color.DarkGoldenrod);
if(colors[THINGCOLOR19].ToInt() == 0) colors[THINGCOLOR19] = PixelColor.FromColor(Color.Goldenrod);
// Create assist colors
CreateAssistColors();

View file

@ -59,6 +59,8 @@ namespace CodeImp.DoomBuilder.Rendering
private Viewport viewport;
private List<ID3DResource> resources;
private ShaderManager shaders;
private Surface backbuffer;
private Surface depthbuffer;
// Disposing
private bool isdisposed = false;
@ -74,6 +76,8 @@ namespace CodeImp.DoomBuilder.Rendering
public RenderTargetControl RenderTarget { get { return rendertarget; } }
public Viewport Viewport { get { return viewport; } }
public ShaderManager Shaders { get { return shaders; } }
public Surface BackBuffer { get { return backbuffer; } }
public Surface DepthBuffer { get { return depthbuffer; } }
#endregion
@ -117,9 +121,6 @@ namespace CodeImp.DoomBuilder.Rendering
// This completes initialization after the device has started or has been reset
private void SetupSettings()
{
int intvalue;
float floatvalue;
// Setup renderstates
device.SetRenderState(RenderState.AntialiasedLineEnable, false);
device.SetRenderState(RenderState.Ambient, Color.White.ToArgb());
@ -141,15 +142,11 @@ namespace CodeImp.DoomBuilder.Rendering
device.SetRenderState(RenderState.Clipping, true);
device.SetRenderState(RenderState.CullMode, Cull.None);
// Make LOD bias hack until SlimDX has a SetSamplerState overload that accepts a float
floatvalue = -0.99f;
unsafe { General.CopyMemory(&intvalue, &floatvalue, new UIntPtr(4)); }
// Sampler settings
device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear);
device.SetSamplerState(0, SamplerState.MipMapLodBias, intvalue);
device.SetSamplerState(0, SamplerState.MipMapLodBias, 0f);
// Texture addressing
device.SetSamplerState(0, SamplerState.AddressU, TextureAddress.Wrap);
@ -181,6 +178,10 @@ namespace CodeImp.DoomBuilder.Rendering
material.Specular = ColorValue.FromColor(Color.White);
device.Material = material;
// Keep a reference to the original buffers
backbuffer = device.GetBackBuffer(0, 0);
depthbuffer = device.GetDepthStencilSurface();
// Get the viewport
viewport = device.Viewport;
@ -242,7 +243,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Add event to cancel resize event
//device.DeviceResizing += new CancelEventHandler(CancelResize);
// Create renderers
renderer2d = new Renderer2D(this);
renderer3d = new Renderer3D(this);

View file

@ -37,7 +37,7 @@ using System.Drawing.Imaging;
namespace CodeImp.DoomBuilder.Rendering
{
internal sealed class Base2DShader : D3DShader
internal sealed class Display2DShader : D3DShader
{
#region ================== Variables
@ -56,10 +56,10 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Constructor / Disposer
// Constructor
public Base2DShader(ShaderManager manager) : base(manager)
public Display2DShader(ShaderManager manager) : base(manager)
{
// Load effect from file
effect = LoadEffect("base2d.fx");
effect = LoadEffect("display2d.fx");
// Get the property handlers from effect
if(effect != null)
@ -102,9 +102,9 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Methods
// This sets the settings
public void SetSettings(float texelx, float texely, float blendfactor)
public void SetSettings(float texelx, float texely, float fsaafactor, float alpha)
{
Vector4 values = new Vector4(texelx, texely, blendfactor, 0f);
Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha);
if(manager.Enabled) effect.SetValue(settings, values);
}

View file

@ -49,25 +49,35 @@ namespace CodeImp.DoomBuilder.Rendering
private const float THING_CIRCLE_SIZE = 1f;
private const float THING_CIRCLE_SHRINK = 2f;
private const int THING_BUFFER_STEP = 100;
private const byte THINGS_BACK_ALPHA = 80;
private const float THINGS_BACK_ALPHA = 0.4f;
#endregion
#region ================== Variables
// Rendering memory for lines and vertices
private Texture tex;
private int width, height;
private int pwidth, pheight;
private Plotter plotter;
private FlatVertex[] screenverts = new FlatVertex[4];
private LockedRect lockedrect;
// Rendertargets
private Texture structtex;
private Texture thingstex;
// Locking data
private LockedRect structlocked;
private Surface thingssurface;
// Rendertarget sizes
private Size windowsize;
private Size structsize;
private Size thingssize;
// Buffers for rendering things
// Geometry plotter
private Plotter plotter;
// Vertices to present the textures
private FlatVertex[] structverts;
private FlatVertex[] thingsverts;
// Batch buffer for things rendering
private VertexBuffer thingsvertices;
private PixelColor[] thingcolors;
private int numthings;
private int maxthings;
private int maxthings, numthings;
// Render settings
private bool thingsfront;
@ -105,8 +115,8 @@ namespace CodeImp.DoomBuilder.Rendering
thingtexture.LoadImage();
thingtexture.CreateTexture();
// Create texture for rendering lines/vertices
CreateTexture();
// Create rendertargets
CreateRendertargets();
// We have no destructor
GC.SuppressFinalize(this);
@ -118,12 +128,8 @@ namespace CodeImp.DoomBuilder.Rendering
// Not already disposed?
if(!isdisposed)
{
// Clean up
if(tex != null) tex.Dispose();
tex = null;
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
thingtexture.Dispose();
// Destroy rendertargets
DestroyRendertargets();
// Done
base.Dispose();
@ -132,35 +138,39 @@ namespace CodeImp.DoomBuilder.Rendering
#endregion
#region ================== Control
#region ================== Displaying
// This draws the image on screen
public void Present()
{
// Start drawing
if(graphics.StartRendering(General.Colors.Background.ToInt()))
{
// Renderstates that count for this whole sequence
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
// Render things in back?
if(!thingsfront) PresentThings();
if(!thingsfront) PresentThings(THINGS_BACK_ALPHA);
// Set renderstates
graphics.Device.SetTexture(0, tex);
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
graphics.Shaders.Base2D.Texture1 = tex;
graphics.Shaders.Base2D.SetSettings(1f / pwidth, 1f / pheight, FSAA_BLEND_FACTOR);
graphics.Device.SetTexture(0, structtex);
graphics.Shaders.Display2D.Texture1 = structtex;
graphics.Shaders.Display2D.SetSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_BLEND_FACTOR, 1f);
// Draw the lines and vertices texture
graphics.Shaders.Base2D.Begin();
graphics.Shaders.Base2D.BeginPass(0);
try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, screenverts); } catch(Exception) { }
graphics.Shaders.Base2D.EndPass();
graphics.Shaders.Base2D.End();
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0);
try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts); } catch(Exception) { }
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
// Render things in front?
if(thingsfront) PresentThings();
if(thingsfront) PresentThings(1f);
// Done
graphics.FinishRendering();
@ -168,52 +178,47 @@ namespace CodeImp.DoomBuilder.Rendering
}
// This presents the things
private void PresentThings()
private void PresentThings(float alpha)
{
// Do we have things to render?
if((numthings > 0) && (thingsvertices != null))
{
// Set renderstates
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
graphics.Device.SetTexture(0, thingtexture.Texture);
graphics.Shaders.Things2D.Texture1 = thingtexture.Texture;
// Set renderstates
//graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
//graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
//graphics.Device.SetRenderState(RenderState.AlphaFunc, Compare.GreaterEqual);
//graphics.Device.SetRenderState(RenderState.AlphaRef, 0x0000007F);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
graphics.Device.SetTexture(0, thingstex);
graphics.Shaders.Display2D.Texture1 = thingstex;
graphics.Shaders.Display2D.SetSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_BLEND_FACTOR, alpha);
// Set the vertex buffer
graphics.Device.SetStreamSource(0, thingsvertices, 0, FlatVertex.Stride);
// Draw the thing circle
graphics.Shaders.Things2D.Begin();
graphics.Shaders.Things2D.BeginPass(0);
try { graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 4 * numthings); } catch(Exception) { }
graphics.Shaders.Things2D.EndPass();
graphics.Shaders.Things2D.End();
}
// Draw the lines and vertices texture
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0);
try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts); } catch(Exception) { }
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
#endregion
#region ================== Management
// This is called before a device is reset
// (when resized or display adapter was changed)
public override void UnloadResource()
{
// Trash old texture
if(tex != null) tex.Dispose();
tex = null;
// Trash things buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
maxthings = 0;
numthings = 0;
// Destroy rendertargets
DestroyRendertargets();
}
// This is called resets when the device is reset
// (when resized or display adapter was changed)
public override void ReloadResource()
{
// Re-create texture
CreateTexture();
// Re-create rendertargets
CreateRendertargets();
}
// This resets the graphics
@ -223,97 +228,86 @@ namespace CodeImp.DoomBuilder.Rendering
ReloadResource();
}
// This destroys the rendertargets
private void DestroyRendertargets()
{
// Trash rendertargets
if(structtex != null) structtex.Dispose();
if(thingstex != null) thingstex.Dispose();
structtex = null;
thingstex = null;
// Trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
numthings = 0;
maxthings = 0;
}
// Allocates new image memory to render on
public void CreateTexture()
public void CreateRendertargets()
{
SurfaceDescription sd;
// Destroy rendertargets
DestroyRendertargets();
// Get new width and height
width = graphics.RenderTarget.ClientSize.Width;
height = graphics.RenderTarget.ClientSize.Height;
// Trash old texture
if(tex != null) tex.Dispose();
tex = null;
// Create new texture
tex = new Texture(graphics.Device, width, height, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
windowsize.Width = graphics.RenderTarget.ClientSize.Width;
windowsize.Height = graphics.RenderTarget.ClientSize.Height;
// Get the real surface size
sd = tex.GetLevelDescription(0);
pwidth = sd.Width;
pheight = sd.Height;
// Create rendertargets textures
structtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
thingstex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
// Get the real surface sizes
sd = structtex.GetLevelDescription(0);
structsize.Width = sd.Width;
structsize.Height = sd.Height;
sd = thingstex.GetLevelDescription(0);
thingssize.Width = sd.Width;
thingssize.Height = sd.Height;
// Setup screen vertices
structverts = CreateScreenVerts(structsize);
thingsverts = CreateScreenVerts(thingssize);
}
// This makes screen vertices for display
private FlatVertex[] CreateScreenVerts(Size texturesize)
{
FlatVertex[] screenverts = new FlatVertex[4];
screenverts[0].x = 0.5f;
screenverts[0].y = 0.5f;
screenverts[0].w = 1f;
screenverts[0].c = -1;
screenverts[0].u = 1f / pwidth;
screenverts[0].v = 1f / pheight;
screenverts[1].x = pwidth - 1.5f;
screenverts[0].u = 1f / texturesize.Width;
screenverts[0].v = 1f / texturesize.Height;
screenverts[1].x = texturesize.Width - 1.5f;
screenverts[1].y = 0.5f;
screenverts[1].w = 1f;
screenverts[1].c = -1;
screenverts[1].u = 1f - 1f / pwidth;
screenverts[1].v = 1f / pheight;
screenverts[1].u = 1f - 1f / texturesize.Width;
screenverts[1].v = 1f / texturesize.Height;
screenverts[2].x = 0.5f;
screenverts[2].y = pheight - 1.5f;
screenverts[2].y = texturesize.Height - 1.5f;
screenverts[2].c = -1;
screenverts[2].w = 1f;
screenverts[2].u = 1f / pwidth;
screenverts[2].v = 1f - 1f / pheight;
screenverts[3].x = pwidth - 1.5f;
screenverts[3].y = pheight - 1.5f;
screenverts[2].u = 1f / texturesize.Width;
screenverts[2].v = 1f - 1f / texturesize.Height;
screenverts[3].x = texturesize.Width - 1.5f;
screenverts[3].y = texturesize.Height - 1.5f;
screenverts[3].w = 1f;
screenverts[3].c = -1;
screenverts[3].u = 1f - 1f / pwidth;
screenverts[3].v = 1f - 1f / pheight;
screenverts[3].u = 1f - 1f / texturesize.Width;
screenverts[3].v = 1f - 1f / texturesize.Height;
return screenverts;
}
// This begins a drawing session
public unsafe bool StartRendering(bool cleardisplay)
{
LockFlags lockflags;
// Do we have a texture?
if(tex != null)
{
// Determine lock requirements
if(cleardisplay) lockflags = LockFlags.Discard | LockFlags.NoSystemLock;
else lockflags = LockFlags.NoSystemLock;
// Lock memory
lockedrect = tex.LockRectangle(0, lockflags);
// Create plotter
plotter = new Plotter((PixelColor*)lockedrect.Data.DataPointer.ToPointer(), lockedrect.Pitch / sizeof(PixelColor), pheight, width, height);
if(cleardisplay) plotter.Clear();
// Reset things buffer when display is cleared
if(cleardisplay) ReserveThingsMemory(0, false);
// Ready for rendering
return true;
}
else
{
// Can't render!
return false;
}
}
// This ends a drawing session
public void FinishRendering()
{
// Unlock memory
tex.UnlockRectangle(0);
lockedrect.Data.Dispose();
// Present new image
Present();
}
#endregion
#region ================== Coordination
// This changes view position
public void PositionView(float x, float y)
{
@ -341,8 +335,8 @@ namespace CodeImp.DoomBuilder.Rendering
private void UpdateTransformations()
{
scaleinv = 1f / scale;
translatex = -offsetx + (width * 0.5f) * scaleinv;
translatey = -offsety - (height * 0.5f) * scaleinv;
translatex = -offsetx + (windowsize.Width * 0.5f) * scaleinv;
translatey = -offsety - (windowsize.Height * 0.5f) * scaleinv;
linenormalsize = 10f * scaleinv;
vertexsize = (int)(1.7f * scale + 0.5f);
if(vertexsize < 0) vertexsize = 0;
@ -365,7 +359,6 @@ namespace CodeImp.DoomBuilder.Rendering
int newmaxthings;
DataStream stream;
FlatVertex[] verts = null;
PixelColor[] oldcolors = null;
// Do we need to make changes?
if((newnumthings > maxthings) || !preserve)
@ -377,7 +370,6 @@ namespace CodeImp.DoomBuilder.Rendering
verts = stream.ReadRange<FlatVertex>(numthings * 12);
thingsvertices.Unlock();
stream.Dispose();
oldcolors = thingcolors;
}
// Buffer needs to be reallocated?
@ -391,7 +383,6 @@ namespace CodeImp.DoomBuilder.Rendering
// Create new buffer
thingsvertices = new VertexBuffer(graphics.Device, newmaxthings * 12 * FlatVertex.Stride, Usage.Dynamic, VertexFormat.None, Pool.Default);
thingcolors = new PixelColor[newmaxthings];
maxthings = newmaxthings;
}
else
@ -408,7 +399,6 @@ namespace CodeImp.DoomBuilder.Rendering
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
oldcolors.CopyTo(thingcolors, 0);
}
else
{
@ -434,11 +424,11 @@ namespace CodeImp.DoomBuilder.Rendering
arrowsize = (t.Size - THING_ARROW_SHRINK) * scale * THING_ARROW_SIZE;
// Check if the thing is actually on screen
if(((screenpos.x + circlesize) > 0.0f) && ((screenpos.x - circlesize) < (float)width) &&
((screenpos.y + circlesize) > 0.0f) && ((screenpos.x - circlesize) < (float)height))
if(((screenpos.x + circlesize) > 0.0f) && ((screenpos.x - circlesize) < (float)windowsize.Width) &&
((screenpos.y + circlesize) > 0.0f) && ((screenpos.y - circlesize) < (float)windowsize.Height))
{
// Determine color and alpha
if(thingsfront) color = c.ToInt(); else color = c.WithAlpha(THINGS_BACK_ALPHA).ToInt();
// Get integral color
color = c.ToInt();
// Setup fixed rect for circle
verts[offset].x = screenpos.x - circlesize;
@ -517,6 +507,26 @@ namespace CodeImp.DoomBuilder.Rendering
}
}
// This draws a set of things
private void RenderThingsBatch(int offset, int count)
{
// Set renderstates for things rendering
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaFunc, Compare.GreaterEqual);
graphics.Device.SetRenderState(RenderState.AlphaRef, 0x0000007F);
graphics.Device.SetTexture(0, thingtexture.Texture);
graphics.Shaders.Things2D.Texture1 = thingtexture.Texture;
graphics.Device.SetStreamSource(0, thingsvertices, 0, FlatVertex.Stride);
// Draw the things batched
graphics.Shaders.Things2D.Begin();
graphics.Shaders.Things2D.BeginPass(0);
try { graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, offset * 12, count * 4); } catch(Exception) { }
graphics.Shaders.Things2D.EndPass();
graphics.Shaders.Things2D.End();
}
#endregion
#region ================== Colors
@ -574,6 +584,71 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Rendering
// This begins a drawing session
public unsafe bool StartRendering(bool clearstructs, bool clearthings)
{
LockFlags lockflags;
// Rendertargets available?
if((structtex != null) && (thingstex != null))
{
// Determine lock requirements
if(clearstructs) lockflags = LockFlags.Discard | LockFlags.NoSystemLock;
else lockflags = LockFlags.NoSystemLock;
// Lock structures rendertarget memory
structlocked = structtex.LockRectangle(0, lockflags);
// Create structures plotter
plotter = new Plotter((PixelColor*)structlocked.Data.DataPointer.ToPointer(), structlocked.Pitch / sizeof(PixelColor), structsize.Height, structsize.Width, structsize.Height);
if(clearstructs) plotter.Clear();
// Set the rendertarget to the things texture
thingssurface = thingstex.GetSurfaceLevel(0);
graphics.Device.SetDepthStencilSurface(null);
graphics.Device.SetRenderTarget(0, thingssurface);
// Clear the things?
if(clearthings)
{
// Clear rendertarget
graphics.Device.Clear(ClearFlags.Target, 0, 1f, 0);
}
// Always trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
numthings = 0;
maxthings = 0;
// Ready for rendering
return true;
}
else
{
// Can't render!
return false;
}
}
// This ends a drawing session
public void FinishRendering()
{
// Unlock memory
structtex.UnlockRectangle(0);
structlocked.Data.Dispose();
plotter = null;
// Release rendertarget
graphics.Device.SetDepthStencilSurface(graphics.DepthBuffer);
graphics.Device.SetRenderTarget(0, graphics.BackBuffer);
thingssurface.Dispose();
thingssurface = null;
// Present new image
Present();
}
// This adds a thing in the things buffer for rendering
public void RenderThing(Thing t, PixelColor c)
{
@ -592,7 +667,8 @@ namespace CodeImp.DoomBuilder.Rendering
thingsvertices.Unlock();
stream.Dispose();
// Thing added!
// Thing added, render it
RenderThingsBatch(numthings, 1);
numthings++;
}
}
@ -602,7 +678,7 @@ namespace CodeImp.DoomBuilder.Rendering
{
FlatVertex[] verts = new FlatVertex[things.Count * 12];
DataStream stream;
int offset = 0;
int addcount = 0;
// Make sure there is enough memory reserved
ReserveThingsMemory(numthings + things.Count, true);
@ -611,10 +687,10 @@ namespace CodeImp.DoomBuilder.Rendering
foreach(Thing t in things)
{
// Create vertices
if(CreateThingVerts(t, ref verts, offset * 12, DetermineThingColor(t)))
if(CreateThingVerts(t, ref verts, addcount * 12, DetermineThingColor(t)))
{
// Next
offset++;
addcount++;
}
}
@ -624,8 +700,9 @@ namespace CodeImp.DoomBuilder.Rendering
thingsvertices.Unlock();
stream.Dispose();
// Things added!
numthings += offset;
// Things added, render them
RenderThingsBatch(numthings, addcount);
numthings += addcount;
}
// This renders the linedefs of a sector with special color

View file

@ -50,7 +50,7 @@ namespace CodeImp.DoomBuilder.Rendering
private bool useshaders;
// Shaders
private Base2DShader base2dshader;
private Display2DShader display2dshader;
private Things2DShader things2dshader;
// Disposing
@ -62,7 +62,7 @@ namespace CodeImp.DoomBuilder.Rendering
public bool Enabled { get { return useshaders; } }
public string ShaderTechnique { get { return shadertechnique; } }
public Base2DShader Base2D { get { return base2dshader; } }
public Display2DShader Display2D { get { return display2dshader; } }
public Things2DShader Things2D { get { return things2dshader; } }
public bool IsDisposed { get { return isdisposed; } }
@ -81,7 +81,7 @@ namespace CodeImp.DoomBuilder.Rendering
shadertechnique = "SM20";
// Initialize effects
base2dshader = new Base2DShader(this);
display2dshader = new Display2DShader(this);
things2dshader = new Things2DShader(this);
// We have no destructor
@ -95,7 +95,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(!isdisposed)
{
// Clean up
base2dshader.Dispose();
display2dshader.Dispose();
things2dshader.Dispose();
// Done

Binary file not shown.

Before

Width:  |  Height:  |  Size: 660 B

After

Width:  |  Height:  |  Size: 618 B

View file

@ -1,4 +1,4 @@
// Base 2D rendering shader
// 2D display rendering shader
// Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
// Pixel input data
@ -12,7 +12,8 @@ struct PixelData
// Settings
// x = texel width
// y = texel height
// z = blend factor
// z = FSAA blend factor
// w = transparency
float4 settings;
// Texture1 input
@ -60,9 +61,9 @@ float4 ps_main(PixelData pd) : COLOR
// If any pixels nearby where found, return a blend, otherwise return nothing
//if(n.a > 0.1f) return float4(n.rgb, n.a * settings.z); else return (float4)0;
return float4(n.rgb, n.a * settings.z);
return float4(n.rgb, n.a * settings.z * settings.w);
}
else return c;
else return float4(c.rgb, c.a * settings.w);
}
// Technique for shader model 2.0
@ -72,10 +73,5 @@ technique SM20
{
VertexShader = null;
PixelShader = compile ps_2_0 ps_main();
CullMode = None;
ZEnable = false;
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
}
}

View file

@ -50,10 +50,5 @@ technique SM20
{
VertexShader = null;
PixelShader = compile ps_2_0 ps_circle();
CullMode = None;
ZEnable = false;
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
}
}