Skip to content

Commit

Permalink
Add alpha support to SpriteRenderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and abcdefg30 committed Jan 5, 2021
1 parent b88495c commit 67754e8
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 46 deletions.
8 changes: 4 additions & 4 deletions OpenRA.Game/Graphics/RgbaSpriteRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float
parent.DrawSprite(s, a, b, c, d);
}

public void DrawSpriteWithTint(Sprite s, in float3 location, in float3 size, in float3 tint)
public void DrawSprite(Sprite s, in float3 location, in float3 size, in float3 tint, float alpha)
{
if (s.Channel != TextureChannel.RGBA)
throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite.");

parent.DrawSpriteWithTint(s, location, 0, size, tint);
parent.DrawSprite(s, location, 0, size, tint, alpha);
}

public void DrawSpriteWithTint(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint)
public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint, float alpha)
{
if (s.Channel != TextureChannel.RGBA)
throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite.");

parent.DrawSpriteWithTint(s, a, b, c, d, tint);
parent.DrawSprite(s, a, b, c, d, tint, alpha);
}
}
}
12 changes: 6 additions & 6 deletions OpenRA.Game/Graphics/SpriteFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ void DrawTextContrast(string text, float2 location, Color contrastColor, int con
if (g.Sprite != null)
{
var contrastSprite = contrastGlyphs[(s, screenContrast)];
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(contrastSprite,
Game.Renderer.RgbaSpriteRenderer.DrawSprite(contrastSprite,
(screen + g.Offset - contrastVector) / deviceScale,
contrastSprite.Size / deviceScale,
tint);
tint, 1f);
}

screen += new int2((int)(g.Advance + 0.5f), 0);
Expand Down Expand Up @@ -120,10 +120,10 @@ public void DrawText(string text, float2 location, Color c)

// Convert screen coordinates back to UI coordinates for drawing
if (g.Sprite != null)
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite,
Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite,
(screen + g.Offset).ToFloat2() / deviceScale,
g.Sprite.Size / deviceScale,
tint);
tint, 1f);

screen += new int2((int)(g.Advance + 0.5f), 0);
}
Expand Down Expand Up @@ -172,12 +172,12 @@ public void DrawText(string text, float2 location, Color c, float angle)

// Offset rotated glyph to align the top-left corner with the screen pixel grid
var screenOffset = new float2((int)(ra.X * deviceScale + 0.5f), (int)(ra.Y * deviceScale + 0.5f)) / deviceScale - ra;
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite,
Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite,
ra + screenOffset,
rb + screenOffset,
rc + screenOffset,
rd + screenOffset,
tint);
tint, 1f);
}

p += new float2(g.Advance / deviceScale, 0);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/SpriteRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void Render(WorldRenderer wr)
if (wr.TerrainLighting != null)
t *= wr.TerrainLighting.TintAt(pos);

wsr.DrawSpriteWithTint(sprite, ScreenPosition(wr), palette, scale * sprite.Size, t);
wsr.DrawSprite(sprite, ScreenPosition(wr), palette, scale * sprite.Size, t, 1f);
}
}

Expand Down
16 changes: 8 additions & 8 deletions OpenRA.Game/Graphics/SpriteRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int2 SetRenderStateForSprite(Sprite s)
internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, float3.Ones);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, float3.Ones, 1f);
nv += 6;
}

Expand All @@ -128,26 +128,26 @@ public void DrawSprite(Sprite s, in float3 location, PaletteReference pal, float
public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, float3.Ones, nv);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, float3.Ones, 1f, nv);
nv += 6;
}

internal void DrawSpriteWithTint(Sprite s, in float3 location, float paletteTextureIndex, in float3 size, in float3 tint)
internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size, in float3 tint, float alpha)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, tint);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, tint, alpha);
nv += 6;
}

public void DrawSpriteWithTint(Sprite s, in float3 location, PaletteReference pal, in float3 size, in float3 tint)
public void DrawSprite(Sprite s, in float3 location, PaletteReference pal, in float3 size, in float3 tint, float alpha)
{
DrawSpriteWithTint(s, location, pal.TextureIndex, size, tint);
DrawSprite(s, location, pal.TextureIndex, size, tint, alpha);
}

public void DrawSpriteWithTint(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint)
public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint, float alpha)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, tint, nv);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, tint, alpha, nv);
nv += 6;
}

Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Game/Graphics/TerrainSpriteLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void UpdatePaletteIndices()
for (var i = 0; i < vertices.Length; i++)
{
var v = vertices[i];
vertices[i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, v.R, v.G, v.B);
vertices[i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, v.R, v.G, v.B, v.A);
}

for (var row = 0; row < map.MapSize.Y; row++)
Expand Down Expand Up @@ -106,7 +106,7 @@ void UpdateTint(MPos uv)
for (var i = 0; i < 6; i++)
{
var v = vertices[offset + i];
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, noTint);
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, noTint, 1f);
}

return;
Expand All @@ -131,7 +131,7 @@ void UpdateTint(MPos uv)
for (var i = 0; i < 6; i++)
{
var v = vertices[offset + i];
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, weights[CornerVertexMap[i]]);
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, weights[CornerVertexMap[i]], 1f);
}

dirtyRows.Add(uv.V);
Expand All @@ -155,7 +155,7 @@ public void Update(MPos uv, Sprite sprite, in float3 pos, bool ignoreTint)
return;

