Added point light

This commit is contained in:
Daniel 2023-08-10 11:14:21 +02:00
parent 35085524d3
commit f7ba23fb17
2 changed files with 38 additions and 21 deletions

View File

@ -16,7 +16,7 @@
#include "vertices.h"
// Continue: https://learnopengl.com/Lighting/Light-casters
// Chapter: not yet started
// Chapter: Spot light
//
// TODO: look at project->properties->VC++ Directories-> change everything to the Environment var
// For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS
@ -225,8 +225,11 @@ int main()
lighting->SetFloat("light.ambientIntensity", ambientColour);
lighting->SetFloat("light.diffuseIntensity", diffuseColour);
lighting->SetFloat("light.specularIntensity", 1.0f, 1.0f, 1.0f);
lighting->SetFloat("light.direction", -0.2f, -1.0f, -0.3f);
lighting->SetFloat("light.position", lightPosition);
lighting->SetFloat("viewPosition", cam.Position);
lighting->SetFloat("light.constant", 1.0f);
lighting->SetFloat("light.linear", 0.09f);
lighting->SetFloat("light.quadratic", 0.032f);
diffuseMap.BindTexture();
diffuseMap.BindTexture(GL_TEXTURE1);
@ -235,17 +238,21 @@ int main()
{
allShaders[i]->Use();
for (size_t j = 0; j < 10; j++)
if (i == 0)
{
allObjects[i].Position = cubePositions[j];
glm::mat4 model = allObjects[i].GetModelMatrix();
if (i == 0)
for (size_t j = 0; j < 10; j++)
{
allObjects[i].Position = cubePositions[j];
glm::mat4 model = allObjects[i].GetModelMatrix();
float angle = 20.0f * j;// * currentFrameTime;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
}
allShaders[i]->SetMatrix("modelMatrix", model);
allShaders[i]->SetMatrix("modelMatrix", model);
allObjects[i].Draw();
}
}
else
{
allObjects[i].Draw();
}
}

View File

@ -1,21 +1,24 @@
#version 330 core
struct Material {
sampler2D diffuseMap;
sampler2D specularMap;
float shininess;
sampler2D diffuseMap;
sampler2D specularMap;
float shininess;
};
struct Light {
// vec3 position;
vec3 direction;
vec3 position;
vec3 ambientIntensity;
vec3 diffuseIntensity;
vec3 specularIntensity;
vec3 ambientIntensity;
vec3 diffuseIntensity;
vec3 specularIntensity;
float constant;
float linear;
float quadratic;
};
out vec4 FragColor;
uniform vec3 lightPosition, viewPosition;
uniform vec3 viewPosition;
uniform Material material;
uniform Light light;
@ -27,16 +30,23 @@ void main()
{
vec3 normal = normalize(Normal);
vec3 ambientLight = light.ambientIntensity * vec3(texture(material.diffuseMap, TexCoords));
vec3 ambientLight = light.ambientIntensity * texture(material.diffuseMap, TexCoords).rgb;
vec3 lightDir = normalize(-light.direction); //normalize(lightPosition - FragmentPos);
vec3 lightDir = normalize(light.position - FragmentPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuseLight = light.diffuseIntensity * diff * vec3(texture(material.diffuseMap, TexCoords));
vec3 diffuseLight = light.diffuseIntensity * diff * texture(material.diffuseMap, TexCoords).rgb;
vec3 viewDir = normalize(viewPosition - FragmentPos);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specularLight = light.specularIntensity * spec * vec3(texture(material.specularMap, TexCoords));
vec3 specularLight = light.specularIntensity * spec * texture(material.specularMap, TexCoords).rgb;
float distance = length(light.position - FragmentPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
ambientLight *= attenuation;
diffuseLight *= attenuation;
specularLight *= attenuation;
vec3 result = ambientLight + diffuseLight + specularLight;
FragColor = vec4(result, 1.0);