From 69e858e2b9109abff8dd31f2e2d44efa07b91a2e Mon Sep 17 00:00:00 2001 From: xkamikoo <58092037+xkamikoo@users.noreply.github.com> Date: Sun, 24 Jan 2021 23:18:02 +0100 Subject: [PATCH] fixed lighting --- grk-project.vcxproj | 4 ++-- shaders/shader_4_sun.frag | 2 +- shaders/shader_4_tex.frag | 13 ++++++++++--- src/main.cpp | 16 ++++++++-------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/grk-project.vcxproj b/grk-project.vcxproj index 672ac76..4fdbc9a 100644 --- a/grk-project.vcxproj +++ b/grk-project.vcxproj @@ -49,7 +49,7 @@ {1B448102-E76C-4347-BDC7-40D02A567DB6} Win32Proj grk-cw9 - 10.0 + 10.0.17134.0 grk-project @@ -64,7 +64,7 @@ false true Unicode - v142 + v141 diff --git a/shaders/shader_4_sun.frag b/shaders/shader_4_sun.frag index 0a53ef0..af2cf33 100644 --- a/shaders/shader_4_sun.frag +++ b/shaders/shader_4_sun.frag @@ -14,7 +14,7 @@ void main() { vec3 normal = normalize(interpNormal); vec3 V = normalize(cameraPos-fragPos); - float coef = pow(max(0,dot(normal,V)),3); + float coef = pow(max(0,dot(normal,V)),2); vec4 textureColor = texture2D(colorTexture, -vTexCoord); vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z) * colorTex; gl_FragColor = vec4(texture + texture * coef, 1.0); diff --git a/shaders/shader_4_tex.frag b/shaders/shader_4_tex.frag index 9d30908..62ea4c9 100644 --- a/shaders/shader_4_tex.frag +++ b/shaders/shader_4_tex.frag @@ -30,10 +30,17 @@ void main() vec3 lightDir = normalize(pointLights[i].position - fragPos); vec3 V = normalize(cameraPos-fragPos); - vec3 R = reflect(-normalize(lightDir),normal); + vec3 R = reflect(-lightDir,normal); + + float dist = 1 + distance(fragPos, pointLights[i].position) / 50; + float distance = (1/dist) * (1/dist); - float specular = pow(max(0,dot(R,V)),10); - float diffuse = max(0,dot(normal,normalize(lightDir))); + float spec = pow(max(0,dot(R,V)),2); + float diff = max(0,dot(normal,normalize(lightDir))); + + vec3 diffuse = pointLights[i].color * diff * distance; + vec3 specular = spec * pointLights[i].color * (1/dist); + vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z) * pointLights[i].color; fragColor += mix(texture,texture*diffuse+vec3(1)*specular,0.9); } diff --git a/src/main.cpp b/src/main.cpp index 0a2b184..652f4ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -201,10 +201,10 @@ void drawObjectTexture(GLuint program, Core::RenderContext context, glm::mat4 mo } //funkcja rysujaca planety (bez obracania wokol wlasnej osi bo ksiezyce sie psuja) -glm::mat4 drawPlanet(float time, glm::vec3 orbit, glm::vec3 translation, glm::vec3 scale) +glm::mat4 drawPlanet(float time, glm::vec3 sunPos, glm::vec3 orbit, glm::vec3 translation, glm::vec3 scale) { glm::mat4 planetModelMatrix = glm::mat4(1.0f); - planetModelMatrix = glm::translate(planetModelMatrix, glm::vec3(2, 0, 2)); //pozycja słońca + planetModelMatrix = glm::translate(planetModelMatrix, sunPos); planetModelMatrix = glm::rotate(planetModelMatrix, time, orbit); planetModelMatrix = glm::translate(planetModelMatrix, translation); planetModelMatrix = glm::scale(planetModelMatrix, scale); @@ -237,17 +237,17 @@ void renderScene() glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); //ustalanie pozycji słońc (lightPos) - glm::vec3 sunPos = glm::vec3(0.0f, 0.0f, 0.0f); - glm::vec3 sunPos2 = glm::vec3(5.0f, -1.0f, 10.0f); + glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f); + glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f); //rysowanie słońc glm::mat4 sunModelMatrix = glm::mat4(1.0f); sunModelMatrix = glm::translate(sunModelMatrix, sunPos); - drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(0.5f, 0.8f, 0.8f), sunTexture); + sunModelMatrix = glm::scale(sunModelMatrix, glm::vec3(3.0f, 3.0f, 3.0f)); + drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(1.5f, 1.8f, 1.8f), sunTexture); glm::mat4 sunModelMatrix2 = glm::mat4(1.0f); sunModelMatrix2 = glm::translate(sunModelMatrix2, sunPos2); - sunModelMatrix2 = glm::rotate(sunModelMatrix2, time / 100.0f, glm::vec3(0.0f, 1.0f, 1.0f)); drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture); @@ -272,8 +272,8 @@ void renderScene() drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1)); //rysowanie Ziemi z księżycem - glm::mat4 earth = drawPlanet(time / 5.0f, glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-3.5f, 0.0f, -3.5f), glm::vec3(0.5f, 0.5f, 0.5f)); - glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.2f, 0.2f, 0.2f)); + glm::mat4 earth = drawPlanet(time / 5.0f, sunPos*glm::vec3(1.5f,1,1), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-10.5f, 0.0f, -10.5f), glm::vec3(0.5f, 0.5f, 0.5f)); + glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.5f, 1.0f, 1.0f), glm::vec3(0.3f, 0.3f, 0.3f)); earth = glm::rotate(earth, time/3.0f, glm::vec3(0.0f, 0.0f, 1.0f)); drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture); drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);