[nostalgia] Move much of the OpenGL code to glutils
This commit is contained in:
@@ -67,8 +67,26 @@ ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag)
|
||||
return prgm;
|
||||
}
|
||||
|
||||
ox::Result<GLProgram> buildShaderProgram(const ox::String &vert, const ox::String &frag) noexcept {
|
||||
return buildShaderProgram(vert.c_str(), frag.c_str());
|
||||
}
|
||||
|
||||
GLVertexArray generateVertexArrayObject() noexcept {
|
||||
GLVertexArray vao;
|
||||
glGenVertexArrays(1, &vao.id);
|
||||
return vao;
|
||||
}
|
||||
|
||||
GLBuffer generateBuffer() noexcept {
|
||||
GLBuffer buff;
|
||||
glGenBuffers(1, &buff.id);
|
||||
return buff;
|
||||
}
|
||||
|
||||
FrameBuffer generateFrameBuffer(int width, int height) noexcept {
|
||||
FrameBuffer fb;
|
||||
fb.width = width;
|
||||
fb.height = height;
|
||||
glGenFramebuffers(1, &fb.fbo.id);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fb);
|
||||
// color texture
|
||||
@@ -82,12 +100,25 @@ FrameBuffer generateFrameBuffer(int width, int height) noexcept {
|
||||
glGenRenderbuffers(1, &fb.depth.id);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, fb.depth);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb.depth, 0);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb.depth);
|
||||
// verify FBO
|
||||
oxAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame Buffer is incomplete");
|
||||
// restore primary FB
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
oxAssert(glGetError() == 0, "Framebuffer creation failed");
|
||||
return fb;
|
||||
}
|
||||
|
||||
void sendVbo(const BufferSet &bg) noexcept {
|
||||
const auto bufferSize = static_cast<GLsizeiptr>(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 sendEbo(const BufferSet &bg) noexcept {
|
||||
const auto bufferSize = static_cast<GLsizeiptr>(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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/defines.hpp>
|
||||
#include <ox/std/string.hpp>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES 1
|
||||
#ifdef OX_OS_Darwin
|
||||
@@ -21,9 +22,12 @@
|
||||
#endif
|
||||
|
||||
#include <ox/std/error.hpp>
|
||||
#include <ox/std/vector.hpp>
|
||||
|
||||
namespace nostalgia::glutils {
|
||||
|
||||
constexpr auto GlslVersion = "#version 150";
|
||||
|
||||
struct Empty {};
|
||||
|
||||
struct TextureBase {
|
||||
@@ -125,6 +129,8 @@ using GLVertexArray = GLObject<deleteVertexArray>;
|
||||
* and not its dependencies.
|
||||
*/
|
||||
struct FrameBuffer {
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
GLFrameBuffer fbo;
|
||||
GLTexture color;
|
||||
GLRenderBuffer depth;
|
||||
@@ -139,10 +145,28 @@ struct FrameBuffer {
|
||||
};
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept;
|
||||
|
||||
ox::Result<GLProgram> buildShaderProgram(const ox::String &vert, const ox::String &frag) noexcept;
|
||||
|
||||
glutils::GLVertexArray generateVertexArrayObject() noexcept;
|
||||
|
||||
glutils::GLBuffer generateBuffer() noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
FrameBuffer generateFrameBuffer(int width, int height) noexcept;
|
||||
|
||||
}
|
||||
struct BufferSet {
|
||||
glutils::GLVertexArray vao;
|
||||
glutils::GLBuffer vbo;
|
||||
glutils::GLBuffer ebo;
|
||||
glutils::GLTexture tex;
|
||||
ox::Vector<float> vertices;
|
||||
ox::Vector<GLuint> elements;
|
||||
};
|
||||
|
||||
void sendVbo(const BufferSet &bg) noexcept;
|
||||
|
||||
void sendEbo(const BufferSet &bg) noexcept;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user