Skip to content

Commit

Permalink
cache the gererated sky image to correclty generate sky teūtures
Browse files Browse the repository at this point in the history
  • Loading branch information
nstlaurent committed Jun 13, 2024
1 parent 9c22392 commit fc163dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Helion.Graphics;
using Helion.Render.OpenGL.Renderers.Legacy.World.Sky.Sphere;
using Helion.Render.OpenGL.Shared;
using Helion.Render.OpenGL.Texture.Legacy;
Expand All @@ -14,7 +15,7 @@ public class LegacySkyRenderer : IDisposable
{
private const int MaxSkyTextures = 255;

public static readonly Dictionary<int, GLLegacyTexture> GeneratedTextures = new();
public static readonly Dictionary<int, Image> GeneratedImages = new();

private readonly ArchiveCollection m_archiveCollection;
private readonly LegacyGLTextureManager m_textureManager;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void Reset()

m_skyComponents.Clear();
m_skyComponentsList.Clear();
GeneratedTextures.Clear();
GeneratedImages.Clear();
}

public void Clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Helion.Render.OpenGL.Texture.Legacy;
using Helion.Render.OpenGL.Vertex;
using Helion.Resources.Archives.Collection;
using Helion.Util;
using OpenTK.Graphics.OpenGL;

namespace Helion.Render.OpenGL.Renderers.Legacy.World.Sky.Sphere;
Expand All @@ -15,8 +14,9 @@ public class SkySphereRenderer : IDisposable
{
private const int HorizontalSpherePoints = 64;
private const int VerticalSpherePoints = 64;
private static readonly SphereTable SphereTable = new(HorizontalSpherePoints, VerticalSpherePoints);
private static readonly vec3 UpOpenGL = new(0, 1, 0);
private static SkySphereVertex[] SpherePoints = new SkySphereVertex[VerticalSpherePoints * HorizontalSpherePoints * 6];
private static bool SphereInitialized;

private readonly StaticVertexBuffer<SkySphereVertex> m_vbo;
private readonly VertexArrayObject m_vao;
Expand Down Expand Up @@ -95,10 +95,22 @@ private void DrawSphere(GLLegacyTexture texture)

private void GenerateSphereVerticesAndUpload()
{
int newLength = m_vbo.Data.Length + (VerticalSpherePoints * HorizontalSpherePoints * 6);
m_vbo.Data.EnsureCapacity(newLength);
int index = m_vbo.Data.Length;
var vertices = m_vbo.Data.Data;
if (!SphereInitialized)
{
SphereInitialized = true;
InitializeSpherePoints();
}

m_vbo.Data.Data = SpherePoints;
m_vbo.Data.Length = SpherePoints.Length;
m_vbo.SetNotUploaded();
m_vbo.UploadIfNeeded();
}

private static void InitializeSpherePoints()
{
SphereTable sphereTable = new(HorizontalSpherePoints, VerticalSpherePoints);
int index = 0;
for (int row = 0; row < VerticalSpherePoints; row++)
{
for (int col = 0; col < HorizontalSpherePoints; col++)
Expand All @@ -107,23 +119,20 @@ private void GenerateSphereVerticesAndUpload()
// out of range because we specifically made sure that the
// code adds in one extra vertex for us on both the top row
// and the right column.
SkySphereVertex bottomLeft = SphereTable.MercatorRectangle[row, col];
SkySphereVertex bottomRight = SphereTable.MercatorRectangle[row, col + 1];
SkySphereVertex topLeft = SphereTable.MercatorRectangle[row + 1, col];
SkySphereVertex topRight = SphereTable.MercatorRectangle[row + 1, col + 1];

vertices[index++] = topLeft;
vertices[index++] = bottomLeft;
vertices[index++] = topRight;

vertices[index++] = topRight;
vertices[index++] = bottomLeft;
vertices[index++] = bottomRight;
SkySphereVertex bottomLeft = sphereTable.MercatorRectangle[row, col];
SkySphereVertex bottomRight = sphereTable.MercatorRectangle[row, col + 1];
SkySphereVertex topLeft = sphereTable.MercatorRectangle[row + 1, col];
SkySphereVertex topRight = sphereTable.MercatorRectangle[row + 1, col + 1];

SpherePoints[index++] = topLeft;
SpherePoints[index++] = bottomLeft;
SpherePoints[index++] = topRight;

SpherePoints[index++] = topRight;
SpherePoints[index++] = bottomLeft;
SpherePoints[index++] = bottomRight;
}
}

m_vbo.Data.Length = newLength;
m_vbo.UploadIfNeeded();
}

