Added VertexArray
This commit is contained in:
parent
99404714e8
commit
0724c6a84a
@ -56,7 +56,7 @@ namespace nf {
|
||||
}
|
||||
}
|
||||
else {
|
||||
Error("More than one default state defined");
|
||||
Error("More than one default state defined!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,7 +176,7 @@ namespace nf {
|
||||
RegisterClass(&wclass);
|
||||
}
|
||||
else {
|
||||
Error("Cannot run two NF applications at once.");
|
||||
Error("Cannot run two NF applications at once!");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ namespace nf {
|
||||
wglMakeCurrent(m_hdc, m_hglrc);
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK) {
|
||||
Error("Could not initialize GLEW");
|
||||
Error("Could not initialize GLEW!");
|
||||
}
|
||||
const int attrib[] = {
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||
|
@ -1,9 +1,46 @@
|
||||
#include "VertexArray.h"
|
||||
|
||||
namespace nf{
|
||||
VertexArray::VertexArray(VertexBuffer& buffer) {
|
||||
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)
|
||||
{
|
||||
glGenVertexArrays(1, &m_id);
|
||||
//TODO: Bind buffer and set the layout
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void VertexArray::push<float>(unsigned int count) {
|
||||
m_layoutElements.push_back({ GL_FLOAT, count, GL_FALSE });
|
||||
m_vertexStride += VertexBufferElement::getSizeOfType(GL_FLOAT) * count;
|
||||
}
|
||||
|
||||
VertexArray::~VertexArray() {
|
||||
|
@ -4,7 +4,7 @@ namespace nf {
|
||||
VertexBuffer::VertexBuffer(const void* data, size_t size) {
|
||||
glGenBuffers(1, &m_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);//TODO: See if I need to change this to dynamic
|
||||
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
void VertexBuffer::bind() const {
|
||||
|
@ -77,7 +77,7 @@ namespace nf {
|
||||
std::ifstream in;
|
||||
in.open(filename);
|
||||
if (!in) {
|
||||
Error("Cannot find file");
|
||||
Error(("File \"" + (std::string)filename + (std::string)"\" could not be read!").c_str());
|
||||
return NULL;
|
||||
}
|
||||
std::stringstream ss;
|
||||
|
@ -8,6 +8,8 @@
|
||||
#include "Utility.h"
|
||||
#include "IntroGamestate.h"
|
||||
#include "Renderer.h"
|
||||
//TODO: Check #include consistency
|
||||
//TODO: Document ALL frontend functions
|
||||
|
||||
namespace nf {
|
||||
class Application {
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "Renderer.h"
|
||||
#include "GL/glew.h"
|
||||
|
||||
namespace nf {
|
||||
class IndexBuffer {
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace nf {
|
||||
#ifdef _DEBUG
|
||||
|
@ -1,13 +1,35 @@
|
||||
#pragma once
|
||||
#ifdef NFENGINE
|
||||
#include "GL/glew.h"
|
||||
#endif
|
||||
#include <vector>
|
||||
|
||||
#include "VertexBuffer.h"
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
struct VertexBufferElement {
|
||||
unsigned int type;
|
||||
unsigned int count;
|
||||
unsigned char normalized;
|
||||
|
||||
static unsigned int getSizeOfType(unsigned int type);
|
||||
};
|
||||
|
||||
class VertexArray {
|
||||
public:
|
||||
VertexArray(VertexBuffer& buffer);
|
||||
VertexArray(const void* bufferData, size_t bufferSize);
|
||||
|
||||
void bind();
|
||||
template<typename T>
|
||||
void push(unsigned int count);
|
||||
|
||||
~VertexArray();
|
||||
private:
|
||||
unsigned int m_id;
|
||||
VertexBuffer m_vb;
|
||||
bool m_hasLayout;
|
||||
std::vector<VertexBufferElement> m_layoutElements;
|
||||
unsigned int m_vertexStride;
|
||||
};
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Renderer.h"
|
||||
#ifdef NFENGINE
|
||||
#include "GL/glew.h"
|
||||
#endif
|
||||
|
||||
namespace nf {
|
||||
class VertexBuffer {
|
||||
|
Reference in New Issue
Block a user