finished section "Lighting"

This commit is contained in:
Daniel 2023-09-14 13:06:59 +02:00
parent b6477b6617
commit 725c8d8fa3
7 changed files with 153 additions and 37 deletions

View File

@ -151,6 +151,8 @@
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<ClInclude Include="lights\directionalLight.h" />
<ClInclude Include="lights\pointLight.h" />
<ClInclude Include="object\game_object.h" />
<ClInclude Include="util\camera\camera.h" />
<ClInclude Include="Exceptions\IOException.h" />

View File

@ -65,6 +65,12 @@
<ClInclude Include="vertices.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lights\directionalLight.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lights\pointLight.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />

View File

@ -0,0 +1,37 @@
#pragma once
#include <glm/glm.hpp>
#include "../shaders/Shader.h"
namespace Nebulix
{
struct DirectionalLight
{
public:
DirectionalLight(glm::vec3 direction, glm::vec3 ambient, glm::vec3 diffuse, glm::vec3 specular)
: direction{ direction }, ambient{ambient}, diffuse{diffuse}, specular{specular}
{
}
/**
Sets the values of a directional light in the shader
name: The name of the variable in the shader. Needs to be a struct with the same components as this struct
shader: The shader to set the values of
*/
void SetLight(std::string name, Shader& shader)
{
shader.SetFloat(name + ".direction", direction);
shader.SetFloat(name + ".ambient", ambient);
shader.SetFloat(name + ".diffuse", diffuse);
shader.SetFloat(name + ".specular", specular);
}
glm::vec3 direction;
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
};
}

View File

@ -0,0 +1,45 @@
#pragma once
#include <glm/glm.hpp>
#include "../shaders/Shader.h"
namespace Nebulix
{
struct PointLight
{
public:
PointLight(glm::vec3 &position, glm::vec3 &ambient, glm::vec3 &diffuse, glm::vec3 specular, float linear = 0.09f, float quadratic = 0.032f, float constant = 1.0f)
: position{position}, ambient{ambient}, diffuse{diffuse}, specular{specular}, linear{linear}, quadratic{quadratic}, constant{constant}
{
}
/**
Sets the values of a spot light in the shader
name: The name of the variable in the shader. Needs to be a struct with the same components as this struct
shader: The shader to set the values of
*/
void SetLight(std::string name, Shader &shader)
{
shader.SetFloat(name + ".position", position);
shader.SetFloat(name + ".constant", constant);
shader.SetFloat(name + ".linear", linear);
shader.SetFloat(name + ".quadratic", quadratic);
shader.SetFloat(name + ".ambient", ambient);
shader.SetFloat(name + ".diffuse", diffuse);
shader.SetFloat(name + ".specular", specular);
}
glm::vec3 position;
float constant;
float linear;
float quadratic;
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
};
}

View File

