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 task3 #35

Open
wants to merge 16 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,2 +1,3 @@
add_subdirectory(Task1)
add_subdirectory(Task2)
add_subdirectory(Task3)
83 changes: 83 additions & 0 deletions src/devyatovskaya/Task3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
set(Task3_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
)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(DevyatovskayaTask3 ${Task3_SRSC})

target_link_libraries(DevyatovskayaTask3 PRIVATE Qt5::Widgets)

30 changes: 30 additions & 0 deletions src/devyatovskaya/Task3/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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.5f лучше вынести в отдельную compile-time константу с понятным названием.


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/Task3/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 &);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сигнал не реализован


public:
void calculate_fps();

float delta_time() const;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
float delta_time() const;
[[nodiscard]] float delta_time() const;


private:
QString fps_to_str() const;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
QString fps_to_str() const;
[[nodiscard]] 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/Task3/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/Task3/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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему поля класса публичные?

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
QMatrix4x4 get_projection_matrix() const;
[[nodiscard]] QMatrix4x4 get_projection_matrix() const;

QMatrix4x4 get_view_matrix() const;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
QMatrix4x4 get_view_matrix() const;
[[nodiscard]] QMatrix4x4 get_view_matrix() const;


void update_camera_vectors();
void translate(const QVector3D &mult);
};
62 changes: 62 additions & 0 deletions src/devyatovskaya/Task3/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_)];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если move вызовется до установки валидного значения last_pressed_key_, поведение будет странным.

if (direction == QVector3D{0, 0, 0}) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Похоже на "костыль" из-за неправильной работы с пользовательскими событиями.

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для таких целей есть std::clamp

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/Task3/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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не используются

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

ROTATE_LEFT = Qt::Key_E
};

class GLCameraMover final {
public:
void move();
bool activated{false};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Опять публичные поля

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()};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это вообще не правильно. Если вы вызовите у controls_ оператор квадратные скобочки с таким значением, то этот элемент добавится и будет иметь невалидное значение

float initial_camera_speed_{10.5f};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для таких "магических чисел" лучше давать осмысленное название или хотя бы писать комментарий

float current_camera_speed_{initial_camera_speed_};

QPointF last_mouse_pos_{};
bool first_mouse_callback_{true};

std::map<CameraMovement, QVector3D> controls_ = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем вам упорядоченность?

{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