beginning of suitting textures for animation

This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 7adf42ad7b
commit 0f9622a94c
5 changed files with 38 additions and 57 deletions
+21 -23
View File
@@ -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<Texture*> 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");
+2 -8
View File
@@ -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(){
+13 -17
View File
@@ -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]);
}
}
+1 -6
View File
@@ -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){
+1 -3
View File
@@ -4,7 +4,6 @@
#include <string>
#include <ft2build.h>
#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);}
};
}