#include "VertexArray.h"


VertexArray::VertexArray()
{
	GLCALL(glGenVertexArrays(1, &m_renderer_ID));
}


VertexArray::~VertexArray()
{
	GLCALL(glDeleteVertexArrays(1, &m_renderer_ID));
}


void VertexArray::AddBuffer(const VertexBuffer & vb, const VertexBufferLayout & layout)
{
	Bind();

	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)); // Vertex shader 의 in 요소들을 구성한다
		GLCALL(glVertexAttribPointer(i, element.count, element.type, element.normalized, layout.GetStride(), (const void*)offset)); // How to read this buffer and give to vertex shader (first attribute, two vertices, data type, do not need normalize, amount of bytes between each vertices, offset pointer for jump into middle attribute)
		offset += element.count * VertexBufferElement::GetSizeOfType(element.type);
	}
}

void VertexArray::Bind() const
{
	GLCALL(glBindVertexArray(m_renderer_ID));
}

void VertexArray::UnBind() const
{
	GLCALL(glBindVertexArray(0));
}
