src/ui/canvas.cpp

changeset 67
612213a168da
parent 66
77c819262b7a
child 68
ddb07bb6840c
equal deleted inserted replaced
66:77c819262b7a 67:612213a168da
58 this->newStatusText("Position: <none>"_q); 58 this->newStatusText("Position: <none>"_q);
59 } 59 }
60 */ 60 */
61 // use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can 61 // use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can
62 // automatically change it properly 62 // automatically change it properly
63 this->newStatusText("Change: %1"_q.arg(isGridPerpendicularToScreen(0.03f) ? "yes" : "no")); 63 if (isGridPerpendicularToScreen(0.03f))
64 {
65 const glm::vec3 cameraDirection = this->cameraVector();
66 const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
67 const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
68 const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
69 const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
70 if (angle_x < angle_y)
71 {
72 this->newStatusText("rotate by X axis");
73 this->gridMatrix = glm::rotate(this->gridMatrix, float{M_PI} / 2, glm::vec3{1, 0, 0});
74 }
75 else
76 {
77 this->newStatusText("rotate by Y axis");
78 this->gridMatrix = glm::rotate(this->gridMatrix, float{M_PI} / 2, glm::vec3{0, 1, 0});
79 }
80 this->updateGridMatrix();
81 this->update();
82 }
83 else
84 {
85 this->newStatusText("don't rotate");
86 }
64 PartRenderer::mouseMoveEvent(event); 87 PartRenderer::mouseMoveEvent(event);
65 } 88 }
66 89
67 void Canvas::mousePressEvent(QMouseEvent* event) 90 void Canvas::mousePressEvent(QMouseEvent* event)
68 { 91 {
115 const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25; 138 const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
116 this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black); 139 this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
117 } 140 }
118 }); 141 });
119 PartRenderer::initializeGL(); 142 PartRenderer::initializeGL();
143 /*
120 this->gridMatrix = glm::mat4{ 144 this->gridMatrix = glm::mat4{
121 {-4, 0, 0, 0}, 145 {-4, 0, 0, 0},
122 {0, 6.9266, -3.6955, 0}, 146 {0, 6.9266, -3.6955, 0},
123 {0, -16.7222, -1.5307, 0}, 147 {0, -16.7222, -1.5307, 0},
124 {0, -13.273, -9.255, 1}, 148 {0, -13.273, -9.255, 1},
125 }; 149 };
150 */
151 this->gridMatrix = glm::mat4{1};
126 this->updateGridMatrix(); 152 this->updateGridMatrix();
127 } 153 }
128 154
129 void Canvas::paintGL() 155 void Canvas::paintGL()
130 { 156 {
171 }; 197 };
172 this->gridPlane = geom::planeFromTriangle(triangle); 198 this->gridPlane = geom::planeFromTriangle(triangle);
173 this->gridProgram->setGridMatrix(this->gridMatrix); 199 this->gridProgram->setGridMatrix(this->gridMatrix);
174 } 200 }
175 201
202 glm::vec3 Canvas::cameraVector() const
203 {
204 // Find out where the grid is projected on the screen
205 const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
206 // Find out which direction the camera is looking at the grid origin in 3d
207 return glm::normalize(this->cameraLine(gridOrigin2d).direction);
208 }
209
176 /** 210 /**
177 * @brief Calculates if the screen is perpendicular to the current grid 211 * @brief Calculates if the screen is perpendicular to the current grid
178 * @return yes no 212 * @return yes no
179 */ 213 */
180 bool Canvas::isGridPerpendicularToScreen(float threshold) const 214 bool Canvas::isGridPerpendicularToScreen(float threshold) const
181 { 215 {
182 // Find out where the grid is projected on the screen 216 const glm::vec3 cameraDirection = this->cameraVector();
183 const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
184 // Find out which direction the camera is looking at the grid origin in 3d
185 const glm::vec3 cameraDirection = glm::normalize(this->cameraLine(gridOrigin2d).direction);
186 // Compute the dot product. The parameters given are: 217 // Compute the dot product. The parameters given are:
187 // - the normal of the grid plane, which is the vector from the grid origin perpendicular to the grid 218 // - the normal of the grid plane, which is the vector from the grid origin perpendicular to the grid
188 // - the direction of the camera looking at the grid, which is the inverse of the vector from the grid 219 // - the direction of the camera looking at the grid, which is the inverse of the vector from the grid
189 // origin towards the camera 220 // origin towards the camera
190 // If the dot product between these two vectors is 0, the grid normal is perpendicular to the camera vector 221 // If the dot product between these two vectors is 0, the grid normal is perpendicular to the camera vector

mercurial