From 9e9f30fb187bf6ce10c1de9fd9f91027e3a4a3f4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 9 Dec 2023 00:05:14 -0600 Subject: [PATCH] [nostalgia/core] Fix some GL rendering issues --- src/nostalgia/modules/core/src/opengl/gfx.cpp | 75 +++++++++---------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/src/nostalgia/modules/core/src/opengl/gfx.cpp b/src/nostalgia/modules/core/src/opengl/gfx.cpp index 8395614f..21e5358d 100644 --- a/src/nostalgia/modules/core/src/opengl/gfx.cpp +++ b/src/nostalgia/modules/core/src/opengl/gfx.cpp @@ -15,9 +15,12 @@ #include #include "context.hpp" +#include "gfx.hpp" namespace nostalgia::core { +constexpr auto Scale = 10; + [[nodiscard]] inline GlContext &glctx(Context &ctx) noexcept { if constexpr(ox::defines::Debug) { @@ -27,8 +30,6 @@ inline GlContext &glctx(Context &ctx) noexcept { } } -constexpr auto Scale = 5; - namespace renderer { Drawer::Drawer(Context &ctx) noexcept: m_ctx(ctx) {} @@ -37,10 +38,11 @@ void Drawer::draw(turbine::Context &tctx) noexcept { core::gl::draw(m_ctx, turbine::getScreenSize(tctx)); } -constexpr ox::StringView bgvshadTmpl = R"( +constexpr ox::StringView bgvshadTmpl = R"glsl( {} in vec2 vTexCoord; in vec2 vPosition; + in float vTileIdx; out vec2 fTexCoord; uniform float vXScale; uniform float vTileHeight; @@ -49,10 +51,12 @@ constexpr ox::StringView bgvshadTmpl = R"( gl_Position = vec4( vPosition.x * vXScale - xScaleInvert, vPosition.y, 0.0, 1.0); - fTexCoord = vTexCoord; - })"; + fTexCoord = vec2( + vTexCoord.x, + vTexCoord.y * vTileHeight + vTileIdx * vTileHeight); + })glsl"; -constexpr ox::StringView bgfshadTmpl = R"( +constexpr ox::StringView bgfshadTmpl = R"glsl( {} out vec4 outColor; in vec2 fTexCoord; @@ -68,21 +72,11 @@ constexpr ox::StringView bgfshadTmpl = R"( void main() { pixelSz = vec2(1, 1) / (fSrcImgSz); vec2 pixelCoord = floor(fTexCoord / pixelSz) * pixelSz; - vec4 c0 = getColor(pixelCoord + pixelSz * vec2(0, 0)); - vec4 c1 = getColor(pixelCoord + pixelSz * vec2(0.75, 0)); - vec4 c2 = getColor(pixelCoord + pixelSz * vec2(0, 0.75)); - vec4 c3 = getColor(pixelCoord + pixelSz * vec2(0.75, 0.75)); - float u = fTexCoord.x - fTexCoord.x / pixelSz.x; - float v = fTexCoord.y - fTexCoord.y / pixelSz.y; - vec4 b0 = (1.0 - u) * c0 + u * c1; - vec4 b1 = (1.0 - u) * c2 + u * c3; - outColor = (1.0 - v) * b0 + u * b1; outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)]; - //outColor = fPalette[int(texture(image, pixelCoord).rgb.r * 256)]; //outColor = vec4(0.0, 0.7, 1.0, 1.0); - })"; + })glsl"; -constexpr ox::StringView spritevshadTmpl = R"( +constexpr ox::StringView spritevshadTmpl = R"glsl( {} in float vEnabled; in vec2 vTexCoord; @@ -96,7 +90,7 @@ constexpr ox::StringView spritevshadTmpl = R"( vPosition.x * vXScale - xScaleInvert, vPosition.y, 0.0, 1.0); fTexCoord = vTexCoord * vec2(1, vTileHeight) * vec2(vEnabled, vEnabled); - })"; + })glsl"; constexpr ox::StringView spritefshadTmpl = bgfshadTmpl; @@ -142,8 +136,7 @@ static void setTileBufferObject( uint_t vi, float x, float y, - float textureYOffset, - float tileHeight, + float textureTileIdx, float *vbo, GLuint *ebo) noexcept { // don't worry, this memcpy gets optimized to something much more ideal @@ -153,14 +146,14 @@ static void setTileBufferObject( y *= -ymod; x -= 1.0f; y += 1.0f - ymod; - const ox::Array vertices { - x, y, 0, tileHeight + textureYOffset, // bottom left - x + xmod, y, 1, tileHeight + textureYOffset, // bottom right - x + xmod, y + ymod, 1, 0 + textureYOffset, // top right - x, y + ymod, 0, 0 + textureYOffset, // top left + ox::Array const vertices { + x, y, 0, 1, textureTileIdx, // bottom left + x + xmod, y, 1, 1, textureTileIdx, // bottom right + x + xmod, y + ymod, 1, 0, textureTileIdx, // top right + x, y + ymod, 0, 0, textureTileIdx, // top left }; memcpy(vbo, vertices.data(), sizeof(vertices)); - const ox::Array elms { + ox::Array const elms { vi + 0, vi + 1, vi + 2, vi + 2, vi + 3, vi + 0, }; @@ -186,7 +179,6 @@ static void initBackgroundBufferObjects(glutils::BufferSet &bg) noexcept { static_cast(x), static_cast(y), 0, - 0, vbo, ebo); } @@ -236,11 +228,16 @@ static void initBackgroundBufferset( auto const texCoordAttr = static_cast(glGetAttribLocation(shader, "vTexCoord")); glEnableVertexAttribArray(texCoordAttr); glVertexAttribPointer( - texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(bg.vertices[0]), - reinterpret_cast(2 * sizeof(bg.vertices[0]))); + texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), + reinterpret_cast(2 * sizeof(float))); + auto const heightMultAttr = static_cast(glGetAttribLocation(shader, "vTileIdx")); + glEnableVertexAttribArray(heightMultAttr); + glVertexAttribPointer( + heightMultAttr, 1, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), + reinterpret_cast(4 * sizeof(float))); } -static glutils::GLTexture loadTexture( +static glutils::GLTexture createTexture( GLsizei w, GLsizei h, const void *pixels) noexcept { @@ -252,8 +249,8 @@ static glutils::GLTexture loadTexture( glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex.id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); return tex; @@ -353,7 +350,7 @@ static void loadBgTexture( int w, int h) noexcept { oxTracef("nostalgia.core.gfx.gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h); - gctx.cbbs[cbbIdx].tex = loadTexture(w, h, pixels); + gctx.cbbs[cbbIdx].tex = createTexture(w, h, pixels); } static void loadSpriteTexture( @@ -362,7 +359,7 @@ static void loadSpriteTexture( int w, int h) noexcept { oxTracef("nostalgia.core.gfx.gl", "loadSpriteTexture: { w: {}, h: {} }", w, h); - gctx.spriteBlocks.tex = loadTexture(w, h, pixels); + gctx.spriteBlocks.tex = createTexture(w, h, pixels); } } @@ -406,7 +403,8 @@ struct TileSheetData { }; static ox::Result loadTileSheet( - Context &ctx, CompactTileSheet const&tilesheet) noexcept { + Context &ctx, + CompactTileSheet const&tilesheet) noexcept { auto &gctx = glctx(ctx); const uint_t bytesPerTile = tilesheet.bpp == 8 ? PixelsPerTile : PixelsPerTile / 2; const auto tiles = tilesheet.pixels.size() / bytesPerTile; @@ -618,14 +616,11 @@ void setTile( auto &bg = gctx.cbbs[z]; const auto vbo = &bg.vertices[i * renderer::BgVertexVboLength]; const auto ebo = &bg.elements[i * renderer::BgVertexEboLength]; - const auto tileHeight = static_cast(TileHeight * Scale) / static_cast(bg.tex.height); - const auto tileOffset = tileHeight * static_cast(tile); renderer::setTileBufferObject( static_cast(i * renderer::BgVertexVboRows), static_cast(x), static_cast(y), - tileOffset, - tileHeight, + static_cast(tile), vbo, ebo); bg.updated = true;