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" #include "vertices.h"
// Continue: https://learnopengl.com/Lighting/Light-casters // 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 // 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 // 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.ambientIntensity", ambientColour);
lighting->SetFloat("light.diffuseIntensity", diffuseColour); lighting->SetFloat("light.diffuseIntensity", diffuseColour);
lighting->SetFloat("light.specularIntensity", 1.0f, 1.0f, 1.0f); 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("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();
diffuseMap.BindTexture(GL_TEXTURE1); diffuseMap.BindTexture(GL_TEXTURE1);
@ -235,20 +238,24 @@ int main()
{ {
allShaders[i]->Use(); allShaders[i]->Use();
if (i == 0)
{
for (size_t j = 0; j < 10; j++) for (size_t j = 0; j < 10; j++)
{ {
allObjects[i].Position = cubePositions[j]; allObjects[i].Position = cubePositions[j];
glm::mat4 model = allObjects[i].GetModelMatrix(); glm::mat4 model = allObjects[i].GetModelMatrix();
if (i == 0)
{
float angle = 20.0f * j;// * currentFrameTime; float angle = 20.0f * j;// * currentFrameTime;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); 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(); allObjects[i].Draw();
} }
} }
else
{
allObjects[i].Draw();
}
}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@ -5,17 +5,20 @@ struct Material {
float shininess; float shininess;
}; };
struct Light { struct Light {
// vec3 position; vec3 position;
vec3 direction;
vec3 ambientIntensity; vec3 ambientIntensity;
vec3 diffuseIntensity; vec3 diffuseIntensity;
vec3 specularIntensity; vec3 specularIntensity;
float constant;
float linear;
float quadratic;
}; };
out vec4 FragColor; out vec4 FragColor;
uniform vec3 lightPosition, viewPosition; uniform vec3 viewPosition;
uniform Material material; uniform Material material;
uniform Light light; uniform Light light;
@ -27,16 +30,23 @@ void main()
{ {
vec3 normal = normalize(Normal); 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); 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 viewDir = normalize(viewPosition - FragmentPos);
vec3 reflectDir = reflect(-lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); 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; vec3 result = ambientLight + diffuseLight + specularLight;
FragColor = vec4(result, 1.0); FragColor = vec4(result, 1.0);