diff --git a/include/TRS.h b/include/TRS.h index 6399f3c..864f617 100644 --- a/include/TRS.h +++ b/include/TRS.h @@ -3,6 +3,7 @@ #define T_SENSITIVITY 0.001f #define R_SENSITIVITY 0.01f +#define S_SENSITIVITY 0.00001f #include "Scene.h" @@ -33,4 +34,12 @@ class Rotate : public TRS virtual void process(GLuint64 selectedID, GLuint etc, const glm::vec4& drot); // Взаимодействие с инструментом }; +// Инструмент масштабирования +class Scale : public TRS +{ + public: + Scale(); + virtual void process(GLuint64 selectedID, GLuint etc, const glm::vec4& dscale); // Взаимодействие с инструментом +}; + #endif // TRS_H \ No newline at end of file diff --git a/src/TRS.cpp b/src/TRS.cpp index c140612..0f0ca44 100644 --- a/src/TRS.cpp +++ b/src/TRS.cpp @@ -132,3 +132,56 @@ void Rotate::process(GLuint64 selectedID, GLuint etc, const glm::vec4& drot) selectedRot = glm::angleAxis(drot.z * R_SENSITIVITY, parentRotation * glm::vec3(0.0f, 0.0f, 1.0f)) * selectedRot; } } + +Scale::Scale() +{ + tool = loadOBJtoScene("../resources/models/tools/scale.obj", "../resources/models/tools/", "../resources/textures/"); + + // Масштабирование + tool.root.e_scale() = glm::vec3(0.3); + + // Инициализация дополнительной информации + init_etc(); +} + +// Взаимодействие с инструментом +void Scale::process(GLuint64 selectedID, GLuint etc, const glm::vec4& dscale) +{ + // Если взаимодействие с осями инструмента + if (etc > 0) + // Если есть выбранная модель - рендерим инструмент для неё + if (selectedID) + { + // Указатель на объект + Node* selectedObject = (Node*) selectedID; + + // Для хранения результата + glm::vec3 dVec(0); + + // Масштабирование с учетом чувствительности для соответствующих осей + if (etc & 01) + dVec.x = dscale.x * S_SENSITIVITY; + if (etc & 02) + dVec.y = dscale.y * S_SENSITIVITY; + if (etc & 04) + dVec.z = dscale.z * S_SENSITIVITY; + + // Если есть родитель - требуется учесть его поворот + Node* parent = selectedObject->getParent(); + if (parent) + { + // Извлекаем 3x3 подматрицу, отвечающую за вращение и масштаб + glm::mat3 rotationMatrix = glm::mat3(parent->getTransformMatrix()); + + // Нормализуем столбцы подматрицы, чтобы исключить масштабирование + for (int i = 0; i < 3; i++) + rotationMatrix[i] = glm::normalize(rotationMatrix[i]); + + // Применим поворот родителя к вектору сдвига + dVec = glm::inverse(rotationMatrix) * dVec; + } + + // Прибавим вектор масштабирования к объекту + selectedObject->e_scale() += dVec; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f66a14f..ee121fd 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -445,7 +445,8 @@ int main(void) // Инструменты Transform transform; Rotate rotate; - TRS& currentTool = rotate; + Scale scale; + TRS& currentTool = scale; // Пока не произойдет событие запроса закрытия окна while(!glfwWindowShouldClose(window))