Add Mod.Common Graphics

Moved Graphics to Mod.Common
This commit is contained in:
steelphase
2014-09-20 00:11:34 -04:00
parent ded39d1933
commit f948bf0d1b
47 changed files with 422 additions and 66 deletions

View File

@@ -0,0 +1,45 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Graphics
{
public interface IActorPreview
{
void Tick();
IEnumerable<IRenderable> Render(WorldRenderer wr, WPos pos);
}
public class ActorPreviewInitializer
{
public readonly ActorInfo Actor;
public readonly Player Owner;
public readonly WorldRenderer WorldRenderer;
public World World { get { return WorldRenderer.world; } }
readonly TypeDictionary dict;
public ActorPreviewInitializer(ActorInfo actor, Player owner, WorldRenderer worldRenderer, TypeDictionary dict)
{
Actor = actor;
Owner = owner;
WorldRenderer = worldRenderer;
this.dict = dict;
}
public T Get<T>() where T : IActorInit { return dict.Get<T>(); }
public U Get<T, U>() where T : IActorInit<U> { return dict.Get<T>().Value(World); }
public bool Contains<T>() where T : IActorInit { return dict.Contains<T>(); }
}
}

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics
{
public struct BeamRenderable : IRenderable
{
readonly WPos pos;
readonly int zOffset;
readonly WVec length;
readonly Color color;
readonly float width;
public BeamRenderable(WPos pos, int zOffset, WVec length, float width, Color color)
{
this.pos = pos;
this.zOffset = zOffset;
this.length = length;
this.color = color;
this.width = width;
}
public WPos Pos { get { return pos; } }
public float Scale { get { return 1f; } }
public PaletteReference Palette { get { return null; } }
public int ZOffset { get { return zOffset; } }
public bool IsDecoration { get { return true; } }
public IRenderable WithScale(float newScale) { return new BeamRenderable(pos, zOffset, length, width, color); }
public IRenderable WithPalette(PaletteReference newPalette) { return new BeamRenderable(pos, zOffset, length, width, color); }
public IRenderable WithZOffset(int newOffset) { return new BeamRenderable(pos, zOffset, length, width, color); }
public IRenderable OffsetBy(WVec vec) { return new BeamRenderable(pos + vec, zOffset, length, width, color); }
public IRenderable AsDecoration() { return this; }
public void BeforeRender(WorldRenderer wr) {}
public void Render(WorldRenderer wr)
{
var wlr = Game.Renderer.WorldLineRenderer;
var src = wr.ScreenPosition(pos);
var dest = wr.ScreenPosition(pos + length);
var oldWidth = wlr.LineWidth;
wlr.LineWidth = wr.Viewport.Zoom * width;
wlr.DrawLine(src, dest, color, color);
wlr.LineWidth = oldWidth;
}
public void RenderDebugGeometry(WorldRenderer wr) {}
}
}

View File

@@ -0,0 +1,120 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics
{
public struct ContrailRenderable : IRenderable
{
public int Length { get { return trail.Length; } }
readonly World world;
// Store trail positions in a circular buffer
readonly WPos[] trail;
int next;
int length;
int skip;
readonly Color color;
readonly int zOffset;
public ContrailRenderable(World world, Color color, int length, int skip, int zOffset)
: this(world, new WPos[length], 0, 0, skip, color, zOffset) {}
ContrailRenderable(World world, WPos[] trail, int next, int length, int skip, Color color, int zOffset)
{
this.world = world;
this.trail = trail;
this.next = next;
this.length = length;
this.skip = skip;
this.color = color;
this.zOffset = zOffset;
}
public WPos Pos { get { return trail[idx(next-1)]; } }
public float Scale { get { return 1f; } }
public PaletteReference Palette { get { return null; } }
public int ZOffset { get { return zOffset; } }
public bool IsDecoration { get { return true; } }
public IRenderable WithScale(float newScale) { return new ContrailRenderable(world, (WPos[])trail.Clone(), next, length, skip, color, zOffset); }
public IRenderable WithPalette(PaletteReference newPalette) { return new ContrailRenderable(world, (WPos[])trail.Clone(), next, length, skip, color, zOffset); }
public IRenderable WithZOffset(int newOffset) { return new ContrailRenderable(world, (WPos[])trail.Clone(), next, length, skip, color, newOffset); }
public IRenderable OffsetBy(WVec vec) { return new ContrailRenderable(world, trail.Select(pos => pos + vec).ToArray(), next, length, skip, color, zOffset); }
public IRenderable AsDecoration() { return this; }
public void BeforeRender(WorldRenderer wr) {}
public void Render(WorldRenderer wr)
{
// Need at least 4 points to smooth the contrail over
if (length - skip < 4)
return;
var wlr = Game.Renderer.WorldLineRenderer;
var oldWidth = wlr.LineWidth;
wlr.LineWidth = wr.Viewport.Zoom;
// Start of the first line segment is the tail of the list - don't smooth it.
var curPos = trail[idx(next - skip - 1)];
var curCell = wr.world.Map.CellContaining(curPos);
var curColor = color;
for (var i = 0; i < length - skip - 4; i++)
{
var j = next - skip - i - 2;
var nextPos = Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
var nextCell = wr.world.Map.CellContaining(nextPos);
var nextColor = Exts.ColorLerp(i * 1f / (length - 4), color, Color.Transparent);
if (!world.FogObscures(curCell) && !world.FogObscures(nextCell))
wlr.DrawLine(wr.ScreenPosition(curPos), wr.ScreenPosition(nextPos), curColor, nextColor);
curPos = nextPos;
curCell = nextCell;
curColor = nextColor;
}
wlr.LineWidth = oldWidth;
}
public void RenderDebugGeometry(WorldRenderer wr) {}
// Array index modulo length
int idx(int i)
{
var j = i % trail.Length;
return j < 0 ? j + trail.Length : j;
}
static WPos Average(params WPos[] list)
{
return list.Average();
}
public void Update(WPos pos)
{
trail[next] = pos;
next = idx(next+1);
if (length < trail.Length)
length++;
}
public static Color ChooseColor(Actor self)
{
var ownerColor = Color.FromArgb(255, self.Owner.Color.RGB);
return Exts.ColorLerp(0.5f, ownerColor, Color.White);
}
}
}

View File

@@ -0,0 +1,59 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics
{
public struct RangeCircleRenderable : IRenderable
{
readonly WPos centerPosition;
readonly WRange radius;
readonly int zOffset;
readonly Color color;
readonly Color contrastColor;
public RangeCircleRenderable(WPos centerPosition, WRange radius, int zOffset, Color color, Color contrastColor)
{
this.centerPosition = centerPosition;
this.radius = radius;
this.zOffset = zOffset;
this.color = color;
this.contrastColor = contrastColor;
}
public WPos Pos { get { return centerPosition; } }
public float Scale { get { return 1f; } }
public PaletteReference Palette { get { return null; } }
public int ZOffset { get { return zOffset; } }
public bool IsDecoration { get { return true; } }
public IRenderable WithScale(float newScale) { return new RangeCircleRenderable(centerPosition, radius, zOffset, color, contrastColor); }
public IRenderable WithPalette(PaletteReference newPalette) { return new RangeCircleRenderable(centerPosition, radius, zOffset, color, contrastColor); }
public IRenderable WithZOffset(int newOffset) { return new RangeCircleRenderable(centerPosition, radius, newOffset, color, contrastColor); }
public IRenderable OffsetBy(WVec vec) { return new RangeCircleRenderable(centerPosition + vec, radius, zOffset, color, contrastColor); }
public IRenderable AsDecoration() { return this; }
public void BeforeRender(WorldRenderer wr) {}
public void Render(WorldRenderer wr)
{
var wlr = Game.Renderer.WorldLineRenderer;
var oldWidth = wlr.LineWidth;
wlr.LineWidth = 3;
wr.DrawRangeCircle(centerPosition, radius, contrastColor);
wlr.LineWidth = 1;
wr.DrawRangeCircle(centerPosition, radius, color);
wlr.LineWidth = oldWidth;
}
public void RenderDebugGeometry(WorldRenderer wr) {}
}
}

