This commit is contained in:
2025-11-24 12:54:13 +00:00
parent a786d26dde
commit 767796919a
7 changed files with 179 additions and 31 deletions

57
inc/engine/buffers.hpp Normal file
View File

@@ -0,0 +1,57 @@
#ifndef BUFFERS_HPP
#define BUFFERS_HPP
#define GL_GLEXT_PROTOTYPES
#include <SDL3/SDL_opengl.h>
#include <cstddef>
#include <memory>
// 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> vertexBuffer;
std::unique_ptr<ElementBuffer> elementBuffer;
};
#endif

View File

@@ -4,6 +4,8 @@
// Includes
#include <SDL3/SDL_opengl.h>
#include "engine/sdl.hpp"
#include "engine/buffers.hpp"
#include "engine/shaders.hpp"
// OpenGL Context Class
class OpenGLContext {

View File

@@ -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
}

103
src/engine/buffers.cpp Normal file
View File

@@ -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<GLsizei>(elementCount), GL_UNSIGNED_INT, 0);
}
// Vertex Array Object Constructor
VertexArrayObject::VertexArrayObject() {
glGenVertexArrays(1, &arrayID);
glBindVertexArray(arrayID);
// Initialize associated buffers
vertexBuffer = std::make_unique<VertexBuffer>();
elementBuffer = std::make_unique<ElementBuffer>();
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);
}

View File

@@ -1,4 +1,4 @@
#include "engine/shader.hpp"
#include "engine/shaders.hpp"
#include <iostream>
#include <fstream>
#include <SDL3/SDL_log.h>

View File

@@ -1,10 +1,10 @@
#define GL_GLEXT_PROTOTYPES
#include "engine/sdl.hpp"
#include "engine/opengl.hpp"
#include "engine/shader.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
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();
}