From 0bf222d38d794e8e2be930b220832c00d8cd1ee6 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 6 Mar 2021 19:17:47 +0200 Subject: [PATCH] texture mixing --- material.cpp | 44 +++++++++++++++++++++++--------------------- material.h | 2 +- texture.cpp | 13 +++++++------ texture.frag | 7 ++++++- texture.h | 9 ++++++--- 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/material.cpp b/material.cpp index 1bff30e..3df0645 100755 --- a/material.cpp +++ b/material.cpp @@ -55,20 +55,22 @@ namespace vb01{ void Material::update(){ shader->use(); shader->setBool(texturingEnabled, "texturingEnabled"); - const int DIFFUSE_SLOT = Texture::DIFFUSE * 2; - const int NORMAL_SLOT = Texture::NORMAL * 2; - const int SPECULAR_SLOT = Texture::SPECULAR * 2; - const int PARALLAX_SLOT = Texture::PARALLAX * 2; - const int ENVIRONMENT_SLOT = Texture::ENVIRONMENT * 2; + const int TEXTURE_SLOTS[]{ + Texture::DIFFUSE * 2, + Texture::NORMAL * 2, + Texture::SPECULAR * 2, + Texture::PARALLAX * 2, + Texture::ENVIRONMENT * 2 + }; if(type == MATERIAL_2D){ shader->setBool(lightingEnabled, "lightingEnabled"); shader->setBool(normalMapEnabled, "normalMapEnabled"); - shader->setInt(DIFFUSE_SLOT, "textures["+to_string(DIFFUSE_SLOT)+"].pastTexture"); - shader->setInt(NORMAL_SLOT, "textures["+to_string(NORMAL_SLOT)+"].pastTexture"); - shader->setInt(SPECULAR_SLOT, "textures["+to_string(SPECULAR_SLOT)+"].pastTexture"); - shader->setInt(PARALLAX_SLOT, "textures["+to_string(PARALLAX_SLOT)+"].pastTexture"); - shader->setInt(ENVIRONMENT_SLOT, "environmentMap"); + for(int i = 0; i < 4; i++){ + shader->setInt(TEXTURE_SLOTS[i], "textures[" + to_string(TEXTURE_SLOTS[i]) + "].pastTexture"); + shader->setInt(TEXTURE_SLOTS[i] + 1, "textures[" + to_string(TEXTURE_SLOTS[i]) + "].nextTexture"); + } + shader->setInt(TEXTURE_SLOTS[4], "environmentMap"); } else if(type == MATERIAL_GUI){ shader->setBool(diffuseColorEnabled, "diffuseColorEnabled"); @@ -77,18 +79,18 @@ namespace vb01{ } if(texturingEnabled){ - int textureSlot = DIFFUSE_SLOT; - for(Texture *t : diffuseMapTextures){ - t->update(textureSlot); - if(t->getNumFrames() > 0) - shader->setBool(true, "textures[" + to_string(textureSlot) + "].animated"); + vector textures; + textures.insert(textures.end(), diffuseMapTextures.begin(), diffuseMapTextures.end()); + textures.insert(textures.end(), normalMapTextures.begin(), normalMapTextures.end()); + textures.insert(textures.end(), specularMapTextures.begin(), specularMapTextures.end()); + textures.insert(textures.end(), parallaxMapTextures.begin(), parallaxMapTextures.end()); + + for(Texture *t : textures){ + int id = t->getTextureTypeId() * 2; + shader->setBool(t->getNumFrames() > 0, "textures[" + to_string(id) + "].animated"); + shader->setFloat(t->getMixRatio(), "textures[" + to_string(id) + "].mixRatio"); + t->update(id); } - for(Texture *t : normalMapTextures) - t->update(NORMAL_SLOT); - for(Texture *t : specularMapTextures) - t->update(SPECULAR_SLOT); - for(Texture *t : parallaxMapTextures) - t->update(PARALLAX_SLOT); } else shader->setVec4(diffuseColor, "diffuseColor"); diff --git a/material.h b/material.h index c2dcb30..0940488 100755 --- a/material.h +++ b/material.h @@ -16,7 +16,7 @@ namespace vb01{ void update(); inline void addDiffuseMap(std::string diffuseMap, bool flip = false){ std::string p[]{diffuseMap}; - diffuseMapTextures.push_back(new Texture(p, 1, 0, flip)); + diffuseMapTextures.push_back(new Texture(p, 1, Texture::DIFFUSE, flip)); } inline void addDiffuseMap(std::string diffuseMap[], int numFrames){diffuseMapTextures.push_back(new Texture(diffuseMap, numFrames));} inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));} diff --git a/texture.cpp b/texture.cpp index e7415c3..73f8894 100755 --- a/texture.cpp +++ b/texture.cpp @@ -34,9 +34,10 @@ namespace vb01{ } } - Texture::Texture(string path[], int numPaths, int updateRate, bool flip){ + Texture::Texture(string path[], int numPaths, TextureTypeId textureTypeId, bool flip){ + mixRatio = .1; + this->typeId = textureTypeId; this->numFrames = numPaths; - this->updateRate = updateRate; texture = new u32[numPaths]; @@ -148,10 +149,10 @@ namespace vb01{ } void Texture::update(int id){ - updateFrame(); + //updateFrame(); select(id); if(numFrames > 0) - select(id + 1); + select(id + 1, 1); } void Texture::updateFrame(){ @@ -163,8 +164,8 @@ namespace vb01{ } } - void Texture::select(int id){ + void Texture::select(int id, int fr){ glActiveTexture(GL_TEXTURE0 + id); - glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[frame]); + glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[fr]); } } diff --git a/texture.frag b/texture.frag index e5cab48..3a61aad 100755 --- a/texture.frag +++ b/texture.frag @@ -75,7 +75,12 @@ float getShadow(int id){ } void main(){ - vec4 finalColor = texturingEnabled?texture(textures[0].pastTexture, texCoords) : diffuseColor; + vec4 finalColor = diffuseColor; + if(texturingEnabled){ + vec4 textureColor = texture(textures[0].pastTexture, texCoords); + finalColor = textures[0].animated ? mix(textureColor, texture(textures[0].nextTexture, texCoords), textures[0].mixRatio) : textureColor; + } + vec3 normal = norm; if(lightingEnabled){ if(normalMapEnabled){ diff --git a/texture.h b/texture.h index f5b21df..4329099 100755 --- a/texture.h +++ b/texture.h @@ -14,22 +14,25 @@ namespace vb01{ ~Texture(); Texture(int, int, bool = true); - Texture(std::string[], int, int = 0, bool = false); + Texture(std::string[], int, TextureTypeId = DIFFUSE, bool = false); Texture(std::string[6], bool = false); Texture(int, bool = true); Texture(FT_Face&, char); - void select(int = 0); + void select(int = 0, int = 0); void update(int = 0); inline unsigned int* getTexture(int i = 0){return &(texture[i]);} inline std::string getPath(){return path;} inline int getNumFrames(){return numFrames;} + inline float getMixRatio(){return mixRatio;} + inline TextureTypeId getTextureTypeId(){return typeId;} private: TextureType type = TextureType::TEXTURE_2D; + TextureTypeId typeId = DIFFUSE; u32 *texture = nullptr; s32 updateRate = 0, numFrames = 0,frame = 0; s64 lastUpdateTime = 0; int width, height, numChannels; - float weight; + float mixRatio; u8 *data; std::string path = "", paths[6];