From 0f9622a94ca99b421e73877d446cb3aab2c4430f Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 6 Mar 2021 14:51:07 +0200 Subject: [PATCH] beginning of suitting textures for animation --- material.cpp | 44 +++++++++++++++++++++----------------------- particle.frag | 10 ++-------- texture.cpp | 30 +++++++++++++----------------- texture.frag | 7 +------ texture.h | 4 +--- 5 files changed, 38 insertions(+), 57 deletions(-) diff --git a/material.cpp b/material.cpp index 7320b84..cd2fe93 100755 --- a/material.cpp +++ b/material.cpp @@ -84,22 +84,20 @@ namespace vb01{ void Material::update(){ shader->use(); shader->setBool(texturingEnabled, "texturingEnabled"); - const int TEXTURE_SLOTS[]{ - Texture::DIFFUSE * 2, - Texture::NORMAL * 2, - Texture::SPECULAR * 2, - Texture::PARALLAX * 2, - Texture::ENVIRONMENT * 2 - }; + 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; if(type == MATERIAL_2D){ shader->setBool(lightingEnabled, "lightingEnabled"); shader->setBool(normalMapEnabled, "normalMapEnabled"); - 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"); + 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"); } else if(type == MATERIAL_GUI){ shader->setBool(diffuseColorEnabled, "diffuseColorEnabled"); @@ -108,18 +106,18 @@ namespace vb01{ } if(texturingEnabled){ - 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); + int textureSlot = DIFFUSE_SLOT; + for(Texture *t : diffuseMapTextures){ + t->update(textureSlot); + if(t->getNumFrames() > 0) + shader->setBool(true, "textures[" + to_string(textureSlot) + "].animated"); } + 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/particle.frag b/particle.frag index b188d4e..044f44f 100755 --- a/particle.frag +++ b/particle.frag @@ -9,14 +9,8 @@ const int numParticles = 1; layout (location = 0) out vec4 FragColor; layout (location = 1) out vec4 BrightColor; -struct Texture{ - float mixRatio; - sampler2D pastTexture, nextTexture; - bool animated; -}; - -uniform Texture tex; -uniform float lifePercentage[numParticles]; +uniform sampler2D tex; +uniform float lifePercentage[500]; uniform vec4 startColor, endColor; void main(){ diff --git a/texture.cpp b/texture.cpp index c542fb6..13ebf2b 100755 --- a/texture.cpp +++ b/texture.cpp @@ -148,28 +148,24 @@ namespace vb01{ } } - void Texture::animate(float value, KeyframeChannel keyframeChannel){ - switch(keyframeChannel.type){ - case KeyframeChannel::TEXTURE_MIX_RATIO: - mixRatio = value; - break; - case KeyframeChannel::TEXTURE_FRAME_A: - frameA = (int)value; - break; - case KeyframeChannel::TEXTURE_FRAME_B: - frameB = (int)value; - break; - } + void Texture::update(int id){ + updateFrame(); + select(id); + if(numFrames > 0) + select(id + 1); } - void Texture::update(int id){ - select(id, frameA); - if(numFrames > 0) - select(id + 1, frameB); + void Texture::updateFrame(){ + if(numFrames > 0){ + if(getTime() - lastUpdateTime > updateRate){ + frame = getNextFrame(frame); + lastUpdateTime = getTime(); + } + } } void Texture::select(int id, int fr){ glActiveTexture(GL_TEXTURE0 + id); - glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[fr]); + glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[frame]); } } diff --git a/texture.frag b/texture.frag index 3a61aad..e5cab48 100755 --- a/texture.frag +++ b/texture.frag @@ -75,12 +75,7 @@ float getShadow(int id){ } void main(){ - 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; - } - + vec4 finalColor = texturingEnabled?texture(textures[0].pastTexture, texCoords) : diffuseColor; vec3 normal = norm; if(lightingEnabled){ if(normalMapEnabled){ diff --git a/texture.h b/texture.h index 7443505..6e27c35 100755 --- a/texture.h +++ b/texture.h @@ -4,7 +4,6 @@ #include #include #include "util.h" -#include "animatable.h" #include FT_FREETYPE_H namespace vb01{ @@ -25,8 +24,6 @@ namespace vb01{ 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; @@ -38,6 +35,7 @@ namespace vb01{ std::string path = "", paths[6]; void createCubemap(bool, bool); + void updateFrame(); inline int getNextFrame(int frameId){return (frameId + 1 < numFrames ? frameId + 1 : 0);} }; }