[nostalgia] Move much of the OpenGL code to glutils
This commit is contained in:
@@ -6,19 +6,13 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <ox/claw/claw.hpp>
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
#include <nostalgia/core/media.hpp>
|
||||
|
||||
#include "gfx.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
template<typename T>
|
||||
static ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) noexcept {
|
||||
oxRequire(buff, ctx->rom->read(file));
|
||||
return ox::readClaw<T>(buff);
|
||||
}
|
||||
|
||||
ox::Error initConsole(Context *ctx) noexcept {
|
||||
constexpr auto TilesheetAddr = "/TileSheets/Charset.ng";
|
||||
constexpr auto PaletteAddr = "/Palettes/Charset.npal";
|
||||
|
||||
@@ -33,15 +33,14 @@ constexpr auto BgVertexVboRowLength = 4;
|
||||
constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength;
|
||||
constexpr auto BgVertexEboLength = 6;
|
||||
|
||||
struct Background {
|
||||
glutils::GLVertexArray vao;
|
||||
glutils::GLBuffer vbo;
|
||||
glutils::GLBuffer ebo;
|
||||
glutils::GLTexture tex;
|
||||
struct Background: public glutils::BufferSet {
|
||||
bool enabled = false;
|
||||
bool updated = false;
|
||||
std::array<float, TileCount * BgVertexVboLength> bgVertices = {};
|
||||
std::array<GLuint, TileCount * BgVertexEboLength> bgElements = {};
|
||||
|
||||
inline Background() noexcept {
|
||||
vertices.resize(TileCount * BgVertexVboLength);
|
||||
elements.resize(TileCount * BgVertexEboLength);
|
||||
}
|
||||
};
|
||||
|
||||
struct GlImplData {
|
||||
@@ -54,11 +53,11 @@ struct GlImplData {
|
||||
constexpr const GLchar *bgvshad = R"(
|
||||
{}
|
||||
in vec2 vTexCoord;
|
||||
in vec2 position;
|
||||
in vec2 vPosition;
|
||||
out vec2 fTexCoord;
|
||||
uniform float vTileHeight;
|
||||
void main() {
|
||||
gl_Position = vec4(position, 0.0, 1.0);
|
||||
gl_Position = vec4(vPosition, 0.0, 1.0);
|
||||
fTexCoord = vTexCoord * vec2(1, vTileHeight);
|
||||
})";
|
||||
|
||||
@@ -77,7 +76,8 @@ static constexpr auto bgVertexRow(unsigned x, unsigned y) noexcept {
|
||||
return y * TileRows + x;
|
||||
}
|
||||
|
||||
static void setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int textureRow, float *vbo, GLuint *ebo) {
|
||||
static void
|
||||
setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int textureRow, float *vbo, GLuint *ebo) noexcept {
|
||||
// don't worry, this memcpy gets optimized to something much more ideal
|
||||
const auto [sw, sh] = getScreenSize(ctx);
|
||||
constexpr float ymod = 2.0f / 20.0f;
|
||||
@@ -100,59 +100,35 @@ static void setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int
|
||||
memcpy(ebo, elms, sizeof(elms));
|
||||
}
|
||||
|
||||
static void sendVbo(const Background &bg) noexcept {
|
||||
// vbo
|
||||
glBindBuffer(GL_ARRAY_BUFFER, bg.vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(bg.bgVertices), &bg.bgVertices, GL_DYNAMIC_DRAW);
|
||||
}
|
||||
|
||||
static void sendEbo(const Background &bg) noexcept {
|
||||
// ebo
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bg.ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bg.bgElements), &bg.bgElements, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept {
|
||||
static void initBackgroundBufferObjects(Context *ctx, 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 = &bg->bgVertices[i * BgVertexVboLength];
|
||||
auto ebo = &bg->bgElements[i * BgVertexEboLength];
|
||||
auto vbo = &bg->vertices[i * BgVertexVboLength];
|
||||
auto ebo = &bg->elements[i * BgVertexEboLength];
|
||||
setTileBufferObject(ctx, i * BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), 0, vbo, ebo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static glutils::GLVertexArray genVertexArrayObject() noexcept {
|
||||
GLuint vao = 0;
|
||||
glGenVertexArrays(1, &vao);
|
||||
return glutils::GLVertexArray(vao);
|
||||
}
|
||||
|
||||
static glutils::GLBuffer genBuffer() noexcept {
|
||||
GLuint buff = 0;
|
||||
glGenBuffers(1, &buff);
|
||||
return glutils::GLBuffer(buff);
|
||||
}
|
||||
|
||||
static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) noexcept {
|
||||
static void initBackgroundBufferset(Context *ctx, GLuint shader, glutils::BufferSet *bg) noexcept {
|
||||
// vao
|
||||
bg->vao = genVertexArrayObject();
|
||||
bg->vao = glutils::generateVertexArrayObject();
|
||||
glBindVertexArray(bg->vao);
|
||||
// vbo & ebo
|
||||
bg->vbo = genBuffer();
|
||||
bg->ebo = genBuffer();
|
||||
bg->vbo = glutils::generateBuffer();
|
||||
bg->ebo = glutils::generateBuffer();
|
||||
initBackgroundBufferObjects(ctx, bg);
|
||||
sendVbo(*bg);
|
||||
sendEbo(*bg);
|
||||
glutils::sendVbo(*bg);
|
||||
glutils::sendEbo(*bg);
|
||||
// vbo layout
|
||||
auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "position"));
|
||||
auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
|
||||
glEnableVertexAttribArray(posAttr);
|
||||
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
|
||||
auto texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
|
||||
glEnableVertexAttribArray(texCoordAttr);
|
||||
glVertexAttribPointer(texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
|
||||
ox::bit_cast<void*>(2 * sizeof(float)));
|
||||
std::bit_cast<void*>(2 * sizeof(float)));
|
||||
}
|
||||
|
||||
static glutils::GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept {
|
||||
@@ -192,10 +168,10 @@ static void drawBackground(Background *bg) noexcept {
|
||||
glBindVertexArray(bg->vao);
|
||||
if (bg->updated) {
|
||||
bg->updated = false;
|
||||
renderer::sendVbo(*bg);
|
||||
glutils::sendVbo(*bg);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, bg->tex);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(bg->bgElements.size()), GL_UNSIGNED_INT, nullptr);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(bg->elements.size()), GL_UNSIGNED_INT, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,16 +186,15 @@ static void drawBackgrounds(GlImplData *id) noexcept {
|
||||
}
|
||||
|
||||
ox::Error init(Context *ctx) noexcept {
|
||||
constexpr auto GlslVersion = "#version 150";
|
||||
const auto id = new GlImplData;
|
||||
ctx->setRendererData(id);
|
||||
const auto vshad = ox::sfmt(bgvshad, GlslVersion);
|
||||
const auto fshad = ox::sfmt(bgfshad, GlslVersion);
|
||||
const auto vshad = ox::sfmt(bgvshad, glutils::GlslVersion);
|
||||
const auto fshad = ox::sfmt(bgfshad, glutils::GlslVersion);
|
||||
oxReturnError(glutils::buildShaderProgram(vshad.c_str(), fshad.c_str()).moveTo(&id->bgShader));
|
||||
for (auto &bg : id->backgrounds) {
|
||||
initBackgroundBufferset(ctx, id->bgShader, &bg);
|
||||
}
|
||||
ImGui_ImplOpenGL3_Init(GlslVersion);
|
||||
ImGui_ImplOpenGL3_Init(glutils::GlslVersion);
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
@@ -280,8 +255,6 @@ void draw(Context *ctx) noexcept {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// render
|
||||
renderer::drawBackgrounds(id);
|
||||
//bool showDemo = true;
|
||||
//ImGui::ShowDemoWindow(&showDemo);
|
||||
for (const auto cd : ctx->drawers) {
|
||||
cd->draw(ctx);
|
||||
}
|
||||
@@ -318,9 +291,10 @@ void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcep
|
||||
const auto x = static_cast<unsigned>(column);
|
||||
const auto i = renderer::bgVertexRow(x, y);
|
||||
auto &bg = id->backgrounds[z];
|
||||
auto vbo = &bg.bgVertices[i * renderer::BgVertexVboLength];
|
||||
auto ebo = &bg.bgElements[i * renderer::BgVertexEboLength];
|
||||
renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), tile, vbo, ebo);
|
||||
auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
|
||||
auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
|
||||
renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows,
|
||||
static_cast<float>(x), static_cast<float>(y), tile, vbo, ebo);
|
||||
bg.updated = true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user