diff --git a/.gitmodules b/.gitmodules index b44e47387fc30a41c3f5c8d09b5ad525b354f233..97fc7d9d689b41b55f2c3308a086f8f167869258 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/glfw"] path = lib/glfw url = https://github.com/glfw/glfw.git +[submodule "modules/camera/lib/glm"] + path = modules/camera/lib/glm + url = https://github.com/g-truc/glm.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 569efcf20fe84a64205b7060bbb89587cbe579da..ddcb8274be02c5265924bc9b69c39f788ba50132 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,12 +72,15 @@ endif() # add include directories from dependencies as system includes target_include_directories(vkcv SYSTEM BEFORE PRIVATE ${vkcv_includes}) +message(STATUS ${vkcv_includes}) # add the own include directory for public headers target_include_directories(vkcv BEFORE PUBLIC ${vkcv_include}) +message(STATUS ${vkcv_include}) # link the framework using all required libraries target_link_libraries(vkcv ${vkcv_libraries}) +message(STATUS ${vkcv_libraries}) # add sub-projects/examples as targets add_subdirectory(projects) diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index e2fc5c587247a915f5bca345f8077f33ee5d8977..66db4776d053352d8ccb2eea5e09c3fb77b68561 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -187,7 +187,7 @@ namespace vkcv * @brief render a beautiful triangle */ void renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle, - const int width, const int height); + const int width, const int height, const size_t pushConstantSize, const void* pushConstantData); /** * @brief end recording and present image diff --git a/include/vkcv/Event.hpp b/include/vkcv/Event.hpp index 092f271c025e63be80b994b6b3921795c3e99671..0836e836e84ff7dfc4931a7cedd65497bf9a89cf 100644 --- a/include/vkcv/Event.hpp +++ b/include/vkcv/Event.hpp @@ -34,8 +34,9 @@ namespace vkcv { * adds a function handle to the event to be called * @param handle of the function */ - void add(typename event_function<T...>::type handle) { + typename event_function<T...>::type add(typename event_function<T...>::type handle) { this->m_handles.push_back(handle); + return handle; } /** diff --git a/include/vkcv/Window.hpp b/include/vkcv/Window.hpp index bb8081c166360e97595ff6994971390e53d6aa32..7428c7c73eb481f7352821faed36257211dfd5bf 100644 --- a/include/vkcv/Window.hpp +++ b/include/vkcv/Window.hpp @@ -12,7 +12,7 @@ struct GLFWwindow; namespace vkcv { - + class Window final { private: GLFWwindow *m_window; @@ -32,6 +32,16 @@ namespace vkcv { */ static void onMouseMoveEvent(GLFWwindow *window, double x, double y); + /** + * mouseButton callback for mouse buttons + * @param[in] button The [mouse button](@ref buttons) that was pressed or released. + * @param[in] action One of `GLFW_PRESS` or `GLFW_RELEASE`. Future releases may add more actions. + * @param[in] mods Bit field describing which [modifier keys](@ref mods) were held down. + */ + static void onMouseButtonEvent(GLFWwindow *callbackWindow, int button, int action, int mods); + + static void onMouseScrollEvent(GLFWwindow *callbackWindow, double xoffset, double yoffset); + /** * resize callback for the resize option of the window * @param[in] window The window that was resized. @@ -81,7 +91,9 @@ namespace vkcv { /** * basic events of the window */ + event< int, int, int> e_mouseButton; event< double, double > e_mouseMove; + event< double, double > e_mouseScroll; event< int, int > e_resize; event< int, int, int, int > e_key; @@ -129,5 +141,5 @@ namespace vkcv { */ virtual ~Window(); }; - + } \ No newline at end of file diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 3309d860b300dd378460338d9fece48066c38993..e9911a597d036e1123628bfc50b2fb7718e7a58c 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -1,3 +1,4 @@ # Add new modules here: +add_subdirectory(camera) add_subdirectory(testing) diff --git a/modules/camera/CMakeLists.txt b/modules/camera/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..28080bf2b1cd3bbc88d6c13d7ef26a43d7c3e19a --- /dev/null +++ b/modules/camera/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.16) +project(vkcv_camera) + +# setting c++ standard for the project +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(vkcv_camera_source ${PROJECT_SOURCE_DIR}/src) +set(vkcv_camera_include ${PROJECT_SOURCE_DIR}/include) + +set(vkcv_camera_sources + ${vkcv_camera_include}/vkcv/camera/Camera.hpp + ${vkcv_camera_source}/vkcv/camera/Camera.cpp + ${vkcv_camera_include}/vkcv/camera/TrackballCamera.hpp + ${vkcv_camera_source}/vkcv/camera/TrackballCamera.cpp + ${vkcv_camera_include}/vkcv/camera/CameraManager.hpp + ${vkcv_camera_source}/vkcv/camera/CameraManager.cpp +) + +# adding source files to the project +add_library(vkcv_camera STATIC ${vkcv_camera_sources}) + +# Setup some path variables to load libraries +set(vkcv_camera_lib lib) +set(vkcv_camera_lib_path ${PROJECT_SOURCE_DIR}/${vkcv_camera_lib}) + +include(config/GLM.cmake) + +target_link_libraries(vkcv_camera PUBLIC ${vkcv_camera_libraries} vkcv) + +target_include_directories(vkcv_camera SYSTEM BEFORE PRIVATE ${vkcv_camera_includes} ${vkcv_include}) + +# add the own include directory for public headers +target_include_directories(vkcv_camera BEFORE PUBLIC ${vkcv_camera_include} ${vkcv_camera_includes}) + diff --git a/modules/camera/config/GLM.cmake b/modules/camera/config/GLM.cmake new file mode 100644 index 0000000000000000000000000000000000000000..c4d14392ef0ea24243a45b19cd8583d90d3267be --- /dev/null +++ b/modules/camera/config/GLM.cmake @@ -0,0 +1,15 @@ + +find_package(glm QUIET) + +if (glm_FOUND) + list(APPEND vkcv_camera_includes ${GLM_INCLUDE_DIRS}) + list(APPEND vkcv_camera_libraries glm) +else() + if (EXISTS "${vkcv_camera_lib_path}/glm") + add_subdirectory(${vkcv_camera_lib}/glm) + + list(APPEND vkcv_camera_libraries glm) + else() + message(WARNING "GLM is required..! Update the submodules!") + endif () +endif () diff --git a/modules/camera/include/vkcv/camera/Camera.hpp b/modules/camera/include/vkcv/camera/Camera.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7e177b9a2fbde0890e0c8ea6a1d9a19d6e277c7c --- /dev/null +++ b/modules/camera/include/vkcv/camera/Camera.hpp @@ -0,0 +1,101 @@ +#pragma once + +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/matrix_access.hpp> + +namespace vkcv { + + class Camera { + protected: + glm::mat4 m_view; + glm::mat4 m_projection; + + int m_width; + int m_height; + + float m_near; + float m_far; + float m_fov; + float m_ratio; + + glm::vec3 m_up; + glm::vec3 m_position; + float m_cameraSpeed; + float m_pitch; + float m_yaw; + + int m_fov_nsteps; + float m_fov_min; + float m_fov_max; + + bool m_forward; + bool m_backward; + bool m_left; + bool m_right; + + public: + Camera(); + + virtual ~Camera(); + + void setPerspective(float fov, float ratio, float near, float far); + + const glm::mat4 getView() const; + + void getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos); + + glm::mat4 updateView(double deltatime); + + void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up); + + const glm::mat4& getProjection() const; + + void setProjection(const glm::mat4 projection); + + void getNearFar(float &near, float &far) const; + + void setUp(const glm::vec3 &Up); + + float getFov() const; + + void setFov(float fov); + + void changeFov(double fov); + + void updateRatio(float ratio); + + float getRatio() const; + + void setNearFar(float near, float far); + + glm::vec3 getFront() const; + + glm::vec3 getPosition() const; + + void setPosition( glm::vec3 position ); + + float getPitch() const; + + void setPitch(float pitch); + + float getYaw() const; + + void setYaw(float yaw); + + void panView( double xOffset, double yOffset ); + + void updatePosition(double deltatime); + + void moveForward(int action); + + void moveBackward(int action); + + void moveLeft(int action); + + void moveRight(int action); + + + }; + +} diff --git a/modules/camera/include/vkcv/camera/CameraManager.hpp b/modules/camera/include/vkcv/camera/CameraManager.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9511b752e972afb1e10f41a118433a4e8933fd65 --- /dev/null +++ b/modules/camera/include/vkcv/camera/CameraManager.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "TrackballCamera.hpp" +#include "vkcv/Window.hpp" +#include <GLFW/glfw3.h> +#include <functional> + +namespace vkcv{ + + class CameraManager{ + private: + std::function<void(int, int, int, int)> m_keyHandle; + std::function<void(double, double)> m_mouseMoveHandle; + std::function<void(double, double)> m_mouseScrollHandle; + std::function<void(int, int, int)> m_mouseButtonHandle; + + Window &m_window; + Camera m_camera; + float m_width; + float m_height; +// std::shared_ptr<vkcv::TrackballCamera> m_trackball; + + bool m_roationActive = false; + double m_lastX ; + double m_lastY ; + + void bindCamera(); + void keyCallback(int key, int scancode, int action, int mods); + void scrollCallback( double offsetX, double offsetY); + void mouseMoveCallback( double offsetX, double offsetY); + void mouseButtonCallback(int button, int action, int mods); + + public: + CameraManager(Window &window, float width, float height, glm::vec3 up = glm::vec3(0.0f,-1.0f,0.0f), glm::vec3 position = glm::vec3(0.0f,0.0f,0.0f)); + + Camera &getCamera(); + }; +} diff --git a/modules/camera/include/vkcv/camera/TrackballCamera.hpp b/modules/camera/include/vkcv/camera/TrackballCamera.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c9e269e9f7ad708c68158d5b358efbf37c5bb7a9 --- /dev/null +++ b/modules/camera/include/vkcv/camera/TrackballCamera.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "Camera.hpp" + +namespace vkcv { + + class TrackballCamera : public vkcv::Camera { + public: + + TrackballCamera( int width, int height, glm::mat4 projection); + + TrackballCamera(int width, int height); + + ~TrackballCamera(); + + float getSensitivity() const; + + void setSensitivity(float sensitivity); + + float getStepSize() const; + + void setStepSize(float stepSize); + + float getTheta() const; + + void setTheta(float theta); + + float getPhi() const; + + void setPhi(float phi); + + float getRadius() const; + + void setRadius(float radius); + + const glm::vec3& getCenter() const; + + void setCenter(const glm::vec3 ¢er); + + private: + float m_sensitivity; + float m_stepSize, m_theta, m_phi, m_radius; + glm::vec3 m_center; + + float m_oldX; + float m_oldY; + }; + +} \ No newline at end of file diff --git a/modules/camera/lib/glm b/modules/camera/lib/glm new file mode 160000 index 0000000000000000000000000000000000000000..66062497b104ca7c297321bd0e970869b1e6ece5 --- /dev/null +++ b/modules/camera/lib/glm @@ -0,0 +1 @@ +Subproject commit 66062497b104ca7c297321bd0e970869b1e6ece5 diff --git a/modules/camera/src/vkcv/camera/Camera.cpp b/modules/camera/src/vkcv/camera/Camera.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc8a8498e67a6bd751f5a6ed1d4c4fba0279a68d --- /dev/null +++ b/modules/camera/src/vkcv/camera/Camera.cpp @@ -0,0 +1,177 @@ +#include "vkcv/camera/Camera.hpp" +#include <iostream> + +namespace vkcv { + + Camera::Camera(){ + m_up = glm::vec3(0.0f, -1.0f, 0.0f); + m_position = glm::vec3(0.0f, 0.0f, 0.0f); + m_cameraSpeed = 2.f; + // front + m_pitch = 0.0; + m_yaw = 180.0; + + m_fov_nsteps = 100; + m_fov_min = 10; + m_fov_max = 120; + + m_forward = false; + m_backward = false; + m_left = false; + m_right = false; + } + + Camera::~Camera() = default; + + void Camera::lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up){ + m_view = glm::lookAt(position, center, up); + } + + glm::mat4 Camera::updateView(double deltatime){ + updatePosition(deltatime); + return m_view = glm::lookAt(m_position, m_position + getFront() , m_up); + } + + void Camera::getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos){ + x = glm::vec3( glm::row(m_view, 0)); + y = glm::vec3( glm::row(m_view, 1)); + z = glm::vec3( glm::row(m_view, 2)); + pos = glm::vec3( glm::column(m_view, 3)); + glm::mat3 mat_inv = glm::inverse( glm::mat3(m_view)); + pos = -mat_inv * pos; + } + + void Camera::getNearFar( float &near, float &far) const { + near = m_near; + far = m_far; + } + + + const glm::mat4 Camera::getView() const { + return m_view; + } + + const glm::mat4& Camera::getProjection() const { + return m_projection; + } + + void Camera::setProjection(const glm::mat4 projection){ + m_projection = projection; + } + + float Camera::getFov() const { + return m_fov; + } + + void Camera::setFov( float fov){ + m_fov = fov; + setPerspective( m_fov, m_ratio, m_near, m_far); + } + + void Camera::changeFov(double offset){ + float fov = m_fov; + float fov_range = m_fov_max - m_fov_min; + float fov_stepsize = glm::radians(fov_range)/m_fov_nsteps; + fov -= (float) offset*fov_stepsize; + if (fov < glm::radians(m_fov_min)) { + fov = glm::radians(m_fov_min); + } + if (fov > glm::radians(m_fov_max)) { + fov = glm::radians(m_fov_max); + } + setFov(fov); + } + + void Camera::updateRatio( float ratio){ + m_ratio = ratio; + setPerspective( m_fov, m_ratio, m_near, m_far); + } + + float Camera::getRatio() const { + return 0.0f; + } + + void Camera::setNearFar( float near, float far){ + m_near = near; + m_far = far; + setPerspective(m_fov, m_ratio, m_near, m_far); + } + + void Camera::setPerspective(float fov, float ratio, float near, float far){ + m_fov = fov; + m_ratio = ratio; + m_near = near; + m_far = far; + m_projection = glm::perspective( m_fov, ratio, m_near, m_far); + } + + glm::vec3 Camera::getFront() const { + glm::vec3 direction; + direction.x = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); + direction.y = sin(glm::radians(m_pitch)); + direction.z = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); + return glm::normalize(direction); + } + + glm::vec3 Camera::getPosition() const { + return m_position; + } + + void Camera::setPosition( glm::vec3 position ){ + m_position = position; + } + + void Camera::setUp(const glm::vec3 &up) { + m_up = up; + } + + float Camera::getPitch() const { + return m_pitch; + } + + void Camera::setPitch(float pitch) { + if (pitch > 89.0f) { + pitch = 89.0f; + } + if (pitch < -89.0f) { + pitch = -89.0f; + } + m_pitch = pitch; + } + + float Camera::getYaw() const { + return m_yaw; + } + + void Camera::setYaw(float yaw) { + m_yaw = yaw; + } + + void Camera::panView(double xOffset, double yOffset) { + m_yaw += xOffset; + m_pitch += yOffset; + } + + void Camera::updatePosition(double deltatime ){ + m_position += (m_cameraSpeed * getFront() * static_cast<float> (m_forward) * static_cast<float>(deltatime)); + m_position -= (m_cameraSpeed * getFront() * static_cast<float> (m_backward) * static_cast<float>(deltatime)); + m_position -= (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_left) * static_cast<float>(deltatime)); + m_position += (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_right) * static_cast<float>(deltatime)); + } + + void Camera::moveForward(int action){ + m_forward = static_cast<bool>(action); + } + + void Camera::moveBackward(int action){ + m_backward = static_cast<bool>(action); + } + + void Camera::moveLeft(int action){ + m_left = static_cast<bool>(action); + } + + void Camera::moveRight(int action){ + m_right = static_cast<bool>(action); + } +} \ No newline at end of file diff --git a/modules/camera/src/vkcv/camera/CameraManager.cpp b/modules/camera/src/vkcv/camera/CameraManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18f499a2b34b64c1442c5d9e267d6476b8d69199 --- /dev/null +++ b/modules/camera/src/vkcv/camera/CameraManager.cpp @@ -0,0 +1,82 @@ +#include <iostream> +#include "vkcv/camera/CameraManager.hpp" + +namespace vkcv{ + + CameraManager::CameraManager(Window &window, float width, float height, glm::vec3 up, glm::vec3 position): + m_window(window), m_width(width), m_height(height) + { + m_camera.setUp(up); + m_camera.setPosition(position); + m_camera.setPerspective( glm::radians(60.0f), m_width / m_height, 0.1f, 10.f); + m_lastX = width/2.0; + m_lastY = height/2.0; + bindCamera(); + } + + void CameraManager::bindCamera(){ + m_keyHandle = m_window.e_key.add( [&](int key, int scancode, int action, int mods) { this->keyCallback(key, scancode, action, mods); }); + m_mouseMoveHandle = m_window.e_mouseMove.add( [&]( double offsetX, double offsetY) {this->mouseMoveCallback( offsetX, offsetY);} ); + m_mouseScrollHandle = m_window.e_mouseScroll.add([&](double offsetX, double offsetY) {this->scrollCallback( offsetX, offsetY);} ); + m_mouseButtonHandle = m_window.e_mouseButton.add([&] (int button, int action, int mods) {this->mouseButtonCallback( button, action, mods);}); + } + + void CameraManager::mouseButtonCallback(int button, int action, int mods){ + if(button == GLFW_MOUSE_BUTTON_2 && m_roationActive == false && action == GLFW_PRESS){ + glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); + m_roationActive = true; + }else if(button == GLFW_MOUSE_BUTTON_2 && m_roationActive == true && action == GLFW_RELEASE){ + glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); + m_roationActive = false; + } + } + + void CameraManager::mouseMoveCallback(double x, double y){ + + float xoffset = x - m_lastX; + float yoffset = m_lastY - y; + m_lastX = x; + m_lastY = y; + + if(!m_roationActive){ + return; + } + + float sensitivity = 0.05f; + xoffset *= sensitivity; + yoffset *= sensitivity; + + m_camera.panView( xoffset , yoffset ); + } + + void CameraManager::scrollCallback(double offsetX, double offsetY) { + m_camera.changeFov(offsetY); + } + + void CameraManager::keyCallback(int key, int scancode, int action, int mods) { + + switch (key) { + case GLFW_KEY_W: + m_camera.moveForward(action); + break; + case GLFW_KEY_S: + m_camera.moveBackward(action); + break; + case GLFW_KEY_A: + m_camera.moveLeft(action); + break; + case GLFW_KEY_D: + m_camera.moveRight(action); + break; + case GLFW_KEY_ESCAPE: + glfwSetWindowShouldClose(m_window.getWindow(), 1); + break; + default: + break; + } + } + Camera &CameraManager::getCamera(){ + return m_camera; + } + +} \ No newline at end of file diff --git a/modules/camera/src/vkcv/camera/TrackballCamera.cpp b/modules/camera/src/vkcv/camera/TrackballCamera.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bbb8611dd234499fb9ba08ba87009c8c76660f6 --- /dev/null +++ b/modules/camera/src/vkcv/camera/TrackballCamera.cpp @@ -0,0 +1,146 @@ +#include "vkcv/camera/TrackballCamera.hpp" + +namespace vkcv{ + + TrackballCamera::TrackballCamera( int width, int height, glm::mat4 projection) + { + setPosition( glm::vec3(0.0f, 0.0f, 5.0) ); + m_center = glm::vec3( 0.0f, 0.0f, 0.0f); + m_up = glm::vec3(0.0f, 1.0f, 0.0f); + + m_width = width; + m_height = height; + + m_sensitivity = 0.010f; + m_stepSize = 0.1f; + m_theta = glm::pi<float>() / 2.0f; + m_phi = 0.f; + m_radius = 1.5; + + m_view = glm::lookAt( m_center + getPosition(), m_center, m_up); + + m_oldX = width/2.f; + m_oldY = height/2.f; + + setProjection(projection); + } + + + TrackballCamera::TrackballCamera(int width, int height) + { + setPosition( glm::vec3(0.0f, 0.0f, 5.0)); + m_center = glm::vec3( 0.0f, 0.0f, 0.0f); + m_up = glm::vec3(0.0f, 1.0f, 0.0f); + + m_width = width; + m_height = height; + + m_sensitivity = 0.010f; + m_stepSize = 0.1f; + m_theta = glm::pi<float>() / 2.0f; + m_phi = 0.f; + m_radius = 1.5; + + m_view = glm::lookAt( m_center + getPosition(), m_center, m_up); + + m_oldX = width/2.f; + m_oldY = height/2.f; + + m_fov = glm::radians(60.f); + m_ratio = m_width / (float) m_height; + m_near = 0.001f; + m_far = 10.f; + glm::mat4 projection = glm::perspective(m_fov, m_ratio, m_near, m_far); + + setProjection(projection); + } + + TrackballCamera::~TrackballCamera() + { + } + + // TODO: Can be done as events... (mouseMove, mouseDown, mouseUp) + /*void TrackballCamera::update( GLFWwindow* window) { + + double x, y; + + glfwGetCursorPos( window, &x, &y); + if (glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) + { + float changeX = ((float) x - m_oldX) * m_sensitivity; + float changeY = ((float) y - m_oldY) * m_sensitivity; + + m_theta -= changeY; + if (m_theta < 0.01f) m_theta = 0.01f; + else if (m_theta > glm::pi<float>() - 0.01f) m_theta = glm::pi<float>() - 0.01f; + + m_phi -= changeX; + if (m_phi < 0) m_phi += 2*glm::pi<float>(); + else if (m_phi > 2*glm::pi<float>()) m_phi -= 2*glm::pi<float>(); + } + + m_oldX = (float) x; + m_oldY = (float) y; + + if (glfwGetKey( window, GLFW_KEY_UP) == GLFW_PRESS) + m_radius -= m_stepSize; + if (glfwGetKey( window, GLFW_KEY_DOWN) == GLFW_PRESS) + m_radius += m_stepSize; + if (m_radius < 0.1f) m_radius = 0.1f; + + m_position.x = m_center.x + m_radius * sin(m_theta) * sin(m_phi); + m_position.y = m_center.y + m_radius * cos(m_theta); + m_position.z = m_center.z + m_radius * sin(m_theta) * cos(m_phi); + + m_view = glm::lookAt( m_position, m_center, m_up); + + }*/ + + float TrackballCamera::getSensitivity() const { + return m_sensitivity; + } + + void TrackballCamera::setSensitivity(float sensitivity) { + m_sensitivity = sensitivity; + } + + float TrackballCamera::getStepSize() const { + return m_stepSize; + } + + void TrackballCamera::setStepSize(float stepSize) { + m_stepSize = stepSize; + } + + float TrackballCamera::getTheta() const { + return m_theta; + } + + void TrackballCamera::setTheta(float theta) { + m_theta = theta; + } + + float TrackballCamera::getPhi() const { + return m_phi; + } + + void TrackballCamera::setPhi(float phi) { + m_phi = phi; + } + + float TrackballCamera::getRadius() const { + return m_radius; + } + + void TrackballCamera::setRadius(float radius) { + m_radius = radius; + } + + const glm::vec3& TrackballCamera::getCenter() const { + return m_center; + } + + void TrackballCamera::setCenter(const glm::vec3 ¢er) { + m_center = center; + } +} \ No newline at end of file diff --git a/modules/testing/CMakeLists.txt b/modules/testing/CMakeLists.txt index e869c7974fb0e91894d796e045c9104564173873..a22e547646fd4ef59860245d51365b98df59b578 100644 --- a/modules/testing/CMakeLists.txt +++ b/modules/testing/CMakeLists.txt @@ -18,3 +18,4 @@ add_library(vkcv_testing STATIC ${vkcv_testing_sources}) # add the own include directory for public headers target_include_directories(vkcv_testing BEFORE PUBLIC ${vkcv_testing_include}) + diff --git a/projects/first_triangle/CMakeLists.txt b/projects/first_triangle/CMakeLists.txt index 400f7b1be1e5063777acb4c489d247ec8db34b1d..e7c8373e085df6497060b8d1d8164cf740dfb01f 100644 --- a/projects/first_triangle/CMakeLists.txt +++ b/projects/first_triangle/CMakeLists.txt @@ -22,7 +22,7 @@ if(MSVC) endif() # including headers of dependencies and the VkCV framework -target_include_directories(first_triangle SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include}) +target_include_directories(first_triangle SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include}) # linking with libraries from all dependencies and the VkCV framework -target_link_libraries(first_triangle vkcv ${vkcv_libraries} vkcv_testing) +target_link_libraries(first_triangle vkcv vkcv_testing vkcv_camera) diff --git a/projects/first_triangle/shaders/shader.vert b/projects/first_triangle/shaders/shader.vert index 1d278d5f41f803bb657a303dcc95ffcd2a92fd6e..e129186a4f9b9f8ceca180391210ccaf2953c08e 100644 --- a/projects/first_triangle/shaders/shader.vert +++ b/projects/first_triangle/shaders/shader.vert @@ -3,11 +3,15 @@ layout(location = 0) out vec3 fragColor; +layout( push_constant ) uniform constants{ + mat4 mvp; +}; + void main() { vec3 positions[3] = { - vec3(-0.5, 0.5, 0), - vec3( 0.5, 0.5, 0), - vec3(0, -0.5, 0) + vec3(-0.5, 0.5, -1), + vec3( 0.5, 0.5, -1), + vec3(0, -0.5, -1) }; vec3 colors[3] = { @@ -16,6 +20,6 @@ void main() { vec3(0, 0, 1) }; - gl_Position = vec4(positions[gl_VertexIndex], 1.0); + gl_Position = mvp * vec4(positions[gl_VertexIndex], 1.0); fragColor = colors[gl_VertexIndex]; } \ No newline at end of file diff --git a/projects/first_triangle/shaders/vert.spv b/projects/first_triangle/shaders/vert.spv index bd1e0e682c52e6e38a5f5aba4eeaf8e73a70d741..03af5758ffff1b5b6505fe98b02044849026832d 100644 Binary files a/projects/first_triangle/shaders/vert.spv and b/projects/first_triangle/shaders/vert.spv differ diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index 07da9506c5546985d4852e5d7d87ecbbcfe5e788..275c87cf67f059c5896272639363733634e26ca2 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -1,9 +1,8 @@ #include <iostream> #include <vkcv/Core.hpp> -#include <vkcv/Window.hpp> -#include <vkcv/ShaderProgram.hpp> #include <GLFW/glfw3.h> -#include <vkcv/DescriptorConfig.hpp> +#include <vkcv/camera/CameraManager.hpp> +#include <chrono> int main(int argc, const char** argv) { const char* applicationName = "First Triangle"; @@ -17,6 +16,8 @@ int main(int argc, const char** argv) { false ); + vkcv::CameraManager cameraManager(window, windowWidth, windowHeight); + window.initEvents(); vkcv::Core core = vkcv::Core::create( @@ -128,11 +129,18 @@ int main(int argc, const char** argv) { * * PipelineHandle trianglePipeline = core.CreatePipeline(trianglePipeline); */ - + auto start = std::chrono::system_clock::now(); while (window.isWindowOpen()) { core.beginFrame(); - core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight); + window.pollEvents(); + auto end = std::chrono::system_clock::now(); + auto deltatime = end - start; + start = end; + cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); + const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); + + core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp); core.endFrame(); } return 0; diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index fa53b4faa666134bde9230863f78c96e648c9037..0ee414fb974676b1d2df820e69218f5bb247341b 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -158,13 +158,13 @@ namespace vkcv if (acquireSwapchainImage() != Result::SUCCESS) { return; } - m_window.pollEvents(); m_Context.getDevice().waitIdle(); // FIMXE: this is a sin against graphics programming, but its getting late - Alex destroyTemporaryFramebuffers(); } void Core::renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle, - const int width, const int height) { + const int width, const int height, const size_t pushConstantSize, const void *pushConstantData) { + if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) { return; } @@ -172,6 +172,7 @@ namespace vkcv const vk::RenderPass renderpass = m_PassManager->getVkPass(renderpassHandle); const vk::ImageView imageView = m_swapchainImageViews[m_currentSwapchainImageIndex]; const vk::Pipeline pipeline = m_PipelineManager->getVkPipeline(pipelineHandle); + const vk::PipelineLayout pipelineLayout = m_PipelineManager->getVkPipelineLayout(pipelineHandle); const vk::Rect2D renderArea(vk::Offset2D(0, 0), vk::Extent2D(width, height)); const vk::Framebuffer framebuffer = createFramebuffer(m_Context.getDevice(), renderpass, width, height, imageView); @@ -180,16 +181,17 @@ namespace vkcv SubmitInfo submitInfo; submitInfo.queueType = QueueType::Graphics; submitInfo.signalSemaphores = { m_SyncResources.renderFinished }; - submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline](const vk::CommandBuffer& cmdBuffer) { + submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline, pipelineLayout, pushConstantSize, pushConstantData](const vk::CommandBuffer& cmdBuffer) { const std::array<float, 4> clearColor = { 0.f, 0.f, 0.f, 1.f }; const vk::ClearValue clearValues(clearColor); - + const vk::RenderPassBeginInfo beginInfo(renderpass, framebuffer, renderArea, 1, &clearValues); const vk::SubpassContents subpassContents = {}; cmdBuffer.beginRenderPass(beginInfo, subpassContents, {}); - + cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline, {}); + cmdBuffer.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eAll, 0, pushConstantSize, pushConstantData); cmdBuffer.draw(3, 1, 0, 0, {}); cmdBuffer.endRenderPass(); }, nullptr); diff --git a/src/vkcv/PipelineManager.cpp b/src/vkcv/PipelineManager.cpp index e6d6beea463f17c86d958c9080f34eb61e9076c2..576cb47397a29184160423fdba6d1a09104a5566 100644 --- a/src/vkcv/PipelineManager.cpp +++ b/src/vkcv/PipelineManager.cpp @@ -140,14 +140,14 @@ namespace vkcv { 1.f,1.f,1.f,1.f } ); + const size_t matrixPushConstantSize = 4 * 4 * sizeof(float); + const vk::PushConstantRange pushConstantRange(vk::ShaderStageFlagBits::eAll, 0, matrixPushConstantSize); + // pipeline layout vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( - {}, - 0, - {}, - 0, - {} - ); + {}, + {}, + (pushConstantRange)); vk::PipelineLayout vkPipelineLayout{}; if (m_Device.createPipelineLayout(&pipelineLayoutCreateInfo, nullptr, &vkPipelineLayout) != vk::Result::eSuccess) { diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp index 87f302c146f79f82a9b5334fd6a651fa00a92616..c21271b78f7501721d5c0496d0344dd68e2e7e52 100644 --- a/src/vkcv/Window.cpp +++ b/src/vkcv/Window.cpp @@ -46,17 +46,30 @@ namespace vkcv { glfwSetWindowUserPointer(m_window, this); // combine Callbacks with Events + glfwSetMouseButtonCallback(m_window, Window::onMouseButtonEvent); + glfwSetCursorPosCallback(m_window, Window::onMouseMoveEvent); glfwSetWindowSizeCallback(m_window, Window::onResize); glfwSetKeyCallback(m_window, Window::onKeyEvent); + + glfwSetScrollCallback(m_window, Window::onMouseScrollEvent); } void Window::pollEvents() { glfwPollEvents(); } + void Window::onMouseButtonEvent(GLFWwindow *callbackWindow, int button, int action, int mods) { + + auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); + + if (window != nullptr) { + window->e_mouseButton(button, action, mods); + } + } + void Window::onMouseMoveEvent(GLFWwindow *callbackWindow, double x, double y) { auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); @@ -66,6 +79,14 @@ namespace vkcv { } } + void Window::onMouseScrollEvent(GLFWwindow *callbackWindow, double xoffset, double yoffset) { + auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); + + if (window != nullptr) { + window->e_mouseScroll(xoffset, yoffset); + } + } + void Window::onResize(GLFWwindow *callbackWindow, int width, int height) { auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); @@ -103,4 +124,4 @@ namespace vkcv { GLFWwindow *Window::getWindow() const { return m_window; } -} +} \ No newline at end of file