UltimateZoneBuilder/Source/Rendering/Renderer2D.cs

645 lines
19 KiB
C#
Raw Normal View History

2007-06-15 18:50:18 +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;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Map;
using SlimDX.Direct3D;
using SlimDX.Direct3D9;
using SlimDX;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing.Imaging;
2007-10-21 04:07:36 +00:00
using CodeImp.DoomBuilder.Data;
2007-06-15 18:50:18 +00:00
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
2007-09-24 19:54:47 +00:00
internal unsafe class Renderer2D : Renderer
2007-06-15 18:50:18 +00:00
{
#region ================== Constants
2007-10-20 12:34:27 +00:00
private const byte DOUBLESIDED_LINE_ALPHA = 130;
private const float FSAA_BLEND_FACTOR = 0.6f;
2007-10-21 04:07:36 +00:00
private const float THING_ARROW_SIZE = 15f;
private const float THING_CIRCLE_SIZE = 10f;
private const int THING_BUFFER_STEP = 100;
2007-10-20 12:34:27 +00:00
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Variables
2007-10-21 04:07:36 +00:00
// Rendering memory for lines and vertices
private Texture tex;
private int width, height;
private int pwidth, pheight;
private Plotter plotter;
2007-10-21 04:07:36 +00:00
private FlatVertex[] screenverts = new FlatVertex[4];
private LockedRect lockedrect;
// Buffers for rendering things
private VertexBuffer thingsvertices;
private PixelColor[] thingcolors;
private int numthings;
private int maxthings;
// Images
private ResourceImage thingtexture;
2007-06-24 18:56:43 +00:00
// View settings (world coordinates)
private float scale;
2007-10-20 19:50:03 +00:00
private float scaleinv;
2007-06-24 18:56:43 +00:00
private float offsetx;
private float offsety;
2007-10-20 19:50:03 +00:00
private float translatex;
private float translatey;
private float linenormalsize;
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Properties
2007-06-24 18:56:43 +00:00
public float OffsetX { get { return offsetx; } }
public float OffsetY { get { return offsety; } }
public float Scale { get { return scale; } }
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Constructor / Disposer
2007-06-15 18:50:18 +00:00
// Constructor
public Renderer2D(D3DGraphics graphics) : base(graphics)
2007-06-15 18:50:18 +00:00
{
// Initialize
2007-10-21 04:07:36 +00:00
thingtexture = new ResourceImage("Thing2D.png");
thingtexture.LoadImage();
thingtexture.CreateTexture();
// Create texture for rendering lines/vertices
CreateTexture();
2007-06-15 18:50:18 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
2007-09-24 19:54:47 +00:00
public override void Dispose()
2007-06-15 18:50:18 +00:00
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
if(tex != null) tex.Dispose();
tex = null;
2007-10-21 04:07:36 +00:00
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
thingtexture.Dispose();
2007-06-15 22:38:42 +00:00
2007-06-15 18:50:18 +00:00
// Done
2007-09-24 19:54:47 +00:00
base.Dispose();
2007-06-15 18:50:18 +00:00
}
}
#endregion
#region ================== Control
2007-06-24 18:56:43 +00:00
// This draws the image on screen
public void Present()
{
// Start drawing
if(graphics.StartRendering(General.Colors.Background.PixelColor.ToInt()))
{
2007-10-21 04:07:36 +00:00
// Set renderstates
graphics.Device.SetTexture(0, tex);
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
2007-10-20 12:34:27 +00:00
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
2007-10-21 04:07:36 +00:00
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
2007-10-19 17:05:21 +00:00
graphics.Shaders.Base2D.Texture1 = tex;
2007-10-20 12:34:27 +00:00
graphics.Shaders.Base2D.SetSettings(1f / pwidth, 1f / pheight, FSAA_BLEND_FACTOR);
2007-10-19 17:05:21 +00:00
2007-10-21 04:07:36 +00:00
// Draw the lines and vertices texture
2007-10-19 17:05:21 +00:00
graphics.Shaders.Base2D.Begin();
graphics.Shaders.Base2D.BeginPass(0);
2007-10-21 04:07:36 +00:00
try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, screenverts); } catch(Exception) { }
2007-10-19 17:05:21 +00:00
graphics.Shaders.Base2D.EndPass();
graphics.Shaders.Base2D.End();
2007-10-21 04:07:36 +00:00
// 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 the vertex buffer
graphics.Device.SetStreamSource(0, thingsvertices, 0, FlatVertex.Stride);
// Go for all things
for(int i = 0; i < numthings; i++)
{
// Set renderstates
graphics.Device.SetRenderState(RenderState.TextureFactor, thingcolors[i].ToInt());
graphics.Shaders.Things2D.SetColors(thingcolors[i]);
// Draw the thing circle
graphics.Shaders.Things2D.Begin();
graphics.Shaders.Things2D.BeginPass(0);
try { graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, i * 8, 2); } catch(Exception) { }
graphics.Shaders.Things2D.EndPass();
// Draw the thing icon
graphics.Shaders.Things2D.BeginPass(1);
try { graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, i * 8 + 4, 2); } catch(Exception) { }
graphics.Shaders.Things2D.EndPass();
graphics.Shaders.Things2D.End();
}
}
// Done
graphics.FinishRendering();
}
}
// 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;
2007-10-21 04:07:36 +00:00
// Trash things buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
maxthings = 0;
numthings = 0;
}
// This is called resets when the device is reset
// (when resized or display adapter was changed)
public override void ReloadResource()
2007-06-24 18:56:43 +00:00
{
// Re-create texture
CreateTexture();
}
2007-09-24 19:54:47 +00:00
// This resets the graphics
public override void Reset()
{
UnloadResource();
ReloadResource();
2007-06-24 18:56:43 +00:00
}
// Allocates new image memory to render on
public void CreateTexture()
2007-06-15 22:38:42 +00:00
{
SurfaceDescription sd;
// 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);
// Get the real surface size
sd = tex.GetLevelDescription(0);
pwidth = sd.Width;
pheight = sd.Height;
2007-10-21 04:07:36 +00:00
// Setup screen vertices
screenverts[0].x = 0.5f;
screenverts[0].y = 0.5f;
screenverts[0].w = 1f;
screenverts[0].u = 1f / pwidth;
screenverts[0].v = 1f / pheight;
screenverts[1].x = pwidth - 1.5f;
screenverts[1].y = 0.5f;
screenverts[1].w = 1f;
screenverts[1].u = 1f - 1f / pwidth;
screenverts[1].v = 1f / pheight;
screenverts[2].x = 0.5f;
screenverts[2].y = pheight - 1.5f;
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[3].w = 1f;
screenverts[3].u = 1f - 1f / pwidth;
screenverts[3].v = 1f - 1f / pheight;
}
2007-06-24 18:56:43 +00:00
// This begins a drawing session
public unsafe bool StartRendering(bool cleardisplay)
{
2007-10-20 19:50:03 +00:00
LockFlags lockflags;
// Do we have a texture?
if(tex != null)
{
2007-10-20 19:50:03 +00:00
// Determine lock requirements
if(cleardisplay) lockflags = LockFlags.Discard | LockFlags.NoSystemLock;
else lockflags = LockFlags.NoSystemLock;
// Lock memory
2007-10-21 04:07:36 +00:00
lockedrect = tex.LockRectangle(0, lockflags);
// Create plotter
2007-10-21 04:07:36 +00:00
plotter = new Plotter((PixelColor*)lockedrect.Data.DataPointer.ToPointer(), lockedrect.Pitch / sizeof(PixelColor), pheight, width, height);
if(cleardisplay) plotter.Clear();
2007-10-21 04:07:36 +00:00
// Reset things buffer when display is cleared
if(cleardisplay) ReserveThingsMemory(0, false);
// Ready for rendering
return true;
}
else
{
// Can't render!
return false;
}
2007-06-15 22:38:42 +00:00
}
// This ends a drawing session
public void FinishRendering()
{
// Unlock memory
tex.UnlockRectangle(0);
2007-10-21 04:07:36 +00:00
lockedrect.Data.Dispose();
// Present new image
Present();
2007-06-15 22:38:42 +00:00
}
2007-06-24 18:56:43 +00:00
// This changes view position
public void PositionView(float x, float y)
{
// Change position in world coordinates
offsetx = x;
offsety = y;
2007-10-20 19:50:03 +00:00
UpdateTransformations();
2007-06-24 18:56:43 +00:00
}
// This changes zoom
public void ScaleView(float scale)
{
// Change zoom scale
this.scale = scale;
2007-10-20 19:50:03 +00:00
UpdateTransformations();
2007-09-24 19:54:47 +00:00
// Show zoom on main window
General.MainWindow.UpdateZoom(scale);
// Recalculate linedefs (normal lengths must be adjusted)
2007-10-05 11:17:58 +00:00
foreach(Linedef l in General.Map.Map.Linedefs) l.NeedUpdate();
2007-06-24 18:56:43 +00:00
}
2007-10-20 19:50:03 +00:00
// This updates some maths
private void UpdateTransformations()
{
scaleinv = 1f / scale;
translatex = -offsetx + (width * 0.5f) * scaleinv;
translatey = -offsety - (height * 0.5f) * scaleinv;
linenormalsize = 10f * scaleinv;
}
// This unprojects mouse coordinates into map coordinates
public Vector2D GetMapCoordinates(Vector2D mousepos)
2007-06-15 22:38:42 +00:00
{
2007-10-20 19:50:03 +00:00
return mousepos.GetInvTransformed(-translatex, -translatey, scaleinv, -scaleinv);
}
#endregion
2007-10-21 04:07:36 +00:00
#region ================== Things
// This ensures there is enough place in the things buffer
private void ReserveThingsMemory(int newnumthings, bool preserve)
{
int newmaxthings;
DataStream stream;
FlatVertex[] verts = null;
PixelColor[] oldcolors = null;
// Do we need to resize the buffer?
if((newnumthings > maxthings) || !preserve)
{
// Calculate new size
newmaxthings = newnumthings + THING_BUFFER_STEP;
// Read old things data if we want to keep it
if(preserve && (thingsvertices != null) && (numthings > 0))
{
stream = thingsvertices.Lock(0, numthings * 8 * FlatVertex.Stride, LockFlags.ReadOnly);
verts = stream.ReadRange<FlatVertex>(numthings * 8);
thingsvertices.Unlock();
stream.Dispose();
thingsvertices.Dispose();
oldcolors = thingcolors;
}
// Create new buffer
thingsvertices = new VertexBuffer(graphics.Device, newmaxthings * 8 * FlatVertex.Stride, Usage.Dynamic, VertexFormat.None, Pool.Default);
thingcolors = new PixelColor[newmaxthings];
maxthings = newmaxthings;
// Keep old things?
if(preserve && (verts != null))
{
// Write old things into new buffer
stream = thingsvertices.Lock(0, maxthings * 8 * FlatVertex.Stride, LockFlags.Discard);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
oldcolors.CopyTo(thingcolors, 0);
}
else
{
// Things were trashed
numthings = 0;
}
}
}
// This makes vertices for a thing
private void CreateThingVerts(Thing t, ref FlatVertex[] verts, int offset)
{
// Transform to screen coordinates
Vector2D screenpos = ((Vector2D)t.Position).GetTransformed(translatex, translatey, scale, -scale);
// Setup fixed rect for circle
verts[offset].x = screenpos.x - THING_CIRCLE_SIZE;
verts[offset].y = screenpos.y - THING_CIRCLE_SIZE;
verts[offset].w = 1f;
verts[offset].u = 1f / 512f;
verts[offset].v = 1f / 128f;
offset++;
verts[offset].x = screenpos.x + THING_CIRCLE_SIZE;
verts[offset].y = screenpos.y - THING_CIRCLE_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.25f - 1f / 512f;
verts[offset].v = 1f / 128f;
offset++;
verts[offset].x = screenpos.x - THING_CIRCLE_SIZE;
verts[offset].y = screenpos.y + THING_CIRCLE_SIZE;
verts[offset].w = 1f;
verts[offset].u = 1f / 512f;
verts[offset].v = 1f - 1f / 128f;
offset++;
verts[offset].x = screenpos.x + THING_CIRCLE_SIZE;
verts[offset].y = screenpos.y + THING_CIRCLE_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.25f - 1f / 512f;
verts[offset].v = 1f - 1f / 128f;
offset++;
// Setup rotated rect for arrow
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle - Angle2D.PI * 0.25f) * THING_ARROW_SIZE;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle - Angle2D.PI * 0.25f) * THING_ARROW_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.50f;
verts[offset].v = 0f;
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle + Angle2D.PI * 0.25f) * THING_ARROW_SIZE;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle + Angle2D.PI * 0.25f) * THING_ARROW_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.75f;
verts[offset].v = 0f;
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle - Angle2D.PI * 0.75f) * THING_ARROW_SIZE;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle - Angle2D.PI * 0.75f) * THING_ARROW_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.50f;
verts[offset].v = 1f;
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle + Angle2D.PI * 0.75f) * THING_ARROW_SIZE;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle + Angle2D.PI * 0.75f) * THING_ARROW_SIZE;
verts[offset].w = 1f;
verts[offset].u = 0.75f;
verts[offset].v = 1f;
}
#endregion
2007-10-20 19:50:03 +00:00
#region ================== Colors
2007-10-21 04:07:36 +00:00
// This returns the color for a thing
public PixelColor DetermineThingColor(Thing t)
{
// Determine color
if(t.Selected > 0) return General.Colors.Selection;
else return PixelColor.FromColor(Color.Tomato);
// TODO: Check against game configuration or embed color into thing
}
2007-10-20 19:50:03 +00:00
// This returns the color for a vertex
2007-10-21 12:58:56 +00:00
public string DetermineVertexColor(Vertex v)
2007-10-20 19:50:03 +00:00
{
// Determine color
2007-10-21 12:58:56 +00:00
if(v.Selected > 0) return "selection";
else return "vertices";
2007-10-20 19:50:03 +00:00
}
// This returns the color for a linedef
public PixelColor DetermineLinedefColor(Linedef l)
{
// Sinlgesided lines
if((l.Back == null) || (l.Front == null))
{
// Determine color
if(l.Selected > 0) return General.Colors.Selection;
else if(l.Action != 0) return General.Colors.Actions;
else return General.Colors.Linedefs;
}
// Doublesided lines
else
{
// Determine color
if(l.Selected > 0) return General.Colors.Selection;
else if(l.Action != 0) return General.Colors.Actions.WithAlpha(DOUBLESIDED_LINE_ALPHA);
else if((l.Flags & General.Map.Settings.SoundLinedefFlags) != 0) return General.Colors.Sounds.WithAlpha(DOUBLESIDED_LINE_ALPHA);
else return General.Colors.Linedefs.WithAlpha(DOUBLESIDED_LINE_ALPHA);
}
}
#endregion
#region ================== Map Rendering
2007-10-21 04:07:36 +00:00
// This adds a thing in the things buffer for rendering
public void RenderThing(Thing t, PixelColor c)
{
FlatVertex[] verts = new FlatVertex[8];
DataStream stream;
// TODO: Check if the thing is actually on screen
// Make sure there is enough memory reserved
ReserveThingsMemory(numthings + 1, true);
// Store the thing color
thingcolors[numthings] = c;
// Store vertices in buffer
stream = thingsvertices.Lock(numthings * 8 * FlatVertex.Stride, 8 * FlatVertex.Stride, LockFlags.NoSystemLock);
CreateThingVerts(t, ref verts, 0);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
// Thing added!
numthings++;
}
// This adds a thing in the things buffer for rendering
public void RenderThingSet(ICollection<Thing> things)
{
FlatVertex[] verts = new FlatVertex[things.Count * 8];
DataStream stream;
int offset = 0;
int added = 0;
// Make sure there is enough memory reserved
ReserveThingsMemory(numthings + things.Count, true);
// Go for all things
foreach(Thing t in things)
{
// TODO: Check if the thing is actually on screen
// Store the thing color
thingcolors[numthings + offset] = DetermineThingColor(t);
// Create vertices
CreateThingVerts(t, ref verts, offset * 8);
// Next
added++;
offset++;
}
// Store vertices in buffer
stream = thingsvertices.Lock(numthings * 8 * FlatVertex.Stride, things.Count * 8 * FlatVertex.Stride, LockFlags.NoSystemLock);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
// Things added!
numthings += added;
}
2007-10-20 19:50:03 +00:00
// This renders the linedefs of a sector with special color
public void RenderSector(Sector s, PixelColor c)
{
2007-10-20 19:50:03 +00:00
// Go for all sides in the sector
foreach(Sidedef sd in s.Sidedefs)
{
2007-10-20 19:50:03 +00:00
// Render this linedef
RenderLinedef(sd.Line, c);
// Render the two vertices on top
RenderVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
RenderVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
}
}
2007-10-20 19:50:03 +00:00
// This renders the linedefs of a sector
public void RenderSector(Sector s)
2007-06-24 18:56:43 +00:00
{
2007-10-20 19:50:03 +00:00
// Go for all sides in the sector
foreach(Sidedef sd in s.Sidedefs)
{
2007-10-20 19:50:03 +00:00
// Render this linedef
RenderLinedef(sd.Line, DetermineLinedefColor(sd.Line));
// Render the two vertices on top
RenderVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
RenderVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
}
2007-10-20 19:50:03 +00:00
}
// This renders a single linedef
public void RenderLinedef(Linedef l, PixelColor c)
{
// Transform vertex coordinates
Vector2D v1 = l.Start.Position.GetTransformed(translatex, translatey, scale, -scale);
Vector2D v2 = l.End.Position.GetTransformed(translatex, translatey, scale, -scale);
// Draw line
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, c);
// Calculate normal indicator
float mx = (v2.x - v1.x) * 0.5f;
float my = (v2.y - v1.y) * 0.5f;
// Draw normal indicator
plotter.DrawLineSolid((int)(v1.x + mx), (int)(v1.y + my),
(int)((v1.x + mx) - (my * l.LengthInv) * linenormalsize),
(int)((v1.y + my) + (mx * l.LengthInv) * linenormalsize), c);
}
// This renders a set of linedefs
2007-10-21 04:07:36 +00:00
public void RenderLinedefSet(ICollection<Linedef> linedefs)
2007-10-20 19:50:03 +00:00
{
// Go for all linedefs
foreach(Linedef l in linedefs) RenderLinedef(l, DetermineLinedefColor(l));
}
// This renders a single vertex
2007-10-21 12:58:56 +00:00
public void RenderVertex(Vertex v, string colorname)
2007-10-20 19:50:03 +00:00
{
// Transform vertex coordinates
Vector2D nv = v.Position.GetTransformed(translatex, translatey, scale, -scale);
// Draw pixel here
2007-10-21 12:58:56 +00:00
plotter.DrawVertexSolid((int)nv.x, (int)nv.y, 2, General.Colors.ColorByName[colorname], General.Colors.BrightColorByName[colorname], General.Colors.DarkColorByName[colorname]);
2007-10-20 19:50:03 +00:00
}
// This renders a set of vertices
2007-10-21 04:07:36 +00:00
public void RenderVerticesSet(ICollection<Vertex> vertices)
2007-10-20 19:50:03 +00:00
{
// Go for all vertices
foreach(Vertex v in vertices) RenderVertex(v, DetermineVertexColor(v));
2007-06-15 22:38:42 +00:00
}
2007-06-15 18:50:18 +00:00
#endregion
}
}