materials are now created by specifying shaders

shader uniforms are added to materials
This commit is contained in:
devZoGok
2021-11-10 20:23:15 +02:00
parent 34e7a74bba
commit ec8dd7ac1e
17 changed files with 321 additions and 213 deletions
+148 -33
View File
@@ -7,49 +7,164 @@
#include "animatable.h"
#include <vector>
#include <map>
namespace vb01{
class Texture;
class Material : public Animatable{
public:
enum Type{MATERIAL_2D, MATERIAL_PARTICLE, MATERIAL_SKYBOX, MATERIAL_GUI, MATERIAL_POST, MATERIAL_TEXT};
struct Uniform : public Animatable{
void animate(float val, KeyframeChannel channel){
switch(channel.type){
case KeyframeChannel::UNIFORM_1:
animateComponent1(val);
break;
case KeyframeChannel::UNIFORM_2:
animateComponent2(val);
break;
case KeyframeChannel::UNIFORM_3:
animateComponent3(val);
break;
case KeyframeChannel::UNIFORM_4:
animateComponent4(val);
break;
}
}
Material(Type = MATERIAL_2D);
enum Type{
INT,
BOOL,
FLOAT,
VECTOR_2,
VECTOR_3,
VECTOR_4,
TEXTURE
};
std::string name;
Type type;
Material *material = nullptr;
protected:
virtual void animateComponent1(float val){}
virtual void animateComponent2(float val){}
virtual void animateComponent3(float val){}
virtual void animateComponent4(float val){}
};
struct IntUniform : public Uniform{
int value;
IntUniform(std::string name, int value){
this->name = name;
this->value = value;
this->type = Uniform::INT;
}
void animateComponent1(float val){value = (int)val;}
};
struct BoolUniform : public Uniform{
bool value;
BoolUniform(std::string name, bool value){
this->name = name;
this->value = value;
this->type = Uniform::BOOL;
}
void animateComponent1(float val){value = (bool)val;}
};
struct FloatUniform : public Uniform{
float value;
FloatUniform(std::string name, float value){
this->name = name;
this->value = value;
this->type = Uniform::FLOAT;
}
void animateComponent1(float val){value = val;}
};
struct Vector2Uniform : public Uniform{
Vector2 value;
Vector2Uniform(std::string name, Vector2 value){
this->name = name;
this->value = value;
this->type = Uniform::VECTOR_2;
}
void animateComponent1(float val){value.x = val;}
void animateComponent2(float val){value.y = val;}
};
struct Vector3Uniform : public Uniform{
Vector3 value;
Vector3Uniform(std::string name, Vector3 value){
this->name = name;
this->value = value;
this->type = Uniform::VECTOR_3;
}
void animateComponent1(float val){value.x = val;}
void animateComponent2(float val){value.y = val;}
void animateComponent3(float val){value.z = val;}
};
struct Vector4Uniform : public Uniform{
Vector4 value;
Vector4Uniform(std::string name, Vector4 value){
this->name = name;
this->value = value;
this->type = Uniform::VECTOR_4;
}
void animateComponent1(float val){value.x = val;}
void animateComponent2(float val){value.y = val;}
void animateComponent3(float val){value.z = val;}
void animateComponent4(float val){value.w = val;}
};
struct TextureUniform : public Uniform{
Texture *value = nullptr;
bool animatable = false;
TextureUniform(std::string name, Texture *value, bool animatable){
this->name = name;
this->value = value;
this->animatable = animatable;
this->type = Uniform::TEXTURE;
}
};
Material(std::string, bool = false);
~Material();
void update();
inline void addDiffuseMap(Texture *texture){diffuseMapTextures.push_back(texture);}
inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));}
inline void addNormalMap(Texture *texture){normalMapTextures.push_back(texture);}
inline void addSpecularMap(Texture *texture){specularMapTextures.push_back(texture);}
inline void addParallaxMap(Texture *texture){parallaxMapTextures.push_back(texture);}
inline void setDiffuseColor(Vector4 diffuse){this->diffuseColor = diffuse;}
inline void setSpecularColor(Vector4 specular){this->specularColor = specular;}
inline void setLightingEnabled(bool lighting){this->lightingEnabled = lighting;}
inline void setTexturingEnabled(bool texturing){this->texturingEnabled = texturing;}
inline void setDiffuseColorEnabled(bool diffuseColor){this->diffuseColorEnabled = diffuseColor;}
inline void setNormalMapEnabled(bool enabled){this->normalMapEnabled = enabled;}
inline void setSpecularMapEnabled(bool enabled){this->specularMapEnabled = enabled;}
inline void setParallaxMapEnabled(bool enabled){this->parallaxMapEnabled = enabled;}
inline void setShinyness(int shinyness){this->shinyness = shinyness;}
inline void setSpecularStrength(float specStr){this->specularStrength = specStr;}
inline bool isLightingEnabled(){return lightingEnabled;}
inline bool isTexturingEnabled(){return texturingEnabled;}
inline bool isDiffuseColorEnabled(){return diffuseColorEnabled;}
inline Texture* getDiffuseMap(int i){return diffuseMapTextures[i];}
inline Texture* getNormalMap(int i){return normalMapTextures[i];}
inline Texture* getSpecularMap(int i){return specularMapTextures[i];}
Uniform* getUniform(std::string);
inline Uniform* getUniform(int i){return uniforms[i];}
inline void addVariable(std::string name, int value){uniforms.push_back(new IntUniform(name, value));}
inline void addVariable(std::string name, bool value){uniforms.push_back(new BoolUniform(name, value));}
inline void addVariable(std::string name, float value){uniforms.push_back(new BoolUniform(name, value));}
inline void addVariable(std::string name, Vector2 value){uniforms.push_back(new Vector2Uniform(name, value));}
inline void addVariable(std::string name, Vector3 value){uniforms.push_back(new Vector3Uniform(name, value));}
inline void addVariable(std::string name, Vector4 value){uniforms.push_back(new Vector4Uniform(name, value));}
inline void addVariable(std::string name, Texture *value, bool animatable){uniforms.push_back(new TextureUniform(name, value, animatable));}
inline Shader* getShader(){return shader;}
inline Type getType(){return type;}
private:
Type type;
int shinyness = 32;
float specularStrength = .5;
bool lightingEnabled = false, texturingEnabled = true, diffuseColorEnabled = false, normalMapEnabled = false, specularMapEnabled = false, parallaxMapEnabled = false;
std::vector<Texture*> diffuseMapTextures, normalMapTextures, specularMapTextures, parallaxMapTextures;
Vector4 diffuseColor = Vector4::VEC_IJKL, specularColor = Vector4::VEC_ZERO;
std::vector<Uniform*> uniforms;
Shader *shader = nullptr;
void initShader();
void animate(float, KeyframeChannel);
};
}