Abstraction over layouts
This commit is contained in:
parent
d1d56b0b80
commit
7b1268d4a4
5 changed files with 137 additions and 8 deletions
|
@ -13,6 +13,7 @@ add_executable(
|
|||
Renderer.cpp
|
||||
VertexBuffer.cpp
|
||||
IndexBuffer.cpp
|
||||
VertexArray.cpp
|
||||
glew.c
|
||||
)
|
||||
|
||||
|
|
36
src/opengl/VertexArray.cpp
Normal file
36
src/opengl/VertexArray.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include "VertexArray.h"
|
||||
#include "Renderer.h"
|
||||
|
||||
VertexArray::VertexArray()
|
||||
{
|
||||
GLCall(glGenVertexArrays(1,&m_RendererID));
|
||||
}
|
||||
|
||||
VertexArray::~VertexArray()
|
||||
{
|
||||
GLCall(glDeleteVertexArrays(1,&m_RendererID));
|
||||
}
|
||||
|
||||
void VertexArray::AddBuffer(const VertexBuffer &vb, const VertexBufferLayout& layout)
|
||||
{
|
||||
vb.Bind();
|
||||
const auto &elements = layout.GetElements();
|
||||
unsigned int offset=0;
|
||||
for (unsigned int i = 0; i < elements.size(); i++)
|
||||
{
|
||||
const auto &element = elements[i];
|
||||
GLCall(glEnableVertexAttribArray(i));
|
||||
GLCall(glVertexAttribPointer(i, elements[i].count, element.type,element.normalized, layout.GetStride(), (const void*)offset));
|
||||
offset+=element.count * VertexBufferElement::GetSizeOfType(element.type);
|
||||
}
|
||||
}
|
||||
|
||||
void VertexArray::Bind() const
|
||||
{
|
||||
GLCall(glBindVertexArray(m_RendererID));
|
||||
}
|
||||
|
||||
void VertexArray::Unbind() const
|
||||
{
|
||||
GLCall(glBindVertexArray(0));
|
||||
}
|
18
src/opengl/VertexArray.h
Normal file
18
src/opengl/VertexArray.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#include "VertexBuffer.h"
|
||||
#include "VertexBufferLayout.h"
|
||||
|
||||
class VertexArray
|
||||
{
|
||||
private:
|
||||
unsigned int m_RendererID;
|
||||
|
||||
public:
|
||||
VertexArray();
|
||||
~VertexArray();
|
||||
|
||||
void AddBuffer(const VertexBuffer &vb, const VertexBufferLayout &layout);
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
};
|
73
src/opengl/VertexBufferLayout.h
Normal file
73
src/opengl/VertexBufferLayout.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "glew.h"
|
||||
#include "Renderer.h"
|
||||
|
||||
struct VertexBufferElement
|
||||
{
|
||||
unsigned int type;
|
||||
unsigned int count;
|
||||
unsigned char normalized;
|
||||
static unsigned int GetSizeOfType(unsigned int type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case GL_FLOAT:
|
||||
return 4;
|
||||
case GL_UNSIGNED_INT:
|
||||
return 4;
|
||||
case GL_UNSIGNED_BYTE:
|
||||
return 1;
|
||||
}
|
||||
ASSERT(false);
|
||||
return 0;
|
||||
};
|
||||
};
|
||||
|
||||
class VertexBufferLayout
|
||||
{
|
||||
private:
|
||||
std::vector<VertexBufferElement> m_Elements;
|
||||
unsigned int m_Stride;
|
||||
|
||||
public:
|
||||
VertexBufferLayout() : m_Stride(0)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Push(unsigned count);
|
||||
template <>
|
||||
void Push<float>(unsigned int count)
|
||||
{
|
||||
VertexBufferElement vbe = {GL_FLOAT, count, GL_FALSE};
|
||||
m_Elements.push_back(vbe);
|
||||
m_Stride += count * VertexBufferElement::GetSizeOfType(GL_FLOAT);
|
||||
};
|
||||
|
||||
template <>
|
||||
void Push<unsigned int>(unsigned int count)
|
||||
{
|
||||
VertexBufferElement vbe = {GL_UNSIGNED_INT, count, GL_FALSE};
|
||||
m_Elements.push_back(vbe);
|
||||
m_Stride += count * VertexBufferElement::GetSizeOfType(GL_UNSIGNED_INT);
|
||||
}
|
||||
|
||||
template <>
|
||||
void Push<unsigned char>(unsigned int count)
|
||||
{
|
||||
VertexBufferElement vbe = {GL_UNSIGNED_BYTE, count, GL_TRUE};
|
||||
m_Elements.push_back(vbe);
|
||||
m_Stride += count * VertexBufferElement::GetSizeOfType(GL_UNSIGNED_BYTE);
|
||||
}
|
||||
|
||||
inline const std::vector<VertexBufferElement> GetElements() const &
|
||||
{
|
||||
return m_Elements;
|
||||
}
|
||||
inline unsigned int GetStride() const
|
||||
{
|
||||
return m_Stride;
|
||||
}
|
||||
};
|
|
@ -7,6 +7,7 @@
|
|||
#include "Renderer.h"
|
||||
#include "VertexBuffer.h"
|
||||
#include "IndexBuffer.h"
|
||||
#include "VertexArray.h"
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
|
@ -141,11 +142,14 @@ int main(void)
|
|||
|
||||
//Create buffer
|
||||
//Buffer id
|
||||
VertexBuffer* vb = new VertexBuffer(positions, 4 * 2 * sizeof(float));
|
||||
GLCall(glEnableVertexAttribArray(0));
|
||||
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
|
||||
VertexArray va ;
|
||||
VertexBuffer vb(positions, 4 * 2 * sizeof(float));
|
||||
|
||||
VertexBufferLayout layout;
|
||||
layout.Push<float>(2);
|
||||
va.AddBuffer(vb,layout);
|
||||
|
||||
IndexBuffer* ib = new IndexBuffer(indexes,6);
|
||||
IndexBuffer ib(indexes,6);
|
||||
ShaderProgramSource source = ParseShader(SHADERS_PATH);
|
||||
|
||||
uint shader = CreateShader(source.VertexSource, source.FragmentSource);
|
||||
|
@ -183,8 +187,7 @@ int main(void)
|
|||
|
||||
GLCall(glUniform4f(uniform_location, 0.2f, 0.3f, 0.8f, 1.0f));
|
||||
|
||||
//GLCall(glBindVertexArray(vb));
|
||||
ib->Bind();
|
||||
ib.Bind();
|
||||
|
||||
GLCall(glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr));
|
||||
|
||||
|
@ -202,8 +205,6 @@ int main(void)
|
|||
glfwPollEvents();
|
||||
}
|
||||
|
||||
delete ib;
|
||||
delete vb;
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue