diff --git a/src/nostalgia/glutils/glutils.cpp b/src/nostalgia/glutils/glutils.cpp index 211823fc..66dc2970 100644 --- a/src/nostalgia/glutils/glutils.cpp +++ b/src/nostalgia/glutils/glutils.cpp @@ -30,12 +30,12 @@ void deleteVertexArray(GLuint v) noexcept { glDeleteVertexArrays(1, &v); } -void deleteProgram(GLuint v) noexcept { - glDeleteProgram(v); +void deleteProgram(GLuint p) noexcept { + glDeleteProgram(p); } -void deleteShader(GLuint v) noexcept { - glDeleteShader(v); +void deleteShader(GLuint s) noexcept { + glDeleteShader(s); } template struct GLObject; @@ -46,7 +46,6 @@ template struct GLObject; template struct GLObject; template struct GLObject; -[[nodiscard]] static ox::Result buildShader(GLuint shaderType, const GLchar *src, ox::CRStringView shaderName) noexcept { GLShader shader(glCreateShader(shaderType)); glShaderSource(shader, 1, &src, nullptr); @@ -54,7 +53,7 @@ static ox::Result buildShader(GLuint shaderType, const GLchar *src, ox GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { - ox::Vector errMsg(1000); + ox::Vector errMsg(ox::units::KB); glGetShaderInfoLog(shader, static_cast(errMsg.size()), nullptr, errMsg.data()); oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data()); return OxError(1, "shader compile error"); @@ -62,7 +61,7 @@ static ox::Result buildShader(GLuint shaderType, const GLchar *src, ox return shader; } -ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag, [[maybe_unused]] const GLchar *geo) noexcept { +ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag, const GLchar *geo) noexcept { GLProgram prgm(glCreateProgram()); oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad")); glAttachShader(prgm, vs); @@ -116,19 +115,43 @@ FrameBuffer generateFrameBuffer(int width, int height) noexcept { oxAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame Buffer is incomplete"); // restore primary FB glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); return fb; } -void sendVbo(const BufferSet &bg) noexcept { - const auto bufferSize = static_cast(sizeof(decltype(bg.vertices)::value_type) * bg.vertices.size()); - glBindBuffer(GL_ARRAY_BUFFER, bg.vbo); - glBufferData(GL_ARRAY_BUFFER, bufferSize, bg.vertices.data(), GL_DYNAMIC_DRAW); +void resizeInitFrameBuffer(FrameBuffer *fb, int width, int height) noexcept { + if (!*fb) { + *fb = generateFrameBuffer(width, height); + return; + } + width = ox::max(1, width); + height = ox::max(1, height); + fb->width = width; + fb->height = height; + glBindFramebuffer(GL_FRAMEBUFFER, *fb); + // color texture + glBindTexture(GL_TEXTURE_2D, fb->color); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + // depth texture + glBindRenderbuffer(GL_RENDERBUFFER, fb->depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + // restore primary FB + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); } -void sendEbo(const BufferSet &bg) noexcept { - const auto bufferSize = static_cast(sizeof(decltype(bg.elements)::value_type) * bg.elements.size()); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bg.ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferSize, bg.elements.data(), GL_STATIC_DRAW); +void sendVbo(const BufferSet &bs) noexcept { + const auto bufferSize = static_cast(sizeof(decltype(bs.vertices)::value_type) * bs.vertices.size()); + glBindBuffer(GL_ARRAY_BUFFER, bs.vbo); + glBufferData(GL_ARRAY_BUFFER, bufferSize, bs.vertices.data(), GL_DYNAMIC_DRAW); +} + +void sendEbo(const BufferSet &bs) noexcept { + const auto bufferSize = static_cast(sizeof(decltype(bs.elements)::value_type) * bs.elements.size()); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs.ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferSize, bs.elements.data(), GL_STATIC_DRAW); } } diff --git a/src/nostalgia/glutils/glutils.hpp b/src/nostalgia/glutils/glutils.hpp index c3ffaae1..be57a4b4 100644 --- a/src/nostalgia/glutils/glutils.hpp +++ b/src/nostalgia/glutils/glutils.hpp @@ -146,6 +146,11 @@ glutils::GLBuffer generateBuffer() noexcept; [[nodiscard]] FrameBuffer generateFrameBuffer(int width, int height) noexcept; +/** + * Resizes a FrameBuffer, and creates if it does not already exist. + */ +void resizeInitFrameBuffer(FrameBuffer *fb, int width, int height) noexcept; + struct BufferSet { glutils::GLVertexArray vao; glutils::GLBuffer vbo; @@ -155,8 +160,8 @@ struct BufferSet { ox::Vector elements; }; -void sendVbo(const BufferSet &bg) noexcept; +void sendVbo(const BufferSet &bs) noexcept; -void sendEbo(const BufferSet &bg) noexcept; +void sendEbo(const BufferSet &bs) noexcept; }