var offset = rowStride * uv.V + 6 * uv.U;
Util.FastCreateQuad(vertices, pos, sprite, int2.Zero, palette.TextureIndex, offset, sprite.Size, float3.Ones);
Util.FastCreateQuad(vertices, pos, sprite, int2.Zero, palette.TextureIndex, offset, sprite.Size, float3.Ones, 1f);

if (worldRenderer.TerrainLighting != null)
{
Expand Down
18 changes: 9 additions & 9 deletions OpenRA.Game/Graphics/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public static class Util
// yes, our channel order is nuts.
static readonly int[] ChannelMasks = { 2, 1, 0, 3 };

public static void FastCreateQuad(Vertex[] vertices, in float3 o, Sprite r, int2 samplers, float paletteTextureIndex, int nv, in float3 size, in float3 tint)
public static void FastCreateQuad(Vertex[] vertices, in float3 o, Sprite r, int2 samplers, float paletteTextureIndex, int nv, in float3 size, in float3 tint, float alpha)
{
var b = new float3(o.X + size.X, o.Y, o.Z);
var c = new float3(o.X + size.X, o.Y + size.Y, o.Z + size.Z);
var d = new float3(o.X, o.Y + size.Y, o.Z + size.Z);
FastCreateQuad(vertices, o, b, c, d, r, samplers, paletteTextureIndex, tint, nv);
FastCreateQuad(vertices, o, b, c, d, r, samplers, paletteTextureIndex, tint, alpha, nv);
}

public static void FastCreateQuad(Vertex[] vertices,
in float3 a, in float3 b, in float3 c, in float3 d,
Sprite r, int2 samplers, float paletteTextureIndex,
in float3 tint, int nv)
in float3 tint, float alpha, int nv)
{
float sl = 0;
float st = 0;
Expand All @@ -54,12 +54,12 @@ public static void FastCreateQuad(Vertex[] vertices,
}

var fAttribC = (float)attribC;
vertices[nv] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint);
vertices[nv + 1] = new Vertex(b, r.Right, r.Top, sr, st, paletteTextureIndex, fAttribC, tint);
vertices[nv + 2] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint);
vertices[nv + 3] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint);
vertices[nv + 4] = new Vertex(d, r.Left, r.Bottom, sl, sb, paletteTextureIndex, fAttribC, tint);
vertices[nv + 5] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint);
vertices[nv] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 1] = new Vertex(b, r.Right, r.Top, sr, st, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 2] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 3] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 4] = new Vertex(d, r.Left, r.Bottom, sl, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 5] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint, alpha);
}

public static void FastCopyIntoChannel(Sprite dest, byte[] src, SpriteFrameType srcType)
Expand Down
16 changes: 8 additions & 8 deletions OpenRA.Game/Graphics/Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ public struct Vertex
public readonly float P, C;

// Color tint
public readonly float R, G, B;
public readonly float R, G, B, A;

public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c)
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, float3.Ones) { }
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, float3.Ones, 1f) { }

public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c, in float3 tint)
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { }
public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c, in float3 tint, float a)
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z, a) { }

public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, in float3 tint)
: this(x, y, z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { }
public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, in float3 tint, float a)
: this(x, y, z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z, a) { }

public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, float r, float g, float b)
public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, float r, float g, float b, float a)
{
X = x; Y = y; Z = z;
S = s; T = t;
U = u; V = v;
P = p; C = c;
R = r; G = g; B = b;
R = r; G = g; B = b; A = a;
}
}
}
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Graphics/ModelRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public void Render(WorldRenderer wr)
if (wr.TerrainLighting != null)
t *= wr.TerrainLighting.TintAt(model.pos);

wrsr.DrawSpriteWithTint(renderProxy.ShadowSprite, sa, sb, sc, sd, t);
wrsr.DrawSpriteWithTint(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t);
wrsr.DrawSprite(renderProxy.ShadowSprite, sa, sb, sc, sd, t, 1f);
wrsr.DrawSprite(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t, 1f);
}

public void RenderDebugGeometry(WorldRenderer wr)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Platforms.Default/VertexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void Bind()
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28));
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 3, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36));
OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36));
OpenGL.CheckGLError();
}

Expand Down
6 changes: 3 additions & 3 deletions glsl/combined.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uniform vec3 r1, r2;
attribute vec4 aVertexPosition;
attribute vec4 aVertexTexCoord;
attribute vec2 aVertexTexMetadata;
attribute vec3 aVertexTint;
attribute vec4 aVertexTint;

varying vec4 vTexCoord;
varying vec2 vTexMetadata;
Expand All @@ -23,7 +23,7 @@ varying vec4 vTint;
in vec4 aVertexPosition;
in vec4 aVertexTexCoord;
in vec2 aVertexTexMetadata;
in vec3 aVertexTint;
in vec4 aVertexTint;

out vec4 vTexCoord;
out vec2 vTexMetadata;
Expand Down Expand Up @@ -127,5 +127,5 @@ void main()
vPalettedFraction = SelectPalettedFraction(attrib.s);
vDepthMask = SelectChannelMask(attrib.t);
vTexSampler = attrib.pq;
vTint = vec4(aVertexTint, 1.0);
vTint = aVertexTint;
}

0 comments on commit 67754e8

Please sign in to comment.