Rewrote VertexArray to support multiple VBOs; Minor fixes and organization
This commit is contained in:
1 parent
f75f98d9db
commit
e17d2e283a
18 files changed
+129
-86
No files matched your search
@@ -1,5 +1,7 @@
|
||||
#include "IndexBuffer.h"
|
||||
|
||||
#include "GL/glew.h"
|
||||
|
||||
namespace nf {
|
||||
IndexBuffer::IndexBuffer(const void* data, size_t count) {
|
||||
m_count = count;
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#include "Renderer.h"
|
||||
|
||||
#include "GL/glew.h"
|
||||
#include "GL\wglew.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
Renderer::Renderer(Application* app) {
|
||||
@@ -54,7 +58,7 @@ namespace nf {
|
||||
|
||||
GLenum err = glGetError();
|
||||
if (err != GL_NO_ERROR) {
|
||||
Error(("OpenGL error " + std::to_string(err)).c_str());
|
||||
Error("OpenGL error " + std::to_string(err));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "Shader.h"
|
||||
|
||||
#include "GL/glew.h"
|
||||
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
Shader::Shader(const char* vertexSource, const char* fragmentSource) {
|
||||
m_id = glCreateProgram();
|
||||
@@ -19,7 +23,7 @@ namespace nf {
|
||||
char* message = new char[length];
|
||||
glGetShaderInfoLog(curr, length, &length, message);
|
||||
message[length - 2] = 0;
|
||||
Error(("OpenGL Error: " + (std::string)message).c_str());
|
||||
Error("OpenGL Error: " + (std::string)message);
|
||||
}
|
||||
}
|
||||
glAttachShader(m_id, vs);
|
||||
@@ -33,7 +37,7 @@ namespace nf {
|
||||
glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &length);
|
||||
char* message = new char[length];
|
||||
glGetProgramInfoLog(m_id, length, &length, message);
|
||||
Error(("OpenGL Error: " + (std::string)message).c_str());
|
||||
Error("OpenGL Error: " + (std::string)message);
|
||||
}
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
@@ -42,6 +46,14 @@ namespace nf {
|
||||
void Shader::bind() {
|
||||
glUseProgram(m_id);
|
||||
}
|
||||
//TODO: Create overloaded setUniform function
|
||||
void Shader::getUniformLocation(const char* uniformName) {
|
||||
unsigned int loc = glGetUniformLocation(m_id, uniformName);
|
||||
if (loc == -1) {
|
||||
Error("Uniform \"" + (std::string)uniformName + "\" does not exist!");
|
||||
}
|
||||
m_uniformLocations[uniformName] = loc;
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
glDeleteProgram(m_id);
|
||||
|
||||
@@ -1,50 +1,63 @@
|
||||
#include "VertexArray.h"
|
||||
|
||||
#include "GL/glew.h"
|
||||
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
unsigned int VertexBufferElement::getSizeOfType(unsigned int type) {
|
||||
switch (type)
|
||||
{
|
||||
case GL_FLOAT:
|
||||
return sizeof(type);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
VertexArray::VertexArray(const void* bufferData, size_t bufferSize) :
|
||||
m_id(0),
|
||||
m_vb(bufferData, bufferSize),
|
||||
m_hasLayout(false),
|
||||
m_vertexStride(0)
|
||||
{
|
||||
VertexArray::VertexArray() {
|
||||
glGenVertexArrays(1, &m_id);
|
||||
glBindVertexArray(m_id);
|
||||
m_lastBufferHasLayout = true;
|
||||
m_attribute = 0;
|
||||
m_lastStride = 0;
|
||||
}
|
||||
|
||||
void VertexArray::bind() {
|
||||
glBindVertexArray(m_id);
|
||||
m_vb.bind();
|
||||
if (!m_hasLayout && m_vertexStride > 0) {
|
||||
unsigned int offset = 0;
|
||||
for (unsigned int i = 0; i < m_layoutElements.size(); i++) {
|
||||
const VertexBufferElement& element = m_layoutElements[i];
|
||||
glEnableVertexAttribArray(i);
|
||||
glVertexAttribPointer(i, element.count, element.type, element.normalized, m_vertexStride, (const void*)offset);
|
||||
offset += element.count * VertexBufferElement::getSizeOfType(element.type);
|
||||
}
|
||||
m_hasLayout = true;
|
||||
}
|
||||
else if(!m_hasLayout) {
|
||||
Error("No layout specified for vertex buffer!");
|
||||
void VertexArray::addBuffer(const void* data, const size_t size) {
|
||||
if (!m_lastBufferHasLayout) {
|
||||
Error("Buffer added to vertex array has no layout!");
|
||||
}
|
||||
m_buffers.push_back(new VertexBuffer(data, size));
|
||||
m_buffers.back()->bind();
|
||||
m_lastBufferHasLayout = false;
|
||||
m_lastStride = 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
void VertexArray::push<float>(unsigned int count) {
|
||||
m_layoutElements.push_back({ GL_FLOAT, count, GL_FALSE });
|
||||
m_vertexStride += VertexBufferElement::getSizeOfType(GL_FLOAT) * count;
|
||||
if (m_lastBufferHasLayout) {
|
||||
Error("Tried to modify a vertex array's buffer after the layout was final!");
|
||||
}
|
||||
m_lastBufferLayout.push_back({ GL_FLOAT, count, GL_FALSE });
|
||||
m_lastStride += count * sizeof(GL_FLOAT);
|
||||
}
|
||||
|
||||
void VertexArray::finishBufferLayout() {
|
||||
unsigned int offset = 0;
|
||||
for (; m_attribute < m_lastBufferLayout.size(); m_attribute++) {
|
||||
const VertexBufferElement& curr = m_lastBufferLayout[m_attribute];
|
||||
glEnableVertexAttribArray(m_attribute);
|
||||
glVertexAttribPointer(m_attribute, curr.count, curr.type, curr.normalized, m_lastStride, (const void*)offset);
|
||||
offset += sizeof(curr.type) * curr.count;
|
||||
}
|
||||
m_lastBufferHasLayout = true;
|
||||
m_lastStride = 0;
|
||||
}
|
||||
|
||||
void VertexArray::bind(unsigned int buffer) {
|
||||
if (m_buffers.empty()) {
|
||||
Error("No buffers and layouts added to vertex array before being bound!");
|
||||
}
|
||||
if (!m_lastBufferHasLayout) {
|
||||
Error("Buffer added to vertex array has no layout!");
|
||||
}
|
||||
glBindVertexArray(m_id);
|
||||
}
|
||||
|
||||
VertexArray::~VertexArray() {
|
||||
for (VertexBuffer* curr : m_buffers) {
|
||||
delete curr;
|
||||
}
|
||||
glDeleteVertexArrays(1, &m_id);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "VertexBuffer.h"
|
||||
|
||||
#include "GL/glew.h"
|
||||
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
VertexBuffer::VertexBuffer(const void* data, const size_t size) {
|
||||
glGenBuffers(1, &m_id);
|
||||
|
||||
Reference in new issue
Block a user