diff --git a/material.cpp b/material.cpp index a610ad0..7b2b4dc 100755 --- a/material.cpp +++ b/material.cpp @@ -40,8 +40,11 @@ namespace vb01{ void Material::update(){ shader->use(); - if(type==MATERIAL_2D) + if(type==MATERIAL_2D){ shader->setBool(lightingEnabled,"lightingEnabled"); + if(normalMapEnabled) + shader->setBool(normalMapEnabled,"normalMapEnabled"); + } shader->setBool(texturingEnabled,"texturingEnabled"); if(type==MATERIAL_GUI){ shader->setBool(diffuseColorEnabled,"diffuseColorEnabled"); @@ -52,9 +55,9 @@ namespace vb01{ for(Texture *t : diffuseMapTextures) t->update(); for(Texture *t : normalMapTextures) - t->update(); + t->update(2); for(Texture *t : specularMapTextures) - t->update(); + t->update(3); } else { shader->setVec4(diffuseColor,"diffuseColor"); diff --git a/material.h b/material.h index 0e5967a..8631e79 100755 --- a/material.h +++ b/material.h @@ -39,6 +39,7 @@ namespace vb01{ 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 bool isLightingEnabled(){return lightingEnabled;} inline bool isTexturingEnabled(){return texturingEnabled;} inline bool isDiffuseColorEnabled(){return diffuseColorEnabled;} @@ -49,7 +50,7 @@ namespace vb01{ inline Type getType(){return type;} private: Type type; - bool lightingEnabled=false,texturingEnabled=true,diffuseColorEnabled=false; + bool lightingEnabled=false,texturingEnabled=true,diffuseColorEnabled=false,normalMapEnabled=false; std::vector diffuseMapTextures,normalMapTextures,specularMapTextures; Vector4 diffuseColor=Vector4::VEC_IJKL,specularColor=Vector4::VEC_IJKL; Shader *shader=nullptr; diff --git a/mesh.cpp b/mesh.cpp index 747ce26..d5e3601 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -55,6 +55,10 @@ namespace vb01{ glEnableVertexAttribArray(1); glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)(offsetof(Vertex,texCoords))); glEnableVertexAttribArray(2); + glVertexAttribPointer(3,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)(offsetof(Vertex,tan))); + glEnableVertexAttribArray(3); + glVertexAttribPointer(4,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)(offsetof(Vertex,biTan))); + glEnableVertexAttribArray(4); } void Mesh::update(){ diff --git a/mesh.h b/mesh.h index 5995954..80a52a9 100755 --- a/mesh.h +++ b/mesh.h @@ -14,7 +14,7 @@ namespace vb01{ class Mesh{ public: struct Vertex{ - Vector3 pos,norm; + Vector3 pos,tan,biTan,norm; Vector2 texCoords; }; struct VertexGroup{ diff --git a/model.cpp b/model.cpp index 6c2a7d8..6583dde 100755 --- a/model.cpp +++ b/model.cpp @@ -40,6 +40,14 @@ namespace vb01{ v.norm.y=mesh->mNormals[i].y; v.norm.z=mesh->mNormals[i].z; + v.tan.x=mesh->mTangents[i].x; + v.tan.y=mesh->mTangents[i].y; + v.tan.z=mesh->mTangents[i].z; + + v.biTan.x=mesh->mBitangents[i].x; + v.biTan.y=mesh->mBitangents[i].y; + v.biTan.z=mesh->mBitangents[i].z; + if(mesh->mTextureCoords[0]){ v.texCoords.x=mesh->mTextureCoords[0][i].x; v.texCoords.y=mesh->mTextureCoords[0][i].y; @@ -63,7 +71,7 @@ namespace vb01{ Model::Model(string path,bool b) : Node(){ if(b){ Importer importer; - const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals); + const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals | aiProcess_CalcTangentSpace); if(!scene||scene->mFlags&AI_SCENE_FLAGS_INCOMPLETE||!scene->mRootNode){ cout<<"Failed to load model:"<0?frame:0]); } - void Texture::update(){ + void Texture::update(int id){ if(numFrames>0){ if(getTime()-lastUpdateTime>updateRate){ frame++; @@ -162,6 +162,6 @@ namespace vb01{ lastUpdateTime=getTime(); } } - select(); + select(id); } } diff --git a/texture.frag b/texture.frag index 7bfea3f..cd0e95c 100755 --- a/texture.frag +++ b/texture.frag @@ -2,6 +2,8 @@ const int numLights=1; in vec3 fragPos; +in vec3 tan; +in vec3 biTan; in vec3 norm; in vec2 texCoords; in vec4 lightSpaceFragPos; @@ -20,10 +22,12 @@ struct Light{ mat4 lightMat; }; -uniform sampler2D tex; +uniform sampler2D diffuseMap; +uniform sampler2D normalMap; uniform Light light[numLights]; uniform bool lightingEnabled; uniform bool texturingEnabled; +uniform bool normalMapEnabled; uniform bool castShadow; uniform vec4 diffuseColor; uniform vec3 camPos; @@ -37,13 +41,15 @@ float getShadow(int id){ int type=light[id].type; float shadow=0,closestDepth=0,currentDepth=0,bias=.005,val=.7; +/* if(type==0){ vec3 projDir=fragPos-light[id].pos; closestDepth=texture(light[id].depthMapCube,projDir).r*light[id].far; currentDepth=length(projDir); shadow=(currentDepth-bias>closestDepth)?val:0; } - else{ +*/ + if(type!=0){ vec3 projCoords=(light[id].lightMat*vec4(fragPos,1)).xyz/(light[id].lightMat*vec4(fragPos,1)).w; projCoords=projCoords*.5+.5; closestDepth=texture(light[id].depthMap,projCoords.xy).r; @@ -57,13 +63,16 @@ float getShadow(int id){ shadow=0; } return shadow; -/* -*/ } void main(){ - vec4 finalColor=texturingEnabled?texture(tex,texCoords):diffuseColor; + vec4 finalColor=texturingEnabled?texture(diffuseMap,texCoords):diffuseColor; + vec3 normal=norm; if(lightingEnabled){ + if(normalMapEnabled){ + vec3 n=vec3(texture(normalMap,texCoords)); + normal=mat3(tan,biTan,norm)*n; + } vec3 diffuseColor=vec3(0),specularColor=vec3(0); float coef=1; for(int i=0;i