diff --git a/inc/engine/buffers.hpp b/inc/engine/buffers.hpp new file mode 100644 index 0000000..da20436 --- /dev/null +++ b/inc/engine/buffers.hpp @@ -0,0 +1,57 @@ +#ifndef BUFFERS_HPP +#define BUFFERS_HPP +#define GL_GLEXT_PROTOTYPES + +#include +#include +#include + +// Vertex Buffer Class +class VertexBuffer { +public: + VertexBuffer(); + ~VertexBuffer(); + + void bind() const; + void setData(const void* data, GLsizeiptr size); + void setAttributePointer(GLuint index, std::size_t size, GLenum type, bool normalized, std::size_t stride, std::size_t pointer); + +private: + GLuint bufferID; +}; + +// Element Buffer Class +class ElementBuffer { +public: + ElementBuffer(); + ~ElementBuffer(); + + void bind() const; + void setData(const void* data, GLsizeiptr size); + void draw() const; + +private: + GLuint bufferID; + std::size_t elementCount; + GLenum drawMode; +}; + +// Vertex Array Object Class +class VertexArrayObject { +public: + VertexArrayObject(); + ~VertexArrayObject(); + + void bind() const; + void draw() const; + void bufferVertexData(const void* data, GLsizeiptr size); + void bufferElementData(const void* data, GLsizeiptr size); + void setAttributePointer(GLuint index, std::size_t size, GLenum type, bool normalized, std::size_t stride, std::size_t pointer); + +private: + GLuint arrayID; + std::unique_ptr vertexBuffer; + std::unique_ptr elementBuffer; +}; + +#endif \ No newline at end of file diff --git a/inc/engine/opengl.hpp b/inc/engine/opengl.hpp index fa0bee3..9155c6e 100644 --- a/inc/engine/opengl.hpp +++ b/inc/engine/opengl.hpp @@ -4,6 +4,8 @@ // Includes #include #include "engine/sdl.hpp" +#include "engine/buffers.hpp" +#include "engine/shaders.hpp" // OpenGL Context Class class OpenGLContext { diff --git a/inc/engine/shader.hpp b/inc/engine/shaders.hpp similarity index 100% rename from inc/engine/shader.hpp rename to inc/engine/shaders.hpp diff --git a/shaders/triangle.vert b/shaders/triangle.vert index 920604a..5bd8af9 100644 --- a/shaders/triangle.vert +++ b/shaders/triangle.vert @@ -1,6 +1,7 @@ #version 330 core layout(location = 0) in vec3 position; // Vertex position +layout(location = 1) in vec3 inColor; // Vertex color out vec3 color; // Output color to the fragment shader uniform mat4 model; @@ -9,5 +10,5 @@ uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(position, 1.0); // Apply transformation matrices - color = position + vec3(0.5); // Set the color based on the vertex position + color = inColor; // Set the color output } \ No newline at end of file diff --git a/src/engine/buffers.cpp b/src/engine/buffers.cpp new file mode 100644 index 0000000..612ae2d --- /dev/null +++ b/src/engine/buffers.cpp @@ -0,0 +1,103 @@ +#include "engine/buffers.hpp" + +// Vertex Buffer Constructor +VertexBuffer::VertexBuffer() { + glGenBuffers(1, &bufferID); +} + +// Vertex Buffer Destructor +VertexBuffer::~VertexBuffer() { + glDeleteBuffers(1, &bufferID); +} + +// Bind Vertex Buffer +void VertexBuffer::bind() const { + glBindBuffer(GL_ARRAY_BUFFER, bufferID); +} + +// Set Data for Vertex Buffer +void VertexBuffer::setData(const void* data, GLsizeiptr size) { + bind(); + glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); +} + +// Set Attribute Pointer for Vertex Buffer +void VertexBuffer::setAttributePointer(GLuint index, std::size_t size, GLenum type, bool normalized, std::size_t stride, std::size_t pointer) { + bind(); + glVertexAttribPointer(index, size, type, normalized ? GL_TRUE : GL_FALSE, stride, (void*)pointer); + glEnableVertexAttribArray(index); +} + +// Element Buffer Constructor +ElementBuffer::ElementBuffer(): elementCount(0), drawMode(GL_TRIANGLES) { + glGenBuffers(1, &bufferID); +} + +// Element Buffer Destructor +ElementBuffer::~ElementBuffer() { + glDeleteBuffers(1, &bufferID); +} + +// Bind Element Buffer +void ElementBuffer::bind() const { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID); +} + +// Set Data for Element Buffer +void ElementBuffer::setData(const void* data, GLsizeiptr size) { + bind(); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); + elementCount = size / sizeof(GLuint); // Assuming GLuint indices +} + +// Draw Elements using Element Buffer +void ElementBuffer::draw() const { + bind(); + glDrawElements(drawMode, static_cast(elementCount), GL_UNSIGNED_INT, 0); +} + +// Vertex Array Object Constructor +VertexArrayObject::VertexArrayObject() { + glGenVertexArrays(1, &arrayID); + glBindVertexArray(arrayID); + + // Initialize associated buffers + vertexBuffer = std::make_unique(); + elementBuffer = std::make_unique(); + vertexBuffer->bind(); + elementBuffer->bind(); +} + +// Vertex Array Object Destructor +VertexArrayObject::~VertexArrayObject() { + glDeleteVertexArrays(1, &arrayID); +} + +// Bind Vertex Array Object +void VertexArrayObject::bind() const { + glBindVertexArray(arrayID); +} + +// Draw using Vertex Array Object +void VertexArrayObject::draw() const { + bind(); + elementBuffer->draw(); +} + +// Buffer Vertex Data +void VertexArrayObject::bufferVertexData(const void* data, GLsizeiptr size) { + bind(); + vertexBuffer->setData(data, size); +} + +// Buffer Element Data +void VertexArrayObject::bufferElementData(const void* data, GLsizeiptr size) { + bind(); + elementBuffer->setData(data, size); +} + +// Set Attribute Pointer +void VertexArrayObject::setAttributePointer(GLuint index, std::size_t size, GLenum type, bool normalized, std::size_t stride, std::size_t pointer) { + bind(); + vertexBuffer->setAttributePointer(index, size, type, normalized, stride, pointer); +} \ No newline at end of file diff --git a/src/engine/shader.cpp b/src/engine/shaders.cpp similarity index 99% rename from src/engine/shader.cpp rename to src/engine/shaders.cpp index 22e850b..0fb0765 100644 --- a/src/engine/shader.cpp +++ b/src/engine/shaders.cpp @@ -1,4 +1,4 @@ -#include "engine/shader.hpp" +#include "engine/shaders.hpp" #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 7f31244..ec4acec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,10 @@ #define GL_GLEXT_PROTOTYPES #include "engine/sdl.hpp" #include "engine/opengl.hpp" -#include "engine/shader.hpp" #include #include +#include int main(int argc, char* argv[]) { SDLWindow window("My SDL Application", 800, 600); @@ -23,39 +23,26 @@ int main(int argc, char* argv[]) { Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag"); // test setup + // vertex, color float vertexData[] = { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, 0.5f, 0.0f, // top left - -0.5f, -0.5f, 0.0f, // bottom left + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom right + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // top left + -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f // bottom left }; unsigned int indexData[] = { 0, 1, 2, // first triangle 1, 3, 2 // second triangle }; - - unsigned int VBO, VAO, EBO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + VertexArrayObject VAO; + VAO.bufferVertexData(vertexData, sizeof(vertexData)); + VAO.bufferElementData(indexData, sizeof(indexData)); + VAO.setAttributePointer(0, 3, GL_FLOAT, false, 6 * sizeof(float), 0); // position + VAO.setAttributePointer(1, 3, GL_FLOAT, false, 6 * sizeof(float), 3 * sizeof(float)); // color window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); window.showWindow(); @@ -69,10 +56,8 @@ int main(int argc, char* argv[]) { shader_program.setMat4("view", viewMatrix); shader_program.setMat4("projection", projectionMatrix); shader_program.setMat4("model", modelMatrix); - - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + + VAO.draw(); glContext.swapBuffers(); }