47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include "entity.h"
|
|
#include "shader.h"
|
|
#include <glm/glm.hpp>
|
|
#include "material.h"
|
|
#include "mesh.h"
|
|
|
|
Entity::Entity(Mesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {}
|
|
|
|
Entity::Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat)
|
|
{
|
|
myParent->addChild(this);
|
|
}
|
|
|
|
void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
|
|
{
|
|
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
|
|
glm::mat4 mvp = projectionMatrix * modelViewMatrix;
|
|
glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
|
|
mat->bindAttributes();
|
|
Shader* shader = mat->getShader();
|
|
shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix);
|
|
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix);
|
|
shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix);
|
|
shader->bindMatrix(shader->getLocation("MVP"), mvp);
|
|
mesh->draw();
|
|
}
|
|
|
|
glm::mat4* Entity::getTransform()
|
|
{
|
|
return &modelMatrix;
|
|
}
|
|
|
|
Shader* Entity::getShader()
|
|
{
|
|
return mat->getShader();
|
|
}
|
|
|
|
Material* Entity::getMaterial()
|
|
{
|
|
return mat;
|
|
}
|
|
|
|
void Entity::addChild(Entity* child)
|
|
{
|
|
children.push_back(child);
|
|
}
|