add light shader and some models

This commit is contained in:
Artur Tamborski 2021-02-19 16:09:53 +01:00
parent 2309e5ad3c
commit eb231e31cb
4 changed files with 323 additions and 84 deletions

147
6.multiple_lights.frag Normal file
View File

@ -0,0 +1,147 @@
#version 330 core
out vec4 FragColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 4
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
// function prototypes
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
// properties
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
// == =====================================================
// Our lighting is set up in 3 phases: directional, point lights and an optional flashlight
// For each phase, a calculate function is defined that calculates the corresponding color
// per lamp. In the main() function we take all the calculated colors and sum them up for
// this fragment's final color.
// == =====================================================
// phase 1: directional lighting
vec3 result = CalcDirLight(dirLight, norm, viewDir);
// phase 2: point lights
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
// phase 3: spot light
result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
FragColor = vec4(result, 1.0);
}
// calculates the color when using a directional light.
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
return (ambient + diffuse + specular);
}
// calculates the color when using a point light.
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
// calculates the color when using a spot light.
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// spotlight intensity
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (ambient + diffuse + specular);
}

View File

@ -3,6 +3,8 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
uniform mat4 model;
@ -11,6 +13,9 @@ uniform mat4 projection;
void main()
{
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0);
gl_Position = projection * view * vec4(FragPos, 1.0);
}

View File

@ -1,12 +0,0 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
}

View File

