diff --git a/BaseGLProject/Models/Mesh.cpp b/BaseGLProject/Models/Mesh.cpp index b03468b..00505f8 100644 --- a/BaseGLProject/Models/Mesh.cpp +++ b/BaseGLProject/Models/Mesh.cpp @@ -188,12 +188,15 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m) float *tangents = new float[mesh->mNumVertices * 3]; float *bittangents = new float[mesh->mNumVertices * 3]; 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; - bittangents[i * 3 + 1] = mesh->mBitangents[i].y; 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]); @@ -395,7 +398,7 @@ void Mesh::textureBinding(Shader *shd) { glActiveTexture(GL_TEXTURE0 + i); glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref); - //shd->applyTextureMaterial(parent->textures[i]); + shd->applyTextureMaterial(textures[i]); if (textures[i].isNmap) { shd->addUniform("nmap", (int)i); @@ -408,7 +411,7 @@ void Mesh::textureBinding(Shader *shd) shd->addUniform("TexCount", (int)textures.size() - nmap_counter); } -void Mesh::draw(SceneContext ctx) { +void Mesh::draw(SceneContext &ctx) { shader->enable(); 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 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); - } - if (glm::length(diffuse) == 0) { - diffuse = glm::vec3(0.9, 0.9, 0.9); - } + if (glm::length(diffuse) == 0 && textures.size() > 0 && textures[0].mat.enabled) + 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); - } - 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; shader->mat.ka = ambient; diff --git a/BaseGLProject/Models/Mesh.h b/BaseGLProject/Models/Mesh.h index b4ae36a..98bf562 100644 --- a/BaseGLProject/Models/Mesh.h +++ b/BaseGLProject/Models/Mesh.h @@ -93,7 +93,7 @@ public: glm::vec3 getPosition(); Shader *shader; - void draw(SceneContext ctx); + void draw(SceneContext &ctx); void assignTexture(Texture &texture); }; \ No newline at end of file diff --git a/BaseGLProject/Models/cube/color_map.jpg b/BaseGLProject/Models/cube/color_map.jpg new file mode 100644 index 0000000..4e0f227 Binary files /dev/null and b/BaseGLProject/Models/cube/color_map.jpg differ diff --git a/BaseGLProject/Models/cube/cube.obj b/BaseGLProject/Models/cube/cube.obj new file mode 100644 index 0000000..8554237 --- /dev/null +++ b/BaseGLProject/Models/cube/cube.obj @@ -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 diff --git a/BaseGLProject/Models/cube/normal_map.jpg b/BaseGLProject/Models/cube/normal_map.jpg new file mode 100644 index 0000000..52d88b6 Binary files /dev/null and b/BaseGLProject/Models/cube/normal_map.jpg differ diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index 5ab32ad..6b7dc39 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -66,8 +66,19 @@ void MyGlWindow::textureSetup() _scnctx.textures.emplace("MossTex", Texture("moss.png")); _scnctx.textures.emplace("EarthTex", Texture("earth.jpg")); _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["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() @@ -80,6 +91,7 @@ void MyGlWindow::shaderSetup() shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.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"]->light_type = Light::LightType::SPOT; @@ -100,7 +112,7 @@ void MyGlWindow::shaderSetup() void MyGlWindow::lightSetup() { //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))); //Party lights @@ -121,10 +133,16 @@ void MyGlWindow::setup() shaderSetup(); lightSetup(); - //meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["NmapBaseLight"])); - //meshes["Ogre"]->addTranslation(glm::vec4(0, 1, 0, 0)); - //meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]); - //meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]); + meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"])); + meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0)); + meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]); + 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"]->addRotation(glm::vec4(0, 1, 0, 180)); @@ -141,7 +159,7 @@ void MyGlWindow::setup() //meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0)); //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"]->cullMode = CullFace::BACK; diff --git a/BaseGLProject/Source.cpp b/BaseGLProject/Source.cpp index 678b0db..6668ab9 100644 --- a/BaseGLProject/Source.cpp +++ b/BaseGLProject/Source.cpp @@ -182,9 +182,12 @@ int loop(GLFWwindow *window) 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 Z", &(glWin._scnctx.lights["Spotlight1"].location.z), -20, 20); - - ImGui::End(); + ImGui::SliderFloat("Light X Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.x), 0, 1); + 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); glWin.draw(); diff --git a/BaseGLProject/Texture.cpp b/BaseGLProject/Texture.cpp index 965532d..ef3da71 100644 --- a/BaseGLProject/Texture.cpp +++ b/BaseGLProject/Texture.cpp @@ -24,6 +24,8 @@ Texture::Texture(std::string file_name) Texture::Texture(const Texture &other) { tex_ref = other.tex_ref; + mat = other.mat; + isNmap = other.isNmap; } diff --git a/BaseGLProject/imgui.ini b/BaseGLProject/imgui.ini index fcb4f74..44dd3c0 100644 --- a/BaseGLProject/imgui.ini +++ b/BaseGLProject/imgui.ini @@ -45,6 +45,6 @@ Collapsed=0 [Window][First Window] Pos=20,20 -Size=321,139 +Size=357,188 Collapsed=0 diff --git a/BaseGLProject/nmap.frag b/BaseGLProject/nmap.frag index ae6aa9d..4220628 100644 --- a/BaseGLProject/nmap.frag +++ b/BaseGLProject/nmap.frag @@ -3,7 +3,6 @@ in vec3 f_color; out vec4 FragColors; -uniform vec3 Kd; uniform vec3 Ka; uniform vec3 Ks; uniform float Shininess; @@ -33,31 +32,34 @@ void main() vec3 finalColor; vec3 diffuse_sum; vec3 specular_sum; - vec3 ambient; - - ambient = Ka * Light[0].Intensity; + vec3 ambient_sum; + + 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