Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Engine::Engine()
{
m_last_time = std::chrono::high_resolution_clock::now();

mp_camera = std::make_shared<Camera>();
mp_player = std::make_shared<Player>(mp_camera);
mp_main_camera = std::make_shared<Camera>();
mp_controller = std::make_shared<Controller>(mp_main_camera);
mp_config = std::make_shared<Config>();

mp_window = std::make_unique<Window>(mp_config, *mp_player.get());
mp_window = std::make_unique<Window>(mp_config, *mp_controller.get());

mp_renderer = std::make_shared<Renderer>(mp_window->getHandle(), mp_config->width, mp_config->height);

Expand Down Expand Up @@ -80,6 +80,16 @@ const std::shared_ptr<GameObject> Engine::CreateGameObject(const std::string& ob
return go;
}

void Engine::BindKeyToFunc(const int& key, std::function<void()>& func)
{
mp_controller->SetKeyToFunc(key, func);
}

const std::shared_ptr<Camera> Engine::GetMainCamera()
{
return mp_main_camera;
}

const bool& Engine::shouldClose()
{
return glfwWindowShouldClose(&mp_window->getHandle());
Expand All @@ -92,23 +102,22 @@ void Engine::update()
std::chrono::steady_clock::time_point current_time = std::chrono::high_resolution_clock::now();
float delta_time = std::chrono::duration<float, std::chrono::seconds::period>(current_time - m_last_time).count();

mp_player->setDeltaTime(delta_time);
mp_player->updatePosition();
mp_controller->Update(delta_time);

m_last_time = current_time;

const int frame = mp_renderer->getFrameIndex();
if (mp_scene->isUpdate(frame))
const int32_t frame = mp_renderer->AcquireNextImage();
if (frame != -1 && (mp_scene->isUpdate(frame) || mp_renderer->IsUpdated(frame)))
{
mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame));

mp_scene->render(mp_renderer->getCommandBuffer(frame), frame);

mp_renderer->endRecordCommandBuffers(mp_renderer->getCommandBuffer(frame));

mp_renderer->SetUpdate(frame);
}

mp_camera->updateUBO(static_cast<float>(mp_config->width), static_cast<float>(mp_config->height));
mp_scene->updateUBO(mp_camera, mp_renderer);
mp_main_camera->UpdateUBO(static_cast<float>(mp_config->width), static_cast<float>(mp_config->height));
mp_scene->updateUBO(mp_main_camera, mp_renderer);

//std::this_thread::sleep_for(std::chrono::nanoseconds(500));//delete when not streaming
}
Expand Down
10 changes: 8 additions & 2 deletions Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Engine
const std::shared_ptr<GameObject> CreateGameObject();
const std::shared_ptr<GameObject> CreateGameObject(const std::string& object_file);

// CONTROLLER
void BindKeyToFunc(const int& key, std::function<void()>& func);

// CAMERA
const std::shared_ptr<Camera> GetMainCamera();

const bool& shouldClose();

private:
Expand All @@ -35,8 +41,8 @@ class Engine
std::shared_ptr<Renderer> mp_renderer;
std::shared_ptr<Scene> mp_scene;

std::shared_ptr<Camera> mp_camera;
std::shared_ptr<Player> mp_player;
std::shared_ptr<Camera> mp_main_camera;
std::shared_ptr<Controller> mp_controller;
std::shared_ptr<Config> mp_config;

