initial commit

This commit is contained in:
2023-10-14 22:41:16 +02:00
commit 724940f27f
29 changed files with 18776 additions and 0 deletions
+186
View File
@@ -0,0 +1,186 @@
#version 330 core
in vec3 fragNormals;
in vec3 fragPos;
in vec2 texCoords;
out vec4 FragColor;
struct Material {
sampler2D texture_diffuse1;
sampler2D texture_diffuse2;
sampler2D texture_diffuse3;
sampler2D texture_diffuse4;
sampler2D texture_diffuse5;
sampler2D texture_diffuse6;
sampler2D texture_diffuse7;
sampler2D texture_diffuse8;
sampler2D texture_diffuse9;
sampler2D texture_diffuse10;
sampler2D texture_diffuse11;
sampler2D texture_diffuse12;
sampler2D texture_diffuse13;
sampler2D texture_diffuse14;
sampler2D texture_diffuse15;
sampler2D texture_diffuse16;
sampler2D texture_specular1;
sampler2D texture_specular2;
sampler2D texture_specular3;
sampler2D texture_specular4;
sampler2D texture_specular5;
sampler2D texture_specular6;
sampler2D texture_specular7;
sampler2D texture_specular8;
sampler2D texture_specular9;
sampler2D texture_specular10;
sampler2D texture_specular11;
sampler2D texture_specular12;
sampler2D texture_specular13;
sampler2D texture_specular14;
sampler2D texture_specular15;
sampler2D texture_specular16;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
#define NR_POINT_LIGHTS 4
uniform vec3 viewPos;
uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
vec3 CalculateDirLight(DirLight light, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(lightDir, normal), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
//vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, texCoords)) * material.ambient;
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
return (ambient + diffuse + specular);
}
vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance + distance));
//vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, texCoords)) * material.ambient;
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance + distance));
// spotlight
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
//vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, texCoords)) * material.ambient;
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
diffuse *= intensity;
specular *= intensity;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
void main()
{
vec3 norm = normalize(fragNormals);
vec3 viewDir = normalize(viewPos - fragPos);
// direction light
vec3 result = CalculateDirLight(dirLight, norm, viewDir);
// point lights
for (int i = 0; i < NR_POINT_LIGHTS; i++) {
result += CalculatePointLight(pointLights[i], norm, fragPos, viewDir);
}
result += CalculateSpotLight(spotLight, norm, fragPos, viewDir);
FragColor = vec4(result, 1.0);
}
+63
View File
@@ -0,0 +1,63 @@
#version 330 core
in vec3 nearPoint;
in vec3 farPoint;
out vec4 FragColor;
uniform mat4 projection;
uniform mat4 view;
uniform float near;
uniform float far;
vec4 grid(vec3 fragPos3D, float scale, float lineWidth, float lineOpacity) {
vec2 coord = fragPos3D.xz * scale; // use the scale variable to set the distance between the lines
vec2 derivative = fwidth(coord);
vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative;
float line = min(grid.x, grid.y);
float minimumz = min(derivative.y, 1);
float minimumx = min(derivative.x, 1);
vec4 color = vec4(0.2, 0.2, 0.2, lineOpacity * (1.0 - min(line, 1.0)));
// z axis
if(abs(fragPos3D.x) < lineWidth * minimumx)
color.z = 1.0;
// x axis
if(abs(fragPos3D.z) < lineWidth * minimumz)
color.x = 1.0;
return color;
}
float computeDepth(vec3 pos) {
vec4 clip_space_pos = projection * view * vec4(pos.xyz, 1.0);
return (clip_space_pos.z / clip_space_pos.w);
}
float computeLinearDepth(vec3 pos) {
vec4 clip_space_pos = projection * view * vec4(pos.xyz, 1.0);
float clip_space_depth = (clip_space_pos.z / clip_space_pos.w) * 2.0 - 1.0; // put back between -1 and 1
float linearDepth = (2.0 * near * far) / (far + near - clip_space_depth * (far - near)); // get linear value between 0.01 and 100
return linearDepth / far; // normalize
}
void main()
{
float t = -nearPoint.y / (farPoint.y - nearPoint.y);
vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint);
//gl_FragDepth = computeDepth(fragPos3D);
// above depth calculation is buggy
// see https://stackoverflow.com/questions/72791713/issues-with-infinite-grid-in-opengl-4-5-with-glsl
// for solution
gl_FragDepth = ((gl_DepthRange.diff * computeDepth(fragPos3D)) +
gl_DepthRange.near + gl_DepthRange.far) / 2.0;
float linearDepth = computeLinearDepth(fragPos3D);
float fading = max(0, (0.5 - linearDepth));
float lineWidth = 0.1; // width of the grid lines in world space units
float lineOpacity = 0.7; // opacity of the grid lines
// Compute the grid colors at different resolutions and blend them together
FragColor = (grid(fragPos3D, 5, lineWidth, lineOpacity) + grid(fragPos3D, 0.5, lineWidth, lineOpacity)) * float(t > 0);
// Fade the grid out as it gets farther away
FragColor.a *= fading;
}
+27
View File
@@ -0,0 +1,27 @@
#version 330 core
out vec3 nearPoint;
out vec3 farPoint;
uniform mat4 projection;
uniform mat4 view;
// Grid position are in xy clipped space
vec3 gridPlane[6] = vec3[](
vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0)
);
vec3 UnprojectPoint(float x, float y, float z, mat4 v, mat4 proj) {
mat4 viewInv = inverse(v);
mat4 projInv = inverse(proj);
vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0);
return unprojectedPoint.xyz / unprojectedPoint.w;
}
void main() {
vec3 p = gridPlane[gl_VertexID].xyz;
nearPoint = UnprojectPoint(p.x, p.y, 0.0, view, projection).xyz; // unprojecting on the near plane
farPoint = UnprojectPoint(p.x, p.y, 1.0, view, projection).xyz; // unprojecting on the far plane
gl_Position = vec4(p, 1.0); // using directly the clipped coordinates
}
+7
View File
@@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0);
}
+20
View File
@@ -0,0 +1,20 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec3 fragNormals;
out vec3 fragPos;
out vec2 texCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0f);
fragNormals = mat3(transpose(inverse(model))) * aNormal;
fragPos = vec3(model * vec4(aPos, 1.0));
texCoords = aTexCoords;
}