-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from all commits
e171a17
7b05d0a
e39cfb9
2923fb2
7a0f445
6b60ead
1f42dd4
51818c3
3b92dcf
d5c7649
9268ba9
ebb6d1e
f481778
f00d30c
a0285a6
a1549d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(Task1) | ||
add_subdirectory(Task2) | ||
add_subdirectory(Task3) |
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) | ||
|
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; | ||
} |
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 &); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сигнал не реализован |
||||||
|
||||||
public: | ||||||
void calculate_fps(); | ||||||
|
||||||
float delta_time() const; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
private: | ||||||
QString fps_to_str() const; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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}; | ||||||
}; |
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(); | ||
} |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
QMatrix4x4 get_view_matrix() const; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
void update_camera_vectors(); | ||||||
void translate(const QVector3D &mult); | ||||||
}; |
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_)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если |
||
if (direction == QVector3D{0, 0, 0}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для таких целей есть |
||
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)); | ||
} |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не используются
|
||
ROTATE_LEFT = Qt::Key_E | ||
}; | ||
|
||
class GLCameraMover final { | ||
public: | ||
void move(); | ||
bool activated{false}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это вообще не правильно. Если вы вызовите у |
||
float initial_camera_speed_{10.5f}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_ = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.5f
лучше вынести в отдельную compile-time константу с понятным названием.