std::chrono::steady_clock::time_point m_last_time;
Expand Down
4 changes: 2 additions & 2 deletions Engine/Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="cameras\Camera.cpp" />
<ClCompile Include="cameras\Player.cpp" />
<ClCompile Include="cameras\Controller.cpp" />
<ClCompile Include="Engine.cpp" />
<ClCompile Include="Logger.cpp" />
<ClCompile Include="renderer\Renderer.cpp" />
Expand All @@ -67,7 +67,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="cameras\Camera.h" />
<ClInclude Include="cameras\Player.h" />
<ClInclude Include="cameras\Controller.h" />
<ClInclude Include="Engine.h" />
<ClInclude Include="errors.h" />
<ClInclude Include="graphics\Config.h" />
Expand Down
4 changes: 2 additions & 2 deletions Engine/Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<ClCompile Include="world\Texture.cpp">
<Filter>Engine\World</Filter>
</ClCompile>
<ClCompile Include="cameras\Player.cpp">
<ClCompile Include="cameras\Controller.cpp">
<Filter>Engine\Cameras</Filter>
</ClCompile>
<ClCompile Include="Window.cpp">
Expand Down Expand Up @@ -152,7 +152,7 @@
<ClInclude Include="world\Texture.h">
<Filter>Engine\World</Filter>
</ClInclude>
<ClInclude Include="cameras\Player.h">
<ClInclude Include="cameras\Controller.h">
<Filter>Engine\Cameras</Filter>
</ClInclude>
<ClInclude Include="graphics\Vertex.h">
Expand Down
14 changes: 14 additions & 0 deletions Engine/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ int main()
room->bindMatToMesh(0, room_texture);
room->setPosition({ 3.0f, 0.0f, 0.0f });

std::shared_ptr<Material> room2_texture = engine.CreateMaterial(TSHADER::NO_TEXTURE);
std::shared_ptr<GameObject> room2 = engine.CreateGameObject();
room2->loadMesh("assets/models/viking_room.obj");
room2->bindMatToMesh(0, room2_texture);
room2->setPosition({ 6.0f, 0.0f, 0.0f });

std::shared_ptr<Scene> scene = std::make_shared<Scene>();
scene->addGameObject(room);

engine.setScene(scene);

std::function<void()> my_func = []() {std::clog << "Test d'un key bind" << std::endl; };
engine.BindKeyToFunc(GLFW_KEY_Q, my_func);

int init = 0;
// running loop
do
{
if (init++ == 30000)
{
scene->addGameObject(room2);
}

engine.update();
engine.draw();
} while (!engine.shouldClose());
Expand Down
12 changes: 6 additions & 6 deletions Engine/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@



