SparrowRenderer/phongmaterial.cpp

81 lines
2.3 KiB
C++

#include "phongmaterial.h"
#include "texture.h"
#include "phongmodule.h"
#include "sparrowrenderer.h"
#include "glassert.h"
#include <glm/ext.hpp>
#define TEX_ID 0
void PhongMaterial::updateShader()
{
shader = PhongModule::getShader(diffuse_texture == NULL ? PhongModule::PHONG_COLOR : PhongModule::PHONG_TEXTURE);
}
void PhongMaterial::bindAttributes()
{
if(SparrowRenderer::isModernOpenGLAvailable())
{
shader->bind();
shader->bindVec3(shader->getLocation("materialAmbient"), emission);
shader->bindVec3(shader->getLocation("materialKd"), diffuse);
shader->bindVec3(shader->getLocation("materialKs"), specular);
shader->bindFloat(shader->getLocation("materialNs"), shininess);
if(diffuse_texture != NULL)
{
diffuse_texture->bind(TEX_ID);
shader->bindInteger(shader->getLocation("baseTexture"), TEX_ID);
}
}
else
{
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, glm::value_ptr(glm::vec4(emission, 1))));
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glm::value_ptr(glm::vec4(diffuse, 1))));
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glm::value_ptr(glm::vec4(specular, 1))));
glAssert(glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess));
if(diffuse_texture != NULL)
diffuse_texture->bind(TEX_ID);
else
{
GLenum texSlot = GL_TEXTURE0+TEX_ID;
glAssert(glActiveTexture(texSlot));
glAssert(glBindTexture(GL_TEXTURE_2D, 0));
}
}
}
unsigned int PhongMaterial::getFlags()
{
unsigned int flags = 0;
if(normal_map != NULL)
flags |= NORMAL_MAP;
if(diffuse_texture != NULL)
flags |= DIFFUSE_TEXTURE;
if(specular_texture != NULL)
flags |= SPECULAR_TEXTURE;
return flags;
}
void PhongMaterial::setTexture(Texture* myTexture)
{
setDiffuseTexture(myTexture);
}
void PhongMaterial::setDiffuseTexture(Texture* myTexture)
{
diffuse_texture = myTexture;
updateShader();
}
void PhongMaterial::setSpecularTexture(Texture* myTexture)
{
specular_texture = myTexture;
updateShader();
}
void PhongMaterial::setNormalMap(Texture* myNormalMap)
{
normal_map = myNormalMap;
updateShader();
}