Directional light implementation

This commit is contained in:
Nigel Barink 2022-02-16 19:57:18 +01:00
parent fd29b303e2
commit 345ae95835
2 changed files with 32 additions and 9 deletions

View File

@ -12,7 +12,9 @@ struct Material {
}; };
struct Light{ struct Light{
vec3 position; // vec3 position; // Not neccessary when using directional lights.
vec3 direction;
vec3 ambient; vec3 ambient;
vec3 diffuse; vec3 diffuse;
@ -34,7 +36,7 @@ void main()
// diffuse lighting calculation // diffuse lighting calculation
vec3 norm = normalize(Normal); vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos); vec3 lightDir = normalize(-light.direction); // simplified with directional lights normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0); float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

View File

@ -20,7 +20,7 @@ Camera camera(glm::vec3(0.0f, 0.0f, 8.0f));
float lastX = 400, lastY = 300; float lastX = 400, lastY = 300;
bool firstMouse = true; bool firstMouse = true;
glm::vec3 lightpos(1.2f, 1.0f, 2.0f); glm::vec3 lightpos(-0.2f, -1.0f, -0.3f);
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0,0, width, height); glViewport(0,0, width, height);
@ -136,6 +136,19 @@ float vertices[] = {
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
}; };
glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
unsigned int VBO, VAO; unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
@ -233,9 +246,7 @@ while(!glfwWindowShouldClose(window))
float orbital_speed = .4 ; float orbital_speed = .4 ;
// move light around
lightpos.x = 1.0 + sin(glfwGetTime()) * 2.0f;
lightpos.y = sin(glfwGetTime() / 2.0f) * 1.0f;
// draw cubes // draw cubes
@ -248,7 +259,7 @@ while(!glfwWindowShouldClose(window))
shader.setVec3("light.position", lightpos); shader.setVec3("light.direction", lightpos);
shader.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f)); shader.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
shader.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f)); shader.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f));
@ -271,10 +282,20 @@ while(!glfwWindowShouldClose(window))
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap); glBindTexture(GL_TEXTURE_2D, specularMap);
for( unsigned int i = 0; i < 10; i++ ){
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model , cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
shader.setMat4("model", model);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
}
// draw lights // draw lights