|
|
|
@ -27,6 +27,8 @@ inline GlContext &glctx(Context &ctx) noexcept {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr auto Scale = 5;
|
|
|
|
|
|
|
|
|
|
namespace renderer {
|
|
|
|
|
|
|
|
|
|
Drawer::Drawer(Context &ctx) noexcept: m_ctx(ctx) {}
|
|
|
|
@ -47,7 +49,7 @@ constexpr ox::StringView bgvshadTmpl = R"(
|
|
|
|
|
gl_Position = vec4(
|
|
|
|
|
vPosition.x * vXScale - xScaleInvert, vPosition.y,
|
|
|
|
|
0.0, 1.0);
|
|
|
|
|
fTexCoord = vTexCoord * vec2(1, vTileHeight);
|
|
|
|
|
fTexCoord = vTexCoord;
|
|
|
|
|
})";
|
|
|
|
|
|
|
|
|
|
constexpr ox::StringView bgfshadTmpl = R"(
|
|
|
|
@ -55,10 +57,28 @@ constexpr ox::StringView bgfshadTmpl = R"(
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
in vec2 fTexCoord;
|
|
|
|
|
uniform sampler2D image;
|
|
|
|
|
uniform vec2 fSrcImgSz;
|
|
|
|
|
uniform vec4 fPalette[256];
|
|
|
|
|
vec2 pixelSz;
|
|
|
|
|
vec4 getColor(vec2 offset) {
|
|
|
|
|
vec2 p = fTexCoord + pixelSz * offset;
|
|
|
|
|
int idx = int(texture(image, p).rgb.r * 256);
|
|
|
|
|
return fPalette[idx];
|
|
|
|
|
}
|
|
|
|
|
void main() {
|
|
|
|
|
int idx = int(texture(image, fTexCoord).rgb.r * 256);
|
|
|
|
|
outColor = fPalette[idx];
|
|
|
|
|
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);
|
|
|
|
|
})";
|
|
|
|
|
|
|
|
|
@ -118,11 +138,12 @@ static void setSpriteBufferObject(
|
|
|
|
|
memcpy(ebo, elms.data(), sizeof(elms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setTileBufferObject(
|
|
|
|
|
static void setTileBufferObject(
|
|
|
|
|
uint_t vi,
|
|
|
|
|
float x,
|
|
|
|
|
float y,
|
|
|
|
|
uint_t textureRow,
|
|
|
|
|
float textureYOffset,
|
|
|
|
|
float tileHeight,
|
|
|
|
|
float *vbo,
|
|
|
|
|
GLuint *ebo) noexcept {
|
|
|
|
|
// don't worry, this memcpy gets optimized to something much more ideal
|
|
|
|
@ -132,12 +153,11 @@ void setTileBufferObject(
|
|
|
|
|
y *= -ymod;
|
|
|
|
|
x -= 1.0f;
|
|
|
|
|
y += 1.0f - ymod;
|
|
|
|
|
const auto textureRowf = static_cast<float>(textureRow);
|
|
|
|
|
const ox::Array<float, BgVertexVboLength> vertices {
|
|
|
|
|
x, y, 0, textureRowf + 1, // bottom left
|
|
|
|
|
x + xmod, y, 1, textureRowf + 1, // bottom right
|
|
|
|
|
x + xmod, y + ymod, 1, textureRowf + 0, // top right
|
|
|
|
|
x, y + ymod, 0, textureRowf + 0, // top left
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
memcpy(vbo, vertices.data(), sizeof(vertices));
|
|
|
|
|
const ox::Array<GLuint, BgVertexEboLength> elms {
|
|
|
|
@ -166,6 +186,7 @@ static void initBackgroundBufferObjects(glutils::BufferSet &bg) noexcept {
|
|
|
|
|
static_cast<float>(x),
|
|
|
|
|
static_cast<float>(y),
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
}
|
|
|
|
@ -230,9 +251,9 @@ static glutils::GLTexture loadTexture(
|
|
|
|
|
tex.height = h;
|
|
|
|
|
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_NEAREST);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
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_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
return tex;
|
|
|
|
@ -253,6 +274,7 @@ static void drawBackgrounds(
|
|
|
|
|
ox::Size const&renderSz) noexcept {
|
|
|
|
|
// load background shader and its uniforms
|
|
|
|
|
glUseProgram(gctx.bgShader);
|
|
|
|
|
const auto uniformSrcImgSz = glGetUniformLocation(gctx.bgShader, "fSrcImgSz");
|
|
|
|
|
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vXScale"));
|
|
|
|
|
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vTileHeight"));
|
|
|
|
|
const auto [wi, hi] = renderSz;
|
|
|
|
@ -262,8 +284,12 @@ static void drawBackgrounds(
|
|
|
|
|
for (const auto &bg : gctx.backgrounds) {
|
|
|
|
|
if (bg.enabled) {
|
|
|
|
|
auto &cbb = gctx.cbbs[bg.cbbIdx];
|
|
|
|
|
const auto tileRows = cbb.tex.height / TileHeight;
|
|
|
|
|
const auto tileRows = cbb.tex.height / (TileHeight * Scale);
|
|
|
|
|
glUniform1f(uniformTileHeight, 1.0f / static_cast<float>(tileRows));
|
|
|
|
|
glUniform2f(
|
|
|
|
|
uniformSrcImgSz,
|
|
|
|
|
static_cast<float>(cbb.tex.width),
|
|
|
|
|
static_cast<float>(cbb.tex.height));
|
|
|
|
|
drawBackground(cbb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -272,8 +298,8 @@ static void drawBackgrounds(
|
|
|
|
|
static void drawSprites(GlContext &gctx, ox::Size const&renderSz) noexcept {
|
|
|
|
|
glUseProgram(gctx.spriteShader);
|
|
|
|
|
auto &sb = gctx.spriteBlocks;
|
|
|
|
|
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vXScale"));
|
|
|
|
|
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx.spriteShader, "vTileHeight"));
|
|
|
|
|
const auto uniformXScale = glGetUniformLocation(gctx.bgShader, "vXScale");
|
|
|
|
|
const auto uniformTileHeight = glGetUniformLocation(gctx.spriteShader, "vTileHeight");
|
|
|
|
|
const auto [wi, hi] = renderSz;
|
|
|
|
|
const auto wf = static_cast<float>(wi);
|
|
|
|
|
const auto hf = static_cast<float>(hi);
|
|
|
|
@ -285,7 +311,7 @@ static void drawSprites(GlContext &gctx, ox::Size const&renderSz) noexcept {
|
|
|
|
|
glutils::sendVbo(sb);
|
|
|
|
|
}
|
|
|
|
|
// set vTileHeight uniform
|
|
|
|
|
const auto tileRows = sb.tex.height / TileHeight;
|
|
|
|
|
const auto tileRows = sb.tex.height / (TileHeight * Scale);
|
|
|
|
|
glUniform1f(uniformTileHeight, 1.0f / static_cast<float>(tileRows));
|
|
|
|
|
// draw
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, sb.tex);
|
|
|
|
@ -326,7 +352,7 @@ static void loadBgTexture(
|
|
|
|
|
const void *pixels,
|
|
|
|
|
int w,
|
|
|
|
|
int h) noexcept {
|
|
|
|
|
oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h);
|
|
|
|
|
oxTracef("nostalgia.core.gfx.gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h);
|
|
|
|
|
gctx.cbbs[cbbIdx].tex = loadTexture(w, h, pixels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -335,7 +361,7 @@ static void loadSpriteTexture(
|
|
|
|
|
const void *pixels,
|
|
|
|
|
int w,
|
|
|
|
|
int h) noexcept {
|
|
|
|
|
oxTracef("nostalgia::core::gfx::gl", "loadSpriteTexture: { w: {}, h: {} }", w, h);
|
|
|
|
|
oxTracef("nostalgia.core.gfx.gl", "loadSpriteTexture: { w: {}, h: {} }", w, h);
|
|
|
|
|
gctx.spriteBlocks.tex = loadTexture(w, h, pixels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -373,6 +399,10 @@ struct TileSheetData {
|
|
|
|
|
ox::Vector<uint32_t> pixels;
|
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr ox::Size size() const noexcept {
|
|
|
|
|
return {width, height};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ox::Result<TileSheetData> loadTileSheet(
|
|
|
|
@ -408,7 +438,13 @@ ox::Error loadBgTileSheet(
|
|
|
|
|
auto &kctx = gctx.turbineCtx.keelCtx;
|
|
|
|
|
oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr));
|
|
|
|
|
oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
|
|
|
|
|
oxRequire(tsd, loadTileSheet(*ctx, *tilesheet));
|
|
|
|
|
oxRequire(tsd, loadTileSheet(*ctx, *tilesheet).to([](TileSheetData const&t) -> TileSheetData {
|
|
|
|
|
return {
|
|
|
|
|
.pixels = resizeTileSheetData(t.pixels, t.size(), Scale),
|
|
|
|
|
.width = t.width * Scale,
|
|
|
|
|
.height = t.height * Scale,
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
renderer::loadBgTexture(gctx, cbb, tsd.pixels.data(), tsd.width, tsd.height);
|
|
|
|
|
renderer::loadBgPalette(gctx, *palette);
|
|
|
|
|
return {};
|
|
|
|
@ -537,8 +573,15 @@ void setSprite(
|
|
|
|
|
const auto cidx = idx + i;
|
|
|
|
|
auto vbo = &gctx.spriteBlocks.vertices[cidx * renderer::SpriteVertexVboLength];
|
|
|
|
|
auto ebo = &gctx.spriteBlocks.elements[cidx * renderer::SpriteVertexEboLength];
|
|
|
|
|
renderer::setSpriteBufferObject(cidx * renderer::SpriteVertexVboRows, 1,
|
|
|
|
|
fX, fY, tileIdx + i, flipX, vbo, ebo);
|
|
|
|
|
renderer::setSpriteBufferObject(
|
|
|
|
|
cidx * renderer::SpriteVertexVboRows,
|
|
|
|
|
1,
|
|
|
|
|
fX,
|
|
|
|
|
fY,
|
|
|
|
|
tileIdx + i,
|
|
|
|
|
flipX,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
++i;
|
|
|
|
|
};
|
|
|
|
|
if (!flipX) {
|
|
|
|
@ -564,7 +607,7 @@ void setTile(
|
|
|
|
|
int row,
|
|
|
|
|
uint8_t tile) noexcept {
|
|
|
|
|
oxTracef(
|
|
|
|
|
"nostalgia::core::gfx::setTile",
|
|
|
|
|
"nostalgia.core.gfx.setTile",
|
|
|
|
|
"bgIdx: {}, column: {}, row: {}, tile: {}",
|
|
|
|
|
bgIdx, column, row, tile);
|
|
|
|
|
auto &gctx = glctx(*ctx);
|
|
|
|
@ -573,13 +616,16 @@ void setTile(
|
|
|
|
|
const auto x = static_cast<uint_t>(column);
|
|
|
|
|
const auto i = renderer::bgVertexRow(x, y);
|
|
|
|
|
auto &bg = gctx.cbbs[z];
|
|
|
|
|
auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
|
|
|
|
|
auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
|
|
|
|
|
const auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
|
|
|
|
|
const auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
|
|
|
|
|
const auto tileHeight = static_cast<float>(TileHeight * Scale) / static_cast<float>(bg.tex.height);
|
|
|
|
|
const auto tileOffset = tileHeight * static_cast<float>(tile);
|
|
|
|
|
renderer::setTileBufferObject(
|
|
|
|
|
static_cast<uint_t>(i * renderer::BgVertexVboRows),
|
|
|
|
|
static_cast<float>(x),
|
|
|
|
|
static_cast<float>(y),
|
|
|
|
|
tile,
|
|
|
|
|
tileOffset,
|
|
|
|
|
tileHeight,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
bg.updated = true;
|
|
|
|
|