@ -21,8 +21,8 @@ unsigned int loadTexture(const char *path);
unsigned int loadCubemap(vector<std::string> faces);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const unsigned int SCR_WIDTH = 800 * 2;
const unsigned int SCR_HEIGHT = 600 * 2;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
@ -30,15 +30,23 @@ float lastX = (float)SCR_WIDTH / 2.0;
float lastY = (float)SCR_HEIGHT / 2.0;
bool firstMouse = true;
glm::vec3 backpackTranslate(1.0f);
glm::vec3 spaceshipTranslate(1.0f);
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
//
glm::vec3 lightPos(-2.0f, 4.0f, -1.0f);
float angle = 0;
// positions of the point lights
glm::vec3 pointLightPositions[] = {
glm::vec3(-18.0f, 3.0f, 99999.0f),
glm::vec3( 0.8f, -16.6f, -0.6f),
glm::vec3( 6.8f, -7.4f, -7.2f),
glm::vec3( -11.8f, -7.889f, 15.0f),
glm::vec3(-14.0f, 3.0f, 2.0f),
};
size_t pointLightPositionsNum = sizeof(pointLightPositions) / sizeof(pointLightPositions[0]);
int main(int argc, char** argv)
@ -85,13 +93,16 @@ int main(int argc, char** argv)
// build and compile shaders
// -------------------------
Shader shader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
Shader cubeShader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag");
Shader lightingShader("6.multiple_lights.vert", "6.multiple_lights.frag");
Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
Model kniedeModel("../resources/objects/kniede/kniede.obj");
// build and compile models
// ------------------------
Model kniedeModel("../resources/objects/kniede/kniede.obj1");
Model spaceshipModel("../resources/objects/spaceship/Intergalactic_Spaceship.obj");
Model barrelModel("../resources/objects/barrel/barrel.obj");
Shader backpackShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
stbi_set_flip_vertically_on_load(true);
Model backpackModel("../resources/objects/backpack/backpack.obj");
stbi_set_flip_vertically_on_load(false);
@ -198,6 +209,7 @@ int main(int argc, char** argv)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
// skybox VAO
unsigned int skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO);
@ -226,14 +238,17 @@ int main(int argc, char** argv)
// shader configuration
// --------------------
shader.use();
shader.setInt("skybox", 0);
cubeShader.use();
cubeShader.setInt("skybox", 0);
skyboxShader.use();
skyboxShader.setInt("skybox", 0);
lightingShader.use();
lightingShader.setInt("material.diffuse", 0);
lightingShader.setInt("material.specular", 1);
// render loop
// -----------
float angle = 0;
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
@ -252,64 +267,143 @@ int main(int argc, char** argv)
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw scene as normal
shader.use();
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, angle/10.f, glm::vec3(0, 1, 0));
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setVec3("cameraPos", camera.Position);
// cubes
glBindVertexArray(cubeVAO);
// be sure to activate shader when setting uniforms/drawing objects
lightingShader.use();
lightingShader.setVec3("viewPos", camera.Position);
lightingShader.setFloat("material.shininess", 12.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::rotate(model, angle, glm::vec3(0, 1, 0));
// directional light
lightingShader.setVec3("dirLight.direction", -0.2f, -1.0f, -0.3f);
lightingShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
lightingShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
lightingShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
// point light 1
lightingShader.setVec3("pointLights[0].position", pointLightPositions[0]);
lightingShader.setVec3("pointLights[0].ambient", 8.0f, 8.0f, 5.0f);
lightingShader.setVec3("pointLights[0].diffuse", 0.8f, 0.8f, 0.8f);
lightingShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[0].constant", 1.0f);
lightingShader.setFloat("pointLights[0].linear", 0.09);
lightingShader.setFloat("pointLights[0].quadratic", 0.032);
// point light 2
lightingShader.setVec3("pointLights[1].position", pointLightPositions[1]);
lightingShader.setVec3("pointLights[1].ambient", 0.1f, 0.5f, 0.0f);
lightingShader.setVec3("pointLights[1].diffuse", 0.5f, 1.0f, 0.8f);
lightingShader.setVec3("pointLights[1].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[1].constant", 0.1f);
lightingShader.setFloat("pointLights[1].linear", 0.09);
lightingShader.setFloat("pointLights[1].quadratic", 0.032);
// point light 3
lightingShader.setVec3("pointLights[2].position", pointLightPositions[2]);
lightingShader.setVec3("pointLights[2].ambient", 0.7f, 0.3f, 0.0f);
lightingShader.setVec3("pointLights[2].diffuse", 4.0f, 1.0f, 0.0f);
lightingShader.setVec3("pointLights[2].specular", 0.5f, 0.5f, 0.0f);
lightingShader.setFloat("pointLights[2].constant", 0.3f);
lightingShader.setFloat("pointLights[2].linear", 0.09);
lightingShader.setFloat("pointLights[2].quadratic", 0.032);
// point light 4
// pointLightPositions[3].z = 14.0f + (glm::cos(glfwGetTime() * 3) * 2);
lightingShader.setVec3("pointLights[3].position", pointLightPositions[3]);
lightingShader.setVec3("pointLights[3].ambient", 1.0f, (pointLightPositions[3].z - 12) / 10, 0.0f);
lightingShader.setVec3("pointLights[3].diffuse", 1.0f, 1.0f, 0.0f);
lightingShader.setVec3("pointLights[3].specular", 0.5f, 0.5f, 0.0f);
lightingShader.setFloat("pointLights[3].constant", 0.3f);
lightingShader.setFloat("pointLights[3].linear", 0.09);
lightingShader.setFloat("pointLights[3].quadratic", 0.033);
// spotLight
lightingShader.setVec3("spotLight.position", pointLightPositions[4]);
lightingShader.setVec3("spotLight.direction", glm::vec3(0.82f, -0.24f, -0.5f));
lightingShader.setVec3("spotLight.ambient", 0.0f, 0.0f, 0.0f);
lightingShader.setVec3("spotLight.diffuse", 40.0f, 40.0f, 40.0f);
lightingShader.setVec3("spotLight.specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("spotLight.constant", 0.5f);
lightingShader.setFloat("spotLight.linear", 0.09);
lightingShader.setFloat("spotLight.quadratic", 0.032);
lightingShader.setFloat("spotLight.cutOff", glm::cos(glm::radians(15.5f)));
lightingShader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(40.0f + glm::sin(glfwGetTime()) * 8)));
// DEFAULTS
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 model = glm::mat4(1.0f);
// ---------------------------------------------------------------------------------------------------
// --------------------------------------------------------------- R Y S O W A N I E M O D E L I ---
// ---------------------------------------------------------------------------------------------------
// wspólne dla modeli oteksturowanych
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
// plecak
// model = glm::translate(glm::mat4(1.0f), glm::vec3(-0.8f, -13.6f, -7.0f));
model = glm::translate(glm::mat4(1.0f), spaceshipTranslate);
// model = glm::translate(model, spaceshipTranslate);
model = glm::scale(model, glm::vec3(0.5f));
model = glm::translate(model, glm::vec3(2.2f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians((float)glfwGetTime() * 60) + 90, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::translate(model, glm::vec3(-2.2f, 0.0f, 0.0f));
lightingShader.setMat4("model", model);
backpackModel.Draw(lightingShader);
// pomnik
model = glm::translate(glm::mat4(1.0f), glm::vec3(0.4, -23, -7));
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::translate(model, glm::vec3(-0.4, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians((float)glfwGetTime() * 60), glm::vec3(0.0f, 0.0f, 1.0f));
lightingShader.setMat4("model", model);
kniedeModel.Draw(lightingShader);
// beczka
model = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::translate(model, glm::vec3(10, -3, -7));
// model = glm::translate(glm::mat4(1.0f), spaceshipTranslate);
lightingShader.setMat4("model", model);
barrelModel.Draw(lightingShader);
// statek kosmiczny
// model = glm::translate(glm::mat4(1.0f), glm::vec3(15, -4, 9));
// model = glm::translate(model, spaceshipTranslate);
// lightingShader.setMat4("model", model);
// spaceshipModel.Draw(lightingShader);
// kostka z odbiciem
model = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1, 0));
model = glm::translate(model, glm::vec3(5, 0, 0));
model = glm::rotate(model, -angle, glm::vec3(0, 1, 0));
model = glm::translate(model, glm::vec3(0, -5, 0));
shader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
model = glm::translate(glm::mat4(1.0f), glm::vec3(10, -5, 9));
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians((float)glfwGetTime() * 8), glm::vec3(0.0f, 0.0f, 1.0f));
kniedeShader.use();
kniedeShader.setMat4("model", model);
kniedeShader.setMat4("view", view);
kniedeShader.setMat4("projection", projection);
kniedeModel.Draw(kniedeShader);
model = glm::translate(glm::mat4(1.0f), glm::vec3(15, -4, 9));
model = glm::translate(model, backpackTranslate);
backpackShader.use();
backpackShader.setMat4("model", model);
backpackShader.setMat4("view", view);
backpackShader.setMat4("projection", projection);
backpackModel.Draw(backpackShader);
// draw skybox as last
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
skyboxShader.use();
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection);
// skybox cube
glBindVertexArray(skyboxVAO);
model = glm::translate(model, lightPos);
cubeShader.use();
cubeShader.setMat4("projection", projection);
cubeShader.setMat4("view", view);
cubeShader.setMat4("model", model);
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS); // set depth function back to default
// pointLightPositions[2] = spaceshipTranslate;
// światła (kostki)
for (unsigned int i = 0; i < pointLightPositionsNum; i++)
{
model = glm::translate(glm::mat4(1.0f), pointLightPositions[i]);
model = glm::scale(model, glm::vec3(0.2f));
cubeShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// skybox
view = glm::mat4(glm::mat3(camera.GetViewMatrix()));
skyboxShader.use();
skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection);
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDepthFunc(GL_LEQUAL);
// glDrawArrays(GL_TRIANGLES, 0, 36);
glDepthFunc(GL_LESS);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
@ -345,17 +439,22 @@ void processInput(GLFWwindow *window)
camera.ProcessKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
backpackTranslate.x += 0.2f;
spaceshipTranslate.x += 0.02f;
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
backpackTranslate.x -= 0.2f;
spaceshipTranslate.x -= 0.02f;
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
backpackTranslate.y -= 0.2f;
spaceshipTranslate.y -= 0.02f;
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
backpackTranslate.y += 0.2f;
spaceshipTranslate.y += 0.02f;
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
backpackTranslate.z += 0.1f;
spaceshipTranslate.z += 0.01f;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
backpackTranslate.z -= 0.1f;
spaceshipTranslate.z -= 0.01f;
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
std::cout << spaceshipTranslate.x << " " << spaceshipTranslate.y << " " << spaceshipTranslate.z << std::endl;
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
spaceshipTranslate = glm::vec3(1.0f);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes