Skip to content

Commit

Permalink
Refactor: Camera class
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 12, 2023
1 parent 9f404ab commit cbf97e9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ qt_standard_project_setup()

qt_add_executable(earth
main.cpp
mainwidget.cpp mainwidget.h
Camera.h
mainwidget.cpp mainwidget.h
Camera.cpp Camera.h
)


Expand Down
43 changes: 43 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include "Camera.h"


Camera::Camera() :
m_ux(1, 0, 0),
m_uy(0, 1, 0),
m_uz(0, 0, 1)
{
}

void Camera::perspective(float fov, float aspect, float z_near, float z_far)
{
m_projection.setToIdentity();
m_projection.perspective(fov, aspect, z_near, z_far);
}


QMatrix4x4 Camera::get_view_matrix() const
{
QMatrix4x4 view;
const QVector3D center = m_pos - m_uz;
view.lookAt(m_pos, center, m_uy);
return view;
}


void Camera::rotate(float theta_around_x, float theta_around_y)
{
// rotate the camera around its x-axis
QMatrix4x4 rot;
rot.rotate(theta_around_x, 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);
m_pos = m_pos * rot;
m_ux = m_ux * rot;
m_uz = m_uz * rot;
}
37 changes: 8 additions & 29 deletions Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,26 @@ class Camera
{
public:

Camera() :
m_ux(1, 0, 0),
m_uy(0, 1, 0),
m_uz(0, 0, 1)
{
}
Camera();

void set_pos(const QVector3D& pos) { m_pos = pos; }
void set_pos(float x, float y, float z) { m_pos = QVector3D(x,y,z); }

QMatrix4x4 get_view_matrix() const
{
QMatrix4x4 view;
const QVector3D center = m_pos - m_uz;
view.lookAt(m_pos, center, m_uy);
return view;
}
void perspective(float fov, float aspect_ratio, float z_near, float z_far);

QMatrix4x4 get_view_matrix() const;
QMatrix4x4 get_projection_matrix() const { return m_projection; }

// rotate the camera around its own axes
void rotate(float theta_around_x, float theta_around_y)
{
// rotate the camera around its x-axis
QMatrix4x4 rot;
rot.rotate(theta_around_x, 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);
m_pos = m_pos * rot;
m_ux = m_ux * rot;
m_uz = m_uz * rot;
}
void rotate(float theta_around_x, float theta_around_y);

private:
QVector3D m_pos;
QVector3D m_ux;
QVector3D m_uy;
QVector3D m_uz;

QMatrix4x4 m_projection;
};


Expand Down
21 changes: 6 additions & 15 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,34 +244,25 @@ void MainWidget::resizeGL(int w, int h)
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;

// Reset projection
m_projection.setToIdentity();
m_projection.perspective(fov, aspect, z_near, z_far);
m_camera.perspective(fov, aspect, z_near, z_far);
}


void MainWidget::paintGL()
{
QMatrix4x4 view;
//const QVector3D eye(0, 10, 10), center(0, 0, 0), up(0, 1, 0);
// view.lookAt(eye, center, up);

//const QVector3D center(0, 0, 0);
//view.lookAt(camera.pos, center, cam_uy);
view = m_camera.get_view_matrix();

QMatrix4x4 model;
//static float angle = 0;
//angle += 1;
//model.rotate(angle, up);
const auto view = m_camera.get_view_matrix();
const auto projection = m_camera.get_projection_matrix();
const auto mvp = projection * view * model;


// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glUseProgram(m_shader);
auto mvp = m_projection * view * model;
glUniformMatrix4fv(m_uniform_mvp, 1, GL_FALSE, mvp.data());

m_sphere->draw();
Expand Down
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
Camera m_camera;
bool m_mouse_pressed = false;
QVector2D m_last_mouse_pos;
QMatrix4x4 m_projection;
//QMatrix4x4 m_projection;


QBasicTimer m_timer;
Expand Down

0 comments on commit cbf97e9

Please sign in to comment.