Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devyatovskaya task4 #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/devyatovskaya/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(Task1)
add_subdirectory(Task4)
89 changes: 89 additions & 0 deletions src/devyatovskaya/Task4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
set(Task4_SRSC
main.cpp
FPSCounter.cpp
FPSCounter.h
GLCamera.cpp
GLCamera.h
GLCameraMover.cpp
GLCameraMover.h
GLDirectedLight.cpp
GLDirectedLight.h
GLMaterial.cpp
GLMaterial.h
GLLightSource.cpp
GLLightSource.h
GLCubeMeshGenerator.cpp
GLCubeMeshGenerator.h
GLFlatMeshGenerator.cpp
GLFlatMeshGenerator.h
GLMesh.cpp
GLMesh.h
GLMeshGenerator.h
GLMeshGenerator.cpp
GLMeshRenderer.cpp
GLMeshRenderer.h
GLMeshRendererGenerator.h
GLPointLight.cpp
GLPointLight.h
GLObject.cpp
GLObject.h
GLScene.cpp
GLScene.h
GLSceneRenderer.cpp
GLSceneRenderer.h
GLSimpleMeshRenderer.h
GLSimpleMeshRenderer.cpp
GLSimpleMeshRendererGenerator.cpp
GLSimpleMeshRendererGenerator.h
GLSphereMeshGenerator.cpp
GLSphereMeshGenerator.h
GLTransform.cpp
GLTransform.h
GLVertex.h
LightingDialog.h
LightingDialog.ui
GLSpotLight.cpp
GLSpotLight.h
LigtingDialog.cpp
MeshGeneratorCollecrion.h
MorphingDialog.cpp
MorphingDialog.h
MorphingDialog.ui
PhongLighting.ui
PhongLightingWidget.cpp
PhongLightingWidget.h
PhongLighting.h
PhongLighting.cpp
PhongLighting.qrc
PreparedScenes.cpp
PreparedScenes.h
RenderDialog.cpp
RenderDialog.h
RenderDialog.ui
ShaderCollection.h
ShaderData.cpp
ShaderData.h

shaders.qrc
Shaders/f_morphing.glsl
Shaders/v_morphing.glsl
Shaders/Lighting/v_light_source.glsl
Shaders/Lighting/f_light_source.glsl
Shaders/Lighting/v_phong_lighting_wo_normal_mapping.glsl
Shaders/Lighting/f_phong_lighting_wo_normal_mapping.glsl
Shaders/Lighting/v_guro_lighting_wo_normal_mapping.glsl
Shaders/Lighting/f_guro_lighting_wo_normal_mapping.glsl
Shaders/Lighting/v_phong_lighting_normal_mapping.glsl
Shaders/Lighting/f_phong_lighting_normal_mapping.glsl

textures.qrc
Texture/Earth_Albedo.jpg
Texture/Earth_NormalMap.jpg
)

find_package(Qt5 COMPONENTS Widgets REQUIRED)


add_executable(DevyatovskayaTask4 ${Task4_SRSC})

target_link_libraries(DevyatovskayaTask4 PRIVATE Qt5::Widgets)
30 changes: 30 additions & 0 deletions src/devyatovskaya/Task4/FPSCounter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "FPSCounter.h"

void FPSCounter::calculate_fps() {
const auto current_time = std::chrono::system_clock::now();
const std::chrono::duration<float> elapsed_seconds =
current_time - last_frame_time_;
last_frame_time_ = current_time;
++frame_counter_;

const auto frame_time = elapsed_seconds.count();
delta_time_ = frame_time;
elapsed_frame_time_ += frame_time;

// update fps every half second
if (elapsed_frame_time_ >= 0.5f) {

average_fps_ = static_cast<float>(frame_counter_) / elapsed_frame_time_;
emit emit_fps("FPS: " + fps_to_str());
elapsed_frame_time_ = 0;
frame_counter_ = 0;
}
}

float FPSCounter::delta_time() const { return delta_time_; }

QString FPSCounter::fps_to_str() const {
QString fps_to_str{};
fps_to_str.setNum(average_fps_, 'g', 3);
return fps_to_str;
}
24 changes: 24 additions & 0 deletions src/devyatovskaya/Task4/FPSCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <QObject>
#include <chrono>

class FPSCounter final : public QObject {

Q_OBJECT
signals:
void emit_fps(const QString &);

public:
void calculate_fps();

float delta_time() const;

private:
QString fps_to_str() const;

std::chrono::time_point<std::chrono::system_clock> last_frame_time_;
int frame_counter_{0};
float delta_time_{0};
float average_fps_{0};
float elapsed_frame_time_{0};
};
42 changes: 42 additions & 0 deletions src/devyatovskaya/Task4/GLCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "GLCamera.h"

GLCamera::GLCamera(const QVector3D &pos, float _aspect_ratio)
: position{pos}, front{0, 0, -1}, right{1, 0, 0}, up{0, 1, 0}, world_up{0,
1,
0},

fov{60.f}, yaw{-90.0f}, pitch{0.0f},
aspect_ratio{_aspect_ratio}, zoom{45.f} {
update_camera_vectors();
}

