Инструмент поворота
This commit is contained in:
		
							parent
							
								
									8e2bfbd2c8
								
							
						
					
					
						commit
						de8c2a7d68
					
				| @ -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
 | ||||
							
								
								
									
										53
									
								
								src/TRS.cpp
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								src/TRS.cpp
									
									
									
									
									
								
							| @ -1,4 +1,5 @@ | ||||
| #include "TRS.h" | ||||
| #include <GLM/gtx/matrix_decompose.hpp> | ||||
| 
 | ||||
| // Инициализирует дополнительную информацию модели
 | ||||
| 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; | ||||
|         } | ||||
| } | ||||
|  | ||||
| @ -396,6 +396,8 @@ int main(void) | ||||
| 
 | ||||
|     // Инструменты
 | ||||
|     Transform transform; | ||||
|     Rotate rotate; | ||||
|     TRS& currentTool = rotate;  | ||||
| 
 | ||||
|     // Пока не произойдет событие запроса закрытия окна
 | ||||
|     while(!glfwWindowShouldClose(window)) | ||||
| @ -421,7 +423,7 @@ int main(void) | ||||
|         // Используем шейдер для инструментов
 | ||||
|         toolsShader.use(); | ||||
|         // Рендерим инструменты для выбранного объекта
 | ||||
|         transform.render(selected.value, toolsShader, material_data); | ||||
|         currentTool.render(selected.value, toolsShader, material_data); | ||||
| 
 | ||||
|         // Выбор объекта
 | ||||
|         if (mouse.left == 0100000) | ||||
| @ -551,6 +553,6 @@ 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)); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user