Corrected initial camera position such that z-axis points to up, x-axis to right, y-axis into the screen

This commit is contained in:
denizdiktas 2023-06-14 10:24:08 +03:00
parent 74b54a3298
commit 9a5bfa00be
4 changed files with 25 additions and 18 deletions

View File

@ -25,22 +25,27 @@ QMatrix4x4 Camera::get_view_matrix() const
}
void Camera::rotate(float theta_around_x, float theta_around_y)
void Camera::rotate_around_x(float theta)
{
// rotate the camera around its x-axis
QMatrix4x4 rot;
rot.rotate(theta_around_x, m_ux);
rot.rotate(theta, m_ux);
m_pos = m_pos * rot;
m_uy = m_uy * rot;
m_uz = m_uz * rot;
// rotate the camera around its y-axis
rot.setToIdentity();
rot.rotate(theta_around_y, m_uy);
}
void Camera::rotate_around_y(float theta)
{
QMatrix4x4 rot;
rot.rotate(theta, m_uy);
m_pos = m_pos * rot;
m_ux = m_ux * rot;
m_uz = m_uz * rot;
}
void Camera::rotate(float theta_around_x, float theta_around_y)
{
rotate_around_x(theta_around_x);
rotate_around_y(theta_around_y);
}
void Camera::move_forward(float distance)
{

View File

@ -22,6 +22,8 @@ public:
QMatrix4x4 get_projection_matrix() const { return m_projection; }
// rotate the camera around its own axes
void rotate_around_x(float theta);
void rotate_around_y(float theta);
void rotate(float theta_around_x, float theta_around_y);
// move the camera forward around its own z-axis

View File

@ -84,26 +84,22 @@ void MainWidget::initializeGL()
#include "World_coordinate_axes.h"
std::unique_ptr<World_coord_axes> m_world_coord_axes;
void MainWidget::init_camera()
{
m_camera.set_pos(0, 0, 10);
m_camera.set_pos(0, 0, 3);
m_camera.rotate_around_x(-90);
}
void MainWidget::init_geometry()
{
int num_slices, num_stacks;
num_slices = num_stacks = 64;
float r = 3;
float r = 1;
m_sphere = std::make_unique<Sphere>(num_slices, num_stacks, r);
const float c = 0.8;
m_sphere->set_color(c, c, c, 1);
m_world_coord_axes = std::make_unique<World_coord_axes>(5);
m_world_coord_axes = std::make_unique<World_coord_axes>(2);
}
void MainWidget::init_shader_programs()
{

View File

@ -18,6 +18,7 @@
#include "Common_defs.h"
#include "Shader_program.h"
#include "Sphere.h"
#include "World_coordinate_axes.h"
class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
@ -50,18 +51,21 @@ protected:
void init_shader_programs();
private:
std::unique_ptr<Sphere> m_sphere;
// Objects in the scene
std::unique_ptr<Sphere> m_sphere;
std::unique_ptr<World_coord_axes> m_world_coord_axes;
// Shaders
Shader_program m_sp_smooth;
Shader_program m_sp_color_only;
// camera & controls
// Camera & controls
Camera m_camera;
bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_last_mouse_pos;
// Timer for continuous screen-updates
QBasicTimer m_timer;
};