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
+9 -9
View File
@@ -41,18 +41,18 @@ int main(){
*/ */
Box *driverBox = new Box(Vector3(1, 1, 1) * .25); Box *driverBox = new Box(Vector3(1, 1, 1) * .25);
Node *driver = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); Node *driver = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController());
Material *driverMat = new Material(); Material *driverMat = new Material(PATH + "texture");
driverMat->setTexturingEnabled(false); driverMat->addVariable("texturingEnabled", false);
driverMat->setDiffuseColor(Vector4(0, 1, 0, 1)); driverMat->addVariable("diffuseColor", Vector4(0, 1, 0, 1));
driverBox->setMaterial(driverMat); driverBox->setMaterial(driverMat);
driver->attachMesh(driverBox); driver->attachMesh(driverBox);
rootNode->attachChild(driver); rootNode->attachChild(driver);
Box *leftBox = new Box(Vector3(1, 1, 1) * .25); Box *leftBox = new Box(Vector3(1, 1, 1) * .25);
Node *leftNode = new Node(); Node *leftNode = new Node();
Material *leftMat = new Material(); Material *leftMat = new Material(PATH + "texture");
leftMat->setTexturingEnabled(false); leftMat->addVariable("texturingEnabled", false);
leftMat->setDiffuseColor(Vector4(1, 0, 0, 1)); leftMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1));
leftBox->setMaterial(leftMat); leftBox->setMaterial(leftMat);
leftNode->attachMesh(leftBox); leftNode->attachMesh(leftBox);
rootNode->attachChild(leftNode); rootNode->attachChild(leftNode);
@@ -60,9 +60,9 @@ int main(){
Box *rightBox = new Box(Vector3(1, 1, 1) * .25); Box *rightBox = new Box(Vector3(1, 1, 1) * .25);
Node *rightNode = new Node(); Node *rightNode = new Node();
Material *rightMat = new Material(); Material *rightMat = new Material(PATH + "texture");
rightMat->setTexturingEnabled(false); rightMat->addVariable("texturingEnabled", false);
rightMat->setDiffuseColor(Vector4(0, 0, 1, 1)); rightMat->addVariable("diffuseColor", Vector4(0, 0, 1, 1));
rightBox->setMaterial(rightMat); rightBox->setMaterial(rightMat);
rightNode->attachMesh(rightBox); rightNode->attachMesh(rightBox);
rootNode->attachChild(rightNode); rootNode->attachChild(rightNode);
+4
View File
@@ -19,6 +19,10 @@ namespace vb01{
SCALE_X, SCALE_X,
SCALE_Y, SCALE_Y,
SCALE_Z, SCALE_Z,
UNIFORM_1,
UNIFORM_2,
UNIFORM_3,
UNIFORM_4,
SHAPE_KEY_MIN, SHAPE_KEY_MIN,
SHAPE_KEY_VALUE, SHAPE_KEY_VALUE,
SHAPE_KEY_MAX, SHAPE_KEY_MAX,
+7 -3
View File
@@ -55,20 +55,24 @@ namespace vb01{
glGenFramebuffers(1, &depthmapFBO); glGenFramebuffers(1, &depthmapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO);
if(type == POINT){ if(type == POINT){
depthMap = new Texture(depthMapSize); depthMap = new Texture(depthMapSize);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, *(depthMap->getTexture()), 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, *(depthMap->getTexture()), 0);
depthMapShader = new Shader(basePath + "vert", basePath + "frag", basePath + "geo"); depthMapShader = new Shader(basePath, true);
} }
else{ else{
depthMap = new Texture(depthMapSize, depthMapSize, true); depthMap = new Texture(depthMapSize, depthMapSize, true);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *(depthMap->getTexture()), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *(depthMap->getTexture()), 0);
depthMapShader = new Shader(basePath + "vert", basePath + "frag"); depthMapShader = new Shader(basePath);
} }
glDrawBuffer(GL_NONE); glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE); glReadBuffer(GL_NONE);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Not complete\n"; cout << "Not complete\n";
glBindFramebuffer(GL_FRAMEBUFFER, *(Root::getSingleton()->getFBO())); glBindFramebuffer(GL_FRAMEBUFFER, *(Root::getSingleton()->getFBO()));
} }
@@ -129,7 +133,7 @@ namespace vb01{
vector<Mesh*> meshes = n->getMeshes(); vector<Mesh*> meshes = n->getMeshes();
for(Mesh *mesh : meshes){ for(Mesh *mesh : meshes){
Material *mat = mesh->getMaterial(); Material *mat = mesh->getMaterial();
if(mesh->isCastShadow() && n->isVisible() && mat && mat->isLightingEnabled()){ if(mesh->isCastShadow() && n->isVisible() && mat && ((Material::BoolUniform*)mat->getUniform("lightingEnabled"))->value){
Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO); Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO);
Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W); Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W);
Vector3 rotAxis = rot.norm().getAxis(); Vector3 rotAxis = rot.norm().getAxis();
+12 -10
View File
@@ -7,6 +7,8 @@
#include "animation.h" #include "animation.h"
#include "animationController.h" #include "animationController.h"
#include "animationChannel.h" #include "animationChannel.h"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace std; using namespace std;
using namespace vb01; using namespace vb01;
@@ -43,27 +45,27 @@ int main(){
/* /*
* Creates a material with diffuse, normal, and specular maps attached. * Creates a material with diffuse, normal, and specular maps attached.
*/ */
Material *mat = new Material(); Material *mat = new Material(PATH + "texture");
mat->setTexturingEnabled(true); mat->addVariable("texturingEnabled", true);
mat->setLightingEnabled(true); mat->addVariable("lightingEnabled", true);
mat->setNormalMapEnabled(true); mat->addVariable("normalMapEnabled", true);
mat->setSpecularMapEnabled(true); mat->addVariable("specularMapEnabled", true);
string im0[] = {TEX_PATH + "bricks.jpg"}; string im0[] = {TEX_PATH + "bricks.jpg"};
Texture *diffuseTex = new Texture(im0, 1); Texture *diffuseTex = new Texture(im0, 1);
mat->addDiffuseMap(diffuseTex); mat->addVariable("textures[0]", diffuseTex);
string im1[] = {TEX_PATH + "bricksNormal.jpg"}; string im1[] = {TEX_PATH + "bricksNormal.jpg"};
Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); Texture *normalTex = new Texture(im1, 1, Texture::NORMAL);
mat->addNormalMap(normalTex); mat->addVariable("textures[1]", normalTex);
string im2[] = {TEX_PATH + "bricksSpecular.jpg"}; string im2[] = {TEX_PATH + "bricksSpecular.jpg"};
Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR);
mat->addSpecularMap(specularTex); mat->addVariable("textures[2]", specularTex);
//Setting specular parameteres //Setting specular parameteres
mat->setSpecularStrength(1); mat->addVariable("specularStrength", 1);
mat->setShinyness(32); mat->addVariable("shinyness", 32);
box->setMaterial(mat); box->setMaterial(mat);
rootNode->attachChild(box); rootNode->attachChild(box);
+58 -112
View File
@@ -3,134 +3,80 @@
#include "root.h" #include "root.h"
using namespace std; using namespace std;
using namespace glm;
namespace vb01{ namespace vb01{
Material::Material(Type type){ Material::Material(string shaderName, bool geometryShader){
this->type = type; shader = new Shader(shaderName);
initShader();
} }
Material::~Material(){ Material::~Material(){
delete shader; delete shader;
for(Texture *t : diffuseMapTextures)
delete t;
for(Texture *t : normalMapTextures)
delete t;
for(Texture *t : specularMapTextures)
delete t;
} }
void Material::initShader(){ Material::Uniform* Material::getUniform(string name){
string basePath = Root::getSingleton()->getLibPath(), shaderName; Uniform *uni = nullptr;
switch(type){
case MATERIAL_2D:
shaderName = "texture.";
break;
case MATERIAL_PARTICLE:
shaderName = "particle.";
break;
case MATERIAL_SKYBOX:
shaderName = "skybox.";
break;
case MATERIAL_GUI:
shaderName = "gui.";
break;
case MATERIAL_POST:
shaderName = "postProcess.";
break;
case MATERIAL_TEXT:
shaderName = "text.";
break;
}
/*
if(type==MATERIAL_2D)
//shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag");
shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag",basePath+shaderName+"geo");
else
*/
shader = new Shader(basePath + shaderName + "vert", basePath + shaderName + "frag");
}
void Material::animate(float value, KeyframeChannel keyframeChannel){ for(Uniform *u : uniforms)
switch(keyframeChannel.type){ if(u->name == name){
case KeyframeChannel::DIFFUSE_COLOR_W: uni = u;
diffuseColor.w = value; break;
break; }
case KeyframeChannel::DIFFUSE_COLOR_X:
diffuseColor.x = value; return uni;
break;
case KeyframeChannel::DIFFUSE_COLOR_Y:
diffuseColor.y = value;
break;
case KeyframeChannel::DIFFUSE_COLOR_Z:
diffuseColor.z = value;
break;
case KeyframeChannel::SPECULAR_COLOR_W:
specularColor.w = value;
break;
case KeyframeChannel::SPECULAR_COLOR_X:
specularColor.x = value;
break;
case KeyframeChannel::SPECULAR_COLOR_Y:
specularColor.y = value;
break;
case KeyframeChannel::SPECULAR_COLOR_Z:
specularColor.z = value;
break;
}
} }
void Material::update(){ void Material::update(){
shader->use(); 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
};
if(type == MATERIAL_2D){ int i = 0;
shader->setBool(lightingEnabled, "lightingEnabled");
shader->setBool(normalMapEnabled, "normalMapEnabled");
shader->setBool(specularMapEnabled, "specularMapEnabled");
shader->setInt(shinyness, "shinyness");
shader->setFloat(specularStrength, "specularStrength");
shader->setVec4(specularColor, "specularColor");
for(int i = 0; i < 4; i++){
shader->setInt(TEXTURE_SLOTS[i], "textures[" + to_string(i) + "].pastTexture");
shader->setInt(TEXTURE_SLOTS[i] + 1, "textures[" + to_string(i) + "].nextTexture");
}
shader->setInt(TEXTURE_SLOTS[4], "environmentMap");
}
else if(type == MATERIAL_GUI){
shader->setBool(diffuseColorEnabled, "diffuseColorEnabled");
if(diffuseColorEnabled)
shader->setVec4(diffuseColor, "diffuseColor");
}
else if(type == MATERIAL_TEXT){
shader->setInt(0, "textures[0].pastTexture");
shader->setInt(1, "textures[0].nextTexture");
shader->setInt(2, "textures[1].pastTexture");
}
if(texturingEnabled){ for(Uniform *uni : uniforms){
vector<Texture*> textures; switch(uni->type){
textures.insert(textures.end(), diffuseMapTextures.begin(), diffuseMapTextures.end()); case Uniform::INT:{
textures.insert(textures.end(), normalMapTextures.begin(), normalMapTextures.end()); IntUniform *u = (IntUniform*)uni;
textures.insert(textures.end(), specularMapTextures.begin(), specularMapTextures.end()); shader->setInt(u->value, u->name);
textures.insert(textures.end(), parallaxMapTextures.begin(), parallaxMapTextures.end()); break;
}
case Uniform::BOOL:{
BoolUniform *u = (BoolUniform*)uni;
shader->setBool(u->value, u->name);
break;
}
case Uniform::FLOAT:{
FloatUniform *u = (FloatUniform*)uni;
shader->setFloat(u->value, u->name);
break;
}
case Uniform::VECTOR_2:{
Vector2Uniform *u = (Vector2Uniform*)uni;
shader->setVec2(u->value, u->name);
break;
}
case Uniform::VECTOR_3:{
Vector3Uniform *u = (Vector3Uniform*)uni;
shader->setVec3(u->value, u->name);
break;
}
case Uniform::VECTOR_4:{
Vector4Uniform *u = (Vector4Uniform*)uni;
shader->setVec4(u->value, u->name);
break;
}
case Uniform::TEXTURE:{
TextureUniform *u = (TextureUniform*)uni;
shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : ""));
for(Texture *t : textures){ if(u->value->getNumFrames() > 1){
int id = t->getTextureTypeId(); shader->setInt(2 * i + 1, u->name + ".nextTexture");
shader->setBool(t->getNumFrames() > 1, "textures[" + to_string(id) + "].animated"); shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio");
shader->setFloat(t->getMixRatio(), "textures[" + to_string(id) + "].mixRatio"); }
t->update(2 * id);
} break;
}
}
++i;
} }
else
shader->setVec4(diffuseColor, "diffuseColor");
} }
} }
+148 -33
View File
@@ -7,49 +7,164 @@
#include "animatable.h" #include "animatable.h"
#include <vector> #include <vector>
#include <map>
namespace vb01{ namespace vb01{
class Texture;
class Material : public Animatable{ class Material : public Animatable{
public: 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(); ~Material();
void update(); void update();
inline void addDiffuseMap(Texture *texture){diffuseMapTextures.push_back(texture);} Uniform* getUniform(std::string);
inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));} inline Uniform* getUniform(int i){return uniforms[i];}
inline void addNormalMap(Texture *texture){normalMapTextures.push_back(texture);} inline void addVariable(std::string name, int value){uniforms.push_back(new IntUniform(name, value));}
inline void addSpecularMap(Texture *texture){specularMapTextures.push_back(texture);} inline void addVariable(std::string name, bool value){uniforms.push_back(new BoolUniform(name, value));}
inline void addParallaxMap(Texture *texture){parallaxMapTextures.push_back(texture);} inline void addVariable(std::string name, float value){uniforms.push_back(new BoolUniform(name, value));}
inline void setDiffuseColor(Vector4 diffuse){this->diffuseColor = diffuse;} inline void addVariable(std::string name, Vector2 value){uniforms.push_back(new Vector2Uniform(name, value));}
inline void setSpecularColor(Vector4 specular){this->specularColor = specular;} inline void addVariable(std::string name, Vector3 value){uniforms.push_back(new Vector3Uniform(name, value));}
inline void setLightingEnabled(bool lighting){this->lightingEnabled = lighting;} inline void addVariable(std::string name, Vector4 value){uniforms.push_back(new Vector4Uniform(name, value));}
inline void setTexturingEnabled(bool texturing){this->texturingEnabled = texturing;} inline void addVariable(std::string name, Texture *value, bool animatable){uniforms.push_back(new TextureUniform(name, value, animatable));}
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];}
inline Shader* getShader(){return shader;} inline Shader* getShader(){return shader;}
inline Type getType(){return type;}
private: private:
Type type; std::vector<Uniform*> uniforms;
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;
Shader *shader = nullptr; Shader *shader = nullptr;
void initShader();
void animate(float, KeyframeChannel);
}; };
} }
+8 -3
View File
@@ -68,7 +68,7 @@ namespace vb01{
u32 RBO; u32 RBO;
string basePath = "../../vb01/depthMap."; string basePath = "../../vb01/depthMap.";
environmentShader = new Shader(basePath + "vert", basePath + "frag", basePath + "geo"); environmentShader = new Shader(basePath);
environmentMap = new Texture(width, false); environmentMap = new Texture(width, false);
glGenFramebuffers(1, &environmentBuffer); glGenFramebuffers(1, &environmentBuffer);
@@ -133,8 +133,10 @@ namespace vb01{
} }
Vector3 rotAxis = orient.getAxis(); Vector3 rotAxis = orient.getAxis();
if(rotAxis == Vector3::VEC_ZERO) if(rotAxis == Vector3::VEC_ZERO)
rotAxis = Vector3::VEC_I; rotAxis = Vector3::VEC_I;
mat4 model = mat4(1.); mat4 model = mat4(1.);
model = translate(model, vec3(pos.x, pos.y, pos.z)); model = translate(model, vec3(pos.x, pos.y, pos.z));
model = rotate(model, orient.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z)); model = rotate(model, orient.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z));
@@ -151,8 +153,10 @@ namespace vb01{
if(reflect) if(reflect)
updateReflection(shader, pos, width, height); updateReflection(shader, pos, width, height);
if(skeleton) if(skeleton)
updateSkeleton(shader); updateSkeleton(shader);
if(numShapeKeys > 0) if(numShapeKeys > 0)
updateShapeKeys(shader); updateShapeKeys(shader);
@@ -164,8 +168,9 @@ namespace vb01{
shader->setVec3(camPos, "camPos"); shader->setVec3(camPos, "camPos");
shader->setMat4(model, "model"); shader->setMat4(model, "model");
if(material->getType() == Material::MATERIAL_GUI){ if(shader->getName() == "gui"){
shader->setVec2(Vector2((float)width, (float)height), "screen"); shader->setVec2(Vector2((float)width, (float)height), "screen");
if(node) if(node)
shader->setVec3(pos, "pos"); shader->setVec3(pos, "pos");
} }
@@ -280,7 +285,7 @@ namespace vb01{
glViewport(0, 0, root->getWidth(), root->getHeight()); glViewport(0, 0, root->getWidth(), root->getHeight());
shader->setBool(reflect, "environmentMapEnabled"); shader->setBool(reflect, "environmentMapEnabled");
root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4); //root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4);
environmentMap->select(4); environmentMap->select(4);
} }
+9 -7
View File
@@ -6,6 +6,8 @@
#include "animationController.h" #include "animationController.h"
#include "animationChannel.h" #include "animationChannel.h"
#include "box.h" #include "box.h"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace std; using namespace std;
using namespace vb01; using namespace vb01;
@@ -38,12 +40,12 @@ int main(){
cam->lookAt(Vector3(1, 0, 0).norm(), Vector3(0, 1, 0).norm()); cam->lookAt(Vector3(1, 0, 0).norm(), Vector3(0, 1, 0).norm());
Model *model = new Model(MODEL_PATH + "kek.vb"); Model *model = new Model(MODEL_PATH + "kek.vb");
Material *mat = new Material(); Material *mat = new Material(PATH + "texture");
mat->setLightingEnabled(false); mat->addVariable("lightingEnabled", false);
mat->setTexturingEnabled(true); mat->addVariable("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"}; string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1); Texture *texture = new Texture(images, 1);
mat->addDiffuseMap(texture); mat->addVariable("textures[0]", texture);
model->setMaterial(mat); model->setMaterial(mat);
rootNode->attachChild(model); rootNode->attachChild(model);
@@ -67,9 +69,9 @@ int main(){
* Creates a box attached to the bone controlling the shape keys. * Creates a box attached to the bone controlling the shape keys.
*/ */
Box *box = new Box(Vector3(1, 1, 1) * .25); Box *box = new Box(Vector3(1, 1, 1) * .25);
Material *boxMat = new Material(); Material *boxMat = new Material(PATH + "texture");
boxMat->setTexturingEnabled(false); boxMat->addVariable("texturingEnabled", false);
boxMat->setDiffuseColor(Vector4(1, 0, 0, 1)); boxMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1));
box->setMaterial(boxMat); box->setMaterial(boxMat);
skeleton->getBone("bone")->attachMesh(box); skeleton->getBone("bone")->attachMesh(box);
+20 -11
View File
@@ -82,8 +82,10 @@ namespace vb01{
glBindFramebuffer(GL_FRAMEBUFFER, FBO); glBindFramebuffer(GL_FRAMEBUFFER, FBO);
u32 colorAttachments[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; u32 colorAttachments[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
for(int i = 0; i < 2; i++) for(int i = 0; i < 2; i++)
glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[i], GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[i], GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0);
glDrawBuffers(2, colorAttachments); glDrawBuffers(2, colorAttachments);
glGenRenderbuffers(1, &RBO); glGenRenderbuffers(1, &RBO);
@@ -93,17 +95,19 @@ namespace vb01{
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Not complete\n"; cout << "Not complete\n";
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Root::initBloomFramebuffer(){ void Root::initBloomFramebuffer(){
string basePath = libPath + "blur."; blurShader = new Shader(libPath + "blur");
blurShader = new Shader(basePath + "vert", basePath + "frag");
glGenFramebuffers(2, pingpongBuffers); glGenFramebuffers(2, pingpongBuffers);
for(int i = 0; i < 2; i++){ for(int i = 0; i < 2; i++){
glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[i]); glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[i]);
pingPongTextures[i] = new Texture(width, height, false); pingPongTextures[i] = new Texture(width, height, false);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *(pingPongTextures[i]->getTexture()), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *(pingPongTextures[i]->getTexture()), 0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Not complete\n"; cout << "Not complete\n";
} }
@@ -111,9 +115,9 @@ namespace vb01{
void Root::initGuiPlane(Texture *fragTexture, Texture *brightTexture){ void Root::initGuiPlane(Texture *fragTexture, Texture *brightTexture){
guiPlane = new Quad(Vector3(width, height, -1), false); guiPlane = new Quad(Vector3(width, height, -1), false);
Material *mat = new Material(Material::MATERIAL_POST); Material *mat = new Material(libPath + "postProcess");
mat->addDiffuseMap(fragTexture); mat->addVariable("frag", fragTexture, false);
mat->addDiffuseMap(brightTexture); mat->addVariable("bright", brightTexture, false);
guiPlane->setMaterial(mat); guiPlane->setMaterial(mat);
} }
@@ -154,16 +158,20 @@ namespace vb01{
bool horizontal = false; bool horizontal = false;
blurShader->use(); blurShader->use();
blurShader->setVec2(Vector2(width, height), "screen"); blurShader->setVec2(Vector2(width, height), "screen");
for(int i = 0; i < blurLevel; i++){ for(int i = 0; i < blurLevel; i++){
glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[horizontal]); glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[horizontal]);
blurShader->setBool(horizontal, "horizontal"); blurShader->setBool(horizontal, "horizontal");
if(i == 0) if(i == 0)
guiPlane->getMaterial()->getDiffuseMap(1)->select(); ((Material::TextureUniform*)guiPlane->getMaterial()->getUniform("bright"))->value->select();
else else
pingPongTextures[!horizontal]->select(); pingPongTextures[!horizontal]->select();
guiPlane->render(); guiPlane->render();
horizontal = !horizontal; horizontal = !horizontal;
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
@@ -180,15 +188,16 @@ namespace vb01{
shader->setInt(0, "frag"); shader->setInt(0, "frag");
shader->setInt(1, "bright"); shader->setInt(1, "bright");
material->getDiffuseMap(0)->select(0); ((Material::TextureUniform*)material->getUniform("frag"))->value->select(0);
material->getDiffuseMap(1)->select(1); ((Material::TextureUniform*)material->getUniform("bright"))->value->select(1);
guiPlane->render(); guiPlane->render();
} }
void Root::createSkybox(string textures[6]){ void Root::createSkybox(string paths[6]){
skybox = new Box(Vector3(1, 1, 1) * 10); skybox = new Box(Vector3(1, 1, 1) * 10);
Material *skyboxMat = new Material(Material::MATERIAL_SKYBOX); Material *skyboxMat = new Material(libPath + "skybox");
skyboxMat->addDiffuseMap(textures); Texture *texture = new Texture(paths);
skyboxMat->addVariable("skybox", texture, true);
skybox->setMaterial(skyboxMat); skybox->setMaterial(skyboxMat);
} }
+10 -4
View File
@@ -12,16 +12,22 @@ using namespace std;
using namespace glm; using namespace glm;
namespace vb01{ namespace vb01{
Shader::Shader(string vertShaderPath, string fragShaderPath, string geoShaderPath){ Shader::Shader(string shaderPath, bool geometry){
initShaders(vertShaderPath, fragShaderPath, geoShaderPath); this->geometry = geometry;
this->path = shaderPath;
initShaders(shaderPath + ".vert", shaderPath + ".frag", shaderPath + ".geo");
loadShaders(); loadShaders();
} }
Shader::~Shader(){} Shader::~Shader(){}
void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ string Shader::getName(){
geometry = (geoShaderPath != ""); int dirId = path.find_last_of('/');
return (dirId != -1 ? path.substr(dirId) : path);
}
void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){
ifstream vertShaderFile, geoShaderFile, fragShaderFile; ifstream vertShaderFile, geoShaderFile, fragShaderFile;
vertShaderFile.open(vertShaderPath); vertShaderFile.open(vertShaderPath);
geoShaderFile.open(geoShaderPath); geoShaderFile.open(geoShaderPath);
+4 -2
View File
@@ -14,8 +14,9 @@ namespace vb01{
enum ShaderType{VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER}; enum ShaderType{VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER};
Shader(){} Shader(){}
Shader(std::string, std::string, std::string = ""); Shader(std::string, bool = false);
~Shader(); ~Shader();
std::string getName();
void setNumLights(int); void setNumLights(int);
void use(); void use();
void setMat4(glm::mat4, std::string); void setMat4(glm::mat4, std::string);
@@ -26,6 +27,7 @@ namespace vb01{
void setBool(bool, std::string); void setBool(bool, std::string);
void setInt(int, std::string); void setInt(int, std::string);
void editShader(ShaderType, int, std::string); void editShader(ShaderType, int, std::string);
inline bool isGeometry(){return geometry;}
private: private:
void initShaders(std::string, std::string, std::string); void initShaders(std::string, std::string, std::string);
void replaceLine(ShaderType, int, std::string); void replaceLine(ShaderType, int, std::string);
@@ -34,7 +36,7 @@ namespace vb01{
void checkCompileErrors(u32, ErrorType); void checkCompileErrors(u32, ErrorType);
u32 id; u32 id;
bool geometry = false; bool geometry = false;
std::string vString, fString, gString; std::string path, vString, fString, gString;
friend class ShaderTest; friend class ShaderTest;
}; };
+10 -8
View File
@@ -7,6 +7,8 @@
#include "animation.h" #include "animation.h"
#include "animationController.h" #include "animationController.h"
#include "animationChannel.h" #include "animationChannel.h"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace std; using namespace std;
using namespace vb01; using namespace vb01;
@@ -40,23 +42,23 @@ int main(){
Box *box = new Box(Vector3(40, .2, 40)); Box *box = new Box(Vector3(40, .2, 40));
Node *boxNode = new Node(); Node *boxNode = new Node();
Material *boxMat = new Material(); Material *boxMat = new Material(PATH + "texture");
boxMat->setTexturingEnabled(true); boxMat->addVariable("texturingEnabled", true);
boxMat->setLightingEnabled(true); boxMat->addVariable("lightingEnabled", true);
string im0[] = {TEX_PATH + "bricks.jpg"}; string im0[] = {TEX_PATH + "bricks.jpg"};
Texture *boxTex = new Texture(im0, 1); Texture *boxTex = new Texture(im0, 1);
boxMat->addDiffuseMap(boxTex); boxMat->addVariable("textures[0]", boxTex);
box->setMaterial(boxMat); box->setMaterial(boxMat);
boxNode->attachMesh(box); boxNode->attachMesh(box);
rootNode->attachChild(boxNode); rootNode->attachChild(boxNode);
Model *jet = new Model(MODEL_PATH + "jet00.obj"); Model *jet = new Model(MODEL_PATH + "jet00.obj");
Material *jetMat = new Material(); Material *jetMat = new Material(PATH + "texture");
jetMat->setTexturingEnabled(true); jetMat->addVariable("texturingEnabled", true);
jetMat->setLightingEnabled(true); jetMat->addVariable("lightingEnabled", true);
string im1[] = {TEX_PATH + "defaultTexture.jpg"}; string im1[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *jetTex = new Texture(im1, 1); Texture *jetTex = new Texture(im1, 1);
jetMat->addDiffuseMap(jetTex); jetMat->addVariable("textures[0]", jetTex);
jet->setMaterial(jetMat); jet->setMaterial(jetMat);
rootNode->attachChild(jet); rootNode->attachChild(jet);
jet->setCastShadow(true); jet->setCastShadow(true);
+6 -4
View File
@@ -5,6 +5,8 @@
#include "skeleton.h" #include "skeleton.h"
#include "animationController.h" #include "animationController.h"
#include "animationChannel.h" #include "animationChannel.h"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace std; using namespace std;
using namespace vb01; using namespace vb01;
@@ -38,12 +40,12 @@ int main(){
cam->lookAt(Vector3(0, 0, -1).norm(), Vector3(0, 1, 0).norm()); cam->lookAt(Vector3(0, 0, -1).norm(), Vector3(0, 1, 0).norm());
Model *model = new Model(MODEL_PATH + "kek.vb"); Model *model = new Model(MODEL_PATH + "kek.vb");
Material *mat = new Material(); Material *mat = new Material(PATH + "texture");
mat->setLightingEnabled(false); mat->addVariable("lightingEnabled", false);
mat->setTexturingEnabled(true); mat->addVariable("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"}; string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1); Texture *texture = new Texture(images, 1);
mat->addDiffuseMap(texture); mat->addVariable("textures[0]", texture);
model->setMaterial(mat); model->setMaterial(mat);
rootNode->attachChild(model); rootNode->attachChild(model);
+9 -2
View File
@@ -5,9 +5,16 @@ in vec3 texCoords;
layout (location = 0) out vec4 FragColor; layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor; layout (location = 1) out vec4 BrightColor;
uniform samplerCube skybox; struct Texture{
float mixRatio;
samplerCube pastTexture, nextTexture;
bool animated;
};
uniform Texture tex;
void main(){ void main(){
BrightColor = vec4(0); BrightColor = vec4(0);
FragColor = texture(skybox, texCoords); //vec4 texColor = (tex.animated ? mix(tex.pastTexture, tex.nextTexture, tex.mixRatio) : tex.nextTexture);
FragColor = texture(tex.pastTexture, texCoords);
} }
+3 -3
View File
@@ -51,12 +51,12 @@ int main(){
* Creates a material for text objects and * Creates a material for text objects and
* attaches a diffuse texture with 2 images * attaches a diffuse texture with 2 images
*/ */
Material *textMat = new Material(Material::MATERIAL_TEXT); Material *textMat = new Material(PATH + "text");
textMat->setTexturingEnabled(true); textMat->addVariable("texturingEnabled", true);
string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(frames, 2); Texture *texture = new Texture(frames, 2);
textMat->addDiffuseMap(texture); textMat->addVariable("textures[0]", texture);
for(int i = 0; i < numTexts; i++){ for(int i = 0; i < numTexts; i++){
Text *text = new Text(FONT_PATH + fonts[i], texts[i]); Text *text = new Text(FONT_PATH + fonts[i], texts[i]);
+2 -1
View File
@@ -1,12 +1,13 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include "texture.h" #include "texture.h"
#include "glad.h" #include "glad.h"
#include <glfw3.h> #include <glfw3.h>
#include <ft2build.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
+2 -1
View File
@@ -2,10 +2,11 @@
#define TEXTURE_H #define TEXTURE_H
#include <string> #include <string>
#include <ft2build.h>
#include "util.h" #include "util.h"
#include "animatable.h" #include "animatable.h"
#include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
namespace vb01{ namespace vb01{