normal mapping done and working

This commit is contained in:
Hurlu 2019-03-27 17:09:42 +09:00
parent 1ea93fa5e2
commit d494e9c56d
12 changed files with 121 additions and 44 deletions

View File

@ -188,12 +188,15 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
float *tangents = new float[mesh->mNumVertices * 3]; float *tangents = new float[mesh->mNumVertices * 3];
float *bittangents = new float[mesh->mNumVertices * 3]; float *bittangents = new float[mesh->mNumVertices * 3];
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) { for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
tangents[i * 3] = mesh->mTangents[i].x;
bittangents[i * 3] = mesh->mBitangents[i].x; tangents[i * 3] = mesh->mTangents[i].x;
tangents[i * 3 + 1] = mesh->mTangents[i].y; tangents[i * 3 + 1] = mesh->mTangents[i].y;
bittangents[i * 3 + 1] = mesh->mBitangents[i].y;
tangents[i * 3 + 2] = mesh->mTangents[i].z; tangents[i * 3 + 2] = mesh->mTangents[i].z;
bittangents[i * 3 + 2] = mesh->mBitangents[i].z;
bittangents[i * 3] = mesh->mBitangents[i].x;
bittangents[i * 3 + 1] = mesh->mBitangents[i].y;
bittangents[i * 3 + 2] = mesh->mBitangents[i].z;
} }
glGenBuffers(1, &vbo[TANGENTS_BUFFER]); glGenBuffers(1, &vbo[TANGENTS_BUFFER]);
@ -395,7 +398,7 @@ void Mesh::textureBinding(Shader *shd)
{ {
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref); glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref);
//shd->applyTextureMaterial(parent->textures[i]); shd->applyTextureMaterial(textures[i]);
if (textures[i].isNmap) if (textures[i].isNmap)
{ {
shd->addUniform("nmap", (int)i); shd->addUniform("nmap", (int)i);
@ -408,7 +411,7 @@ void Mesh::textureBinding(Shader *shd)
shd->addUniform("TexCount", (int)textures.size() - nmap_counter); shd->addUniform("TexCount", (int)textures.size() - nmap_counter);
} }
void Mesh::draw(SceneContext ctx) { void Mesh::draw(SceneContext &ctx) {
shader->enable(); shader->enable();
textureBinding(shader); textureBinding(shader);
@ -429,19 +432,24 @@ void Mesh::draw(SceneContext ctx) {
glm::vec3 specular = glm::vec3(meshEntries.at(i)->scolor.r, meshEntries.at(i)->scolor.g, meshEntries.at(i)->scolor.b); glm::vec3 specular = glm::vec3(meshEntries.at(i)->scolor.r, meshEntries.at(i)->scolor.g, meshEntries.at(i)->scolor.b);
glm::vec3 ambient = glm::vec3(meshEntries.at(i)->acolor.r, meshEntries.at(i)->acolor.g, meshEntries.at(i)->acolor.b); glm::vec3 ambient = glm::vec3(meshEntries.at(i)->acolor.r, meshEntries.at(i)->acolor.g, meshEntries.at(i)->acolor.b);
if (glm::length(ambient) == 0) { if (glm::length(ambient) == 0 && textures.size() > 0 && textures[0].mat.enabled)
ambient = textures[0].mat.ka;
else if (glm::length(ambient) == 0)
ambient = glm::vec3(0.1, 0.1, 0.1); ambient = glm::vec3(0.1, 0.1, 0.1);
}
if (glm::length(diffuse) == 0) { if (glm::length(diffuse) == 0 && textures.size() > 0 && textures[0].mat.enabled)
diffuse = glm::vec3(0.9, 0.9, 0.9); diffuse = textures[0].mat.ks;
} else if (glm::length(diffuse) == 0)
diffuse = glm::vec3(0.9, 0.9, 0.9);
if (glm::length(specular) == 0) { if (glm::length(specular) == 0 && textures.size() > 0 && textures[0].mat.enabled)
specular = textures[0].mat.ks;
else if (glm::length(specular) == 0)
specular = glm::vec3(0.4, 0.4, 0.4); specular = glm::vec3(0.4, 0.4, 0.4);
}
if (shininess == 0) if (shininess == 0 && textures.size() > 0 && textures[0].mat.enabled)
shininess = textures[0].mat.shininess;
else if (shininess == 0)
shininess = 150.0f; shininess = 150.0f;
shader->mat.ka = ambient; shader->mat.ka = ambient;

View File

@ -93,7 +93,7 @@ public:
glm::vec3 getPosition(); glm::vec3 getPosition();
Shader *shader; Shader *shader;
void draw(SceneContext ctx); void draw(SceneContext &ctx);
void assignTexture(Texture &texture); void assignTexture(Texture &texture);
}; };

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

@ -0,0 +1,45 @@
# Unit-volume cube with the same texture coordinates on each face.
#
# Created by Morgan McGuire and released into the Public Domain on
# July 16, 2011.
#
# http://graphics.cs.williams.edu/data
mtllib default.mtl
v -0.5 0.5 -0.5
v -0.5 0.5 0.5
v 0.5 0.5 0.5
v 0.5 0.5 -0.5
v -0.5 -0.5 -0.5
v -0.5 -0.5 0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
vt 0 1
vt 0 0
vt 1 0
vt 1 1
vn 0 1 0
vn -1 0 0
vn 1 0 0
vn 0 0 -1
vn 0 0 1
vn 0 -1 0
g cube
usemtl default
f -8/-4/-6 -7/-3/-6 -6/-2/-6
f -8/-4/-6 -6/-2/-6 -5/-1/-6
f -8/-4/-5 -4/-3/-5 -3/-2/-5
f -8/-4/-5 -3/-2/-5 -7/-1/-5
f -6/-4/-4 -2/-3/-4 -1/-2/-4
f -6/-4/-4 -1/-2/-4 -5/-1/-4
f -5/-4/-3 -1/-3/-3 -4/-2/-3
f -5/-4/-3 -4/-2/-3 -8/-1/-3
f -7/-4/-2 -3/-3/-2 -2/-2/-2
f -7/-4/-2 -2/-2/-2 -6/-1/-2
f -3/-4/-1 -4/-3/-1 -1/-2/-1
f -3/-4/-1 -1/-2/-1 -2/-1/-1

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 KiB

View File

@ -66,8 +66,19 @@ void MyGlWindow::textureSetup()
_scnctx.textures.emplace("MossTex", Texture("moss.png")); _scnctx.textures.emplace("MossTex", Texture("moss.png"));
_scnctx.textures.emplace("EarthTex", Texture("earth.jpg")); _scnctx.textures.emplace("EarthTex", Texture("earth.jpg"));
_scnctx.textures.emplace("OgreTex", Texture("Models/ogre/ogre_diffuse.png")); _scnctx.textures.emplace("OgreTex", Texture("Models/ogre/ogre_diffuse.png"));
_scnctx.textures["OgreTex"].mat.shininess = 3.0f;
_scnctx.textures["OgreTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
_scnctx.textures["OgreTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
_scnctx.textures["OgreTex"].mat.enabled = true;
_scnctx.textures.emplace("OgreNmap", Texture("Models/ogre/ogre_normalmap.png")); _scnctx.textures.emplace("OgreNmap", Texture("Models/ogre/ogre_normalmap.png"));
_scnctx.textures["OgreNmap"].isNmap = true; _scnctx.textures["OgreNmap"].isNmap = true;
_scnctx.textures.emplace("CubeTex", Texture("Models/cube/color_map.jpg"));
_scnctx.textures["CubeTex"].mat.shininess = 3.0f;
_scnctx.textures["CubeTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
_scnctx.textures["CubeTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
_scnctx.textures["CubeTex"].mat.enabled = true;
_scnctx.textures.emplace("CubeNmap", Texture("Models/cube/normal_map.jpg"));
_scnctx.textures["CubeNmap"].isNmap = true;
} }
void MyGlWindow::shaderSetup() void MyGlWindow::shaderSetup()
@ -80,6 +91,7 @@ void MyGlWindow::shaderSetup()
shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag"); shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["TexNmapLight"] = new Shader("nmap.vert", "nmap.frag"); shaders["TexNmapLight"] = new Shader("nmap.vert", "nmap.frag");
shaders["TexNmapLight"]->uniformFlags &= ~ShaderFlags::KD_FLAG;
shaders["SpotLight"] = new Shader("spotlight.vert", "spotlight.frag"); shaders["SpotLight"] = new Shader("spotlight.vert", "spotlight.frag");
shaders["SpotLight"]->light_type = Light::LightType::SPOT; shaders["SpotLight"]->light_type = Light::LightType::SPOT;
@ -100,7 +112,7 @@ void MyGlWindow::shaderSetup()
void MyGlWindow::lightSetup() void MyGlWindow::lightSetup()
{ {
//Showcase lights //Showcase lights
_scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(0, 11, 4.3, 1))); _scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(10, 10, 10, 1)));
// 24, 12, 2, glm::vec4(0, 1, 0, 1))); // 24, 12, 2, glm::vec4(0, 1, 0, 1)));
//Party lights //Party lights
@ -121,10 +133,16 @@ void MyGlWindow::setup()
shaderSetup(); shaderSetup();
lightSetup(); lightSetup();
//meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["NmapBaseLight"])); meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"]));
//meshes["Ogre"]->addTranslation(glm::vec4(0, 1, 0, 0)); meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0));
//meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]); meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]);
//meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]); meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]);
meshes.emplace("Cube", new Mesh("cube/cube.obj", shaders["TexNmapLight"]));
meshes["Cube"]->addTranslation(glm::vec4(0.5, 1, 0, 0));
meshes["Cube"]->assignTexture(_scnctx.textures["CubeTex"]);
meshes["Cube"]->assignTexture(_scnctx.textures["CubeNmap"]);
//meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0)); //meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0));
//meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180)); //meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180));
@ -141,7 +159,7 @@ void MyGlWindow::setup()
//meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0)); //meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0));
//meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"])); //meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); //meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
//meshes["Sponza"]->addScaling(glm::vec4(0.2, 0.2, 0.2, 1)); //meshes["Sponza"]->addScaling(glm::vec4(0.2, 0.2, 0.2, 1));
//meshes["Sponza"]->cullMode = CullFace::BACK; //meshes["Sponza"]->cullMode = CullFace::BACK;

View File

@ -182,9 +182,12 @@ int loop(GLFWwindow *window)
ImGui::SliderFloat("Light X", &(glWin._scnctx.lights["Spotlight1"].location.x), -20, 20); ImGui::SliderFloat("Light X", &(glWin._scnctx.lights["Spotlight1"].location.x), -20, 20);
ImGui::SliderFloat("Light Y", &(glWin._scnctx.lights["Spotlight1"].location.y), -20, 20); ImGui::SliderFloat("Light Y", &(glWin._scnctx.lights["Spotlight1"].location.y), -20, 20);
ImGui::SliderFloat("Light Z", &(glWin._scnctx.lights["Spotlight1"].location.z), -20, 20); ImGui::SliderFloat("Light Z", &(glWin._scnctx.lights["Spotlight1"].location.z), -20, 20);
ImGui::SliderFloat("Light X Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.x), 0, 1);
ImGui::End(); ImGui::SliderFloat("Light Y Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.y), 0, 1);
ImGui::SliderFloat("Light Z Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.z), 0, 1);
} }
ImGui::End();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glWin.draw(); glWin.draw();

View File

@ -24,6 +24,8 @@ Texture::Texture(std::string file_name)
Texture::Texture(const Texture &other) Texture::Texture(const Texture &other)
{ {
tex_ref = other.tex_ref; tex_ref = other.tex_ref;
mat = other.mat;
isNmap = other.isNmap;
} }

View File

@ -45,6 +45,6 @@ Collapsed=0
[Window][First Window] [Window][First Window]
Pos=20,20 Pos=20,20
Size=321,139 Size=357,188
Collapsed=0 Collapsed=0

View File

@ -3,7 +3,6 @@
in vec3 f_color; in vec3 f_color;
out vec4 FragColors; out vec4 FragColors;
uniform vec3 Kd;
uniform vec3 Ka; uniform vec3 Ka;
uniform vec3 Ks; uniform vec3 Ks;
uniform float Shininess; uniform float Shininess;
@ -33,31 +32,34 @@ void main()
vec3 finalColor; vec3 finalColor;
vec3 diffuse_sum; vec3 diffuse_sum;
vec3 specular_sum; vec3 specular_sum;
vec3 ambient; vec3 ambient_sum;
ambient = Ka * Light[0].Intensity; vec3 N = texture(nmap, texCoord).xyz;
vec4 Kd = texture(tex[0], texCoord);
for (int i=1; i < TexCount; i++)
{
vec4 new_tex = texture(tex[i], texCoord);
Kd = mix(new_tex, Kd, new_tex.a);
}
for (int i=0; i<LightCount; i++) for (int i=0; i<LightCount; i++)
{ {
vec3 L = normalize(tangentMatrix * (Light[i].Position.xyz - pos)); vec3 L = normalize(tangentMatrix * (Light[i].Position.xyz - pos));
vec3 N = texture(nmap, texCoord).xyz;
vec3 V = normalize(tangentMatrix * (-pos)); vec3 V = normalize(tangentMatrix * (-pos));
//vec3 R = normalize(reflect(-L, N)); //vec3 R = normalize(reflect(-L, N));
vec3 H = normalize(V + L); vec3 H = normalize(V + L);
vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0); vec3 ambient = Ka * Light[i].Intensity;
vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess); vec3 diffuse = Kd.xyz * Light[i].Intensity * max(dot(L, N), 0.0);
vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess);
ambient_sum += ambient;
diffuse_sum += diffuse; diffuse_sum += diffuse;
specular_sum += specular; specular_sum += specular;
} }
ambient_sum /= LightCount;
vec4 texColor = texture(tex[0], texCoord);
for (int i=1; i < TexCount; i++)
{
vec4 new_tex = texture(tex[i], texCoord);
texColor = mix(new_tex, texColor, new_tex.a);
}
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0)); FragColors = (vec4(diffuse_sum + ambient_sum + specular_sum, 1));
} }

View File

@ -4,8 +4,8 @@ layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal; layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color; layout(location=2) in vec3 v_color;
layout(location=3) in vec2 v_texmap; layout(location=3) in vec2 v_texmap;
layout(location=4) in vec3 v_tangent; layout(location=5) in vec3 v_tangent;
layout(location=5) in vec3 v_bittangent; layout(location=6) in vec3 v_bittangent;
uniform mat4 mvp; uniform mat4 mvp;
uniform mat3 NormalMatrix; uniform mat3 NormalMatrix;

View File

@ -37,8 +37,7 @@ void main()
{ {
vec3 L = normalize(Light[i].Position.xyz - pos); vec3 L = normalize(Light[i].Position.xyz - pos);
vec3 N = fNormal; vec3 N = fNormal;
vec3 V = normalize(-pos); vec3 V = normalize(-pos);
//vec3 R = normalize(reflect(-L, N));
vec3 H = normalize(V + L); vec3 H = normalize(V + L);
vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0); vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0);