Started working on lighting
This commit is contained in:
parent
5e086c884f
commit
9007d33028
|
@ -1,20 +0,0 @@
|
||||||
#version 460 core
|
|
||||||
|
|
||||||
in vec3 VertexColor;
|
|
||||||
in vec2 TexCoord;
|
|
||||||
|
|
||||||
out vec4 fragColor;
|
|
||||||
|
|
||||||
uniform sampler2D texture1;
|
|
||||||
uniform sampler2D texture2;
|
|
||||||
|
|
||||||
uniform float t;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
fragColor = mix(
|
|
||||||
texture(texture1, TexCoord + vec2(t, 0)),
|
|
||||||
texture(texture2, TexCoord + vec2(0, t)), 0.5) * vec4(VertexColor,
|
|
||||||
1.0
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
#version 460 core
|
|
||||||
|
|
||||||
out vec4 fragmentColor;
|
|
||||||
|
|
||||||
in vec2 textureCoords;
|
|
||||||
in vec3 vertexCoords;
|
|
||||||
|
|
||||||
uniform sampler2D lauch;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
fragmentColor = texture(lauch, textureCoords);
|
|
||||||
}
|
|
10
assets/shaders/singleColor.frag
Normal file
10
assets/shaders/singleColor.frag
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
uniform vec3 color;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fragColor = vec4(color, 1.0f);
|
||||||
|
}
|
|
@ -1,19 +1,12 @@
|
||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aCol;
|
|
||||||
layout (location = 2) in vec2 aTex;
|
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
|
|
||||||
out vec3 VertexColor;
|
|
||||||
out vec2 TexCoord;
|
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||||
VertexColor = aCol;
|
|
||||||
TexCoord = aTex;
|
|
||||||
}
|
}
|
12
assets/shaders/textured.frag
Normal file
12
assets/shaders/textured.frag
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
in vec2 vertexUV;
|
||||||
|
|
||||||
|
out vec4 fragmentColor;
|
||||||
|
|
||||||
|
uniform sampler2D modelTexture;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fragmentColor = texture(modelTexture, vertexUV);
|
||||||
|
}
|
|
@ -3,16 +3,15 @@
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec2 aTex;
|
layout (location = 1) in vec2 aTex;
|
||||||
|
|
||||||
|
out vec2 vertexUV;
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
|
|
||||||
out vec3 vertexCoords;
|
|
||||||
out vec2 textureCoords;
|
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||||
textureCoords = aTex;
|
|
||||||
vertexCoords = aPos;
|
vertexUV = aTex;
|
||||||
}
|
}
|
|
@ -3,13 +3,14 @@ set(VENDOR ${CMAKE_SOURCE_DIR}/vendor)
|
||||||
|
|
||||||
add_executable(example
|
add_executable(example
|
||||||
"main.cpp"
|
"main.cpp"
|
||||||
)
|
"objects/lighting/PointLight.cpp")
|
||||||
|
|
||||||
file(GLOB SOURCE_FILES
|
file(GLOB SOURCE_FILES
|
||||||
"${VENDOR}/src/*.c"
|
"${VENDOR}/src/*.c"
|
||||||
"${VENDOR}/src/*.cpp"
|
"${VENDOR}/src/*.cpp"
|
||||||
"objects/*.cpp"
|
"objects/*.cpp"
|
||||||
"objects/primitives/*.cpp"
|
"objects/primitives/*.cpp"
|
||||||
|
"objects/lighting/*.cpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(example PRIVATE
|
target_sources(example PRIVATE
|
||||||
|
@ -22,6 +23,7 @@ target_include_directories(example PRIVATE
|
||||||
${VENDOR}/include/imgui
|
${VENDOR}/include/imgui
|
||||||
objects
|
objects
|
||||||
objects/primitives
|
objects/primitives
|
||||||
|
objects/lighting
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(example
|
target_link_libraries(example
|
||||||
|
|
32
src/main.cpp
32
src/main.cpp
|
@ -13,6 +13,7 @@
|
||||||
#include <imgui/imgui_impl_glfw.h>
|
#include <imgui/imgui_impl_glfw.h>
|
||||||
#include <imgui/imgui_impl_opengl3.h>
|
#include <imgui/imgui_impl_opengl3.h>
|
||||||
|
|
||||||
|
#include "lighting/PointLight.hpp"
|
||||||
#include "Camera.hpp"
|
#include "Camera.hpp"
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
#include "Texture.hpp"
|
#include "Texture.hpp"
|
||||||
|
@ -154,13 +155,12 @@ int main(int argc, char** argv)
|
||||||
glViewport(0, 0, 800, 800);
|
glViewport(0, 0, 800, 800);
|
||||||
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
|
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
|
||||||
|
|
||||||
Shader cubeShader("cube.vert", "cube.frag");
|
|
||||||
cubeShader.Use();
|
|
||||||
|
|
||||||
glm::mat4 perspective = glm::mat4(1.0f);
|
glm::mat4 perspective = glm::mat4(1.0f);
|
||||||
|
|
||||||
|
Shader singleColorShader("singleColor.vert", "singleColor.frag");
|
||||||
|
Shader texturedShader("textured.vert", "textured.frag");
|
||||||
|
|
||||||
Texture cubeTexture("crate.jpg");
|
Texture cubeTexture("crate.jpg");
|
||||||
cubeShader.SetUniformInt("lauch", 0);
|
|
||||||
|
|
||||||
glm::vec3 cubePositions[] = {
|
glm::vec3 cubePositions[] = {
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||||
|
@ -184,6 +184,8 @@ int main(int argc, char** argv)
|
||||||
cubes.push_back(newCube);
|
cubes.push_back(newCube);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PointLight lightSource(glm::vec3(0.0f, 3.0f, 0.0f), glm::vec3(0.5f, 0.1f, 0.1f), 1.0f);
|
||||||
|
|
||||||
// A camera
|
// A camera
|
||||||
Camera cam(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec2(0.0f, -90.0f));
|
Camera cam(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec2(0.0f, -90.0f));
|
||||||
|
|
||||||
|
@ -212,7 +214,9 @@ int main(int argc, char** argv)
|
||||||
static float zNear = 0.1f;
|
static float zNear = 0.1f;
|
||||||
static float zFar = 100.0f;
|
static float zFar = 100.0f;
|
||||||
|
|
||||||
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
static float bgColor[3] = { 0.1f, 0.4f, 0.1f };
|
||||||
|
|
||||||
|
glClearColor(bgColor[0], bgColor[1], bgColor[2], 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
@ -235,17 +239,27 @@ int main(int argc, char** argv)
|
||||||
ImGui::SliderFloat("Sensitivity", &sensitivity, 0.01, 10.0f);
|
ImGui::SliderFloat("Sensitivity", &sensitivity, 0.01, 10.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ImGui::CollapsingHeader("Scene"))
|
||||||
|
{
|
||||||
|
ImGui::ColorPicker3("Background", bgColor);
|
||||||
|
}
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar);
|
perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar);
|
||||||
|
|
||||||
cubeTexture.Bind();
|
cubeTexture.Bind();
|
||||||
cubeShader.Use();
|
|
||||||
cubeShader.SetUniformMat4("projection", &perspective[0][0]);
|
|
||||||
cam.Use(cubeShader);
|
|
||||||
|
|
||||||
|
texturedShader.Use();
|
||||||
|
texturedShader.SetUniformMat4("projection", &perspective[0][0]);
|
||||||
|
cam.Use(texturedShader);
|
||||||
for (Cube* cube : cubes)
|
for (Cube* cube : cubes)
|
||||||
cube->Draw(cubeShader);
|
cube->Draw(texturedShader);
|
||||||
|
|
||||||
|
singleColorShader.Use();
|
||||||
|
singleColorShader.SetUniformMat4("projection", &perspective[0][0]);
|
||||||
|
cam.Use(singleColorShader);
|
||||||
|
lightSource.Draw(singleColorShader);
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
|
|
@ -91,7 +91,7 @@ Shader::~Shader()
|
||||||
glDeleteProgram(program);
|
glDeleteProgram(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::Use()
|
void Shader::Use() const
|
||||||
{
|
{
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
}
|
}
|
||||||
|
@ -106,6 +106,11 @@ void Shader::SetUniformFloat(const char* uniform, float value) const
|
||||||
glUniform1f(glGetUniformLocation(program, uniform), value);
|
glUniform1f(glGetUniformLocation(program, uniform), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Shader::SetUniformFloat3(const char* uniform, const float* ptrToVec3) const
|
||||||
|
{
|
||||||
|
glUniform3f(glGetUniformLocation(program, uniform), ptrToVec3[0], ptrToVec3[1], ptrToVec3[2]);
|
||||||
|
}
|
||||||
|
|
||||||
void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix) const
|
void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix) const
|
||||||
{
|
{
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix);
|
glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix);
|
||||||
|
|
|
@ -11,10 +11,11 @@ public:
|
||||||
Shader(const char* vertShader, const char* fragShader);
|
Shader(const char* vertShader, const char* fragShader);
|
||||||
~Shader();
|
~Shader();
|
||||||
|
|
||||||
void Use();
|
void Use() const;
|
||||||
|
|
||||||
void SetUniformInt(const char* uniform, int value) const;
|
void SetUniformInt(const char* uniform, int value) const;
|
||||||
void SetUniformFloat(const char* uniform, float value) const;
|
void SetUniformFloat(const char* uniform, float value) const;
|
||||||
|
void SetUniformFloat3(const char* uniform, const float* ptrToVec3) const;
|
||||||
void SetUniformMat4(const char* uniform, const float* ptrToMatrix) const;
|
void SetUniformMat4(const char* uniform, const float* ptrToMatrix) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
71
src/objects/lighting/PointLight.cpp
Normal file
71
src/objects/lighting/PointLight.cpp
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#include "PointLight.hpp"
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
unsigned int PointLight::VAO = 0;
|
||||||
|
unsigned int PointLight::VBO = 0;
|
||||||
|
unsigned int PointLight::EBO = 0;
|
||||||
|
|
||||||
|
const float PointLight::vertices[8 * 3] = {
|
||||||
|
-1.0f, 1.0f, 1.0f, // Front Top Left
|
||||||
|
1.0f, 1.0f, 1.0f, // Front Top Right
|
||||||
|
1.0f, -1.0f, 1.0f, // Front Bottom Right
|
||||||
|
-1.0f, -1.0f, 1.0f, // Front Bottom Left
|
||||||
|
-1.0f, 1.0f, -1.0f, // Back Top Left
|
||||||
|
1.0f, 1.0f, -1.0f, // Back Top Right
|
||||||
|
1.0f, -1.0f, -1.0f, // Back Bottom Right
|
||||||
|
-1.0f, -1.0f, -1.0f, // Back Bottom Left
|
||||||
|
};
|
||||||
|
|
||||||
|
const unsigned int PointLight::indices[6 * 2 * 3] = {
|
||||||
|
0, 1, 3, // Front
|
||||||
|
1, 2, 3,
|
||||||
|
4, 5, 7, // Back
|
||||||
|
5, 6, 7,
|
||||||
|
4, 5, 0, // Top
|
||||||
|
5, 1, 0,
|
||||||
|
7, 6, 3, // Bottom
|
||||||
|
6, 2, 3,
|
||||||
|
4, 0, 7, // Left
|
||||||
|
0, 3, 7,
|
||||||
|
1, 5, 2,
|
||||||
|
5, 6, 2
|
||||||
|
};
|
||||||
|
|
||||||
|
PointLight::PointLight(glm::vec3 position, glm::vec3 color, float intensity) :
|
||||||
|
position(position), color(color), intensity(intensity),
|
||||||
|
transformation(1.0f)
|
||||||
|
{
|
||||||
|
if (VAO == 0)
|
||||||
|
{
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
glGenBuffers(1, &EBO);
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
transformation = glm::translate(transformation, position);
|
||||||
|
transformation = glm::scale(transformation, glm::vec3(0.1f));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PointLight::Draw(const Shader& program)
|
||||||
|
{
|
||||||
|
program.SetUniformFloat3("color", &color[0]);
|
||||||
|
program.SetUniformMat4("model", &transformation[0][0]);
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
|
|
||||||
|
glDrawElements(GL_TRIANGLES, 3 * 6 * 2, GL_UNSIGNED_INT, 0);
|
||||||
|
}
|
25
src/objects/lighting/PointLight.hpp
Normal file
25
src/objects/lighting/PointLight.hpp
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "Shader.hpp"
|
||||||
|
|
||||||
|
class PointLight
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const float vertices[8 * 3];
|
||||||
|
static const unsigned int indices[6 * 2 * 3];
|
||||||
|
public:
|
||||||
|
PointLight(glm::vec3 position, glm::vec3 color, float intensity);
|
||||||
|
~PointLight() = default;
|
||||||
|
|
||||||
|
void Draw(const Shader& program);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static unsigned int VAO, VBO, EBO;
|
||||||
|
|
||||||
|
glm::vec3 position, color;
|
||||||
|
float intensity;
|
||||||
|
|
||||||
|
glm::mat4 transformation;
|
||||||
|
};
|
Loading…
Reference in a new issue