Added Entity and transformations with GLM; Minor changes

This commit is contained in:
grayson committed 2021-08-27 01:10:32 -05:00
1 parent bc5c17ebfb
commit 5d32976869
16 files changed
+280 -59

No files matched your search

@@ -4,11 +4,7 @@
namespace nf {
Drawable::Drawable() {
Log("Drawable constructor");
}
Drawable::DrawableType Drawable::identity() {
return DrawableType::NF_NONE;
}
void Drawable::bind() {
@@ -22,6 +18,5 @@ namespace nf {
Drawable::~Drawable() {
delete m_vao;
delete m_ib;
delete m_shader;
}
}
@@ -0,0 +1,57 @@
#include "Entity.h"
namespace nf {
Entity::Entity() :
m_position(0.0),
m_rotation(0.0),
m_scale(1.0)
{
}
void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) {
m_model = new Model;
m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount);
}
void Entity::setLocation(float x, float y, float z) {
m_position = { x, y, z };
}
void Entity::setRotation(float x, float y, float z) {
m_rotation = { x, y, z };
}
void Entity::setScale(float x) {
m_rotation = { x, x, x };
}
void Entity::setScale(float x, float y, float z) {
m_scale = { x, y, z };
}
void Entity::bind(Shader* shader) {
m_model->bind();
shader->bind();
setModelMatrix(shader);
}
Model* Entity::getModel() const {
return m_model;
}
void Entity::setModelMatrix(Shader* shader) {
glm::mat4 model(1.0f);
model = glm::translate(model, glm::vec3(m_position.x, m_position.y, m_position.z));
model = glm::rotate(model, glm::radians(m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians(m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
model = glm::rotate(model, glm::radians(m_rotation.z), glm::vec3(0.0, 0.0, 1.0));
model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, m_scale.z));
//TODO: fill out model matrix
shader->setUniform("model", model);
}
Entity::~Entity() {
}
}
+1 -17
View File
@@ -6,19 +6,13 @@ 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) {
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) {
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() {
@@ -26,16 +20,6 @@ namespace nf {
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() {
+14 -15
View File
@@ -54,37 +54,36 @@ namespace nf {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX);
m_defaultVertex = (const char*)vs.ptr;
const char* defaultVertex = (const char*)vs.ptr;
Win32Res fs(IDR_DEFAULTFRAGMENT);
m_defaultFragment = (const char*)fs.ptr;
m_defaultShader = new Shader(m_defaultVertex, m_defaultFragment);
const char* defaultFragment = (const char*)fs.ptr;
if (defaultVertex == nullptr || defaultFragment == nullptr)
Error("Default engine resources not found! Please link nf.res to your application!");
m_defaultShader = new Shader(defaultVertex, defaultFragment);
}
void Renderer::render(Drawable& in) {
void Renderer::render(Entity& 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);
else
m_lGame.push_back(&in);
Error("Tried to render Entity before being created!");
m_lGame.push_back(&in);
}
void Renderer::doFrame() {
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Drawable* draw : m_lGame) {
Model& curr = (Model&)*draw;
curr.bind();
if (!curr.hasCustomShader())
m_defaultShader->bind();
glDrawElements(GL_TRIANGLES, curr.getIndexCount(), GL_UNSIGNED_INT, nullptr);
for (Entity* draw : m_lGame) {
Entity& curr = *draw;
curr.bind(m_defaultShader);
glDrawElements(GL_TRIANGLES, curr.getModel()->getIndexCount(), GL_UNSIGNED_INT, nullptr);
}
m_lGame.clear();
for (Drawable* draw : m_lUI) {
Drawable& curr = *draw;
}
m_lUI.clear();
SwapBuffers(m_hdc);
+7 -1
View File
@@ -49,7 +49,13 @@ namespace nf {
Shader::current = m_id;
}
}
//TODO: Create overloaded setUniform function
void Shader::setUniform(const char* name, glm::mat4& data) {
if (m_uniformLocations.find(name) != m_uniformLocations.end())
getUniformLocation(name);
glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(data));
}
void Shader::getUniformLocation(const char* uniformName) {
unsigned int loc = glGetUniformLocation(m_id, uniformName);
if (loc == -1)