Window::Window(std::shared_ptr<Config>& config, Player& player)
Window::Window(std::shared_ptr<Config>& config, Controller& controller)
{
glfwInit();

Expand All @@ -12,7 +12,7 @@ Window::Window(std::shared_ptr<Config>& config, Player& player)
m_handle = glfwCreateWindow(config->width, config->height, "Vulkan", nullptr, nullptr);

glfwSetInputMode(m_handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(m_handle, &player);
glfwSetWindowUserPointer(m_handle, &controller);
glfwSetKeyCallback(m_handle, key_callback);
glfwSetCursorPosCallback(m_handle, mouse_callback);
glfwSetCursorPos(m_handle, 0.0, 0.0);
Expand All @@ -27,9 +27,9 @@ Window::~Window()

void Window::key_callback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
Player* pPlayer = static_cast<Player*>(glfwGetWindowUserPointer(window));
Controller* pController = static_cast<Controller*>(glfwGetWindowUserPointer(window));

pPlayer->setInput(static_cast<int32_t>(key), static_cast<int32_t>(scancode), static_cast<int32_t>(mods), static_cast<int32_t>(action));
pController->setInput(static_cast<int32_t>(key), static_cast<int32_t>(scancode), static_cast<int32_t>(mods), static_cast<int32_t>(action));

if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
Expand All @@ -39,9 +39,9 @@ void Window::key_callback(GLFWwindow * window, int key, int scancode, int action

void Window::mouse_callback(GLFWwindow * window, double xpos, double ypos)
{
Player* pPlayer = static_cast<Player*>(glfwGetWindowUserPointer(window));
Controller* pController = static_cast<Controller*>(glfwGetWindowUserPointer(window));

pPlayer->updateRotation(xpos, ypos);
pController->updateRotation(xpos, ypos);

glfwSetCursorPos(window, 0.0, 0.0);
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#include <memory>

#include "graphics/Config.h"
#include "cameras/Player.h"
#include "cameras/Controller.h"

class Window
{
public:
Window(std::shared_ptr<Config>& config, Player& player);
Window(std::shared_ptr<Config>& config, Controller& controller);
~Window();

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
Expand Down
77 changes: 71 additions & 6 deletions Engine/cameras/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Camera::Camera()

m_position = { 0.0f, 0.0f, 0.0f };
m_rotation = { 0.0f, 0.0f, 0.0f };
m_delta_position = { 0.f, 0.f, 0.f };

m_ubo.model = {};
m_ubo.proj = {};
Expand All @@ -23,30 +24,94 @@ Camera::~Camera()
{
}

void Camera::updateUBO(const float& width, const float& height)
void Camera::UpdateUBO(const float& width, const float& height)
{
m_ubo.view = updateView(m_pitch, m_yaw, m_position);
//m_ubo.model = createModelMatrix(glm::vec3(0.f, 0.f, 0.f), m_position); // not used
m_ubo.proj = createProjMatrix(width, height);
m_ubo.proj[1][1] *= -1;
}

void Camera::setPosition(const glm::vec3& position)
void Camera::UpdatePosition(const float& dt)
{
m_position += m_delta_position * dt;

m_delta_position = glm::vec3{ 0.f, 0.f, 0.f };
}

void Camera::MoveForward(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.x -= -glm::sin(m_yaw) * speed;
change.z -= glm::cos(m_yaw) * speed;

m_delta_position += change;
}

void Camera::MoveBackward(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.x += -glm::sin(m_yaw) * speed;
change.z += glm::cos(m_yaw) * speed;

m_delta_position += change;
}

void Camera::MoveRight(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.x -= -glm::sin(m_yaw + glm::radians(90.0f)) * speed;
change.z -= glm::cos(m_yaw + glm::radians(90.0f)) * speed;

m_delta_position += change;
}

void Camera::MoveLeft(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.x += -glm::sin(m_yaw + glm::radians(90.0f)) * speed;
change.z += glm::cos(m_yaw + glm::radians(90.0f)) * speed;

m_delta_position += change;
}

void Camera::MoveUp(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.y += 1.0f * speed;

m_delta_position += change;
}

void Camera::MoveDown(const float& speed)
{
glm::vec3 change = { 0.0f, 0.0f, 0.0f };

change.y -= 1.0f * speed;

m_delta_position += change;
}

void Camera::SetPosition(const glm::vec3& position)
{
m_position = position;
}

const glm::vec3& Camera::getPosition()
const glm::vec3& Camera::GetPosition()
{
return m_position;
}

void Camera::setRotation(const glm::vec3& rotation)
void Camera::SetRotation(const glm::vec3& rotation)
{
m_rotation = rotation;
}

const glm::vec3& Camera::getRotation()
const glm::vec3& Camera::GetRotation()
{
return m_rotation;
}
Expand Down
19 changes: 14 additions & 5 deletions Engine/cameras/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ class Camera
Camera();
~Camera();

void updateUBO(const float& width, const float& height);
void UpdateUBO(const float& width, const float& height);
void UpdatePosition(const float& dt);

void setPosition(const glm::vec3& position);
const glm::vec3& getPosition();
void MoveForward(const float& speed);
void MoveBackward(const float& speed);
void MoveRight(const float& speed);
void MoveLeft(const float& speed);
void MoveUp(const float& speed);
void MoveDown(const float& speed);

void setRotation(const glm::vec3& rotation);
const glm::vec3& getRotation();
void SetPosition(const glm::vec3& position);
const glm::vec3& GetPosition();

void SetRotation(const glm::vec3& rotation);
const glm::vec3& GetRotation();

void setPitch(const float& pitch);
const float& getPitch();
Expand All @@ -36,6 +44,7 @@ class Camera

glm::vec3 m_position;
glm::vec3 m_rotation;
glm::vec3 m_delta_position;

float m_yaw;
float m_pitch;
Expand Down
Loading