Skip to content
Snippets Groups Projects
Verified Commit c6c021b2 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

[#56] Fixed annoying camera to get rid of pitch and yaw members

parent 26757779
No related branches found
No related tags found
1 merge request!45Resolve "Szene-Repräsentation"
Pipeline #26230 passed
......@@ -3,6 +3,8 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_access.hpp>
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
namespace vkcv::camera {
......@@ -20,9 +22,6 @@ namespace vkcv::camera {
glm::vec3 m_up;
glm::vec3 m_position;
glm::vec3 m_center;
float m_pitch;
float m_yaw;
/**
* @brief Sets the view matrix of the camera to @p view
......@@ -156,6 +155,20 @@ namespace vkcv::camera {
* @param[in] center The new center point.
*/
void setCenter(const glm::vec3& center);
/**
* @brief Gets the angles of the camera.
* @param[out] pitch The pitch value in radians
* @param[out] yaw The yaw value in radians
*/
void getAngles(float& pitch, float& yaw);
/**
* @brief Sets the angles of the camera.
* @param pitch The new pitch value in radians
* @param yaw The new yaw value in radians
*/
void setAngles(float pitch, float yaw);
/**
* @brief Gets the pitch value of the camera in degrees.
......
......@@ -10,8 +10,6 @@ namespace vkcv::camera {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f)
);
setFront(glm::normalize(m_center - m_position));
}
Camera::~Camera() = default;
......@@ -93,16 +91,11 @@ namespace vkcv::camera {
}
glm::vec3 Camera::getFront() const {
glm::vec3 direction;
direction.x = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
direction.y = std::sin(glm::radians(m_pitch));
direction.z = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
return glm::normalize(direction);
return glm::normalize(m_center - m_position);
}
void Camera::setFront(const glm::vec3 &front) {
m_pitch = std::atan2(front.y, std::sqrt(front.x * front.x + front.z * front.z));
m_yaw = std::atan2(front.x, front.z);
setCenter(m_position + front);
}
const glm::vec3& Camera::getPosition() const {
......@@ -128,21 +121,47 @@ namespace vkcv::camera {
void Camera::setUp(const glm::vec3 &up) {
lookAt(m_position, m_center, up);
}
float Camera::getPitch() const {
return m_pitch;
void Camera::getAngles(float& pitch, float& yaw) {
const auto front = getFront();
pitch = std::atan2(front[1], std::sqrt(
front[0] * front[0] + front[2] * front[2]
));
yaw = std::atan2(front[0], front[2]);
}
void Camera::setAngles(float pitch, float yaw) {
float cosPitch = std::cos(pitch);
setFront(glm::vec3(
std::sin(yaw) * cosPitch,
std::sin(pitch),
std::cos(yaw) * cosPitch
));
}
float Camera::getPitch() const {
const auto front = getFront();
return glm::degrees(std::atan2(front[1], std::sqrt(
front[0] * front[0] + front[2] * front[2]
)));
}
void Camera::setPitch(float pitch) {
m_pitch = pitch;
setAngles(glm::radians(pitch), glm::radians(getYaw()));
}
float Camera::getYaw() const {
return m_yaw;
const auto front = getFront();
return glm::degrees(std::atan2(front[0], front[2]));
}
void Camera::setYaw(float yaw) {
m_yaw = yaw;
setAngles(glm::radians(getPitch()), glm::radians(yaw));
}
}
\ No newline at end of file
......@@ -82,6 +82,8 @@ namespace vkcv::scene {
static glm::vec3 projectPoint(const glm::mat4& transform, const glm::vec3& point) {
const glm::vec4 position = transform * glm::vec4(point, 1.0f);
//std::cout << "POS: " << position.x << " " << position.y << " " << position.z << " " << position.w << std::endl;
return glm::vec3(
position[0] / position[3],
position[1] / position[3],
......@@ -95,7 +97,7 @@ namespace vkcv::scene {
glm::vec3(+1.0f, +1.0f, +1.0f)
);
return frustum.intersects(bounds);
return bounds.intersects(frustum);
}
void Mesh::recordDrawcalls(const glm::mat4& viewProjection,
......@@ -118,6 +120,7 @@ namespace vkcv::scene {
}
if (!checkFrustum(aabb)) {
m_drawcalls[i].instanceCount = 2;
continue;
}
......
......@@ -66,6 +66,10 @@ namespace vkcv::scene {
return m_materials[index].m_data;
}
std::ostream& operator << (std::ostream& out, const glm::vec3& vec) {
return out << vec.x << " " << vec.y << " " << vec.z;
}
void Scene::recordDrawcalls(CommandStreamHandle &cmdStream,
const camera::Camera &camera,
const PassHandle &pass,
......@@ -74,6 +78,8 @@ namespace vkcv::scene {
std::vector<glm::mat4> matrices;
std::vector<DrawcallInfo> drawcalls;
std::cout << camera.getPosition() << " | " << camera.getFront() << std::endl;
const glm::mat4 viewProjection = camera.getMVP();
for (auto& node : m_nodes) {
......
......@@ -25,8 +25,13 @@ int main(int argc, const char** argv) {
vkcv::camera::CameraManager cameraManager(window);
uint32_t camIndex0 = cameraManager.addCamera(vkcv::camera::ControllerType::PILOT);
uint32_t camIndex1 = cameraManager.addCamera(vkcv::camera::ControllerType::TRACKBALL);
cameraManager.getCamera(camIndex0).setPosition(glm::vec3(0, 0, -3));
glm::vec3 pos (-7.96175f, 0.889579f, -0.514462f);
glm::vec3 front (-0.504636f, -0.603207f, 0.617643f);
cameraManager.getCamera(camIndex0).setPosition(pos);
cameraManager.getCamera(camIndex0).setFront(front);
//cameraManager.getCamera(camIndex0).setPosition(glm::vec3(0, 0, -3));
cameraManager.getCamera(camIndex0).setNearFar(0.1f, 30.0f);
cameraManager.getCamera(camIndex1).setNearFar(0.1f, 30.0f);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment