UltimateZoneBuilder/Source/Core/Rendering/Plotter.cs

211 lines
6.5 KiB
C#
Raw Normal View History

#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.Generic;
using CodeImp.DoomBuilder.Geometry;
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
2019-08-20 09:00:24 +00:00
internal sealed class Plotter : IDisposable
{
2019-08-18 05:43:46 +00:00
public Plotter(int width, int height)
{
this.Texture = new Texture(width, height);
}
2019-08-18 05:43:46 +00:00
~Plotter()
{
Dispose();
}
2019-08-18 05:43:46 +00:00
public int Width { get { return Texture.Width; } }
public int Height { get { return Texture.Height; } }
public Texture Texture { get; set; }
public void DrawContents(RenderDevice graphics)
2019-08-18 05:43:46 +00:00
{
2019-08-20 09:00:24 +00:00
if (clear == false && vertices.Count == 0)
return;
2019-08-20 09:00:24 +00:00
var projmat = Matrix.Scaling(2.0f / this.Texture.Width, 2.0f / this.Texture.Height, 1.0f) * Matrix.Translation(-1.0f, -1.0f, 0.0f);
2019-08-20 09:00:24 +00:00
graphics.StartRendering(clear, new Color4(0), this.Texture, false);
graphics.SetShader(ShaderName.plotter);
graphics.SetUniform(UniformName.transformsettings, projmat);
graphics.SetAlphaBlendEnable(true);
graphics.SetBlendOperation(BlendOperation.Add);
graphics.SetSourceBlend(Blend.SourceAlpha);
graphics.SetDestinationBlend(Blend.InverseSourceAlpha);
graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Count / 3, vertices.ToArray());
graphics.SetAlphaBlendEnable(false);
graphics.FinishRendering();
2019-08-20 09:00:24 +00:00
clear = false;
vertices.Clear();
}
void DrawLine(int x0, int y0, int x1, int y1, int c, bool dotted = false)
{
var v = new FlatVertex();
v.c = c;
float nx, ny, len;
if (x0 == x1)
{
nx = 1.0f;
ny = 0.0f;
len = y1 - y0;
}
else if (y0 == y1)
{
nx = 0.0f;
ny = 1.0f;
len = x1 - x0;
}
else
{
2019-08-20 09:00:24 +00:00
nx = (float)(y1 - y0);
ny = (float)-(x1 - x0);
len = (float)Math.Sqrt(nx * nx + ny * ny);
nx /= len;
ny /= len;
}
2019-08-20 09:00:24 +00:00
float dx = -ny * 0.5f;
float dy = nx * 0.5f;
2019-08-18 06:11:09 +00:00
2019-08-20 09:00:24 +00:00
float xx0 = x0 + 0.5f - dx;
float yy0 = y0 + 0.5f - dy;
float xx1 = x1 + 0.5f + dx;
float yy1 = y1 + 0.5f + dy;
2019-08-20 09:00:24 +00:00
float start, end;
if (!dotted)
{
2019-08-20 09:00:24 +00:00
start = 0.5f;
end = 0.5f;
}
else
{
2019-08-20 09:00:24 +00:00
start = -0.5f;
end = len + 0.5f;
}
2019-08-20 09:00:24 +00:00
float lineextent = 3.0f; // line width in shader + 1
nx *= lineextent;
ny *= lineextent;
2019-08-20 09:00:24 +00:00
v.u = start; v.v = -lineextent; v.x = xx0 - nx; v.y = yy0 - ny; vertices.Add(v);
v.u = start; v.v = lineextent; v.x = xx0 + nx; v.y = yy0 + ny; vertices.Add(v);
v.u = end; v.v = lineextent; v.x = xx1 + nx; v.y = yy1 + ny; vertices.Add(v);
vertices.Add(v);
v.u = end; v.v = -lineextent; v.x = xx1 - nx; v.y = yy1 - ny; vertices.Add(v);
v.u = start; v.v = -lineextent; v.x = xx0 - nx; v.y = yy0 - ny; vertices.Add(v);
}
2019-08-20 09:00:24 +00:00
void FillBox(int x0, int y0, int x1, int y1, int c)
{
var v = new FlatVertex();
v.c = c;
v.u = 0.5f;
v.v = 0.0f;
v.x = x0; v.y = y0; vertices.Add(v);
v.x = x1; v.y = y0; vertices.Add(v);
v.x = x1; v.y = y1; vertices.Add(v);
vertices.Add(v);
v.x = x0; v.y = y1; vertices.Add(v);
v.x = x0; v.y = y0; vertices.Add(v);
}
public void DrawVertexSolid(int x, int y, int size, PixelColor c, PixelColor l, PixelColor d)
{
int x0 = x - size;
int x1 = x + size;
int y0 = y - size;
int y1 = y + size;
int lightcolor = l.ToInt();
int darkcolor = d.ToInt();
int centercolor = c.ToInt();
DrawLine(x1, y1, x0, y1, darkcolor);
DrawLine(x1, y1, x1, y0, darkcolor);
DrawLine(x0, y0, x1, y0, lightcolor);
DrawLine(x0, y0, x0, y1, lightcolor);
FillBox(x0 + 1, y0 + 1, x1, y1, centercolor);
}
2019-08-20 09:00:24 +00:00
public void DrawGridLineH(int y, int x1, int x2, PixelColor c)
{
DrawLine(x1, y, x2, y, c.ToInt());
}
public void DrawGridLineV(int x, int y1, int y2, PixelColor c)
{
DrawLine(x, y1, x, y2, c.ToInt());
}
public void DrawLineSolid(int x1, int y1, int x2, int y2, PixelColor c, bool dotted = false)
{
DrawLine(x1, y1, x2, y2, c.ToInt(), true);
}
public void DrawLine3DFloor(Vector2D start, Vector2D end, PixelColor c, PixelColor c2)
{
Vector2D delta = end - start;
float length = delta.GetLength();
if(length < DASH_INTERVAL * 2)
{
DrawLineSolid((int)start.x, (int)start.y, (int)end.x, (int)end.y, c2);
}
else
{
float d1 = DASH_INTERVAL / length;
float d2 = 1.0f - d1;
Vector2D p1 = CurveTools.GetPointOnLine(start, end, d1);
Vector2D p2 = CurveTools.GetPointOnLine(start, end, d2);
DrawLineSolid((int)start.x, (int)start.y, (int)p1.x, (int)p1.y, c2);
DrawLineSolid((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, c);
DrawLineSolid((int)p2.x, (int)p2.y, (int)end.x, (int)end.y, c2);
}
}
2019-08-20 09:00:24 +00:00
// This clears all pixels black
public void Clear()
{
clear = true;
}
public void Dispose()
{
if (Texture != null) Texture.Dispose();
}
bool clear = true;
2019-08-20 09:00:24 +00:00
List<FlatVertex> vertices = new List<FlatVertex>();
const int DASH_INTERVAL = 16;
}
}