QMatrix4x4 GLCamera::get_projection_matrix() const {
QMatrix4x4 projection;
projection.perspective(zoom, aspect_ratio, 0.1f, 1000.0f);
return projection;
}

QMatrix4x4 GLCamera::get_view_matrix() const {
QMatrix4x4 view;
view.lookAt(position, position + front, up);
return view;
}

void GLCamera::update_camera_vectors() {
QVector3D forward;

forward.setX(cos(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
forward.setY(sin(qDegreesToRadians(pitch)));
forward.setZ(sin(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
front = forward.normalized();

right = QVector3D::crossProduct(front, world_up).normalized();
up = QVector3D::crossProduct(right, front).normalized();
}

void GLCamera::translate(const QVector3D &mult) {
position += front * mult.z();
position += right * mult.x();
position += up * mult.y();
update_camera_vectors();
}
27 changes: 27 additions & 0 deletions src/devyatovskaya/Task4/GLCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <QtMath>
#include <qmatrix4x4.h>
#include <qvector3d.h>

class GLCamera final {
public:
QVector3D position;
QVector3D front;
QVector3D right;
QVector3D up;
QVector3D world_up;

float fov;
float yaw;
float pitch;
float aspect_ratio;
float zoom;

explicit GLCamera(const QVector3D &pos, float aspect_ratio = 16.f / 9.f);

QMatrix4x4 get_projection_matrix() const;
QMatrix4x4 get_view_matrix() const;

void update_camera_vectors();
void translate(const QVector3D &mult);
};
62 changes: 62 additions & 0 deletions src/devyatovskaya/Task4/GLCameraMover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "GLCameraMover.h"

void GLCameraMover::move() {
const auto &direction =
controls_[static_cast<CameraMovement>(last_pressed_key_)];
if (direction == QVector3D{0, 0, 0}) {
return;
}

camera.translate(direction * current_camera_speed_);
}

void GLCameraMover::set_camera_speed(const float delta_time) {
current_camera_speed_ = initial_camera_speed_ * delta_time;
}

void GLCameraMover::set_mouse_pos(const QPointF &pos) {
if (first_mouse_callback_) {
last_mouse_pos_ = pos;
first_mouse_callback_ = false;
return;
}

update_camera_direction(pos);
camera.update_camera_vectors();
}

void GLCameraMover::update_camera_zoom(const float dy) {
if (camera.zoom >= 1.f && camera.zoom <= 45.f) {
camera.zoom -= dy;
} else if (camera.zoom < 1.f) {
camera.zoom = 1.f;
} else {
camera.zoom = 45.f;
}
}

void GLCameraMover::set_key(const int key) { last_pressed_key_ = key; }

void GLCameraMover::reset_key(const int key) {
if (last_pressed_key_ == key) {
last_pressed_key_ = std::numeric_limits<int>::max();
}
}

void GLCameraMover::update_camera_pos(const float dx, const float dy) {
camera.yaw += dx;
camera.pitch += dy;
camera.pitch = std::clamp(camera.pitch, -89.0f, 89.0f);
}

void GLCameraMover::update_camera_direction(const QPointF &pos) {
auto x_offset = pos.x() - last_mouse_pos_.x();
auto y_offset = last_mouse_pos_.y() - pos.y();
last_mouse_pos_ = pos;

const auto sensitivity = 0.05f;
x_offset *= static_cast<qreal>(sensitivity);
y_offset *= static_cast<qreal>(sensitivity);

update_camera_pos(static_cast<float>(x_offset), static_cast<float>(y_offset));
}
47 changes: 47 additions & 0 deletions src/devyatovskaya/Task4/GLCameraMover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once
#include "GLCamera.h"
#include <limits>

enum class CameraMovement {
FORWARD = Qt::Key_W,
BACKWARD = Qt::Key_S,
LEFT = Qt::Key_A,
RIGHT = Qt::Key_D,
UP = Qt::Key_Space,
DOWN = Qt::Key_Shift,

ROTATE_RIGHT = Qt::Key_Q,
ROTATE_LEFT = Qt::Key_E
};

class GLCameraMover final {
public:
void move();
bool activated{false};
GLCamera camera{{0, 0, 3}};

void set_camera_speed(float delta_time);
void set_mouse_pos(const QPointF &pos);
void update_camera_zoom(float dy);
void set_key(int key);
void reset_key(int key);

private:
int last_pressed_key_{std::numeric_limits<int>::max()};
float initial_camera_speed_{10.5f};
float current_camera_speed_{initial_camera_speed_};

QPointF last_mouse_pos_{};
bool first_mouse_callback_{true};

std::map<CameraMovement, QVector3D> controls_ = {
{CameraMovement::FORWARD, {0, 0, 1}},
{CameraMovement::BACKWARD, {0, 0, -1}},
{CameraMovement::LEFT, {-1, 0, 0}},
{CameraMovement::RIGHT, {1, 0, 0}},
{CameraMovement::UP, {0, 1, 0}},
{CameraMovement::DOWN, {0, -1, 0}}};

void update_camera_pos(float dx, float dy);
void update_camera_direction(const QPointF &pos);
};
Loading