lab 5 spaceship light

This commit is contained in:
Eikthyrnir 2023-12-01 15:20:14 +01:00
parent d05ed9c720
commit fe0c001753
3 changed files with 80 additions and 25 deletions

View File

@ -1,26 +1,30 @@
#version 430 core
uniform vec3 color;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 cameraPos;
uniform vec3 sunPos;
uniform vec3 sunColor;
uniform float sunLightExp;
uniform vec3 cameraPos;
uniform float time;
uniform float lightExp;
uniform vec3 reflectorPos;
uniform vec3 reflectorDir;
uniform vec3 reflectorColor;
uniform float reflectorAngle;
uniform float reflectorLightExp;
vec3 normalizedVertexNormal;
in vec3 vertexNormalOut;
in vec3 vertexPosOut;
out vec4 outColor;
void main()
{
vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) {
vec3 lightDir = normalize(vertexPosOut - lightPos);
float lightDistance = length(vertexPosOut - lightPos);
vec3 newLightColor = lightColor / pow(lightDistance, 2);
vec3 normalizedVertexNormal = normalize(vertexNormalOut);
float intensity = dot(normalizedVertexNormal, -lightDir);
intensity = max(intensity, 0.0);
@ -31,6 +35,37 @@ void main()
float specular = pow(max(dot(viewDir, reflectDir), 0.0), glossPow);
float diffuse = intensity;
outColor = vec4(newLightColor * (color * diffuse + specular ), 1.0);
outColor = 1 - exp(-outColor * lightExp);
vec3 resultColor = newLightColor * (fragColor * diffuse + specular );
return vec4(1 - exp(-resultColor * lightExp), 1.0);
}
vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, vec3 lightDir, float lightExp) {
vec3 reflectorLightDir = normalize(vertexPosOut - reflectorPos);
float angleCos = dot(reflectorLightDir, reflectorDir);
float reflectorOutAngle = reflectorAngle + radians(10);
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
vec4 res = vec4(0, 0, 0, 1);
if (angleCos > cos(reflectorOutAngle)) {
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
res = calcPointLight(color, reflectorPos, reflectorColor, reflectorLightExp * intensity);
}
return res;
}
void main()
{
normalizedVertexNormal = normalize(vertexNormalOut);
outColor = calcPointLight(color, sunPos, sunColor, sunLightExp);
vec3 reflectorLightDir = normalize(vertexPosOut - reflectorPos);
float angleCos = dot(reflectorLightDir, reflectorDir);
float reflectorOutAngle = reflectorAngle + radians(10);
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
if (angleCos > cos(reflectorOutAngle)) {
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
outColor += calcSpotLight(color, reflectorPos, reflectorColor, reflectorLightDir, reflectorLightExp * intensity);
}
}

View File

@ -18,6 +18,7 @@ void main()
vec3 viewDir = normalize(cameraPos - vertexPosOut);
float angle = dot(viewDir, normalizedVertexNormal);
//outColor = vec4(color * (angle / M_PI), 1.0);
outColor = vec4(mix(color, vec3(1), angle), 1.0);
// outColor = vec4(color * angle, 1.0);
vec3 red = vec3(1, 0, 0);
outColor = vec4(mix(red, color, angle), 1.0);
}

View File

@ -29,8 +29,13 @@ Core::RenderContext sphereContext;
glm::vec3 cameraPos = glm::vec3(-4.f, 0, 0);
glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
glm::vec3 lightDir = glm::normalize(glm::vec3(1.f, 1.f, 1.f));
glm::vec3 lightColor = glm::vec3(1.f, 1.f, 1.f);
float lightExp = 100;
glm::vec3 sunLightColor = glm::vec3(1.f, 1.f, 1.f);
float sunLightExp = 100;
glm::vec3 reflectorColor = glm::vec3(0.3f, 0.f, 0.f);
float reflectorAngle = glm::radians(25.f);
float reflectorLightExp = 5;
glm::vec3 spaceshipPos = glm::vec3(-4.f, 0, 0);
glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f);
@ -96,10 +101,20 @@ void drawObjectColor(GLuint program, Core::RenderContext& context, glm::mat4 mod
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMat"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
//sun
//glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0.f, 0.f, 0.f);
glUniform3f(glGetUniformLocation(program, "lightColor"), lightColor.x, lightColor.y, lightColor.z);
glUniform1f(glGetUniformLocation(program, "lightExp"), lightExp);
glUniform3f(glGetUniformLocation(program, "sunPos"), 0.f, 0.f, 0.f);
glUniform3f(glGetUniformLocation(program, "sunColor"), sunLightColor.x, sunLightColor.y, sunLightColor.z);
glUniform1f(glGetUniformLocation(program, "sunLightExp"), sunLightExp);
//spaceship reflector
glm::vec3 reflectorPos = spaceshipPos + 0.037f * spaceshipDir;
glUniform3f(glGetUniformLocation(program, "reflectorPos"), reflectorPos.x, reflectorPos.y, reflectorPos.z);
glUniform3f(glGetUniformLocation(program, "reflectorDir"), spaceshipDir.x, spaceshipDir.y, spaceshipDir.z);
glUniform1f(glGetUniformLocation(program, "reflectorAngle"), reflectorAngle);
glUniform3f(glGetUniformLocation(program, "reflectorColor"),
reflectorColor.x, reflectorColor.y, reflectorColor.z);
glUniform1f(glGetUniformLocation(program, "reflectorLightExp"), reflectorLightExp);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
//TEST
glUniform1f(glGetUniformLocation(program, "time"), lastTime);
@ -116,7 +131,7 @@ void renderScene(GLFWwindow* window)
float time = glfwGetTime();
updateDeltaTime(time);
time = 0;
drawObjectColor(programSun, sphereContext, glm::mat4(), glm::vec3(1.0, 1.0, 0.3));
@ -128,7 +143,7 @@ void renderScene(GLFWwindow* window)
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
glm::mat4 specshipCameraRotrationMatrix = glm::mat4({
glm::mat4 spaceshipCameraRotrationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
@ -141,11 +156,10 @@ void renderScene(GLFWwindow* window)
// glm::vec3(0.3, 0.3, 0.5)
// );
drawObjectColor(program, shipContext,
glm::translate(spaceshipPos) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.2f)),
glm::translate(spaceshipPos) * spaceshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.2f)),
glm::vec3(0.3, 0.3, 0.5)
);
glfwSwapBuffers(window);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@ -221,9 +235,14 @@ void processInput(GLFWwindow* window)
//cameraDir = glm::normalize(-cameraPos);
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
lightExp -= 1;
sunLightExp = glm::max(0.f, sunLightExp - 0.01f);
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
lightExp += 1;
sunLightExp += 0.01f;
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
reflectorLightExp = glm::max(0.f, reflectorLightExp - 0.01f);
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
reflectorLightExp += 0.01f;
}
// funkcja jest glowna petla