normal maps

This commit is contained in:
devZoGok
2021-10-19 19:06:40 +03:00
parent d52be66e3d
commit e95e40177a
9 changed files with 53 additions and 16 deletions
+6 -3
View File
@@ -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");
+2 -1
View File
@@ -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<Texture*> diffuseMapTextures,normalMapTextures,specularMapTextures;
Vector4 diffuseColor=Vector4::VEC_IJKL,specularColor=Vector4::VEC_IJKL;
Shader *shader=nullptr;
+4
View File
@@ -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(){
+1 -1
View File
@@ -14,7 +14,7 @@ namespace vb01{
class Mesh{
public:
struct Vertex{
Vector3 pos,norm;
Vector3 pos,tan,biTan,norm;
Vector2 texCoords;
};
struct VertexGroup{
+9 -1
View File
@@ -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:"<<importer.GetErrorString()<<endl;
exit(-1);
+2 -2
View File
@@ -153,7 +153,7 @@ namespace vb01{
glBindTexture(type==TEXTURE_2D?GL_TEXTURE_2D:GL_TEXTURE_CUBE_MAP,texture[numFrames>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);
}
}
+18 -7
View File
@@ -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<numLights;i++){
@@ -92,17 +101,19 @@ void main(){
}
}
float specularStrength=.5;
reflectVec=reflect(lightDir,norm);
reflectVec=reflect(lightDir,normal);
float spec=pow(max(dot(viewDir,reflectVec),0),32);
specularColor+=vec3(1)*spec*specularStrength;
factor=max(dot(lightDir,norm),0.);
factor=max(dot(lightDir,normal),0.);
diffuseColor+=light[i].color*(factor*attenuation*coef);
}
for(int i=0;i<numLights;i++){
float shadow=getShadow(i);
diffuseColor*=(1-shadow);
/*
*/
}
finalColor*=vec4(diffuseColor+specularColor,1);
+1 -1
View File
@@ -18,7 +18,7 @@ namespace vb01{
Texture(int);
Texture(FT_Face&,char);
void select(int=0);
void update();
void update(int=0);
inline unsigned int* getTexture(int i=0){return &(texture[i]);}
inline std::string getPath(){return path;}
private:
+10
View File
@@ -3,18 +3,28 @@
layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aNorm;
layout (location=2) in vec2 aTexCoords;
layout (location=3) in vec3 aTan;
layout (location=4) in vec3 aBiTan;
out vec3 fragPos;
out vec3 norm;
out vec3 tan;
out vec3 biTan;
out vec2 texCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
/*
uniform vec3 aTan;
uniform vec3 aBiTan;
*/
void main(){
gl_Position=proj*view*model*vec4(aPos,1);
fragPos=vec3(model*vec4(aPos,1));
norm=mat3(transpose(inverse(model)))*aNorm;
tan=mat3(transpose(inverse(model)))*aTan;
biTan=mat3(transpose(inverse(model)))*aBiTan;
texCoords=aTexCoords;
}