private void SetUniforms(RenderInfo renderInfo, bool flipSkyHorizontal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ public class SkySphereTexture : IDisposable
record struct SkyTexture(GLLegacyTexture GlTexture, int AnimatedTextureIndex);

private const int PixelRowsToEvaluate = 24;
private const int DefaultPaddingDivisor = 3;

public float ScaleU = 1.0f;
private readonly ArchiveCollection m_archiveCollection;
private readonly LegacyGLTextureManager m_textureManager;
private readonly int m_textureHandleIndex;
private readonly List<SkyTexture> m_skyTextures = new();
private bool m_loadedTextures;
private List<SkyTexture> m_skyTextures = new();

public SkySphereTexture(ArchiveCollection archiveCollection, LegacyGLTextureManager textureManager, int textureHandle)
{
Expand Down Expand Up @@ -223,17 +222,9 @@ private void InitializeAnimatedTextures()
for (int j = 0; j < components.Count; j++)
{
int animatedTextureIndex = components[j].TextureIndex;
if (LegacySkyRenderer.GeneratedTextures.TryGetValue(animatedTextureIndex, out var existingSkyTexture))
{
m_skyTextures.Add(new(existingSkyTexture, animatedTextureIndex));
continue;
}

if (GenerateSkyTextures(animatedTextureIndex, out var skyTexture))
{
LegacySkyRenderer.GeneratedTextures[animatedTextureIndex] = skyTexture;
m_skyTextures.Add(new(skyTexture, animatedTextureIndex));
}
}
}
}
Expand All @@ -248,12 +239,20 @@ private bool GenerateSkyTextures(int textureIndex, [NotNullWhen(true)] out GLLeg
}

ScaleU = CalculateScale(skyImage.Width);
texture = CreateSkyTexture(skyImage);
texture = CreateSkyTexture(textureIndex, skyImage);
return true;
}

private GLLegacyTexture CreateSkyTexture(Image skyImage)
private GLLegacyTexture CreateSkyTexture(int textureIndex, Image skyImage)
{
return CreateTexture(GetFadedSkyImage(textureIndex, skyImage), $"[SKY][{textureIndex}] {m_archiveCollection.TextureManager.SkyTextureName}");
}

private static Image GetFadedSkyImage(int textureIndex, Image skyImage)
{
if (LegacySkyRenderer.GeneratedImages.TryGetValue(textureIndex, out var existingImage))
return existingImage;

// Most (all?) skies are tall enough that we don't have to worry
// about this, but if we run into a sky that is small then we
// don't want to consume more than half of it. We also need to
Expand All @@ -268,8 +267,8 @@ private GLLegacyTexture CreateSkyTexture(Image skyImage)
Color bottomFadeColor = CalculateAverageRowColor(bottomStartY, bottomExclusiveEndY, skyImage);

Image fadedSkyImage = CreateFadedSky(rowsToEvaluate, bottomFadeColor, topFadeColor, skyImage);

return CreateTexture(fadedSkyImage, $"[SKY] {m_archiveCollection.TextureManager.SkyTextureName}");
LegacySkyRenderer.GeneratedImages[textureIndex] = fadedSkyImage;
return fadedSkyImage;
}

private GLLegacyTexture CreateTexture(Image fadedSkyImage, string debugName = "")
Expand Down

0 comments on commit fc163dc

Please sign in to comment.