Added Model and default shader; Organization

This commit is contained in:
grayson committed 2021-08-26 00:23:56 -05:00
1 parent 1c638bdcb9
commit d74f808a17
23 files changed
+293 -64

No files matched your search

-31
View File
@@ -1,31 +0,0 @@
#include "Drawable.h"
namespace nf {
Drawable::Drawable(const char* vertexShader, const char* fragmentShader, const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) :
m_shader(vertexShader, fragmentShader),
m_vao(),
m_ib(indexBufferData, indexBufferCount)
{
m_vao.addBuffer(vertexBufferData, vertexBufferSize);
m_vao.push<float>(2);
m_vao.finishBufferLayout();
}
unsigned int Drawable::getIndexCount() {
return m_ib.getCount();
}
void Drawable::bind() {
m_shader.bind();
m_vao.bind();
m_ib.bind();
}
Drawable::DrawableType Drawable::identity() {
return DrawableType::NF_NONE;
}
Drawable::~Drawable() {
}
}
@@ -0,0 +1,27 @@
#include "Drawable.h"
#include "Utility.h"
namespace nf {
Drawable::Drawable() {
Log("Drawable constructor");
}
Drawable::DrawableType Drawable::identity() {
return DrawableType::NF_NONE;
}
void Drawable::bind() {
Error("Tried to bind an invalid object!");
}
unsigned int Drawable::getIndexCount() {
return m_ib->getCount();
}
Drawable::~Drawable() {
delete m_vao;
delete m_ib;
delete m_shader;
}
}
@@ -0,0 +1,44 @@
#include "Model.h"
#include "Utility.h"
namespace nf {
Model::Model() {
}
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const char* vertexShader, const char* fragmentShader) {
m_vao = new VertexArray;
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
m_vao->push<float>(2);
//TODO: Change this to 3
m_vao->finishBufferLayout();
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
if (vertexShader && fragmentShader)
m_shader = new Shader(vertexShader, fragmentShader);
}
Drawable::DrawableType Model::identity() {
return DrawableType::NF_GAME;
}
void Model::bind() {
if (m_vao == nullptr)
Error("Tried to bind uninitialized model!");
m_vao->bind();
m_ib->bind();
if (m_shader) {
m_shader->bind();
//TODO: Set uniforms here such as position and texture
}
}
bool Model::hasCustomShader() {
if (m_shader)
return true;
return false;
}
Model::~Model() {
}
}
+15 -6
View File
@@ -5,6 +5,7 @@
#include "Application.h"
#include "Utility.h"
#include "resource.h"
namespace nf {
Renderer::Renderer(Application* app) {
@@ -51,15 +52,21 @@ namespace nf {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX);
m_defaultVertex = (const char*)vs.ptr;
Win32Res fs(IDR_DEFAULTFRAGMENT);
m_defaultFragment = (const char*)fs.ptr;
m_defaultShader = new Shader(m_defaultVertex, m_defaultFragment);
}
void Renderer::render(Drawable* in) {
if (in == nullptr)
void Renderer::render(Drawable& in) {
if (&in == nullptr)
Error("Drawable object tried to render before being constructed!");
if (in->identity() == Drawable::DrawableType::NF_UI)
m_lUI.push_back(in);
if (in.identity() == Drawable::DrawableType::NF_UI)
m_lUI.push_back(&in);
else
m_lGame.push_back(in);
m_lGame.push_back(&in);
}
void Renderer::doFrame() {
@@ -67,8 +74,10 @@ namespace nf {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Drawable* draw : m_lGame) {
Drawable& curr = *draw;
Model& curr = (Model&)*draw;
curr.bind();
if (!curr.hasCustomShader())
m_defaultShader->bind();
glDrawElements(GL_TRIANGLES, curr.getIndexCount(), GL_UNSIGNED_INT, nullptr);
}
+1 -1
View File
@@ -42,7 +42,7 @@ namespace nf {
m_lastStride = 0;
}
void VertexArray::bind(unsigned int buffer) {
void VertexArray::bind() {
if (m_buffers.empty())
Error("No buffers and layouts added to vertex array before being bound!");
if (!m_lastBufferHasLayout)