From 04a82a73647fe400a491baa359ac02b8cc28e6a3 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 1 Dec 2015 00:24:50 +0100 Subject: [PATCH] added alpha mask handling and fixed specular color issue --- forward.frag | 8 ++++---- forwardmodule.cpp | 2 ++ phongmaterial.cpp | 13 +++++++++++++ phongmaterial.h | 16 ++++------------ texture.cpp | 18 ++++++++++++++---- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/forward.frag b/forward.frag index b9f7a94..38b98dd 100644 --- a/forward.frag +++ b/forward.frag @@ -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 } diff --git a/forwardmodule.cpp b/forwardmodule.cpp index 42e167d..2d117db 100644 --- a/forwardmodule.cpp +++ b/forwardmodule.cpp @@ -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); diff --git a/phongmaterial.cpp b/phongmaterial.cpp index 783362a..7b40051 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -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; +} diff --git a/phongmaterial.h b/phongmaterial.h index 28e5fdb..2506f67 100644 --- a/phongmaterial.h +++ b/phongmaterial.h @@ -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; diff --git a/texture.cpp b/texture.cpp index 9d1890e..a9c5226 100644 --- a/texture.cpp +++ b/texture.cpp @@ -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)