Added lighting maps

This commit is contained in:
Nigel Barink 2022-02-16 19:46:19 +01:00
parent 59bd1ece6b
commit fd29b303e2
5 changed files with 102 additions and 59 deletions

BIN
Textures/container2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -3,11 +3,11 @@ out vec4 FragColor;
in vec3 Normal; in vec3 Normal;
in vec3 FragPos; in vec3 FragPos;
in vec2 TexCoords;
struct Material { struct Material {
vec3 ambient; sampler2D diffuse;
vec3 diffuse; sampler2D specular;
vec3 specular;
float shininess; float shininess;
}; };
@ -30,21 +30,21 @@ uniform vec3 viewPos;
void main() void main()
{ {
// ambient lighting calculation // ambient lighting calculation
vec3 ambient = light.ambient * material.ambient; vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
// diffuse lighting calculation // diffuse lighting calculation
vec3 norm = normalize(Normal); vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos); vec3 lightDir = 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 * material.diffuse); vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
// specular lighting // specular lighting
vec3 viewDir = normalize(viewPos - FragPos); vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm); vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * ( spec * material.specular); vec3 specular = light.specular * spec * vec3( texture(material.specular, TexCoords));
// Calculate the result; // Calculate the result;

View File

@ -1,6 +1,7 @@
#version 460 core #version 460 core
layout (location=0) in vec3 aPos; layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aNormal; layout (location=1) in vec3 aNormal;
layout (location=2) in vec2 aTexCoords;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@ -8,7 +9,7 @@ uniform mat4 projection;
out vec3 FragPos; out vec3 FragPos;
out vec3 Normal; out vec3 Normal;
out vec2 TexCoords;
void main(){ void main(){
@ -16,4 +17,5 @@ void main(){
gl_Position = projection * view * model * vec4(aPos , 1.0); gl_Position = projection * view * model * vec4(aPos , 1.0);
FragPos = vec3(model * vec4(aPos, 1.0)); FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal; Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords;
} }

View File

@ -92,47 +92,48 @@ int main() {
Shader shader ("shader.vs", "shader.fs"); Shader shader ("shader.vs", "shader.fs");
float vertices[] = { float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // positions // normals // texture coords
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1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f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
}; };
unsigned int VBO, VAO; unsigned int VBO, VAO;
@ -145,21 +146,64 @@ float vertices[] = {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3*sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) (3*sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE, 8 * sizeof(float), (void*) (6*sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int lightVAO; unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO); glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO); glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 6* sizeof(float), (void*) 0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 8* sizeof(float), (void*) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// Load diffuse texture
int width, height, nrChannels;
unsigned int diffuseMap;
glGenTextures(1, &diffuseMap);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
unsigned char* data = stbi_load("Textures/container2.png", &width, &height, &nrChannels, 0);
if(data){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else{
std::cout << "Error loading texture...." << std::endl;
}
stbi_image_free(data);
// Load Specular texture
unsigned int specularMap;
glGenTextures(1, &specularMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap);
unsigned char* specular_data = stbi_load("Textures/container2_specular.png", &width, &height, &nrChannels, 0);
if( specular_data ){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, specular_data);
glGenerateMipmap(GL_TEXTURE_2D);
}else{
std::cout << "Error loading texture...." << std::endl;
}
stbi_image_free(specular_data);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
@ -196,27 +240,18 @@ while(!glfwWindowShouldClose(window))
// draw cubes // draw cubes
shader.use(); shader.use();
shader.setInt("material.diffuse", 0);
shader.setVec3("material.ambient", glm::vec3(1.0f, 0.5f, 0.31f)); shader.setInt("material.specular", 1);
shader.setVec3("material.diffuse", glm::vec3(1.0f, 0.5f, 0.31f));
shader.setVec3("material.specular", glm::vec3(0.5, 0.5f, 0.5f)); shader.setVec3("material.specular", glm::vec3(0.5, 0.5f, 0.5f));
shader.setFloat("material.shininess", 32.0f); shader.setFloat("material.shininess", 32.0f);
glm::vec3 lightColor;
lightColor.x = sin(glfwGetTime() * 2.0f);
lightColor.y = sin(glfwGetTime() * 0.7f);
lightColor.z = sin(glfwGetTime() * 1.3f);
glm::vec3 diffuseColor = lightColor * glm::vec3(0.5f);
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f);
shader.setVec3("light.position", lightpos); shader.setVec3("light.position", lightpos);
shader.setVec3("light.ambient", ambientColor); shader.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
shader.setVec3("light.diffuse", diffuseColor); shader.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f));
shader.setVec3("light.specular", glm::vec3(1.0f, 1.0f, 1.0f)); shader.setVec3("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
@ -230,6 +265,12 @@ while(!glfwWindowShouldClose(window))
model = glm::mat4(1.0); model = glm::mat4(1.0);
shader.setMat4("model", model); shader.setMat4("model", model);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);