color hint

This commit is contained in:
s464971 2024-02-08 01:24:36 +01:00
parent 348a52f47a
commit f3863321a6
3 changed files with 230 additions and 15 deletions

129
cw_8/shaders/install.frag Normal file
View File

@ -0,0 +1,129 @@
#version 430 core
out vec4 FragColor;
in vec2 TexCoords;
in vec3 worldPos;
in vec3 Normal;
uniform vec3 cameraPos;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform sampler2D albedoMap;
uniform sampler2D normalMap;
uniform sampler2D metallicMap;
uniform sampler2D roughnessMap;
uniform sampler2D aoMap;
const float PI = 3.14159265359;
vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(normalMap, TexCoords).xyz * 2.0 - 1.0;
vec3 Q1 = dFdx(worldPos);
vec3 Q2 = dFdy(worldPos);
vec2 st1 = dFdx(TexCoords);
vec2 st2 = dFdy(TexCoords);
vec3 N = normalize(Normal);
vec3 T = normalize(Q1*st2.t - Q2*st1.t);
vec3 B = -normalize(cross(N, T));
mat3 TBN = mat3(T, B, N);
return normalize(TBN * tangentNormal);
}
vec3 fresnelSchlick(float cosTheta, vec3 F0){
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
void main()
{
vec3 albedo = pow(texture(albedoMap, TexCoords).rgb, vec3(2.2));
float metallic = texture(metallicMap, TexCoords).r;
float roughness = texture(roughnessMap, TexCoords).r;
float ao = texture(aoMap, TexCoords).r;
vec3 N = getNormalFromMap();
vec3 V = normalize(cameraPos - worldPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 Lo = vec3(0.0);
vec3 L = normalize(lightPos - worldPos);
vec3 H = normalize(V + L);
float distance = length(lightPos - worldPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = lightColor * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
vec3 ambient = vec3(0.03) * albedo * ao;
vec3 color = ambient + Lo;
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
FragColor = vec4(color, 1.0)*vec4(0.0, 0.8, 0.95, 1.0);
}

24
cw_8/shaders/install.vert Normal file
View File

@ -0,0 +1,24 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
uniform mat4 transformation;
uniform mat4 modelMatrix;
uniform vec3 cameraPos;
out vec3 Normal;
out vec3 worldPos;
out vec2 TexCoords;
void main()
{
TexCoords = vertexTexCoord;
worldPos = vec3(modelMatrix * vec4(vertexPosition,1));
Normal = normalize((modelMatrix * vec4(vertexNormal,0)).xyz);
gl_Position = transformation * vec4(vertexPosition, 1.0);
}

View File

@ -16,6 +16,7 @@ GLuint program;
GLuint programSun; GLuint programSun;
GLuint programParticle; GLuint programParticle;
GLuint programSkybox; GLuint programSkybox;
GLuint programInstall;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -129,7 +130,7 @@ void processInput(GLFWwindow* window)
} }
if (engineIs == 2) if (engineIs == 2)
cameraSpeed = cameraSpeed * 3;//1.5; cameraSpeed = cameraSpeed * 1.8;//1.5;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
@ -281,6 +282,31 @@ void isTouching(glm::vec3 objectPos) {
void drawObjectToInstall(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint normal, GLuint ao, GLuint roughness, GLuint metallic) {
//dodaj shader
//zmien 'program'
glUseProgram(programInstall);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programInstall, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programInstall, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(programInstall, "lightPos"), 0, 0, 0);
glUniform3f(glGetUniformLocation(programInstall, "lightColor"), 300, 300, 300);
Core::SetActiveTexture(albedo, "albedoMap", programInstall, 0);
Core::SetActiveTexture(normal, "normalMap", programInstall, 1);
Core::SetActiveTexture(ao, "aoMap", programInstall, 2);
Core::SetActiveTexture(roughness, "roughnessMap", programInstall, 3);
Core::SetActiveTexture(metallic, "metallicMap", programInstall, 4);
//dodaæ warstwe koloru
Core::DrawContext(context);
glUseProgram(0);
}
void renderScene(GLFWwindow* window) { void renderScene(GLFWwindow* window) {
glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
@ -305,14 +331,53 @@ void renderScene(GLFWwindow* window) {
0.,0.,0.,1., 0.,0.,0.,1.,
}); });
drawObjectPBR(shipContext, if (engineIs == 1)
glm::translate(spaceshipPos) {
* spaceshipRotationMatrix drawObjectToInstall(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.048f)) * glm::eulerAngleY(time / 4), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f)) //drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleX(time/2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
* glm::eulerAngleX(glm::radians(80.f)) drawObjectToInstall(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleYZ(time / 3.f, time * 3.f), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
* glm::eulerAngleY(glm::radians(180.f)) drawObjectToInstall(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.052f)) * glm::eulerAngleXYZ(time / 2, time / 2, time / 2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
* glm::scale(glm::vec3(0.005f)), drawObjectToInstall(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.054f)) * glm::eulerAngleX(0.5f) * glm::eulerAngleXZ(time, time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
if (spaceshipPos.x > -0.7 && 0.3 > spaceshipPos.x && spaceshipPos.z > 3.7 && 4.3 > spaceshipPos.z)
{
drawObjectToInstall(shipContext,
glm::translate(spaceshipPos)
* spaceshipRotationMatrix
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f))
* glm::eulerAngleX(glm::radians(80.f))
* glm::eulerAngleY(glm::radians(180.f))
* glm::scale(glm::vec3(0.005f)),
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
}
else
{
drawObjectPBR(shipContext,
glm::translate(spaceshipPos)
* spaceshipRotationMatrix
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f))
* glm::eulerAngleX(glm::radians(80.f))
* glm::eulerAngleY(glm::radians(180.f))
* glm::scale(glm::vec3(0.005f)),
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
}
}
else{
drawObjectPBR(shipContext,
glm::translate(spaceshipPos)
* spaceshipRotationMatrix
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f))
* glm::eulerAngleX(glm::radians(80.f))
* glm::eulerAngleY(glm::radians(180.f))
* glm::scale(glm::vec3(0.005f)),
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.048f)) * glm::eulerAngleY(time / 4), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleX(time/2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleYZ(time / 3.f, time * 3.f), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.052f)) * glm::eulerAngleXYZ(time / 2, time / 2, time / 2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.054f)) * glm::eulerAngleX(0.5f) * glm::eulerAngleXZ(time, time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
}
isTouching(glm::vec3(4, -0.2f, -3)); isTouching(glm::vec3(4, -0.2f, -3));
@ -327,12 +392,7 @@ void renderScene(GLFWwindow* window) {
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY(-1.5f * time) * glm::scale(glm::vec3(0.1f)), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic); drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY(-1.5f * time) * glm::scale(glm::vec3(0.1f)), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY( 2.f * time) * glm::translate(glm::vec3(0.4f, 0, 0.4f)) * glm::eulerAngleY(-0.5f * time) * glm::scale(glm::vec3(0.04f)), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic); drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY( 2.f * time) * glm::translate(glm::vec3(0.4f, 0, 0.4f)) * glm::eulerAngleY(-0.5f * time) * glm::scale(glm::vec3(0.04f)), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.048f)) * glm::eulerAngleY(time/4), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleX(time/2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.05f)) * glm::eulerAngleY(0.5f) * glm::eulerAngleYZ(time/3.f,time*3.f), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.052f)) * glm::eulerAngleXYZ(time/2,time/2,time/2), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 0.4f, 4.f)) * glm::scale(glm::vec3(0.054f)) * glm::eulerAngleX(0.5f) * glm::eulerAngleXZ(time,time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//desired objects end here //desired objects end here
glUseProgram(0); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
@ -354,6 +414,8 @@ void init(GLFWwindow* window)
program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag"); program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag"); programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag");
programInstall = shaderLoader.CreateProgram("shaders/install.vert", "shaders/install.frag");
//programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag"); //programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag");
//load models here //load models here