SparrowRenderer/src/phongmaterial.cpp

85 lines
2.7 KiB
C++

#include "phongmaterial.h"
#include "texture.h"
#include "sparrowrenderer.h"
#include "shader.h"
#include "mesh.h"
#include <glm/ext.hpp>
void PhongMaterial::bindAttributes(Shader* myShader)
{
if(SparrowRenderer::isModernOpenGLAvailable() && myShader != NULL)
{
// TODO store the attributes location (in the shader class maybe)
myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
if(normal_map != NULL)
{
normal_map->bind(NORMALS_SLOT);
myShader->bindInteger(myShader->getLocation("normalMap"), NORMALS_SLOT);
}
if(ambient_texture != NULL)
{
ambient_texture->bind(AMBIENT_SLOT);
myShader->bindInteger(myShader->getLocation("ambientTexture"), AMBIENT_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKa"), ambient);
if(diffuse_texture != NULL)
{
diffuse_texture->bind(DIFFUSE_SLOT);
myShader->bindInteger(myShader->getLocation("diffuseTexture"), DIFFUSE_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKd"), diffuse);
if(specular_texture != NULL)
{
specular_texture->bind(SPECULAR_SLOT);
myShader->bindInteger(myShader->getLocation("specularTexture"), SPECULAR_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKs"), specular);
if(alpha_mask != NULL)
{
alpha_mask->bind(ALPHA_SLOT);
myShader->bindInteger(myShader->getLocation("alphaMask"), ALPHA_SLOT);
}
}
else
{
// Crappy rendering code
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, glm::value_ptr(glm::vec4(ambient, 1)));
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glm::value_ptr(glm::vec4(diffuse, 1)));
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glm::value_ptr(glm::vec4(specular, 1)));
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
if(diffuse_texture != NULL)
diffuse_texture->bind(0);
else
{
glActiveTexture(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
}
unsigned int PhongMaterial::getFlags()
{
unsigned int flags = 1 << Mesh::MATERIAL_PHONG;
if(normal_map != NULL)
flags |= 1 << Mesh::MATERIAL_PHONG_NORMAL_MAP;
if(ambient_texture != NULL)
flags |= 1 << Mesh::MATERIAL_PHONG_AMBIENT_TEXTURE;
if(diffuse_texture != NULL)
flags |= 1 << Mesh::MATERIAL_PHONG_DIFFUSE_TEXTURE;
if(specular_texture != NULL)
flags |= 1 << Mesh::MATERIAL_PHONG_SPECULAR_TEXTURE;
if(alpha_mask != NULL)
flags |= 1 << Mesh::MATERIAL_ALPHA_MASK;
return flags;
}