Fixed immediate mode lighting, improved-ish modern gl lighting

This commit is contained in:
2019-12-09 11:48:54 -06:00
parent ff1d34deec
commit e4333a82b8
6 changed files with 146 additions and 27 deletions

View File

@@ -1,26 +1,96 @@
# version 330 core
const int MAX_LIGHTS = 4;
struct Light {
float end;
float type;
vec3 position;
vec3 diffuse;
vec3 ambient;
vec3 specular;
vec3 direction;
float intensity;
};
in vec3 outPosition;
in vec3 outColor;
in vec4 outNormal;
in vec3 outUV;
in float outTextureID;
in vec3 outLightPos;
in Light outLights[MAX_LIGHTS];
in float outTotalLights;
in vec3 outFragPos;
in vec3 outCameraPos;
in vec3 outInverseNormal;
// optimizing compilers are annoying at this stage of my understanding of GLSL
vec4 lokiVar;
// https://learnopengl.com/Lighting/Multiple-lights
vec3 calculatePointLight(Light light) {
vec3 viewDir = normalize(outCameraPos - outFragPos);
vec3 lightDir = normalize(light.position - outFragPos);
float diff = max(dot(vec3(outNormal), lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, vec3(outNormal));
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16.0);
float distance = length(light.position - outFragPos);
float attenuation = 1.0 / (1.0 + 0.09 * distance +
0.032 * (distance * distance));
vec3 ambient = light.ambient * outColor;
vec3 diffuse = light.diffuse * outColor;
vec3 specular = light.specular * spec * vec3(1.0, 1.0, 1.0);
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
// https://learnopengl.com/Lighting/Basic-Lighting
vec3 calculateBasicLight(Light light) {
vec3 lightDir = normalize(light.position - outFragPos);
float ambientStrength = 0.25;
vec3 ambient = ambientStrength * light.ambient;
float diff = max(dot(normalize(vec3(outNormal)), lightDir), 0.0);
vec3 diffuse = diff * light.diffuse;
float specularStrength = 0.5;
vec3 viewDir = normalize(outCameraPos - outFragPos);
vec3 reflectDir = reflect(-lightDir, outInverseNormal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * light.specular;
return vec3(ambient + diffuse + specular);
}
vec3 calculateLighting() {
vec3 result = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < min(int(outTotalLights), MAX_LIGHTS); i++) {
if (int(outLights[i].type) == 0) {
result += calculateBasicLight(outLights[i]);
} else if (int(outLights[i].type) == 1) {
result += calculateBasicLight(outLights[i]);
}
}
return result;
}
void main() {
lokiVar = vec4(outColor, 1.0) + outNormal + vec4(outUV, 1.0) + vec4(outTextureID, 1.0, 1.0, 1.0);
lokiVar = normalize(lokiVar);
vec3 lightDir = normalize(outLightPos - outFragPos);
vec3 ambient = vec3(0.5, 0.5, 0.35);
float diffuse = max(dot(vec3(outNormal), lightDir), -0.2);
vec3 specular = vec3(0, 0, 0);
vec3 result =(ambient + diffuse + specular) * outColor;
vec3 result = calculateLighting() * outColor;
gl_FragColor = vec4(result, 1.0);
}

View File

@@ -1,4 +1,19 @@
# version 330 core
const int MAX_LIGHTS = 4;
struct Light {
float end;
float type;
vec3 position;
vec3 diffuse;
vec3 ambient;
vec3 specular;
vec3 direction;
float intensity;
};
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
@@ -12,14 +27,20 @@ out vec4 outNormal;
out vec3 outUV;
out float outTextureID;
out float outHasTexture;
out vec3 outLightPos;
out Light outLights[MAX_LIGHTS];
out float outTotalLights;
out vec3 outFragPos;
out vec3 outViewPos;
out vec3 outCameraPos;
out vec3 outInverseNormal;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform int hasTexture;
uniform vec3 lightPos;
uniform float totalLights;
uniform Light lights[MAX_LIGHTS];
uniform vec3 cameraPos;
void main() {
@@ -30,7 +51,10 @@ void main() {
outUV = inUV;
outTextureID = inTextureID;
outHasTexture = hasTexture;
outLightPos = lightPos;
outLights = lights;
outTotalLights = totalLights;
outCameraPos = cameraPos;
outInverseNormal = mat3(transpose(inverse(model))) * vec3(inNormal);
outFragPos = vec3(model * vec4(inPosition, 1.0));