Skip to content

Commit

Permalink
Fixed fantasy intro drawing, reworked shader positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrdacor committed Mar 6, 2023
1 parent 2832d22 commit 2335c15
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 115 deletions.
10 changes: 10 additions & 0 deletions Ambermoon.Common/Rect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public void ClipRect(Position position, Size size)
size.Height = bottom - position.Y;
}

public void ClipRect(FloatPosition position, FloatSize size)
{
float right = Math.Min(position.X + size.Width, Right);
float bottom = Math.Min(position.Y + size.Height, Bottom);
position.X = Math.Max(position.X, X);
position.Y = Math.Max(position.Y, Y);
size.Width = right - position.X;
size.Height = bottom - position.Y;
}

public void Trap(Position position)
{
position.X = Util.Limit(Left, position.X, Right);
Expand Down
6 changes: 3 additions & 3 deletions Ambermoon.Core/Render/IRenderLayer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* IRenderLayer.cs - Render layer interface
*
* Copyright (C) 2020-2021 Robert Schneckenhaus <[email protected]>
* Copyright (C) 2020-2023 Robert Schneckenhaus <[email protected]>
*
* This file is part of Ambermoon.net.
*
Expand All @@ -21,8 +21,8 @@

namespace Ambermoon.Render
{
public delegate Position PositionTransformation(Position position);
public delegate Size SizeTransformation(Size size);
public delegate FloatPosition PositionTransformation(FloatPosition position);
public delegate FloatSize SizeTransformation(FloatSize size);

public interface IRenderLayer
{
Expand Down
2 changes: 2 additions & 0 deletions Ambermoon.Data.Legacy/Serialization/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Ambermoon.Data.Legacy.Serialization
{
#pragma warning disable CS8981
using word = UInt16;
using dword = UInt32;
using qword = UInt64;
#pragma warning restore CS8981

public class DataReader : IDataReader, ICustomReader
{
Expand Down
5 changes: 1 addition & 4 deletions Ambermoon.Data.Legacy/Serialization/FantasyIntroData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;

namespace Ambermoon.Data.Legacy.Serialization
{


public class FantasyIntroData : IFantasyIntroData
{
static readonly Position[] WandSparkPositions = new[]
Expand Down
2 changes: 0 additions & 2 deletions Ambermoon.Data.Legacy/Serialization/IntroData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Ambermoon.Data.Legacy.Serialization
{


public class IntroData : IIntroData
{
readonly List<Graphic> introPalettes = new();
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.Renderer.OpenGL/AlphaTextureShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal class AlphaTextureShader : TextureShader
protected static string[] AlphaTextureVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"in ivec2 {DefaultTexCoordName};",
$"in uint {DefaultLayerName};",
$"in uint {DefaultPaletteIndexName};",
Expand All @@ -87,7 +87,7 @@ internal class AlphaTextureShader : TextureShader
$"void main()",
$"{{",
$" vec2 atlasFactor = vec2(1.0f / float({DefaultAtlasSizeName}.x), 1.0f / float({DefaultAtlasSizeName}.y));",
$" vec2 pos = vec2(float({DefaultPositionName}.x) + 0.49f, float({DefaultPositionName}.y) + 0.49f);",
$" vec2 pos = vec2({DefaultPositionName}.x + 0.49f, {DefaultPositionName}.y + 0.49f);",
$" varTexCoord = atlasFactor * vec2({DefaultTexCoordName}.x, {DefaultTexCoordName}.y);",
$" palIndex = float({DefaultPaletteIndexName});",
$" a = float({DefaultAlphaName}) / 255.0f;",
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.Renderer.OpenGL/ColorShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected static string GetVertexShaderHeader(State state)
static string[] ColorVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"in uint {DefaultLayerName};",
$"in uvec4 {DefaultColorName};",
$"uniform float {DefaultZName};",
Expand All @@ -86,7 +86,7 @@ protected static string GetVertexShaderHeader(State state)
$"",
$"void main()",
$"{{",
$" vec2 pos = vec2(float({DefaultPositionName}.x) + 0.49f, float({DefaultPositionName}.y) + 0.49f);",
$" vec2 pos = vec2({DefaultPositionName}.x + 0.49f, {DefaultPositionName}.y + 0.49f);",
$" pixelColor = vec4(float({DefaultColorName}.r) / 255.0f, float({DefaultColorName}.g) / 255.0f, float({DefaultColorName}.b) / 255.0f, float({DefaultColorName}.a) / 255.0f);",
$" ",
$" gl_Position = {DefaultProjectionMatrixName} * {DefaultModelViewMatrixName} * vec4(pos, 1.0f - {DefaultZName} - float({DefaultLayerName}) * 0.00001f, 1.0f);",
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.Renderer.OpenGL/EffectShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ internal class EffectShader : ScreenShader
static string[] EffectVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"uniform mat4 {DefaultProjectionMatrixName};",
$"uniform mat4 {DefaultModelViewMatrixName};",
$"uniform vec2 {DefaultResolutionName};",
$"out vec2 varTexCoord;",
$"",
$"void main()",
$"{{",
$" vec2 pos = vec2(float({DefaultPositionName}.x), float({DefaultPositionName}.y));",
$" vec2 pos = vec2({DefaultPositionName}.x, {DefaultPositionName}.y);",
$" varTexCoord = vec2(pos.x / {DefaultResolutionName}.x, ({DefaultResolutionName}.y - pos.y) / {DefaultResolutionName}.y);",
$" gl_Position = {DefaultProjectionMatrixName} * {DefaultModelViewMatrixName} * vec4(pos, 0.001f, 1.0f);",
$"}}"
Expand Down
90 changes: 90 additions & 0 deletions Ambermoon.Renderer.OpenGL/FloatPositionBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* FloatPositionBuffer.cs - Buffer for shader floating-point position data
*
* Copyright (C) 2023 Robert Schneckenhaus <[email protected]>
*
* This file is part of Ambermoon.net.
*
* Ambermoon.net is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ambermoon.net 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.
*
* You should have received a copy of the GNU General Public License
* along with Ambermoon.net. If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace Ambermoon.Renderer
{
internal class FloatPositionBuffer : BufferObject<float>
{
public override int Dimension => 2;

public FloatPositionBuffer(State state, bool staticData)
: base(state, staticData)
{

}

bool UpdatePositionData(float[] buffer, int index, Tuple<float, float> position)
{
bool changed = false;
float x = position.Item1;
float y = position.Item2;

if (!Util.FloatEqual(buffer[index + 0], x) ||
!Util.FloatEqual(buffer[index + 1], y))
{
buffer[index + 0] = x;
buffer[index + 1] = y;
changed = true;
}

return changed || index == Size;
}

public int Add(float x, float y, int index = -1)
{
return Add(UpdatePositionData, Tuple.Create(x, y), index);
}

public void Update(int index, float x, float y)
{
Update(UpdatePositionData, index, Tuple.Create(x, y));
}

public void TransformAll(Func<int, Tuple<float, float>, Tuple<float, float>> updater)
{
bool TransformPositionData(float[] buffer, int index, Tuple<float, float> _)
{
bool changed = false;
var position = Tuple.Create(buffer[index + 0], buffer[index + 1]);
var newPosition = updater(index, position);
float x = newPosition.Item1;
float y = newPosition.Item2;

if (!Util.FloatEqual(buffer[index + 0], x) ||
!Util.FloatEqual(buffer[index + 1], y))
{
buffer[index + 0] = x;
buffer[index + 1] = y;
changed = true;
}

return changed || index == Size;
}

for (int i = 0; i < Size; ++i)
{
Update<Tuple<float, float>>(TransformPositionData, i, null);
}
}
}
}
8 changes: 4 additions & 4 deletions Ambermoon.Renderer.OpenGL/FowShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ internal class FowShader : ColorShader
protected static string[] FowVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"in uint {DefaultLayerName};",
$"in ivec2 {DefaultCenterName};",
$"in vec2 {DefaultCenterName};",
$"in uint {DefaultRadiusName};",
$"uniform float {DefaultZName};",
$"uniform mat4 {DefaultProjectionMatrixName};",
Expand All @@ -58,8 +58,8 @@ internal class FowShader : ColorShader
$"",
$"void main()",
$"{{",
$" vec2 pos = vec2(float({DefaultPositionName}.x), float({DefaultPositionName}.y));",
$" fowCenter = vec2(float({DefaultCenterName}.x), float({DefaultCenterName}.y));",
$" vec2 pos = vec2({DefaultPositionName}.x, {DefaultPositionName}.y);",
$" fowCenter = vec2({DefaultCenterName}.x, {DefaultCenterName}.y);",
$" fowRadius = float({DefaultRadiusName});",
$" currentPos = pos;",
$" gl_Position = {DefaultProjectionMatrixName} * {DefaultModelViewMatrixName} * vec4(pos + vec2(0.49f, 0.49f), 1.0f - {DefaultZName} - float({DefaultLayerName}) * 0.00001f, 1.0f);",
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.Renderer.OpenGL/ImageShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class ImageShader : TextureShader
static string[] ImageVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"in ivec2 {DefaultTexCoordName};",
$"in uint {DefaultLayerName};",
$"in uint {DefaultAlphaName};",
Expand All @@ -56,7 +56,7 @@ internal class ImageShader : TextureShader
$"void main()",
$"{{",
$" vec2 atlasFactor = vec2(1.0f / float({DefaultAtlasSizeName}.x), 1.0f / float({DefaultAtlasSizeName}.y));",
$" vec2 pos = vec2(float({DefaultPositionName}.x), float({DefaultPositionName}.y));",
$" vec2 pos = vec2({DefaultPositionName}.x, {DefaultPositionName}.y);",
$" varTexCoord = atlasFactor * vec2({DefaultTexCoordName}.x, {DefaultTexCoordName}.y);",
$" float z = 1.0f - {DefaultZName} - float({DefaultLayerName}) * 0.00001f;",
$" a = float({DefaultAlphaName}) / 255.0f;",
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.Renderer.OpenGL/OpaqueTextureShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal class OpaqueTextureShader : TextureShader
protected static string[] OpaqueTextureVertexShader(State state) => new string[]
{
GetVertexShaderHeader(state),
$"in ivec2 {DefaultPositionName};",
$"in vec2 {DefaultPositionName};",
$"in ivec2 {DefaultTexCoordName};",
$"in uint {DefaultLayerName};",
$"in uint {DefaultPaletteIndexName};",
Expand All @@ -67,7 +67,7 @@ internal class OpaqueTextureShader : TextureShader
$"void main()",
$"{{",
$" vec2 atlasFactor = vec2(1.0f / float({DefaultAtlasSizeName}.x), 1.0f / float({DefaultAtlasSizeName}.y));",
$" vec2 pos = vec2(float({DefaultPositionName}.x) + 0.49f, float({DefaultPositionName}.y) + 0.49f);",
$" vec2 pos = vec2({DefaultPositionName}.x + 0.49f, {DefaultPositionName}.y + 0.49f);",
$" varTexCoord = atlasFactor * vec2({DefaultTexCoordName}.x, {DefaultTexCoordName}.y);",
$" palIndex = float({DefaultPaletteIndexName});",
$" gl_Position = {DefaultProjectionMatrixName} * {DefaultModelViewMatrixName} * vec4(pos, 1.0f - {DefaultZName} - float({DefaultLayerName}) * 0.00001f, 1.0f);",
Expand Down
Loading

0 comments on commit 2335c15

Please sign in to comment.