@ -12,13 +12,14 @@
#include "util/stb_image.h"
#include "util/camera/camera.h"
#include "object/game_object.h"
#include "lights/directionalLight.h"
#include "lights/pointLight.h"
#include "vertices.h"
// Continue: https://learnopengl.com/Lighting/Multiple-lights
// Chapter: Putting it all together
// Continue: https://learnopengl.com/Model-Loading/Assimp
// Chapter: Not yet started
//
// 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
// (so basically having an "external" folder which has open gl, GLAD, GLM, stb_image etc. in it or something like this)
@ -146,6 +147,12 @@ int main()
vec3(1.5f, 0.2f, -1.5f),
vec3(-1.3f, 1.0f, -1.5f)
};
std::vector<vec3> pointLightPositions = {
vec3(0.7f, 0.2f, 2.0f),
vec3(2.3f, -3.3f, -4.0f),
vec3(-4.0f, 2.0f, -12.0f),
vec3(0.0f, 0.0f, -3.0f)
};
std::string lightingVertexPath = "shaders/default/default.vert", lampVertexPath = "shaders/default/simple.vert";
@ -178,10 +185,24 @@ int main()
vec3 lightPosition = glm::vec3(1.2f, 1.0f, 2.0f);
Nebulix::GameObject lightBulb(lightVerts, objVertexAttribs, lightPosition, glm::vec3(0.2f));
std::vector<Nebulix::GameObject> allObjects = std::vector<Nebulix::GameObject>();
auto allObjects = std::vector<Nebulix::GameObject>();
allObjects.push_back(cube);
allObjects.push_back(lightBulb);
vec3 diffuseColour = vec3(1.0f);
vec3 ambientColour = diffuseColour * vec3(0.1f);
Nebulix::DirectionalLight dirLight(vec3(-0.2f, -1.0f, -0.3f), ambientColour, vec3(0.4f), vec3(0.5f));
auto pointLights = std::vector<Nebulix::PointLight>();
Nebulix::PointLight pLight1(pointLightPositions[0], ambientColour, diffuseColour, vec3(1.0f));
Nebulix::PointLight pLight2(pointLightPositions[1], ambientColour, diffuseColour, vec3(1.0f));
Nebulix::PointLight pLight3(pointLightPositions[2], ambientColour, diffuseColour, vec3(1.0f));
Nebulix::PointLight pLight4(pointLightPositions[3], ambientColour, diffuseColour, vec3(1.0f));
pointLights.push_back(pLight1);
pointLights.push_back(pLight2);
pointLights.push_back(pLight3);
pointLights.push_back(pLight4);
// main loop
while(!glfwWindowShouldClose(window))
{
@ -205,34 +226,32 @@ int main()
shader->Use();
shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera
shader->SetMatrix("projectionMatrix", projectionMatrix);
//shader->SetMatrix("modelMatrix", allObjects[i].GetModelMatrix());
shader->SetMatrix("modelMatrix", model);
}
glm::vec3 lightColour;
lightColour.x = sin(glfwGetTime() * 2.0f);
lightColour.y = sin(glfwGetTime() * 0.7f);
lightColour.z = sin(glfwGetTime() * 1.3f);
glm::vec3 diffuseColour = glm::vec3(1.0f);//lightColour * glm::vec3(0.5f);
glm::vec3 ambientColour = diffuseColour * glm::vec3(0.1f);
lighting->Use();
lighting->SetInt("material.diffuseMap", 0);
lighting->SetInt("material.specularMap", 1);
lighting->SetFloat("material.specularColour", 0.5f, 0.5f, 0.5f);
lighting->SetFloat("material.shininess", 32.0f);
lighting->SetFloat("spotLight.ambientIntensity", ambientColour);
lighting->SetFloat("spotLight.diffuseIntensity", diffuseColour);
lighting->SetFloat("spotLight.specularIntensity", 1.0f, 1.0f, 1.0f);
lighting->SetFloat("spotLight.position", cam.Position);
lighting->SetFloat("spotLight.direction", cam.Front);
lighting->SetFloat("spotLight.cutOffAngle", glm::cos(glm::radians(12.5f)));
lighting->SetFloat("spotLight.outerCutOffAngle", glm::cos(glm::radians(20.0f)));
dirLight.SetLight("dirLight", *lighting.get());
for (size_t i = 0; i < pointLights.size(); i++)
{
pointLights[i].SetLight("pointLights[" + std::to_string(i) + "]", *lighting.get());
}
//lighting->SetFloat("spotLight.ambientIntensity", ambientColour);
//lighting->SetFloat("spotLight.diffuseIntensity", diffuseColour);
//lighting->SetFloat("spotLight.specularIntensity", 1.0f, 1.0f, 1.0f);
//lighting->SetFloat("spotLight.position", cam.Position);
//lighting->SetFloat("spotLight.direction", cam.Front);
//lighting->SetFloat("spotLight.cutOffAngle", glm::cos(glm::radians(12.5f)));
//lighting->SetFloat("spotLight.outerCutOffAngle", glm::cos(glm::radians(20.0f)));
lighting->SetFloat("viewPosition", cam.Position);
lighting->SetFloat("spotLight.constant", 1.0f);
lighting->SetFloat("spotLight.linear", 0.09f);
lighting->SetFloat("spotLight.quadratic", 0.032f);
//lighting->SetFloat("spotLight.constant", 1.0f);
//lighting->SetFloat("spotLight.linear", 0.09f);
//lighting->SetFloat("spotLight.quadratic", 0.032f);
diffuseMap.BindTexture();
diffuseMap.BindTexture(GL_TEXTURE1);
@ -256,7 +275,14 @@ int main()
}
else
{
allObjects[i].Draw();
for (size_t j = 0; j < 4; j++)
{
allObjects[i].Position = pointLightPositions[j];
glm::mat4 model = allObjects[i].GetModelMatrix();
allShaders[i]->SetMatrix("modelMatrix", model);
allObjects[i].Draw();
}
}
}

View File

@ -54,7 +54,7 @@ namespace Nebulix
glDeleteBuffers(1, &vertexBuffer);
}
// NOTE: This method will not call "Shader.Use()" but will not set any properties.
// NOTE: This method will not call "Shader.Use()" and will not set any properties.
// This is because I do not know the names and values of the properties. Most likely this will be possible after learning about materials
void Draw()
{

View File

@ -13,9 +13,9 @@ struct SpotLight {
float cutOffAngle;
float outerCutOffAngle;
vec3 ambientIntensity;
vec3 diffuseIntensity;
vec3 specularIntensity;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
@ -57,10 +57,10 @@ vec3 CalculateDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir
{
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(normal, lightDir), 0);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectionDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectionDir), 0), material.shininess);
float spec = pow(max(dot(viewDir, reflectionDir), 0.0), material.shininess);
vec3 ambient = light.ambient * vec3(texture(material.diffuseMap, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuseMap, TexCoords));
@ -104,9 +104,9 @@ vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
vec3 ambientLight = light.ambientIntensity * texture(material.diffuseMap, TexCoords).rgb * attenuation;
vec3 diffuseLight = light.diffuseIntensity * diff * texture(material.diffuseMap, TexCoords).rgb * attenuation * intensity;
vec3 specularLight = light.specularIntensity * spec * texture(material.specularMap, TexCoords).rgb * attenuation * intensity;
vec3 ambientLight = light.ambient * texture(material.diffuseMap, TexCoords).rgb * attenuation;
vec3 diffuseLight = light.diffuse * diff * texture(material.diffuseMap, TexCoords).rgb * attenuation * intensity;
vec3 specularLight = light.specular * spec * texture(material.specularMap, TexCoords).rgb * attenuation * intensity;
return ambientLight + diffuseLight + specularLight;
}
@ -117,12 +117,12 @@ void main()
vec3 normal = normalize(Normal);
vec3 viewDir = normalize(viewPosition - FragPos);
//vec3 result = CalculateDirectionalLight(dirLight, normal, viewDir);
vec3 result = CalculateDirectionalLight(dirLight, normal, viewDir);
//for(int i = 0; i < NR_POINT_LIGHTS; i++)
// result += CalculatePointLight(pointLights[i], normal, FragPos, viewDir);
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalculatePointLight(pointLights[i], normal, FragPos, viewDir);
vec3 result = CalculateSpotLight(spotLight, normal, FragPos, viewDir);
//result += CalculateSpotLight(spotLight, normal, FragPos, viewDir);
FragColor = vec4(result, 1.0);
}