View File

@@ -0,0 +1,42 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Graphics
{
public class SpriteActorPreview : IActorPreview
{
readonly Animation animation;
readonly WVec offset;
readonly int zOffset;
readonly PaletteReference pr;
readonly float scale;
public SpriteActorPreview(Animation animation, WVec offset, int zOffset, PaletteReference pr, float scale)
{
this.animation = animation;
this.offset = offset;
this.zOffset = zOffset;
this.pr = pr;
this.scale = scale;
}
public void Tick() { animation.Tick(); }
public IEnumerable<IRenderable> Render(WorldRenderer wr, WPos pos)
{
return animation.Render(pos, offset, zOffset, pr, scale);
}
}
}

View File

@@ -0,0 +1,61 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using OpenRA.Graphics;
namespace OpenRA.Mods.RA.Graphics
{
public struct TextRenderable : IRenderable
{
readonly SpriteFont font;
readonly WPos pos;
readonly int zOffset;
readonly Color color;
readonly string text;
public TextRenderable(SpriteFont font, WPos pos, int zOffset, Color color, string text)
{
this.font = font;
this.pos = pos;
this.zOffset = zOffset;
this.color = color;
this.text = text;
}
public WPos Pos { get { return pos; } }
public float Scale { get { return 1f; } }
public PaletteReference Palette { get { return null; } }
public int ZOffset { get { return zOffset; } }
public bool IsDecoration { get { return true; } }
public IRenderable WithScale(float newScale) { return new TextRenderable(font, pos, zOffset, color, text); }
public IRenderable WithPalette(PaletteReference newPalette) { return new TextRenderable(font, pos, zOffset, color, text); }
public IRenderable WithZOffset(int newOffset) { return new TextRenderable(font, pos, zOffset, color, text); }
public IRenderable OffsetBy(WVec vec) { return new TextRenderable(font, pos + vec, zOffset, color, text); }
public IRenderable AsDecoration() { return this; }
public void BeforeRender(WorldRenderer wr) {}
public void Render(WorldRenderer wr)
{
var screenPos = wr.Viewport.Zoom*(wr.ScreenPosition(pos) - wr.Viewport.TopLeft.ToFloat2()) - 0.5f*font.Measure(text).ToFloat2();
var screenPxPos = new float2((float)Math.Round(screenPos.X), (float)Math.Round(screenPos.Y));
font.DrawTextWithContrast(text, screenPxPos, color, Color.Black, 1);
}
public void RenderDebugGeometry(WorldRenderer wr)
{
var size = font.Measure(text).ToFloat2();
var offset = wr.ScreenPxPosition(pos) - 0.5f*size;
Game.Renderer.WorldLineRenderer.DrawRect(offset, offset + size, Color.Red);
}
}
}

View File

