Skip to content
Snippets Groups Projects
Commit 9ddd711f authored by Vanessa Karolek's avatar Vanessa Karolek
Browse files

[#35] add mouse scroll event, mouse move functionalities

For now, camera based callbacks are handled within main. For a camera controller, we need a way to pass callback functions to the 'add' function, i.e. replacing the lambda expressions.
parent 79d415f8
No related branches found
No related tags found
1 merge request!26Resolve "Kamera - Erstellung und Handling"
Pipeline #25033 failed
...@@ -32,6 +32,8 @@ namespace vkcv { ...@@ -32,6 +32,8 @@ namespace vkcv {
*/ */
static void onMouseMoveEvent(GLFWwindow *window, double x, double y); static void onMouseMoveEvent(GLFWwindow *window, double x, double y);
static void onMouseScrollEvent(GLFWwindow *callbackWindow, double xoffset, double yoffset);
/** /**
* resize callback for the resize option of the window * resize callback for the resize option of the window
* @param[in] window The window that was resized. * @param[in] window The window that was resized.
...@@ -82,6 +84,7 @@ namespace vkcv { ...@@ -82,6 +84,7 @@ namespace vkcv {
* basic events of the window * basic events of the window
*/ */
event< double, double > e_mouseMove; event< double, double > e_mouseMove;
event< double, double > e_mouseScroll;
event< int, int > e_resize; event< int, int > e_resize;
event< int, int, int, int > e_key; event< int, int, int, int > e_key;
......
...@@ -13,7 +13,9 @@ set(vkcv_testing_sources ...@@ -13,7 +13,9 @@ set(vkcv_testing_sources
${vkcv_testing_source}/vkcv/testing/Test.cpp ${vkcv_testing_source}/vkcv/testing/Test.cpp
${vkcv_testing_include}/vkcv/camera/Camera.hpp ${vkcv_testing_include}/vkcv/camera/Camera.hpp
${vkcv_testing_source}/vkcv/camera/Camera.cpp ${vkcv_testing_source}/vkcv/camera/Camera.cpp
src/vkcv/camera/TrackballCamera.cpp) ${vkcv_testing_include}/vkcv/camera/TrackballCamera.hpp
${vkcv_testing_source}/vkcv/camera/TrackballCamera.cpp
)
# adding source files to the project # adding source files to the project
add_library(vkcv_testing STATIC ${vkcv_testing_sources}) add_library(vkcv_testing STATIC ${vkcv_testing_sources})
......
...@@ -15,46 +15,44 @@ namespace vkcv { ...@@ -15,46 +15,44 @@ namespace vkcv {
int m_width, m_height; int m_width, m_height;
float m_oldX, m_oldY; float m_oldX, m_oldY,
m_near, m_far,
m_fov, m_ratio;
glm::vec3 m_position, m_direction, m_up; glm::vec3 m_position, m_direction, m_up;
float m_near, m_far;
float m_fov, m_ratio;
public: public:
Camera(); Camera();
~Camera(); ~Camera();
virtual void update(GLFWwindow* window) {}; virtual void update(GLFWwindow *window) {};
void setPerspective(float fov, float ratio, float near, float far); void setPerspective(float fov, float ratio, float near, float far);
const glm::mat4& getView(); const glm::mat4 &getView();
void getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos); void getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos);
void setView( const glm::mat4 view ); void setView(const glm::mat4 view);
void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up); void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up);
const glm::mat4& Camera::getProjection(); const glm::mat4 &Camera::getProjection();
void setProjection( const glm::mat4 projection); void setProjection(const glm::mat4 projection);
void getNearFar( float &near, float &far); void getNearFar(float &near, float &far);
float getFov(); float getFov();
void setFov( float fov); void setFov(float fov);
void updateRatio( float ratio); void updateRatio(float ratio);
float getRatio(); float getRatio();
void setNearFar( float near, float far); void setNearFar(float near, float far);
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <vkcv/camera/Camera.hpp> #include <vkcv/camera/Camera.hpp>
#include <vkcv/camera/TrackballCamera.hpp> #include <vkcv/camera/TrackballCamera.hpp>
int main(int argc, const char** argv) { int main(int argc, const char** argv) {
const char* applicationName = "First Triangle"; const char* applicationName = "First Triangle";
...@@ -32,28 +33,67 @@ int main(int argc, const char** argv) { ...@@ -32,28 +33,67 @@ int main(int argc, const char** argv) {
float roll = 0.0; float roll = 0.0;
float pitch = 0.0; float pitch = 0.0;
float yaw = 0.0; float yaw = 0.0;
// TODO: need scrolling event callback to set yoffset
float yoffset = 10;
// scroll callback
float fov = camera.getFov();
fov -= yoffset;
if (fov < 1.0f)
fov = 1.0f;
if (fov > 45.0f)
fov = 45.0f;
camera.setFov(fov);
//TODO? should the standard camera support rotation? //TODO? should the standard camera support rotation?
bool firstMouse = true;
double lastX, lastY;
// showing basic usage lambda events of window // showing basic usage lambda events of window
window.e_mouseMove.add([&](double x, double y){ window.e_mouseMove.add([&](double x, double y) {
std::cout << "movement: " << x << " , " << y << std::endl; std::cout << "movement: " << x << " , " << y << std::endl;
if (firstMouse) {
lastX = x;
lastY = y;
firstMouse = false;
}
float xoffset = x - lastX;
float yoffset = lastY - y;
lastX = x;
lastY = y;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f) {
pitch = 89.0f;
}
if (pitch < -89.0f) {
pitch = -89.0f;
}
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(direction);
center = position + front;
camera.lookAt(position, center, up);
std::cout << "New center: " << center.x << ", " << center.y << ", " << center.z << std::endl;
}); });
// TODO: need event for press mouse button window.e_mouseScroll.add([&](double xoffset, double yoffset) {
float fov = camera.getFov();
fov -= (float)yoffset;
if (fov < 1.0f) {
fov = 1.0f;
}
if (fov > 45.0f) {
fov = 45.0f;
}
camera.setFov(fov);
std::cout << "New FOV: " << fov << std::endl;
});
window.e_key.add([&](int key, int scancode, int action, int mods){ window.e_key.add([&](int key, int scancode, int action, int mods) {
switch (key) { switch (key) {
case GLFW_KEY_W: case GLFW_KEY_W:
std::cout << "Move forward" << std::endl; std::cout << "Move forward" << std::endl;
......
...@@ -51,6 +51,8 @@ namespace vkcv { ...@@ -51,6 +51,8 @@ namespace vkcv {
glfwSetWindowSizeCallback(m_window, Window::onResize); glfwSetWindowSizeCallback(m_window, Window::onResize);
glfwSetKeyCallback(m_window, Window::onKeyEvent); glfwSetKeyCallback(m_window, Window::onKeyEvent);
glfwSetScrollCallback(m_window, Window::onMouseScrollEvent);
} }
void Window::pollEvents() { void Window::pollEvents() {
...@@ -66,6 +68,14 @@ namespace vkcv { ...@@ -66,6 +68,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) { void Window::onResize(GLFWwindow *callbackWindow, int width, int height) {
auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment