Merge pull request #6029 from pchote/gametomods
Move *Renderables and Render* out of Game.
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 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;
|
||||
|
||||
namespace OpenRA.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) {}
|
||||
}
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 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;
|
||||
|
||||
namespace OpenRA.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 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;
|
||||
|
||||
namespace OpenRA.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
#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;
|
||||
|
||||
namespace OpenRA.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user