diff --git a/grk/cw 6/shaders/shader_pbr.frag b/grk/cw 6/shaders/shader_pbr.frag index e494401..5f58487 100644 --- a/grk/cw 6/shaders/shader_pbr.frag +++ b/grk/cw 6/shaders/shader_pbr.frag @@ -7,6 +7,7 @@ float AMBIENT = 0.05; float PI = 3.14159; uniform sampler2D colorTexture; +uniform sampler2D textureNormal; uniform float exposition; uniform float metallic; @@ -183,6 +184,11 @@ void main() vec3 lightDir = normalize(lightPos - worldPos); vec3 textureColor = texture2D(colorTexture, vtc).rgb; + vec3 N = texture2D(textureNormal, vtc).rgb; + + vec3 normalTexture = normalize((N * 2.0 - 1.0)); + + float diffuseNormal = max(0, dot(normalTexture, lightDir)); if (atmosphereCheck) { @@ -191,8 +197,10 @@ void main() textureColor = mix(textureColor, atmosphereColor, pow(1 - atmosphereDot, 3)); } + vec3 diffuseColor = textureColor * min(1, AMBIENT + diffuseNormal); + vec3 distance = lightColor / pow(length(lightPos - worldPos), 2.0) * 10; - vec3 toneMappedColor = toneMapping(textureColor * distance); + vec3 toneMappedColor = toneMapping(diffuseColor * distance); //gamma correction toneMappedColor = pow(toneMappedColor, vec3(1.0/2.2)); diff --git a/grk/cw 6/shaders/shader_tex.frag b/grk/cw 6/shaders/shader_tex.frag index 9697fe5..13c1dc1 100644 --- a/grk/cw 6/shaders/shader_tex.frag +++ b/grk/cw 6/shaders/shader_tex.frag @@ -7,6 +7,7 @@ float AMBIENT = 0.05; float PI = 3.14159; uniform sampler2D colorTexture; +uniform sampler2D textureNormal; uniform vec3 color; uniform vec3 lightPos; @@ -26,6 +27,10 @@ in vec3 vecNormal; in vec3 worldPos; in vec2 vtc; +in vec3 viewDirTS; +in vec3 lightDirTS; +in vec3 sunDirTS; + //out vec4 outColor; vec3 toneMapping(vec3 color) @@ -104,6 +109,11 @@ void main() float diffuse = max(0, dot(normal, lightDir)); vec3 textureColor = texture2D(colorTexture, vtc).rgb; + vec3 N = texture2D(textureNormal, vtc).rgb; + + vec3 normalTexture = normalize((N * 2.0 - 1.0)); + + float diffuseNormal = max(0, dot(normalTexture, lightDir)); if (atmosphereCheck) { @@ -112,8 +122,8 @@ void main() textureColor = mix(textureColor, atmosphereColor, pow(1 - atmosphereDot, 3)); } - vec3 diffuseColor = textureColor * min(1, AMBIENT + diffuse); - + vec3 diffuseColor = textureColor * min(1, AMBIENT + diffuse) * min(1, AMBIENT + diffuseNormal); + vec3 distance = lightColor / pow(length(lightPos - worldPos), 2.0) * 1000; vec3 toneMappedColor = toneMapping(diffuseColor * distance); //gamma correction @@ -136,4 +146,5 @@ void main() outColor = vec4(finalColor, 1.0); + } \ No newline at end of file diff --git a/grk/cw 6/shaders/shader_tex.vert b/grk/cw 6/shaders/shader_tex.vert index da2afbf..60a59a5 100644 --- a/grk/cw 6/shaders/shader_tex.vert +++ b/grk/cw 6/shaders/shader_tex.vert @@ -3,6 +3,8 @@ layout(location = 0) in vec3 vertexPosition; layout(location = 1) in vec3 vertexNormal; layout(location = 2) in vec2 vertexTexCoord; +layout(location = 3) in vec3 vertexTangent; +layout(location = 4) in vec3 vertexBitangent; uniform mat4 transformation; uniform mat4 modelMatrix; @@ -11,11 +13,30 @@ out vec3 vecNormal; out vec3 worldPos; out vec2 vtc; +uniform vec3 lightPos; +uniform vec3 cameraPos; +uniform vec3 sunDir; + +out vec3 viewDirTS; +out vec3 lightDirTS; +out vec3 sunDirTS; + void main() { worldPos = (modelMatrix * vec4(vertexPosition, 1)).xyz; vecNormal = (modelMatrix * vec4(vertexNormal, 0)).xyz; + gl_Position = transformation * vec4(vertexPosition, 1.0); vtc = vec2(vertexTexCoord.x, 1.0 - vertexTexCoord.y); + + vec3 w_tangent = normalize(mat3(modelMatrix) * vertexTangent); + vec3 w_bitangent = normalize(mat3(modelMatrix) * vertexBitangent); + mat3 TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal)); + + vec3 V = normalize(cameraPos - worldPos); + viewDirTS = TBN * V; + vec3 L = normalize(lightPos - worldPos); + lightDirTS = TBN * L; + sunDirTS = TBN * sunDir; } diff --git a/grk/cw 6/src/Planet.hpp b/grk/cw 6/src/Planet.hpp index 6a38c4e..b6d2966 100644 --- a/grk/cw 6/src/Planet.hpp +++ b/grk/cw 6/src/Planet.hpp @@ -35,7 +35,13 @@ const char* const planetTexPaths[] = { "./textures/planets/rocky.jpg", "./textur "./textures/planets/makemake.jpg" }; int planetTexIndex = 80; GLuint planetTex; -GLuint planetRock; + +const char* const normalTexPaths[] = { "./textures/planets/normalne/rocky.png", "./textures/planets/normalne/beach.png", "./textures/planets/normalne/circles.png", "./textures/planets/normalne/denim.png", + "./textures/planets/normalne/dirty.png", "./textures/planets/normalne/kora.png", "./textures/planets/normalne/leather.png", "./textures/planets/normalne/mchowa.png", "./textures/planets/normalne/red_stone.png", + "./textures/planets/normalne/rocks.png", "./textures/planets/normalne/rust.png", "./textures/planets/normalne/snow-foot.png", "./textures/planets/normalne/swamp.png", "./textures/planets/normalne/toxic.png", + "./textures/planets/normalne/tropical.png", "./textures/planets/normalne/volcanic.png", "./textures/planets/normalne/watermelon.png", "./textures/planets/normalne/wood.png" }; +int normalTexIndex = 80; +GLuint normalTex; glm::vec3 planetPos = glm::vec3(0.f, 0.f, 0.f); float planetSize = 0.005f; @@ -222,9 +228,11 @@ void shaderBloomConfig() { glUniform1i(glGetUniformLocation(programBloomFinal, "bloomBlur"), 1); } -void drawPlanetTex(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) { +void drawPlanetTex(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, GLuint textureNormal) { glUseProgram(programTex); Core::SetActiveTexture(texture, "colorTexture", programTex, 0); + Core::SetActiveTexture(textureNormal, "textureNormal", programTex, 1); + glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(programTex, "transformation"), 1, GL_FALSE, (float*)&transformation); @@ -247,9 +255,10 @@ void drawPlanetTex(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t glUseProgram(0); } -void drawPlanetPbr(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) { +void drawPlanetPbr(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, GLuint textureNormal) { glUseProgram(programPbr); Core::SetActiveTexture(texture, "colorTexture", programPbr, 0); + Core::SetActiveTexture(textureNormal, "textureNormal", programPbr, 1); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(programPbr, "transformation"), 1, GL_FALSE, (float*)&transformation); @@ -354,12 +363,12 @@ void renderScene(GLFWwindow* window) { glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0)); glm::mat4 planetTranslate = glm::translate(planetPos); - /*if (lightingCheck) - drawPlanetPbr(sphereContext, planetTranslate * planetRotate * planetScale, planetTex); + if (lightingCheck) + drawPlanetPbr(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, normalTex); else - drawPlanetTex(sphereContext, planetTranslate * planetRotate * planetScale, planetTex);*/ + drawPlanetTex(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, normalTex); - drawPlanetTex(sphereContext, planetTranslate * planetRotate * planetScale, planetRock); + //drawPlanetTex(sphereContext, planetTranslate * planetRotate * planetScale, planetRock, rockNormal); //rysowanie słońca glm::mat4 sunScale = glm::scale(glm::vec3(sunSize)); @@ -544,8 +553,7 @@ void init(GLFWwindow* window) { planetTex = Core::LoadTexture(planetTexPaths[std::abs(planetTexIndex % 21)]); sunTex = Core::LoadTexture(sunTexPaths[std::abs(sunTexIndex % 5)]); - - planetRock = Core::LoadTexture("textures/planets/rocky.jpg"); + normalTex = Core::LoadTexture(normalTexPaths[std::abs(normalTexIndex % 23)]); skyBoxTex = Core::LoadSkyBox(skyBoxPaths); diff --git a/grk/cw 6/textures/planets/denim.png b/grk/cw 6/textures/planets/denim.png new file mode 100644 index 0000000..bd01315 Binary files /dev/null and b/grk/cw 6/textures/planets/denim.png differ diff --git a/grk/cw 6/textures/planets/dirty.png b/grk/cw 6/textures/planets/dirty.png new file mode 100644 index 0000000..2052821 Binary files /dev/null and b/grk/cw 6/textures/planets/dirty.png differ diff --git a/grk/cw 6/textures/planets/kora.png b/grk/cw 6/textures/planets/kora.png new file mode 100644 index 0000000..e33fd0f Binary files /dev/null and b/grk/cw 6/textures/planets/kora.png differ diff --git a/grk/cw 6/textures/planets/leather.png b/grk/cw 6/textures/planets/leather.png new file mode 100644 index 0000000..877b7cc Binary files /dev/null and b/grk/cw 6/textures/planets/leather.png differ diff --git a/grk/cw 6/textures/planets/mchowa.png b/grk/cw 6/textures/planets/mchowa.png new file mode 100644 index 0000000..0de20d2 Binary files /dev/null and b/grk/cw 6/textures/planets/mchowa.png differ diff --git a/grk/cw 6/textures/planets/normalne/beach.png b/grk/cw 6/textures/planets/normalne/beach.png new file mode 100644 index 0000000..25c0009 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/beach.png differ diff --git a/grk/cw 6/textures/planets/normalne/circles.png b/grk/cw 6/textures/planets/normalne/circles.png new file mode 100644 index 0000000..b7228a3 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/circles.png differ diff --git a/grk/cw 6/textures/planets/normalne/denim.png b/grk/cw 6/textures/planets/normalne/denim.png new file mode 100644 index 0000000..3320816 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/denim.png differ diff --git a/grk/cw 6/textures/planets/normalne/dirty.png b/grk/cw 6/textures/planets/normalne/dirty.png new file mode 100644 index 0000000..70fa131 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/dirty.png differ diff --git a/grk/cw 6/textures/planets/normalne/kora.png b/grk/cw 6/textures/planets/normalne/kora.png new file mode 100644 index 0000000..6d0acaa Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/kora.png differ diff --git a/grk/cw 6/textures/planets/normalne/leather.png b/grk/cw 6/textures/planets/normalne/leather.png new file mode 100644 index 0000000..36c1be9 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/leather.png differ diff --git a/grk/cw 6/textures/planets/normalne/mchowa.png b/grk/cw 6/textures/planets/normalne/mchowa.png new file mode 100644 index 0000000..c4bc6ad Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/mchowa.png differ diff --git a/grk/cw 6/textures/planets/normalne/normalna-proba.jpg b/grk/cw 6/textures/planets/normalne/normalna-proba.jpg new file mode 100644 index 0000000..6c47617 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/normalna-proba.jpg differ diff --git a/grk/cw 6/textures/planets/normalne/red_stone.png b/grk/cw 6/textures/planets/normalne/red_stone.png new file mode 100644 index 0000000..61cb1b1 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/red_stone.png differ diff --git a/grk/cw 6/textures/planets/normalne/rocks.png b/grk/cw 6/textures/planets/normalne/rocks.png new file mode 100644 index 0000000..dbf0092 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/rocks.png differ diff --git a/grk/cw 6/textures/planets/normalne/rocky.png b/grk/cw 6/textures/planets/normalne/rocky.png new file mode 100644 index 0000000..cde640b Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/rocky.png differ diff --git a/grk/cw 6/textures/planets/normalne/rust.png b/grk/cw 6/textures/planets/normalne/rust.png new file mode 100644 index 0000000..7373865 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/rust.png differ diff --git a/grk/cw 6/textures/planets/normalne/snow-foot.png b/grk/cw 6/textures/planets/normalne/snow-foot.png new file mode 100644 index 0000000..ec4b42d Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/snow-foot.png differ diff --git a/grk/cw 6/textures/planets/normalne/swamp.png b/grk/cw 6/textures/planets/normalne/swamp.png new file mode 100644 index 0000000..938df53 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/swamp.png differ diff --git a/grk/cw 6/textures/planets/normalne/toxic.png b/grk/cw 6/textures/planets/normalne/toxic.png new file mode 100644 index 0000000..f45f75c Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/toxic.png differ diff --git a/grk/cw 6/textures/planets/normalne/tropical.png b/grk/cw 6/textures/planets/normalne/tropical.png new file mode 100644 index 0000000..4fc9610 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/tropical.png differ diff --git a/grk/cw 6/textures/planets/normalne/volcanic.png b/grk/cw 6/textures/planets/normalne/volcanic.png new file mode 100644 index 0000000..374f3a8 Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/volcanic.png differ diff --git a/grk/cw 6/textures/planets/normalne/watermelon.png b/grk/cw 6/textures/planets/normalne/watermelon.png new file mode 100644 index 0000000..b74d6ed Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/watermelon.png differ diff --git a/grk/cw 6/textures/planets/normalne/wood.png b/grk/cw 6/textures/planets/normalne/wood.png new file mode 100644 index 0000000..4b8efed Binary files /dev/null and b/grk/cw 6/textures/planets/normalne/wood.png differ diff --git a/grk/cw 6/textures/planets/rocks.png b/grk/cw 6/textures/planets/rocks.png new file mode 100644 index 0000000..e9bccbe Binary files /dev/null and b/grk/cw 6/textures/planets/rocks.png differ diff --git a/grk/cw 6/textures/planets/rocky.jpg b/grk/cw 6/textures/planets/rocky.jpg deleted file mode 100644 index 4242840..0000000 Binary files a/grk/cw 6/textures/planets/rocky.jpg and /dev/null differ diff --git a/grk/cw 6/textures/planets/rocky.png b/grk/cw 6/textures/planets/rocky.png new file mode 100644 index 0000000..bc00110 Binary files /dev/null and b/grk/cw 6/textures/planets/rocky.png differ diff --git a/grk/cw 6/textures/planets/rust.png b/grk/cw 6/textures/planets/rust.png new file mode 100644 index 0000000..4b869c2 Binary files /dev/null and b/grk/cw 6/textures/planets/rust.png differ diff --git a/grk/cw 6/textures/planets/snow-foot.png b/grk/cw 6/textures/planets/snow-foot.png new file mode 100644 index 0000000..0316725 Binary files /dev/null and b/grk/cw 6/textures/planets/snow-foot.png differ diff --git a/grk/cw 6/textures/planets/watermelon.png b/grk/cw 6/textures/planets/watermelon.png new file mode 100644 index 0000000..05e946e Binary files /dev/null and b/grk/cw 6/textures/planets/watermelon.png differ diff --git a/grk/cw 6/textures/planets/wood.png b/grk/cw 6/textures/planets/wood.png new file mode 100644 index 0000000..e2751a3 Binary files /dev/null and b/grk/cw 6/textures/planets/wood.png differ