Add OpenGL backend core classes: Buffer, VertexArray
- Added OpenGLBuffer class for VBO/IBO management - Added OpenGLVertexArray class for VAO management - Updated CMakeLists.txt to include new source files
This commit is contained in:
@@ -26,6 +26,8 @@ add_executable(
|
||||
main.cpp
|
||||
OpenGLDevice.cpp
|
||||
OpenGLShader.cpp
|
||||
OpenGLBuffer.cpp
|
||||
OpenGLVertexArray.cpp
|
||||
./package/src/glad.c
|
||||
)
|
||||
|
||||
|
||||
61
tests/OpenGL/OpenGLBuffer.cpp
Normal file
61
tests/OpenGL/OpenGLBuffer.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "OpenGLBuffer.h"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
OpenGLBuffer::OpenGLBuffer()
|
||||
: m_buffer(0)
|
||||
, m_size(0)
|
||||
, m_isIndexBuffer(false) {
|
||||
}
|
||||
|
||||
OpenGLBuffer::~OpenGLBuffer() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
bool OpenGLBuffer::InitializeVertexBuffer(const void* data, size_t size) {
|
||||
glGenBuffers(1, &m_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
||||
m_size = size;
|
||||
m_isIndexBuffer = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLBuffer::InitializeIndexBuffer(const void* data, size_t size) {
|
||||
glGenBuffers(1, &m_buffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
||||
m_size = size;
|
||||
m_isIndexBuffer = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenGLBuffer::Shutdown() {
|
||||
if (m_buffer) {
|
||||
glDeleteBuffers(1, &m_buffer);
|
||||
m_buffer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLBuffer::Bind() const {
|
||||
if (m_isIndexBuffer) {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffer);
|
||||
} else {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLBuffer::Unbind() const {
|
||||
if (m_isIndexBuffer) {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
} else {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
31
tests/OpenGL/OpenGLBuffer.h
Normal file
31
tests/OpenGL/OpenGLBuffer.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
class OpenGLBuffer {
|
||||
public:
|
||||
OpenGLBuffer();
|
||||
~OpenGLBuffer();
|
||||
|
||||
bool InitializeVertexBuffer(const void* data, size_t size);
|
||||
bool InitializeIndexBuffer(const void* data, size_t size);
|
||||
void Shutdown();
|
||||
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
unsigned int GetID() const { return m_buffer; }
|
||||
size_t GetSize() const { return m_size; }
|
||||
|
||||
private:
|
||||
unsigned int m_buffer;
|
||||
size_t m_size;
|
||||
bool m_isIndexBuffer;
|
||||
};
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
60
tests/OpenGL/OpenGLVertexArray.cpp
Normal file
60
tests/OpenGL/OpenGLVertexArray.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "OpenGLVertexArray.h"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
OpenGLVertexArray::OpenGLVertexArray()
|
||||
: m_vao(0)
|
||||
, m_indexBuffer(0)
|
||||
, m_indexCount(0)
|
||||
, m_vertexBufferCount(0) {
|
||||
}
|
||||
|
||||
OpenGLVertexArray::~OpenGLVertexArray() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
bool OpenGLVertexArray::Initialize() {
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenGLVertexArray::AddVertexBuffer(unsigned int buffer, const VertexAttribute& attribute) {
|
||||
glBindVertexArray(m_vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glEnableVertexAttribArray(attribute.index);
|
||||
glVertexAttribPointer(attribute.index, attribute.count, attribute.type,
|
||||
attribute.normalized ? GL_TRUE : GL_FALSE,
|
||||
attribute.stride, (void*)attribute.offset);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
m_vertexBufferCount++;
|
||||
}
|
||||
|
||||
void OpenGLVertexArray::SetIndexBuffer(unsigned int buffer, unsigned int type) {
|
||||
glBindVertexArray(m_vao);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
|
||||
glBindVertexArray(0);
|
||||
m_indexBuffer = buffer;
|
||||
}
|
||||
|
||||
void OpenGLVertexArray::Shutdown() {
|
||||
if (m_vao) {
|
||||
glDeleteVertexArrays(1, &m_vao);
|
||||
m_vao = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLVertexArray::Bind() const {
|
||||
glBindVertexArray(m_vao);
|
||||
}
|
||||
|
||||
void OpenGLVertexArray::Unbind() const {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
44
tests/OpenGL/OpenGLVertexArray.h
Normal file
44
tests/OpenGL/OpenGLVertexArray.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
struct VertexAttribute {
|
||||
unsigned int index;
|
||||
int count;
|
||||
unsigned int type;
|
||||
bool normalized;
|
||||
size_t stride;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
class OpenGLVertexArray {
|
||||
public:
|
||||
OpenGLVertexArray();
|
||||
~OpenGLVertexArray();
|
||||
|
||||
bool Initialize();
|
||||
void AddVertexBuffer(unsigned int buffer, const VertexAttribute& attribute);
|
||||
void SetIndexBuffer(unsigned int buffer, unsigned int type);
|
||||
void Shutdown();
|
||||
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
unsigned int GetID() const { return m_vao; }
|
||||
unsigned int GetIndexBuffer() const { return m_indexBuffer; }
|
||||
unsigned int GetIndexCount() const { return m_indexCount; }
|
||||
|
||||
private:
|
||||
unsigned int m_vao;
|
||||
unsigned int m_indexBuffer;
|
||||
unsigned int m_indexCount;
|
||||
int m_vertexBufferCount;
|
||||
};
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user