added alpha mask handling and fixed specular color issue

This commit is contained in:
Anselme 2015-12-01 00:24:50 +01:00
parent b4aa0acfda
commit 04a82a7364
5 changed files with 37 additions and 20 deletions

View File

@ -74,15 +74,15 @@ void main(void) {
#endif
#ifdef SPECULAR_TEXTURE
vec3 specular = texture(specularTexture, varTexCoord).rgb;
vec3 specular = vec3(texture(specularTexture, varTexCoord).r);
#else
vec3 specular = materialKs;
#endif
#ifdef AMBIENT_LIGHT
outColor = vec4(ambient + lightColor*diffuse, 1);
outColor = vec4(ambient + lightColor*diffuse, 1);
#else
vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView);
outColor = vec4(light, 1);
vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView);
outColor = vec4(light, 1);
#endif
}

View File

@ -147,6 +147,8 @@ void ForwardModule::compileShaders(Scene* scene)
defines.push_back(flagStr[DIFFUSE_TEXTURE]);
if(i & SPECULAR_TEXTURE_FLAG)
defines.push_back(flagStr[SPECULAR_TEXTURE]);
if(i & ALPHA_MASK_FLAG)
defines.push_back(flagStr[ALPHA_MASK]);
// for each geometry flag, 3 shaders are compiled, one for each kind of lighting
flags.push_back(i);

View File

@ -41,6 +41,12 @@ void PhongMaterial::bindAttributes(Shader* myShader)
}
else
myShader->bindVec3(myShader->getLocation("materialKs"), specular);
if(alpha_mask != NULL)
{
alpha_mask->bind(ALPHA_MASK);
myShader->bindInteger(myShader->getLocation("alphaMask"), ALPHA_MASK);
}
}
else
{
@ -71,6 +77,8 @@ unsigned int PhongMaterial::getFlags()
flags |= DIFFUSE_TEXTURE_FLAG;
if(specular_texture != NULL)
flags |= SPECULAR_TEXTURE_FLAG;
if(alpha_mask != NULL)
flags |= ALPHA_MASK_FLAG;
return flags;
}
@ -93,3 +101,8 @@ void PhongMaterial::setNormalMap(Texture* myNormalMap)
{
normal_map = myNormalMap;
}
void PhongMaterial::setAlphaMask(Texture* myAlphaMask)
{
alpha_mask = myAlphaMask;
}

View File

@ -17,6 +17,7 @@ public:
Texture* diffuse_texture;
Texture* specular_texture;
Texture* normal_map;
Texture* alpha_mask;
PhongMaterial() :
ambient(0),
@ -26,18 +27,8 @@ public:
ambient_texture(NULL),
diffuse_texture(NULL),
specular_texture(NULL),
normal_map(NULL)
{}
PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) :
ambient(0),
diffuse(myKd),
specular(myKs),
shininess(myNs),
ambient_texture(NULL),
diffuse_texture(NULL),
specular_texture(NULL),
normal_map(NULL)
normal_map(NULL),
alpha_mask(NULL)
{}
virtual void bindAttributes(Shader* myShader = NULL);
@ -54,6 +45,7 @@ public:
void setDiffuseTexture(Texture* myTexture);
void setSpecularTexture(Texture* myTexture);
void setNormalMap(Texture* myNormalMap);
void setAlphaMask(Texture* myAlphaMask);
private:
Shader* shader;

View File

@ -72,14 +72,24 @@ void Texture::createNoiseTexture(GLenum textureSlot, int width, int height, floa
}
}
}
glAssert(glTexImage2D(textureSlot, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
glAssert(glTexImage2D(textureSlot, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
}
void Texture::createTexture(Image* myImage, GLenum textureSlot)
{
int bpp = (myImage->depth == 32) ? GL_RGBA : GL_RGB;
int format = (myImage->depth == 32) ? GL_BGRA : GL_BGR;
glAssert(glTexImage2D(textureSlot, 0, bpp, myImage->width, myImage->height, 0, format, GL_UNSIGNED_BYTE, myImage->pixels));
switch(myImage->depth)
{
case 32:
glAssert(glTexImage2D(textureSlot, 0, GL_RGBA, myImage->width, myImage->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, myImage->pixels));
break;
case 24:
glAssert(glTexImage2D(textureSlot, 0, GL_RGB, myImage->width, myImage->height, 0, GL_BGR, GL_UNSIGNED_BYTE, myImage->pixels));
break;
case 8:
glAssert(glTexImage2D(textureSlot, 0, GL_R8, myImage->width, myImage->height, 0, GL_RED, GL_UNSIGNED_BYTE, myImage->pixels));
break;
}
}
void Texture::setWrap(GLint wrap)