add: added point light shader and light object shader
This commit is contained in:
parent
d1b444bcef
commit
0337c70e26
3 changed files with 68 additions and 16 deletions
63
main.cpp
63
main.cpp
|
@ -84,19 +84,31 @@ int main()
|
||||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||||
ImGui_ImplOpenGL3_Init("#version 330 core");
|
ImGui_ImplOpenGL3_Init("#version 330 core");
|
||||||
|
|
||||||
|
// Shaders
|
||||||
Shader ourShader("resources/shaders/vert.glsl", "resources/shaders/frag.glsl");
|
Shader ourShader("resources/shaders/vert.glsl", "resources/shaders/frag.glsl");
|
||||||
Shader directional_lightShader("resources/shaders/vert.glsl", "resources/shaders/directional_light.glsl");
|
Shader directional_lightShader("resources/shaders/vert.glsl", "resources/shaders/directional_light.glsl");
|
||||||
|
Shader point_lightShader("resources/shaders/vert.glsl", "resources/shaders/point_light.glsl");
|
||||||
|
Shader light_cubeShader("resources/shaders/vert.glsl", "resources/shaders/light_object.glsl");
|
||||||
|
|
||||||
|
// Models
|
||||||
Model croissant("resources/models/croissant/source/croissant.obj");
|
Model croissant("resources/models/croissant/source/croissant.obj");
|
||||||
Model cube("resources/models/cube/cube.obj");
|
Model cube("resources/models/cube/cube.obj");
|
||||||
|
|
||||||
|
// Objects
|
||||||
Object object1(croissant, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
Object object1(croissant, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
Object object2(croissant, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
Object object2(croissant, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
Object object3(croissant, glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
Object object3(croissant, glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
|
Object light_cube(cube, glm::vec3(1.2f, 1.0f, 2.0f), glm::vec3(0.05f, 0.05f, 0.05f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
|
|
||||||
static float positionX = 0.0f;
|
static float positionX = 0.0f;
|
||||||
static float positionY = 0.0f;
|
static float positionY = 0.0f;
|
||||||
static float positionZ = 0.0f;
|
static float positionZ = 0.0f;
|
||||||
|
|
||||||
|
static float lightCubePositionX = 1.2f;
|
||||||
|
static float lightCubePositionY = 1.0f;
|
||||||
|
static float lightCubePositionZ = 2.0f;
|
||||||
|
static float* light_color = new float[3]{1.0f, 1.0f, 1.0f};
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window))
|
while(!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
float currentFrame = glfwGetTime();
|
float currentFrame = glfwGetTime();
|
||||||
|
@ -121,43 +133,66 @@ int main()
|
||||||
|
|
||||||
ImGui::Begin("Settings");
|
ImGui::Begin("Settings");
|
||||||
ImGui::Text("FPS: %.2f", 1.0f / deltaTime);
|
ImGui::Text("FPS: %.2f", 1.0f / deltaTime);
|
||||||
ImGui::Text("Mouse Captured: %s", mouseCaptured ? "Yes" : "No");
|
|
||||||
ImGui::Text("ImGui want capture mouse: %s", io.WantCaptureMouse ? "Yes" : "No");
|
|
||||||
ImGui::Text("ImGui mouse position: (%.2f, %.2f)", io.MousePos.x, io.MousePos.y);
|
|
||||||
ImGui::Text("GLFW mouse position: (%.2f, %.2f)", mouseX, mouseY);
|
|
||||||
ImGui::SliderFloat("Object 1 Position X", &positionX, -5.0f, 5.0f);
|
ImGui::SliderFloat("Object 1 Position X", &positionX, -5.0f, 5.0f);
|
||||||
ImGui::SliderFloat("Object 1 Position Y", &positionY, -5.0f, 5.0f);
|
ImGui::SliderFloat("Object 1 Position Y", &positionY, -5.0f, 5.0f);
|
||||||
ImGui::SliderFloat("Object 1 Position Z", &positionZ, -5.0f, 5.0f);
|
ImGui::SliderFloat("Object 1 Position Z", &positionZ, -5.0f, 5.0f);
|
||||||
|
ImGui::SliderFloat("Light Cube Position X", &lightCubePositionX, -5.0f, 5.0f);
|
||||||
|
ImGui::SliderFloat("Light Cube Position Y", &lightCubePositionY, -5.0f, 5.0f);
|
||||||
|
ImGui::SliderFloat("Light Cube Position Z", &lightCubePositionZ, -5.0f, 5.0f);
|
||||||
|
ImGui::ColorEdit3("Light Cube Color", light_color);
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
glm::mat4 view = camera.GetViewMatrix();
|
||||||
|
glm::mat4 projection = glm::perspective(glm::radians(45.0f), float(width) / float(height), 0.1f, 100.0f);
|
||||||
|
|
||||||
// Bacis lighting
|
// Bacis lighting
|
||||||
ourShader.use();
|
ourShader.setVec3("viewPos", camera.Position);
|
||||||
ourShader.setVec3("viewPos", camera.Position);
|
|
||||||
ourShader.setVec3("light.position", 1.2f, 1.0f, 2.0f);
|
ourShader.setVec3("light.position", 1.2f, 1.0f, 2.0f);
|
||||||
ourShader.setVec3("light.color", 1.0f, 1.0f, 1.0f);
|
ourShader.setVec3("light.color", 1.0f, 1.0f, 1.0f);
|
||||||
ourShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
|
ourShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
|
||||||
ourShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
|
ourShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
|
||||||
ourShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
|
ourShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
|
||||||
|
ourShader.setMat4("view", view);
|
||||||
|
ourShader.setMat4("projection", projection);
|
||||||
|
|
||||||
// Directional light
|
// Directional light
|
||||||
directional_lightShader.use();
|
|
||||||
directional_lightShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);
|
directional_lightShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);
|
||||||
directional_lightShader.setVec3("light.color", 1.0f, 1.0f, 1.0f);
|
directional_lightShader.setVec3("light.color", 1.0f, 1.0f, 1.0f);
|
||||||
directional_lightShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
|
directional_lightShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
|
||||||
directional_lightShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
|
directional_lightShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
|
||||||
directional_lightShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
|
directional_lightShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
|
||||||
|
directional_lightShader.setMat4("view", view);
|
||||||
|
directional_lightShader.setMat4("projection", projection);
|
||||||
|
|
||||||
glm::mat4 view = camera.GetViewMatrix();
|
// Point light
|
||||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), float(width) / float(height), 0.1f, 100.0f);
|
point_lightShader.use();
|
||||||
ourShader.setMat4("view", view);
|
point_lightShader.setVec3("viewPos", camera.Position);
|
||||||
ourShader.setMat4("projection", projection);
|
point_lightShader.setVec3("light.position", lightCubePositionX, lightCubePositionY, lightCubePositionZ);
|
||||||
|
point_lightShader.setVec3("light.color", light_color[0], light_color[1], light_color[2]);
|
||||||
|
point_lightShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
|
||||||
|
point_lightShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
|
||||||
|
point_lightShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
|
||||||
|
point_lightShader.setFloat("light.constant", 1.0f);
|
||||||
|
point_lightShader.setFloat("light.linear", 0.09f);
|
||||||
|
point_lightShader.setFloat("light.quadratic", 0.032f);
|
||||||
|
point_lightShader.setMat4("view", view);
|
||||||
|
point_lightShader.setMat4("projection", projection);
|
||||||
|
|
||||||
object1.setPosition(glm::vec3(positionX, positionY, positionZ));
|
object1.setPosition(glm::vec3(positionX, positionY, positionZ));
|
||||||
object1.Draw(directional_lightShader, deltaTime);
|
object1.Draw(point_lightShader, deltaTime);
|
||||||
object2.setAcceleration(glm::vec3(0.0f, 0.001f, 0.0f));
|
object2.setAcceleration(glm::vec3(0.0f, 0.001f, 0.0f));
|
||||||
object2.Draw(directional_lightShader, deltaTime);
|
object2.Draw(point_lightShader, deltaTime);
|
||||||
object3.setAcceleration(glm::vec3(0.0f, -0.001f, 0.0f));
|
object3.setAcceleration(glm::vec3(0.0f, -0.001f, 0.0f));
|
||||||
object3.Draw(directional_lightShader, deltaTime);
|
object3.Draw(point_lightShader, deltaTime);
|
||||||
|
|
||||||
|
// Light cube shader
|
||||||
|
light_cubeShader.use();
|
||||||
|
light_cubeShader.setMat4("view", view);
|
||||||
|
light_cubeShader.setMat4("projection", projection);
|
||||||
|
light_cubeShader.setVec3("lightColor", light_color[0], light_color[1], light_color[2]);
|
||||||
|
|
||||||
|
light_cube.setPosition(glm::vec3(lightCubePositionX, lightCubePositionY, lightCubePositionZ));
|
||||||
|
light_cube.Draw(light_cubeShader, deltaTime);
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
|
9
resources/shaders/light_object.glsl
Normal file
9
resources/shaders/light_object.glsl
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform vec3 lightColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(lightColor, 1.0f);
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ struct Light {
|
||||||
vec3 ambient;
|
vec3 ambient;
|
||||||
vec3 diffuse;
|
vec3 diffuse;
|
||||||
vec3 specular;
|
vec3 specular;
|
||||||
|
vec3 color;
|
||||||
|
|
||||||
float constant;
|
float constant;
|
||||||
float linear;
|
float linear;
|
||||||
|
@ -23,13 +24,16 @@ uniform Light light;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
float distance = length(light.position - FragPos);
|
||||||
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||||
|
|
||||||
// ambient// ambient
|
// ambient// ambient
|
||||||
float ambientStrength = 0.1;
|
float ambientStrength = 0.1;
|
||||||
vec3 ambient = ambientStrength * light.color;
|
vec3 ambient = ambientStrength * light.color;
|
||||||
|
|
||||||
// diffuse
|
// diffuse
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 lightDir = normalize(-light.direction);
|
vec3 lightDir = normalize(light.position - FragPos);
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
vec3 diffuse = diff * light.color;
|
vec3 diffuse = diff * light.color;
|
||||||
|
|
||||||
|
@ -40,6 +44,10 @@ void main()
|
||||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||||
vec3 specular = specularStrength * spec * light.color;
|
vec3 specular = specularStrength * spec * light.color;
|
||||||
|
|
||||||
|
ambient *= attenuation;
|
||||||
|
diffuse *= attenuation;
|
||||||
|
specular *= attenuation;
|
||||||
|
|
||||||
vec3 result = ambient + diffuse + specular;
|
vec3 result = ambient + diffuse + specular;
|
||||||
vec4 light = vec4(result, 1.0);
|
vec4 light = vec4(result, 1.0);
|
||||||
FragColor = texture(texture_diffuse1, TexCoords) * light;
|
FragColor = texture(texture_diffuse1, TexCoords) * light;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue