Skip to content

Commit

Permalink
Camera Movement, Color Selectors, Range-Based Point Lights and more (#21
Browse files Browse the repository at this point in the history
)

* Camera Movement fix, camera and environment tabs

* Color selectors, fixed shading and ranged point light
  • Loading branch information
fszewczyk authored Nov 6, 2024
1 parent 23c6ef4 commit 3369114
Show file tree
Hide file tree
Showing 46 changed files with 526 additions and 226 deletions.
47 changes: 26 additions & 21 deletions resources/shaders/fragment/uber.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
out vec4 FragColor; // Final fragment color

in vec3 FragPos; // Fragment position in world space
in vec3 Normal; // Normal in world space
in vec3 Normal; // normal in world space

uniform vec3 viewPos; // Camera position

struct PointLight {
vec3 position; // Light position
vec3 diffuse; // Diffuse light color
vec3 specular; // Specular light color
vec3 color; // Diffuse light color
float range; // Specular light color
};

struct DirectionalLight {
vec3 direction; // Light position
vec3 diffuse; // Diffuse light color
vec3 specular; // Specular light color
vec3 color; // Diffuse light color
};

uniform vec3 ambientLight;
Expand All @@ -38,33 +37,39 @@ uniform Material material;

void main() {
vec3 result = ambientLight * material.diffuse;
vec3 normal = normalize(Normal);

for (int i = 0; i < numPointLights; ++i) {
// Diffuse
vec3 lightDir = normalize(pointLights[i].position - FragPos);
float diff = max(dot(Normal, lightDir), 0.0);
vec3 diffuse = pointLights[i].diffuse * diff * material.diffuse;

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, Normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = pointLights[i].specular * spec * material.specular;

result += diffuse + specular;
float relativeIntensity = 1.0 - (length(pointLights[i].position - FragPos) /
pointLights[i].range);

if (relativeIntensity > 0) {
// Diffuse
vec3 lightDir = normalize(pointLights[i].position - FragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = pointLights[i].color * diff * material.diffuse;

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = pointLights[i].color * spec * material.specular;

result += relativeIntensity * (diffuse + specular);
}
}

for (int i = 0; i < numDirectionalLights; ++i) {
// Diffuse
vec3 lightDir = directionalLights[i].direction;
float diff = max(dot(Normal, lightDir), 0.0);
vec3 diffuse = directionalLights[i].diffuse * diff * material.diffuse;
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = directionalLights[i].color * diff * material.diffuse;

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, Normal);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = directionalLights[i].specular * spec * material.specular;
vec3 specular = directionalLights[i].color * spec * material.specular;

result += diffuse + specular;
}
Expand Down
6 changes: 0 additions & 6 deletions src/AssetManager/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include "stb_image.h"
#include "stb_image_write.h"

#define GL_SILENCE_DEPRECATION
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif
#include <glad/glad.h>

#include <AssetManager/Image.hpp>

namespace shkyera {
Expand Down
3 changes: 3 additions & 0 deletions src/AssetManager/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Image : public Asset {
*/
Image(const std::string& path);

Image(const Image& other) = delete;
Image& operator=(const Image& other) = delete;

/**
* @brief Load an image from a file.
*
Expand Down
4 changes: 2 additions & 2 deletions src/AssetManager/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ Mesh* Mesh::Factory::createCylinder() {
}

Mesh* Mesh::Factory::createSphere() {
const int stacks = 48;
const int sectors = 96;
const int stacks = 24;
const int sectors = 48;
const float radius = 1.0f;

std::vector<Vertex> vertices;
Expand Down
4 changes: 4 additions & 0 deletions src/AssetManager/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Mesh : public Asset {

Mesh(const std::string& filepath);
Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices);

Mesh(const Mesh& other) = delete;
Mesh& operator=(const Mesh& other) = delete;

~Mesh();

void bind() const { glBindVertexArray(_vao); }
Expand Down
2 changes: 2 additions & 0 deletions src/AssetManager/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Shader : public Asset {
};

Shader(const std::string& filepath, Type type);
Shader(const Shader& other) = delete;
Shader& operator=(const Shader& other) = delete;

~Shader();

Expand Down
4 changes: 4 additions & 0 deletions src/AssetManager/Wireframe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Wireframe : public Asset {

Wireframe(const std::string& filepath);
Wireframe(const std::vector<Edge>& edges);

Wireframe(const Wireframe& other) = delete;
Wireframe& operator=(const Wireframe& other) = delete;

~Wireframe();

void bind() const { glBindVertexArray(_vao); }
Expand Down
17 changes: 17 additions & 0 deletions src/Components/AmbientLightComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <iostream>

#include <glm/glm.hpp>
#include <Components/BaseComponent.hpp>

namespace shkyera {

class AmbientLightComponent : public BaseComponent<AmbientLightComponent> {
public:
float intensity = 1;
glm::vec3 color = {0.1, 0.1, 0.1};
};

} // namespace shkyera
1 change: 1 addition & 0 deletions src/Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(
${CMAKE_CURRENT_LIST_DIR}/WireframeComponent.hpp
${CMAKE_CURRENT_LIST_DIR}/PointLightComponent.hpp
${CMAKE_CURRENT_LIST_DIR}/DirectionalLightComponent.hpp
${CMAKE_CURRENT_LIST_DIR}/AmbientLightComponent.hpp
${CMAKE_CURRENT_LIST_DIR}/BoxColliderComponent.hpp
${CMAKE_CURRENT_LIST_DIR}/SkyboxComponent.hpp
)
Expand Down
6 changes: 3 additions & 3 deletions src/Components/CameraComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class CameraComponent {
glm::vec3 orientation = transformComponent.getOrientation();

glm::vec3 front;
front.x = cos(orientation.y) * cos(orientation.x);
front.y = sin(orientation.x);
front.z = sin(orientation.y) * cos(orientation.x);
front.x = std::cos(orientation.y) * std::cos(orientation.x);
front.y = std::sin(orientation.x);
front.z = std::sin(orientation.y) * std::cos(orientation.x);
front = glm::normalize(front);

glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
Expand Down
4 changes: 2 additions & 2 deletions src/Components/DirectionalLightComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace shkyera {

class DirectionalLightComponent : public BaseComponent<DirectionalLightComponent> {
public:
glm::vec3 diffuse = {0.5, 0.5, 0.5};
glm::vec3 specular = {0.5, 0.5, 0.5};
float intensity = 0.5;
glm::vec3 color = {1.0f, 1.0f, 1.0f};
};

} // namespace shkyera
5 changes: 3 additions & 2 deletions src/Components/PointLightComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace shkyera {

class PointLightComponent : public BaseComponent<PointLightComponent> {
public:
glm::vec3 diffuse = {0.5, 0.5, 0.5};
glm::vec3 specular = {0.5, 0.5, 0.5};
float intensity = 1;
float range = 5;
glm::vec3 color = {1, 1, 1};
};

} // namespace shkyera
1 change: 0 additions & 1 deletion src/Components/SkyboxComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace shkyera {

class SkyboxComponent : public BaseComponent<SkyboxComponent> {
public:
glm::vec3 ambientLight = {0.1, 0.1, 0.1};
CubeMap skyboxCubeMap;
};

Expand Down
8 changes: 5 additions & 3 deletions src/Components/TransformComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <Math/Utils.hpp>
#include <Components/BaseComponent.hpp>

namespace shkyera {
Expand All @@ -28,9 +30,9 @@ class TransformComponent : public BaseComponent<TransformComponent> {
void setScale(const glm::vec3& scale) { _scale = scale; }

glm::mat4 getRotationMatrix() const {
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(_orientation.y), glm::vec3(0.0f, 1.0f, 0.0f));
rotationMatrix = glm::rotate(rotationMatrix, glm::radians(_orientation.x), glm::vec3(1.0f, 0.0f, 0.0f));
rotationMatrix = glm::rotate(rotationMatrix, glm::radians(_orientation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), _orientation.y, glm::vec3(0.0f, 1.0f, 0.0f));
rotationMatrix = glm::rotate(rotationMatrix, _orientation.x, glm::vec3(1.0f, 0.0f, 0.0f));
rotationMatrix = glm::rotate(rotationMatrix, _orientation.z, glm::vec3(0.0f, 0.0f, 1.0f));
return rotationMatrix;
}

Expand Down
5 changes: 5 additions & 0 deletions src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ static constexpr Entity INITIAL_ENTITIES = 100;

Registry::Registry() : _entityProvider(INITIAL_ENTITIES) {
_camera = addEntity();
_environment = addEntity();
}

Entity Registry::addEntity() {
Expand Down Expand Up @@ -39,4 +40,8 @@ Entity Registry::getCamera() const {
return _camera;
}

Entity Registry::getEnvironment() const {
return _environment;
}

} // namespace shkyera
3 changes: 3 additions & 0 deletions src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class Registry {
}

Entity getCamera() const;

Entity getEnvironment() const;

private:
/**
Expand Down Expand Up @@ -189,6 +191,7 @@ class Registry {
EntityProvider _entityProvider; //< Manages the creation and management of entities.

Entity _camera;
Entity _environment;
std::unordered_set<Entity> _selectedEntities;
};

Expand Down
6 changes: 6 additions & 0 deletions src/InputManager/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ glm::vec2 InputManager::getMousePosition(CoordinateSystem system) {
return {0, 0};
}

bool InputManager::isMouseInside(CoordinateSystem system) {
const auto& mousePosition = getRelativeMousePosition(InputManager::CoordinateSystem::SCENE);
return 0.0f < mousePosition.x && 0.0f < mousePosition.y && mousePosition.x < 1.0f && mousePosition.y < 1.0f;
}


void InputManager::registerKeyCallback(Key key, std::function<void()> callback) {
_keyCallbacks[key].push_back(callback);
}
Expand Down
1 change: 1 addition & 0 deletions src/InputManager/InputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class InputManager{

glm::vec2 getRelativeMousePosition(CoordinateSystem system);
glm::vec2 getMousePosition(CoordinateSystem system);
bool isMouseInside(CoordinateSystem system);

void registerKeyCallback(Key key, std::function<void()> callback);

Expand Down
1 change: 1 addition & 0 deletions src/Math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(
STATIC

${CMAKE_CURRENT_LIST_DIR}/Box.cpp
${CMAKE_CURRENT_LIST_DIR}/Utils.cpp
)

target_include_directories(
Expand Down
16 changes: 16 additions & 0 deletions src/Math/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <Math/Utils.hpp>
#include <cmath>

namespace shkyera {

float toRadians(float degrees)
{
return degrees * M_PI / 180;
}

float toDegrees(float radians)
{
return radians * 180 / M_PI;
}

} // namespace shkyera
11 changes: 11 additions & 0 deletions src/Math/Utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <glm/glm.hpp>

namespace shkyera {

float toRadians(float degrees);

float toDegrees(float radians);

} // namespace shkyera
10 changes: 7 additions & 3 deletions src/Systems/CameraMovementSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace shkyera {

constexpr static float MOVEMENT_SPEED = 0.6;
constexpr static float MOUSE_SENSITIVITY = 0.05;
constexpr static float MOUSE_SENSITIVITY = 0.2;

CameraMovementSystem::CameraMovementSystem(std::shared_ptr<Registry> registry)
: _registry(registry), _cameraControl(false) {
Expand All @@ -18,9 +18,13 @@ void CameraMovementSystem::update() {}
void CameraMovementSystem::setupCameraMovement() {
auto& inputManager = InputManager::getInstance();

inputManager.registerMouseButtonDownCallback(GLFW_MOUSE_BUTTON_LEFT, [this]() {
_cameraControl = true;
inputManager.registerMouseButtonDownCallback(GLFW_MOUSE_BUTTON_LEFT, [this, &inputManager]() {
if(inputManager.isMouseInside(InputManager::CoordinateSystem::SCENE))
{
_cameraControl = true;
}
});

inputManager.registerMouseButtonUpCallback(GLFW_MOUSE_BUTTON_LEFT, [this]() {
_cameraControl = false;
});
Expand Down
Loading

0 comments on commit 3369114

Please sign in to comment.