@@ -0,0 +1,59 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics
{
public class VoxelPreview : IActorPreview
{
readonly VoxelAnimation[] components;
readonly float scale;
readonly float[] lightAmbientColor;
readonly float[] lightDiffuseColor;
readonly WRot lightSource;
readonly WRot camera;
readonly PaletteReference colorPalette;
readonly PaletteReference normalsPalette;
readonly PaletteReference shadowPalette;
readonly WVec offset;
readonly int zOffset;
public VoxelPreview(VoxelAnimation[] components, WVec offset, int zOffset, float scale, WAngle lightPitch, WAngle lightYaw, float[] lightAmbientColor, float[] lightDiffuseColor, WAngle cameraPitch,
PaletteReference colorPalette, PaletteReference normalsPalette, PaletteReference shadowPalette)
{
this.components = components;
this.scale = scale;
this.lightAmbientColor = lightAmbientColor;
this.lightDiffuseColor = lightDiffuseColor;
lightSource = new WRot(WAngle.Zero, new WAngle(256) - lightPitch, lightYaw);
camera = new WRot(WAngle.Zero, cameraPitch - new WAngle(256), new WAngle(256));
this.colorPalette = colorPalette;
this.normalsPalette = normalsPalette;
this.shadowPalette = shadowPalette;
this.offset = offset;
this.zOffset = zOffset;
}
public void Tick() { /* not supported */ }
public IEnumerable<IRenderable> Render(WorldRenderer wr, WPos pos)
{
yield return new VoxelRenderable(components, pos + offset, zOffset, camera, this.scale,
lightSource, this.lightAmbientColor, this.lightDiffuseColor,
colorPalette, normalsPalette, shadowPalette);
}
}
}

View File

@@ -0,0 +1,181 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics
{
public struct VoxelRenderable : IRenderable
{
readonly IEnumerable<VoxelAnimation> voxels;
readonly WPos pos;
readonly int zOffset;
readonly WRot camera;
readonly WRot lightSource;
readonly float[] lightAmbientColor;
readonly float[] lightDiffuseColor;
readonly PaletteReference palette;
readonly PaletteReference normalsPalette;
readonly PaletteReference shadowPalette;
readonly float scale;
// Generated at render-time
VoxelRenderProxy renderProxy;
public VoxelRenderable(IEnumerable<VoxelAnimation> voxels, WPos pos, int zOffset, WRot camera, float scale,
WRot lightSource, float[] lightAmbientColor, float[] lightDiffuseColor,
PaletteReference color, PaletteReference normals, PaletteReference shadow)
{
this.voxels = voxels;
this.pos = pos;
this.zOffset = zOffset;
this.scale = scale;
this.camera = camera;
this.lightSource = lightSource;
this.lightAmbientColor = lightAmbientColor;
this.lightDiffuseColor = lightDiffuseColor;
this.palette = color;
this.normalsPalette = normals;
this.shadowPalette = shadow;
this.renderProxy = null;
}
public WPos Pos { get { return pos; } }
public float Scale { get { return scale; } }
public PaletteReference Palette { get { return palette; } }
public int ZOffset { get { return zOffset; } }
public bool IsDecoration { get { return false; } }
public IRenderable WithScale(float newScale)
{
return new VoxelRenderable(voxels, pos, zOffset, camera, newScale,
lightSource, lightAmbientColor, lightDiffuseColor,
palette, normalsPalette, shadowPalette);
}
public IRenderable WithPalette(PaletteReference newPalette)
{
return new VoxelRenderable(voxels, pos, zOffset, camera, scale,
lightSource, lightAmbientColor, lightDiffuseColor,
newPalette, normalsPalette, shadowPalette);
}
public IRenderable WithZOffset(int newOffset)
{
return new VoxelRenderable(voxels, pos, newOffset, camera, scale,
lightSource, lightAmbientColor, lightDiffuseColor,
palette, normalsPalette, shadowPalette);
}
public IRenderable OffsetBy(WVec vec)
{
return new VoxelRenderable(voxels, pos + vec, zOffset, camera, scale,
lightSource, lightAmbientColor, lightDiffuseColor,
palette, normalsPalette, shadowPalette);
}
public IRenderable AsDecoration() { return this; }
// This will need generalizing once we support TS/RA2 terrain
static readonly float[] groundNormal = new float[] {0,0,1,1};
public void BeforeRender(WorldRenderer wr)
{
var draw = voxels.Where(v => v.DisableFunc == null || !v.DisableFunc());
renderProxy = Game.Renderer.WorldVoxelRenderer.RenderAsync(
wr, draw, camera, scale, groundNormal, lightSource,
lightAmbientColor, lightDiffuseColor,
palette, normalsPalette, shadowPalette);
}
public void Render(WorldRenderer wr)
{
var pxOrigin = wr.ScreenPosition(pos);
var groundZ = 0.5f*(pxOrigin.Y - wr.ScreenZPosition(pos, 0));
var shadowOrigin = pxOrigin - groundZ*(new float2(renderProxy.ShadowDirection, 1));
var psb = renderProxy.ProjectedShadowBounds;
var sa = shadowOrigin + psb[0];
var sb = shadowOrigin + psb[2];
var sc = shadowOrigin + psb[1];
var sd = shadowOrigin + psb[3];
Game.Renderer.WorldRgbaSpriteRenderer.DrawSprite(renderProxy.ShadowSprite, sa, sb, sc, sd);
Game.Renderer.WorldRgbaSpriteRenderer.DrawSprite(renderProxy.Sprite, pxOrigin - 0.5f*renderProxy.Sprite.size);
}
public void RenderDebugGeometry(WorldRenderer wr)
{
var pxOrigin = wr.ScreenPosition(pos);
var groundZ = 0.5f*(pxOrigin.Y - wr.ScreenZPosition(pos, 0));
var shadowOrigin = pxOrigin - groundZ*(new float2(renderProxy.ShadowDirection, 1));
// Draw sprite rect
var offset = pxOrigin + renderProxy.Sprite.offset - 0.5f*renderProxy.Sprite.size;
Game.Renderer.WorldLineRenderer.DrawRect(offset, offset + renderProxy.Sprite.size, Color.Red);
// Draw transformed shadow sprite rect
var c = Color.Purple;
var psb = renderProxy.ProjectedShadowBounds;
Game.Renderer.WorldLineRenderer.DrawLine(shadowOrigin + psb[1], shadowOrigin + psb[3], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(shadowOrigin + psb[3], shadowOrigin + psb[0], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(shadowOrigin + psb[0], shadowOrigin + psb[2], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(shadowOrigin + psb[2], shadowOrigin + psb[1], c, c);
// Draw voxel bounding box
var draw = voxels.Where(v => v.DisableFunc == null || !v.DisableFunc());
var scaleTransform = Util.ScaleMatrix(scale, scale, scale);
var cameraTransform = Util.MakeFloatMatrix(camera.AsMatrix());
foreach (var v in draw)
{
var bounds = v.Voxel.Bounds(v.FrameFunc());
var worldTransform = v.RotationFunc().Reverse().Aggregate(scaleTransform,
(x,y) => Util.MatrixMultiply(x, Util.MakeFloatMatrix(y.AsMatrix())));
var pxOffset = wr.ScreenVector(v.OffsetFunc());
var pxPos = pxOrigin + new float2(pxOffset[0], pxOffset[1]);
var screenTransform = Util.MatrixMultiply(cameraTransform, worldTransform);
DrawBoundsBox(pxPos, screenTransform, bounds, Color.Yellow);
}
}
static readonly uint[] ix = new uint[] {0,0,0,0,3,3,3,3};
static readonly uint[] iy = new uint[] {1,1,4,4,1,1,4,4};
static readonly uint[] iz = new uint[] {2,5,2,5,2,5,2,5};
static void DrawBoundsBox(float2 pxPos, float[] transform, float[] bounds, Color c)
{
var corners = new float2[8];
for (var i = 0; i < 8; i++)
{
var vec = new float[] {bounds[ix[i]], bounds[iy[i]], bounds[iz[i]], 1};
var screen = Util.MatrixVectorMultiply(transform, vec);
corners[i] = pxPos + new float2(screen[0], screen[1]);
}
Game.Renderer.WorldLineRenderer.DrawLine(corners[0], corners[1], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[1], corners[3], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[3], corners[2], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[2], corners[0], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[4], corners[5], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[5], corners[7], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[7], corners[6], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[6], corners[4], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[0], corners[4], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[1], corners[5], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[2], corners[6], c, c);
Game.Renderer.WorldLineRenderer.DrawLine(corners[3], corners[7], c, c);
}
}
}