From e2706b13afea149a84d477884688c564d4fb4665 Mon Sep 17 00:00:00 2001 From: "R.E. Kovalev" Date: Tue, 4 Apr 2023 16:47:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=20=D0=BF=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/TRS.h | 9 +++++++++ src/TRS.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 6 ++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/include/TRS.h b/include/TRS.h index 7c3505f..6399f3c 100644 --- a/include/TRS.h +++ b/include/TRS.h @@ -2,6 +2,7 @@ #define TRS_H #define T_SENSITIVITY 0.001f +#define R_SENSITIVITY 0.01f #include "Scene.h" @@ -24,4 +25,12 @@ class Transform : public TRS virtual void process(GLuint64 selectedID, GLuint etc, const glm::vec4& dpos); // Взаимодействие с инструментом }; +// Инструмент поворота +class Rotate : public TRS +{ + public: + Rotate(); + virtual void process(GLuint64 selectedID, GLuint etc, const glm::vec4& drot); // Взаимодействие с инструментом +}; + #endif // TRS_H \ No newline at end of file diff --git a/src/TRS.cpp b/src/TRS.cpp index ec8f714..c140612 100644 --- a/src/TRS.cpp +++ b/src/TRS.cpp @@ -1,4 +1,5 @@ #include "TRS.h" +#include // Инициализирует дополнительную информацию модели void TRS::init_etc() @@ -79,3 +80,55 @@ void Transform::process(GLuint64 selectedID, GLuint etc, const glm::vec4& dpos) selectedObject->e_position() += dVec; } } + +Rotate::Rotate() +{ + tool = loadOBJtoScene("../resources/models/tools/rotate.obj", "../resources/models/tools/", "../resources/textures/"); + + // Масштабирование + tool.root.e_scale() = glm::vec3(0.3); + + int value = 1; + for (auto it = tool.models.begin(); it != tool.models.end(); ++it) + { + it->id.etc = value; + value *= 2; + } +} + +// Взаимодействие с инструментом +void Rotate::process(GLuint64 selectedID, GLuint etc, const glm::vec4& drot) +{ + // Если взаимодействие с осями инструмента + if (etc > 0) + // Если есть выбранная модель - рендерим инструмент для неё + if (selectedID) + { + // Указатель на объект + Node* selectedObject = (Node*) selectedID; + glm::quat &selectedRot = selectedObject->e_rotation(); + + // Матрица родительского поворота + glm::mat3 parentRotation(1); + + // Учет родительского поворота для вращения + Node* parent = selectedObject->getParent(); + if (parent) + { + // Извлекаем 3x3 подматрицу, отвечающую за вращение и масштаб + parentRotation = glm::mat3(parent->getTransformMatrix()); + + // Нормализуем столбцы подматрицы, чтобы исключить масштабирование + for (int i = 0; i < 3; i++) + parentRotation[i] = glm::normalize(parentRotation[i]); + } + + // Поворот по осям + if (etc & 01) + selectedRot = glm::angleAxis(drot.y * R_SENSITIVITY, parentRotation * glm::vec3(1.0f, 0.0f, 0.0f)) * selectedRot; + if (etc & 02) + selectedRot = glm::angleAxis(drot.x * R_SENSITIVITY, parentRotation * glm::vec3(0.0f, 1.0f, 0.0f)) * selectedRot; + if (etc & 04) + selectedRot = glm::angleAxis(drot.z * R_SENSITIVITY, parentRotation * glm::vec3(0.0f, 0.0f, 1.0f)) * selectedRot; + } +} diff --git a/src/main.cpp b/src/main.cpp index 0de3f7c..f66a14f 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -444,6 +444,8 @@ int main(void) // Инструменты Transform transform; + Rotate rotate; + TRS& currentTool = rotate; // Пока не произойдет событие запроса закрытия окна while(!glfwWindowShouldClose(window)) @@ -469,7 +471,7 @@ int main(void) // Используем шейдер для инструментов toolsShader.use(); // Рендерим инструменты для выбранного объекта - transform.render(selected.value, toolsShader, material_data); + currentTool.render(selected.value, toolsShader, material_data); // Выбор объекта if (mouse.left == 0100000) @@ -599,7 +601,7 @@ int main(void) // Взаимодействие с инструментом при зажатой левой кнопке if (mouse.left > 0100000) if (selected.etc) - transform.process(selected.value, selected.etc, glm::transpose(Camera::current().getVP()) * glm::vec4(mouse.x - mouse.prev_x, mouse.prev_y - mouse.y, 0, 1)); + currentTool.process(selected.value, selected.etc, glm::transpose(Camera::current().getVP()) * glm::vec4(mouse.x - mouse.prev_x, mouse.prev_y - mouse.y, 0, 1)); } return 0;