|
|
|
@ -20,7 +20,6 @@
|
|
|
|
|
namespace nostalgia::core {
|
|
|
|
|
|
|
|
|
|
constexpr auto Scale = 1;
|
|
|
|
|
constexpr auto PriorityScale = 0.01f;
|
|
|
|
|
|
|
|
|
|
namespace renderer {
|
|
|
|
|
|
|
|
|
@ -33,19 +32,16 @@ void Drawer::draw(turbine::Context &tctx) noexcept {
|
|
|
|
|
constexpr ox::CStringView bgvshadTmpl = R"glsl(
|
|
|
|
|
{}
|
|
|
|
|
in vec2 vTexCoord;
|
|
|
|
|
in vec3 vPosition;
|
|
|
|
|
in vec2 vPosition;
|
|
|
|
|
in float vTileIdx;
|
|
|
|
|
out vec2 fTexCoord;
|
|
|
|
|
uniform float vXScale;
|
|
|
|
|
uniform float vTileHeight;
|
|
|
|
|
uniform float vBgIdx;
|
|
|
|
|
void main() {
|
|
|
|
|
float xScaleInvert = 1.0 - vXScale;
|
|
|
|
|
gl_Position = vec4(
|
|
|
|
|
vPosition.x * vXScale - xScaleInvert,
|
|
|
|
|
vPosition.y,
|
|
|
|
|
vPosition.z - 0.001 * vBgIdx,
|
|
|
|
|
1.0);
|
|
|
|
|
vPosition.x * vXScale - xScaleInvert, vPosition.y,
|
|
|
|
|
0.5, 1.0);
|
|
|
|
|
fTexCoord = vec2(
|
|
|
|
|
vTexCoord.x,
|
|
|
|
|
vTexCoord.y * vTileHeight + vTileIdx * vTileHeight);
|
|
|
|
@ -59,11 +55,10 @@ constexpr ox::CStringView bgfshadTmpl = R"glsl(
|
|
|
|
|
uniform vec2 fSrcImgSz;
|
|
|
|
|
uniform vec4 fPalette[256];
|
|
|
|
|
void main() {
|
|
|
|
|
vec2 pixelSz = vec2(1, 1) / fSrcImgSz;
|
|
|
|
|
vec2 pixelCoord = floor(fTexCoord / pixelSz) * pixelSz;
|
|
|
|
|
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)];
|
|
|
|
|
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
|
|
|
|
if (outColor.a == 0) {
|
|
|
|
|
discard;
|
|
|
|
|
}
|
|
|
|
|
})glsl";
|
|
|
|
|
|
|
|
|
|
constexpr ox::CStringView spritevshadTmpl = R"glsl(
|
|
|
|
@ -77,11 +72,8 @@ constexpr ox::CStringView spritevshadTmpl = R"glsl(
|
|
|
|
|
void main() {
|
|
|
|
|
float xScaleInvert = 1.0 - vXScale;
|
|
|
|
|
gl_Position = vec4(
|
|
|
|
|
vPosition.x * vXScale - xScaleInvert,
|
|
|
|
|
vPosition.y,
|
|
|
|
|
// offset to ensure sprites draw on top of BGs by default
|
|
|
|
|
vPosition.z - 0.004,
|
|
|
|
|
1.0) * vEnabled;
|
|
|
|
|
vPosition.x * vXScale - xScaleInvert, vPosition.y,
|
|
|
|
|
vPosition.z, 1.0) * vEnabled;
|
|
|
|
|
fTexCoord = vTexCoord * vec2(1, vTileHeight);
|
|
|
|
|
})glsl";
|
|
|
|
|
|
|
|
|
@ -93,11 +85,10 @@ constexpr ox::CStringView spritefshadTmpl = R"glsl(
|
|
|
|
|
uniform vec2 fSrcImgSz;
|
|
|
|
|
uniform vec4 fPalette[256];
|
|
|
|
|
void main() {
|
|
|
|
|
vec2 pixelSz = vec2(1, 1) / fSrcImgSz;
|
|
|
|
|
vec2 pixelCoord = floor(fTexCoord / pixelSz) * pixelSz;
|
|
|
|
|
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)];
|
|
|
|
|
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
|
|
|
|
if (outColor.a == 0) {
|
|
|
|
|
discard;
|
|
|
|
|
}
|
|
|
|
|
})glsl";;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
@ -122,7 +113,7 @@ static void setSpriteBufferObject(
|
|
|
|
|
y *= -ymod;
|
|
|
|
|
x -= 1.f;
|
|
|
|
|
y += 1.f - ymod;
|
|
|
|
|
auto const prif = static_cast<float>(priority) * PriorityScale;
|
|
|
|
|
auto const prif = static_cast<float>(priority) * 0.1f;
|
|
|
|
|
auto const textureRowf = static_cast<float>(textureRow);
|
|
|
|
|
float const L = flipX ? 1 : 0;
|
|
|
|
|
float const R = flipX ? 0 : 1;
|
|
|
|
@ -146,7 +137,6 @@ static void setTileBufferObject(
|
|
|
|
|
float x,
|
|
|
|
|
float y,
|
|
|
|
|
float textureTileIdx,
|
|
|
|
|
float priority,
|
|
|
|
|
float *vbo,
|
|
|
|
|
GLuint *ebo) noexcept {
|
|
|
|
|
// don't worry, this memcpy gets optimized to something much more ideal
|
|
|
|
@ -156,12 +146,11 @@ static void setTileBufferObject(
|
|
|
|
|
y *= -ymod;
|
|
|
|
|
x -= 1.0f;
|
|
|
|
|
y += 1.0f - ymod;
|
|
|
|
|
auto const prif = priority * PriorityScale;
|
|
|
|
|
ox::Array<float, BgVertexVboLength> const vertices {
|
|
|
|
|
x, y, prif, 0, 1, textureTileIdx, // bottom left
|
|
|
|
|
x + xmod, y, prif, 1, 1, textureTileIdx, // bottom right
|
|
|
|
|
x + xmod, y + ymod, prif, 1, 0, textureTileIdx, // top right
|
|
|
|
|
x, y + ymod, prif, 0, 0, textureTileIdx, // top left
|
|
|
|
|
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));
|
|
|
|
|
ox::Array<GLuint, BgVertexEboLength> const elms {
|
|
|
|
@ -179,18 +168,17 @@ static void initSpriteBufferObjects(glutils::BufferSet &bs) noexcept {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void initBackgroundBufferObjects(glutils::BufferSet &bs, float priority) noexcept {
|
|
|
|
|
static void initBackgroundBufferObjects(glutils::BufferSet &bg) noexcept {
|
|
|
|
|
for (auto x = 0u; x < TileColumns; ++x) {
|
|
|
|
|
for (auto y = 0u; y < TileRows; ++y) {
|
|
|
|
|
const auto i = bgVertexRow(x, y);
|
|
|
|
|
auto vbo = &bs.vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
|
|
|
|
|
auto ebo = &bs.elements[i * static_cast<std::size_t>(BgVertexEboLength)];
|
|
|
|
|
auto vbo = &bg.vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
|
|
|
|
|
auto ebo = &bg.elements[i * static_cast<std::size_t>(BgVertexEboLength)];
|
|
|
|
|
setTileBufferObject(
|
|
|
|
|
static_cast<uint_t>(i * BgVertexVboRows),
|
|
|
|
|
static_cast<float>(x),
|
|
|
|
|
static_cast<float>(y),
|
|
|
|
|
0,
|
|
|
|
|
priority,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
}
|
|
|
|
@ -228,31 +216,30 @@ static void initSpritesBufferset(Context &ctx) noexcept {
|
|
|
|
|
|
|
|
|
|
static void initBackgroundBufferset(
|
|
|
|
|
GLuint shader,
|
|
|
|
|
glutils::BufferSet &bs,
|
|
|
|
|
float priority) noexcept {
|
|
|
|
|
glutils::BufferSet &bg) noexcept {
|
|
|
|
|
// vao
|
|
|
|
|
bs.vao = glutils::generateVertexArrayObject();
|
|
|
|
|
glBindVertexArray(bs.vao);
|
|
|
|
|
bg.vao = glutils::generateVertexArrayObject();
|
|
|
|
|
glBindVertexArray(bg.vao);
|
|
|
|
|
// vbo & ebo
|
|
|
|
|
bs.vbo = glutils::generateBuffer();
|
|
|
|
|
bs.ebo = glutils::generateBuffer();
|
|
|
|
|
initBackgroundBufferObjects(bs, priority);
|
|
|
|
|
glutils::sendVbo(bs);
|
|
|
|
|
glutils::sendEbo(bs);
|
|
|
|
|
bg.vbo = glutils::generateBuffer();
|
|
|
|
|
bg.ebo = glutils::generateBuffer();
|
|
|
|
|
initBackgroundBufferObjects(bg);
|
|
|
|
|
glutils::sendVbo(bg);
|
|
|
|
|
glutils::sendEbo(bg);
|
|
|
|
|
// vbo layout
|
|
|
|
|
auto const posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
|
|
|
|
|
glEnableVertexAttribArray(posAttr);
|
|
|
|
|
glVertexAttribPointer(posAttr, 3, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
|
|
|
|
|
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
|
|
|
|
|
auto const texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
|
|
|
|
|
glEnableVertexAttribArray(texCoordAttr);
|
|
|
|
|
glVertexAttribPointer(
|
|
|
|
|
texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
|
|
|
|
|
std::bit_cast<void*>(uintptr_t{3 * sizeof(float)}));
|
|
|
|
|
std::bit_cast<void*>(uintptr_t{2 * sizeof(float)}));
|
|
|
|
|
auto const heightMultAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTileIdx"));
|
|
|
|
|
glEnableVertexAttribArray(heightMultAttr);
|
|
|
|
|
glVertexAttribPointer(
|
|
|
|
|
heightMultAttr, 1, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
|
|
|
|
|
std::bit_cast<void*>(uintptr_t{5 * sizeof(float)}));
|
|
|
|
|
std::bit_cast<void*>(uintptr_t{4 * sizeof(float)}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static glutils::GLTexture createTexture(
|
|
|
|
@ -292,12 +279,10 @@ static void drawBackgrounds(
|
|
|
|
|
const auto uniformSrcImgSz = glGetUniformLocation(ctx.bgShader, "fSrcImgSz");
|
|
|
|
|
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vXScale"));
|
|
|
|
|
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vTileHeight"));
|
|
|
|
|
const auto uniformBgIdx = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vBgIdx"));
|
|
|
|
|
const auto [wi, hi] = renderSz;
|
|
|
|
|
const auto wf = static_cast<float>(wi);
|
|
|
|
|
const auto hf = static_cast<float>(hi);
|
|
|
|
|
glUniform1f(uniformXScale, hf / wf);
|
|
|
|
|
auto bgIdx = 0.f;
|
|
|
|
|
for (const auto &bg : ctx.backgrounds) {
|
|
|
|
|
if (bg.enabled) {
|
|
|
|
|
auto &cbb = ctx.cbbs[bg.cbbIdx];
|
|
|
|
@ -307,9 +292,7 @@ static void drawBackgrounds(
|
|
|
|
|
uniformSrcImgSz,
|
|
|
|
|
static_cast<float>(cbb.tex.width),
|
|
|
|
|
static_cast<float>(cbb.tex.height));
|
|
|
|
|
glUniform1f(uniformBgIdx, bgIdx);
|
|
|
|
|
drawBackground(cbb);
|
|
|
|
|
++bgIdx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -340,7 +323,7 @@ static void drawSprites(Context &ctx, ox::Size const&renderSz) noexcept {
|
|
|
|
|
static void loadPalette(
|
|
|
|
|
GLuint shaderPgrm,
|
|
|
|
|
Palette const&pal,
|
|
|
|
|
bool firstIsTransparent) noexcept {
|
|
|
|
|
bool firstIsTransparent = false) noexcept {
|
|
|
|
|
static constexpr std::size_t ColorCnt = 256;
|
|
|
|
|
ox::Array<GLfloat, ColorCnt * 4> palette{};
|
|
|
|
|
for (auto i = 0u; const auto c : pal.colors) {
|
|
|
|
@ -358,7 +341,7 @@ static void loadPalette(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void loadBgPalette(Context &ctx, Palette const&pal) noexcept {
|
|
|
|
|
loadPalette(ctx.bgShader, pal, true);
|
|
|
|
|
loadPalette(ctx.bgShader, pal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void loadSpritePalette(Context &ctx, Palette const&pal) noexcept {
|
|
|
|
@ -433,7 +416,7 @@ static void setSprite(
|
|
|
|
|
fY,
|
|
|
|
|
s.tileIdx + i,
|
|
|
|
|
s.flipX,
|
|
|
|
|
s.priority & 0b11,
|
|
|
|
|
s.priority,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
++i;
|
|
|
|
@ -473,8 +456,8 @@ ox::Error initGfx(
|
|
|
|
|
oxReturnError(glutils::buildShaderProgram(bgVshad, bgFshad).moveTo(ctx.bgShader));
|
|
|
|
|
oxReturnError(
|
|
|
|
|
glutils::buildShaderProgram(spriteVshad, spriteFshad).moveTo(ctx.spriteShader));
|
|
|
|
|
for (auto &cbb : ctx.cbbs) {
|
|
|
|
|
initBackgroundBufferset(ctx.bgShader, cbb, 0);
|
|
|
|
|
for (auto &bg : ctx.cbbs) {
|
|
|
|
|
initBackgroundBufferset(ctx.bgShader, bg);
|
|
|
|
|
}
|
|
|
|
|
renderer::initSpritesBufferset(ctx);
|
|
|
|
|
if (initParams.glInstallDrawer) {
|
|
|
|
@ -579,11 +562,6 @@ void setBgCbb(Context &ctx, uint_t bgIdx, uint_t cbbIdx) noexcept {
|
|
|
|
|
bg.cbbIdx = cbbIdx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setBgPriority(Context &ctx, uint_t bgIdx, uint_t priority) noexcept {
|
|
|
|
|
auto &bg = ctx.backgrounds[bgIdx];
|
|
|
|
|
bg.priority = static_cast<float>(priority & 0b11);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t bgStatus(Context &ctx) noexcept {
|
|
|
|
|
uint8_t out = 0;
|
|
|
|
|
for (uint_t i = 0; i < ctx.cbbs.size(); ++i) {
|
|
|
|
@ -608,11 +586,9 @@ void setBgStatus(Context &ctx, uint_t bg, bool status) noexcept {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void clearTileLayer(Context &ctx, uint_t bgIdx) noexcept {
|
|
|
|
|
auto &cbb = ctx.cbbs[static_cast<std::size_t>(bgIdx)];
|
|
|
|
|
initBackgroundBufferObjects(cbb, 0);
|
|
|
|
|
cbb.updated = true;
|
|
|
|
|
auto &bg = ctx.backgrounds[static_cast<std::size_t>(bgIdx)];
|
|
|
|
|
bg.priority = 0;
|
|
|
|
|
auto &bg = ctx.cbbs[static_cast<std::size_t>(bgIdx)];
|
|
|
|
|
initBackgroundBufferObjects(bg);
|
|
|
|
|
bg.updated = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hideSprite(Context &ctx, uint_t idx) noexcept {
|
|
|
|
@ -647,19 +623,17 @@ void setTile(
|
|
|
|
|
const auto y = static_cast<uint_t>(row);
|
|
|
|
|
const auto x = static_cast<uint_t>(column);
|
|
|
|
|
const auto i = renderer::bgVertexRow(x, y);
|
|
|
|
|
auto &cbb = ctx.cbbs[z];
|
|
|
|
|
const auto vbo = &cbb.vertices[i * renderer::BgVertexVboLength];
|
|
|
|
|
const auto ebo = &cbb.elements[i * renderer::BgVertexEboLength];
|
|
|
|
|
auto &bg = ctx.backgrounds[bgIdx];
|
|
|
|
|
auto &bg = ctx.cbbs[z];
|
|
|
|
|
const auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
|
|
|
|
|
const auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
|
|
|
|
|
renderer::setTileBufferObject(
|
|
|
|
|
static_cast<uint_t>(i * renderer::BgVertexVboRows),
|
|
|
|
|
static_cast<float>(x),
|
|
|
|
|
static_cast<float>(y),
|
|
|
|
|
static_cast<float>(tile),
|
|
|
|
|
bg.priority,
|
|
|
|
|
vbo,
|
|
|
|
|
ebo);
|
|
|
|
|
cbb.updated = true;
|
|
|
|
|
bg.updated = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace gl {
|
|
|
|
|