Skip to content

Commit

Permalink
Corrected initial camera position such that z-axis points to up, x-ax…
Browse files Browse the repository at this point in the history
…is to right, y-axis into the screen
  • Loading branch information
denizdiktas committed Jun 14, 2023
1 parent 74b54a3 commit 9a5bfa0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
19 changes: 12 additions & 7 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Camera
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
Expand Down
12 changes: 4 additions & 8 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
10 changes: 7 additions & 3 deletions Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,18 +51,21 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
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;
};

Expand Down

0 comments on commit 9a5bfa0

Please sign in to comment.