Skip to content
Snippets Groups Projects
Commit d41529a5 authored by Katharina Krämer's avatar Katharina Krämer
Browse files

[#35] created CameraManager class and managed event handler

parent 620406ad
No related branches found
No related tags found
1 merge request!26Resolve "Kamera - Erstellung und Handling"
Pipeline #25059 failed
...@@ -34,8 +34,9 @@ namespace vkcv { ...@@ -34,8 +34,9 @@ namespace vkcv {
* adds a function handle to the event to be called * adds a function handle to the event to be called
* @param handle of the function * @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); this->m_handles.push_back(handle);
return handle;
} }
/** /**
......
...@@ -13,6 +13,8 @@ set(vkcv_camera_sources ...@@ -13,6 +13,8 @@ set(vkcv_camera_sources
${vkcv_camera_source}/vkcv/camera/Camera.cpp ${vkcv_camera_source}/vkcv/camera/Camera.cpp
${vkcv_camera_include}/vkcv/camera/TrackballCamera.hpp ${vkcv_camera_include}/vkcv/camera/TrackballCamera.hpp
${vkcv_camera_source}/vkcv/camera/TrackballCamera.cpp ${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 # adding source files to the project
......
#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;
Window &m_window;
Camera m_camera;
float m_width;
float m_height;
std::shared_ptr<vkcv::TrackballCamera> m_trackball;
glm::vec3 m_up;
glm::vec3 m_position;
glm::vec3 m_front;
glm::vec3 m_center;
float m_radius;
float m_cameraSpeed;
float m_roll;
float m_pitch;
float m_yaw;
bool m_firstMouse = true;
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);
public:
CameraManager(Window &window, float width, float height);
CameraManager(Window &window, float width, float height, glm::vec3 up, glm::vec3 position, glm::vec3 front);
Camera getCamera();
};
}
#include "vkcv/camera/CameraManager.hpp"
namespace vkcv{
// m_window.e_mouseMove.add(this.onMouseMove);\
CameraManager::CameraManager(Window &window, float width, float height):
m_window(window), m_width(width), m_height(height)
{
glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
m_camera.setPerspective( glm::radians(60.0f), m_width / m_height, 0.1f, 10.f);
m_up = glm::vec3(0.0f, 1.0f, 0.0f);
m_position = glm::vec3(0.0f, 0.0f, 0.0f);
m_front = glm::vec3(0.0f, 0.0f, -1.0f);
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
m_radius = 10.0f;
m_cameraSpeed = 0.05f;
m_roll = 0.0;
m_pitch = 0.0;
m_yaw = 0.0;
bindCamera();
}
CameraManager::CameraManager(Window &window, float width, float height, glm::vec3 up, glm::vec3 position, glm::vec3 front):
m_window(window), m_width(width), m_height(height), m_up(up), m_position(position), m_front(front)
{
glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
m_camera.setPerspective( glm::radians(60.0f), m_width / m_height, 0.1f, 10.f);
m_up = glm::vec3(0.0f, 1.0f, 0.0f);
m_position = glm::vec3(0.0f, 0.0f, 0.0f);
m_front = glm::vec3(0.0f, 0.0f, -1.0f);
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
m_radius = 10.0f;
m_cameraSpeed = 0.05f;
m_roll = 0.0;
m_pitch = 0.0;
m_yaw = 0.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);} );
}
void CameraManager::mouseMoveCallback(double x, double y){
if (m_firstMouse) {
m_lastX = x;
m_lastY = y;
m_firstMouse = false;
}
float xoffset = x - m_lastX;
float yoffset = m_lastY - y;
m_lastX = x;
m_lastY = y;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
m_yaw += xoffset;
m_pitch += yoffset;
if (m_pitch > 89.0f) {
m_pitch = 89.0f;
}
if (m_pitch < -89.0f) {
m_pitch = -89.0f;
}
glm::vec3 direction;
direction.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
direction.y = sin(glm::radians(m_pitch));
direction.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(direction);
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
}
void CameraManager::scrollCallback(double offsetX, double offsetY) {
float fov = m_camera.getFov();
fov -= (float) offsetY;
if (fov < 1.0f) {
fov = 1.0f;
}
if (fov > 45.0f) {
fov = 45.0f;
}
m_camera.setFov(fov);
}
void CameraManager::keyCallback(int key, int scancode, int action, int mods) {
switch (key) {
case GLFW_KEY_W:
//std::cout << "Move forward" << std::endl;
m_position += m_cameraSpeed * m_front;
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
case GLFW_KEY_S:
//std::cout << "Move left" << std::endl;
m_position -= m_cameraSpeed * m_front;
m_center = m_position + m_front;
m_position += m_cameraSpeed * m_front;
m_camera.lookAt(m_position, m_center, m_up);
break;
case GLFW_KEY_A:
//std::cout << "Move backward" << std::endl;
m_position += m_cameraSpeed * m_front;
m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_cameraSpeed;
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
break;
case GLFW_KEY_D:
//std::cout << "Move right" << std::endl;
m_position += m_cameraSpeed * m_front;
m_position += glm::normalize(glm::cross(m_front, m_up)) * m_cameraSpeed;
m_center = m_position + m_front;
m_camera.lookAt(m_position, m_center, m_up);
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
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
#include <vkcv/Window.hpp> #include <vkcv/Window.hpp>
#include <vkcv/ShaderProgram.hpp> #include <vkcv/ShaderProgram.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <vkcv/camera/Camera.hpp> #include <vkcv/camera/CameraManager.hpp>
#include <vkcv/camera/TrackballCamera.hpp>
int main(int argc, const char** argv) { int main(int argc, const char** argv) {
...@@ -19,110 +18,7 @@ int main(int argc, const char** argv) { ...@@ -19,110 +18,7 @@ int main(int argc, const char** argv) {
false false
); );
// TODO: this code will be put in a camera controller class vkcv::CameraManager cameraManager(window, windowWidth, windowHeight);
vkcv::Camera camera;
std::shared_ptr<vkcv::TrackballCamera> trackball;
camera.setPerspective( glm::radians(60.0f), windowWidth / (float)windowHeight, 0.1f, 10.f);
glm::vec3 up(0.0f, 1.0f, 0.0f);
glm::vec3 position(0.0f, 0.0f, 0.0f);
glm::vec3 front(0.0f, 0.0f, -1.0f);
glm::vec3 center = position + front;
camera.lookAt(position, center, up);
const float radius = 10.0f;
const float cameraSpeed = 0.05f;
float roll = 0.0;
float pitch = 0.0;
float yaw = 0.0;
//TODO? should the standard camera support rotation?
bool firstMouse = true;
double lastX, lastY;
// showing basic usage lambda events of window
window.e_mouseMove.add([&](double x, double y) {
//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;
});
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) {
switch (key) {
case GLFW_KEY_W:
//std::cout << "Move forward" << std::endl;
position += cameraSpeed * front;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_S:
//std::cout << "Move left" << std::endl;
position -= cameraSpeed * front;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_A:
//std::cout << "Move backward" << std::endl;
position -= glm::normalize(glm::cross(front, up)) * cameraSpeed;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_D:
//std::cout << "Move right" << std::endl;
position += glm::normalize(glm::cross(front, up)) * cameraSpeed;
center = position + front;
camera.lookAt(position, center, up);
break;
default:
break;//std::cout << "this key is not supported yet: " << std::endl;
}
});
window.initEvents(); window.initEvents();
...@@ -212,7 +108,7 @@ int main(int argc, const char** argv) { ...@@ -212,7 +108,7 @@ int main(int argc, const char** argv) {
{ {
core.beginFrame(); core.beginFrame();
const glm::mat4 mvp = camera.getProjection() * camera.getView(); const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp); core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp);
core.endFrame(); core.endFrame();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment