diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 971e7de..12471bd 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -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!"); } } diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp index fc15f52..410dcb4 100644 --- a/NothinFancy/src/Renderer/Renderer.cpp +++ b/NothinFancy/src/Renderer/Renderer.cpp @@ -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, diff --git a/NothinFancy/src/Renderer/VertexArray.cpp b/NothinFancy/src/Renderer/VertexArray.cpp index a30a846..ae5aa98 100644 --- a/NothinFancy/src/Renderer/VertexArray.cpp +++ b/NothinFancy/src/Renderer/VertexArray.cpp @@ -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(unsigned int count) { + m_layoutElements.push_back({ GL_FLOAT, count, GL_FALSE }); + m_vertexStride += VertexBufferElement::getSizeOfType(GL_FLOAT) * count; } VertexArray::~VertexArray() { diff --git a/NothinFancy/src/Renderer/VertexBuffer.cpp b/NothinFancy/src/Renderer/VertexBuffer.cpp index f87488c..a385bab 100644 --- a/NothinFancy/src/Renderer/VertexBuffer.cpp +++ b/NothinFancy/src/Renderer/VertexBuffer.cpp @@ -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 { diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index bdb71b2..2347400 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -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; diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index d1f8920..2de15cc 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -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 { diff --git a/NothinFancy/src/include/IndexBuffer.h b/NothinFancy/src/include/IndexBuffer.h index ed7b143..06ebbc2 100644 --- a/NothinFancy/src/include/IndexBuffer.h +++ b/NothinFancy/src/include/IndexBuffer.h @@ -1,6 +1,5 @@ #pragma once - -#include "Renderer.h" +#include "GL/glew.h" namespace nf { class IndexBuffer { diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index 7ea7982..9140ecc 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace nf { #ifdef _DEBUG diff --git a/NothinFancy/src/include/VertexArray.h b/NothinFancy/src/include/VertexArray.h index 27a9b0a..66ca245 100644 --- a/NothinFancy/src/include/VertexArray.h +++ b/NothinFancy/src/include/VertexArray.h @@ -1,13 +1,35 @@ #pragma once +#ifdef NFENGINE +#include "GL/glew.h" +#endif +#include + #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 + void push(unsigned int count); ~VertexArray(); private: unsigned int m_id; + VertexBuffer m_vb; + bool m_hasLayout; + std::vector m_layoutElements; + unsigned int m_vertexStride; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/VertexBuffer.h b/NothinFancy/src/include/VertexBuffer.h index 0ea4512..0ab97e9 100644 --- a/NothinFancy/src/include/VertexBuffer.h +++ b/NothinFancy/src/include/VertexBuffer.h @@ -1,6 +1,7 @@ #pragma once - -#include "Renderer.h" +#ifdef NFENGINE +#include "GL/glew.h" +#endif namespace nf { class VertexBuffer { diff --git a/notes.txt b/notes.txt index 86822e2..3bc7bbe 100644 --- a/notes.txt +++ b/notes.txt @@ -15,6 +15,9 @@ Remember to use tasks (//TODO: ) *Engine (Name?) "Nothin' Fancy" *Separate project *Namespaced +Refactor NothinFancy.h to ONLY include stuff the frontend needs +AND get rid of NFENGINE +High CPU usage? *Debug and log system *NatVis *Config changing