src/glrenderer.cpp

changeset 1436
241d3e452b32
parent 1428
ece049033adc
child 1437
1a77c6156db7
child 1440
265b2e95a8e8
equal deleted inserted replaced
1435:b8dc3620e5db 1436:241d3e452b32
32 #include "glcompiler.h" 32 #include "glcompiler.h"
33 #include "primitives.h" 33 #include "primitives.h"
34 #include "documentmanager.h" 34 #include "documentmanager.h"
35 #include "grid.h" 35 #include "grid.h"
36 36
37 const QPen GLRenderer::thinBorderPen {QColor {0, 0, 0, 208}, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin};
38
39 // Transformation matrices for the fixed cameras.
40 const QMatrix4x4 GLRenderer::topCameraMatrix = QMatrix4x4 {};
41 const QMatrix4x4 GLRenderer::frontCameraMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1};
42 const QMatrix4x4 GLRenderer::leftCameraMatrix = {0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1};
43 const QMatrix4x4 GLRenderer::bottomCameraMatrix = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1};
44 const QMatrix4x4 GLRenderer::backCameraMatrix = {-1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1};
45 const QMatrix4x4 GLRenderer::rightCameraMatrix = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1};
46
47 // Conversion matrix from LDraw to OpenGL coordinates.
48 const QMatrix4x4 GLRenderer::ldrawToGLAdapterMatrix = {1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1};
49
50 /* 37 /*
51 * Constructs a GL renderer. 38 * Constructs a GL renderer.
52 */ 39 */
53 GLRenderer::GLRenderer(const Model* model, QWidget* parent) : 40 gl::Renderer::Renderer(const Model* model, QWidget* parent) :
54 QGLWidget {parent}, 41 QGLWidget {parent},
55 HierarchyElement {parent}, 42 HierarchyElement {parent},
56 m_model {model}, 43 m_model {model},
57 m_cameras { 44 m_cameras {
58 {"Top camera", {topCameraMatrix, X, Z, false, false, false}}, // top 45 {"Top camera", {topCameraMatrix, X, Z, false, false, false}}, // top
63 {"Right camera", {rightCameraMatrix, Z, Y, false, true, true}}, // right 50 {"Right camera", {rightCameraMatrix, Z, Y, false, true, true}}, // right
64 {"Free camera", GLCamera::FreeCamera}, // free 51 {"Free camera", GLCamera::FreeCamera}, // free
65 } 52 }
66 { 53 {
67 Q_ASSERT(model != nullptr); 54 Q_ASSERT(model != nullptr);
68 m_camera = (Camera) config::camera(); 55 m_camera = (gl::CameraType) config::camera();
69 m_compiler = new GLCompiler (this); 56 m_compiler = new gl::Compiler (this);
70 m_toolTipTimer = new QTimer (this); 57 m_toolTipTimer = new QTimer (this);
71 m_toolTipTimer->setSingleShot (true); 58 m_toolTipTimer->setSingleShot (true);
72 setAcceptDrops (true); 59 setAcceptDrops (true);
73 connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (showCameraIconTooltip())); 60 connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (showCameraIconTooltip()));
74 resetAllAngles(); 61 resetAllAngles();
75 m_needZoomToFit = true; 62 m_needZoomToFit = true;
76 63
77 // Init camera icons 64 // Init camera icons
78 for (Camera camera : iterateEnum<Camera>()) 65 for (gl::CameraType camera : iterateEnum<gl::CameraType>())
79 { 66 {
80 const char* cameraIconNames[EnumLimits<Camera>::Count] = 67 const char* cameraIconNames[EnumLimits<gl::CameraType>::Count] =
81 { 68 {
82 "camera-top", "camera-front", "camera-left", 69 "camera-top", "camera-front", "camera-left",
83 "camera-bottom", "camera-back", "camera-right", 70 "camera-bottom", "camera-back", "camera-right",
84 "camera-free" 71 "camera-free"
85 }; 72 };
89 info->camera = camera; 76 info->camera = camera;
90 } 77 }
91 78
92 connect( 79 connect(
93 this->m_compiler, 80 this->m_compiler,
94 &GLCompiler::sceneChanged, 81 &gl::Compiler::sceneChanged,
95 this, 82 this,
96 qOverload<>(&GLRenderer::update) 83 qOverload<>(&gl::Renderer::update)
97 ); 84 );
98 85
99 calcCameraIcons(); 86 calcCameraIcons();
100 } 87 }
101 88
102 /* 89 /*
103 * Destructs the GL renderer. 90 * Destructs the GL renderer.
104 */ 91 */
105 GLRenderer::~GLRenderer() 92 gl::Renderer::~Renderer()
106 { 93 {
107 freeAxes(); 94 freeAxes();
108 } 95 }
109 96
110 /* 97 /*
111 * Deletes the axes VBOs 98 * Deletes the axes VBOs
112 */ 99 */
113 void GLRenderer::freeAxes() 100 void gl::Renderer::freeAxes()
114 { 101 {
115 if (m_axesInitialized) 102 if (m_axesInitialized)
116 { 103 {
117 glDeleteBuffers(1, &m_axesVbo); 104 glDeleteBuffers(1, &m_axesVbo);
118 glDeleteBuffers(1, &m_axesColorVbo); 105 glDeleteBuffers(1, &m_axesColorVbo);
121 } 108 }
122 109
123 /* 110 /*
124 * Calculates the camera icon locations. 111 * Calculates the camera icon locations.
125 */ 112 */
126 void GLRenderer::calcCameraIcons() 113 void gl::Renderer::calcCameraIcons()
127 { 114 {
128 int i = 0; 115 int i = 0;
129 const int columns = 3; 116 const int columns = 3;
130 const int firstAtLastRow = countof(m_cameras) - (countof(m_cameras) % columns); 117 const int firstAtLastRow = countof(m_cameras) - (countof(m_cameras) % columns);
131 118
155 } 142 }
156 143
157 /* 144 /*
158 * Returns the camera currently in use. 145 * Returns the camera currently in use.
159 */ 146 */
160 GLCamera& GLRenderer::currentCamera() 147 GLCamera& gl::Renderer::currentCamera()
161 { 148 {
162 return m_cameras[static_cast<int>(camera())]; 149 return m_cameras[static_cast<int>(camera())];
163 } 150 }
164 151
165 /* 152 /*
166 * Returns the camera currently in use. 153 * Returns the camera currently in use.
167 */ 154 */
168 const GLCamera& GLRenderer::currentCamera() const 155 const GLCamera& gl::Renderer::currentCamera() const
169 { 156 {
170 return m_cameras[static_cast<int>(camera())]; 157 return m_cameras[static_cast<int>(camera())];
171 } 158 }
172 159
173 /* 160 /*
174 * Prepares the GL context for rendering. 161 * Prepares the GL context for rendering.
175 */ 162 */
176 void GLRenderer::initGLData() 163 void gl::Renderer::initGLData()
177 { 164 {
178 glEnable (GL_BLEND); 165 glEnable (GL_BLEND);
179 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 166 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
180 glEnable (GL_POLYGON_OFFSET_FILL); 167 glEnable (GL_POLYGON_OFFSET_FILL);
181 glPolygonOffset (1.0f, 1.0f); 168 glPolygonOffset (1.0f, 1.0f);
195 } 182 }
196 183
197 /* 184 /*
198 * Returns the object currently highlighted by the cursor. 185 * Returns the object currently highlighted by the cursor.
199 */ 186 */
200 QPersistentModelIndex GLRenderer::objectAtCursor() const 187 QPersistentModelIndex gl::Renderer::objectAtCursor() const
201 { 188 {
202 return m_objectAtCursor; 189 return m_objectAtCursor;
203 } 190 }
204 191
205 // ============================================================================= 192 // =============================================================================
206 // 193 //
207 void GLRenderer::needZoomToFit() 194 void gl::Renderer::needZoomToFit()
208 { 195 {
209 m_needZoomToFit = true; 196 m_needZoomToFit = true;
210 } 197 }
211 198
212 // ============================================================================= 199 // =============================================================================
213 // 200 //
214 void GLRenderer::resetAngles() 201 void gl::Renderer::resetAngles()
215 { 202 {
216 if (m_initialized) 203 if (m_initialized)
217 { 204 {
218 m_rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); 205 m_rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
219 m_rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); 206 m_rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
222 needZoomToFit(); 209 needZoomToFit();
223 } 210 }
224 211
225 // ============================================================================= 212 // =============================================================================
226 // 213 //
227 void GLRenderer::resetAllAngles() 214 void gl::Renderer::resetAllAngles()
228 { 215 {
229 Camera oldCamera = camera(); 216 gl::CameraType const oldCamera = camera();
230 217
231 for (Camera camera : iterateEnum<Camera>()) 218 for (gl::CameraType camera : iterateEnum<gl::CameraType>())
232 { 219 {
233 setCamera(camera); 220 setCamera(camera);
234 resetAngles(); 221 resetAngles();
235 } 222 }
236 223
237 setCamera(oldCamera); 224 setCamera(oldCamera);
238 } 225 }
239 226
240 // ============================================================================= 227 // =============================================================================
241 // 228 //
242 void GLRenderer::initializeGL() 229 void gl::Renderer::initializeGL()
243 { 230 {
244 initializeOpenGLFunctions(); 231 initializeOpenGLFunctions();
245 232
246 if (glGetError() != GL_NO_ERROR) 233 if (glGetError() != GL_NO_ERROR)
247 { 234 {
260 m_initialized = true; 247 m_initialized = true;
261 // Now that GL is initialized, we can reset angles. 248 // Now that GL is initialized, we can reset angles.
262 resetAllAngles(); 249 resetAllAngles();
263 } 250 }
264 251
265 void GLRenderer::initializeLighting() 252 void gl::Renderer::initializeLighting()
266 { 253 {
267 GLfloat materialShininess[] = {5.0}; 254 GLfloat materialShininess[] = {5.0};
268 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; 255 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
269 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; 256 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
270 glShadeModel(GL_SMOOTH); 257 glShadeModel(GL_SMOOTH);
278 glEnable(GL_DEPTH_TEST); 265 glEnable(GL_DEPTH_TEST);
279 } 266 }
280 267
281 // ============================================================================= 268 // =============================================================================
282 // 269 //
283 void GLRenderer::initializeAxes() 270 void gl::Renderer::initializeAxes()
284 { 271 {
285 freeAxes(); 272 freeAxes();
286 float axisVertexData[3][6]; 273 float axisVertexData[3][6];
287 float axisColorData[3][6]; 274 float axisColorData[3][6];
288 275
312 glBindBuffer(GL_ARRAY_BUFFER, 0); 299 glBindBuffer(GL_ARRAY_BUFFER, 0);
313 } 300 }
314 301
315 // ============================================================================= 302 // =============================================================================
316 // 303 //
317 void GLRenderer::setBackground() 304 void gl::Renderer::setBackground()
318 { 305 {
319 if (not m_isDrawingSelectionScene) 306 if (not m_isDrawingSelectionScene)
320 { 307 {
321 // Otherwise use the background that the user wants. 308 // Otherwise use the background that the user wants.
322 QColor color = config::backgroundColor(); 309 QColor color = config::backgroundColor();
334 // The picking scene requires a black background. 321 // The picking scene requires a black background.
335 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 322 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
336 } 323 }
337 } 324 }
338 325
339 QColor GLRenderer::backgroundColor() const 326 QColor gl::Renderer::backgroundColor() const
340 { 327 {
341 return m_backgroundColor; 328 return m_backgroundColor;
342 } 329 }
343 330
344 // ============================================================================= 331 // =============================================================================
345 // 332 //
346 void GLRenderer::resizeGL (int width, int height) 333 void gl::Renderer::resizeGL (int width, int height)
347 { 334 {
348 calcCameraIcons(); 335 calcCameraIcons();
349 glViewport (0, 0, width, height); 336 glViewport (0, 0, width, height);
350 glMatrixMode (GL_PROJECTION); 337 glMatrixMode (GL_PROJECTION);
351 glLoadIdentity(); 338 glLoadIdentity();
370 }; 357 };
371 } 358 }
372 359
373 // ============================================================================= 360 // =============================================================================
374 // 361 //
375 void GLRenderer::drawGLScene() 362 void gl::Renderer::drawGLScene()
376 { 363 {
377 if (m_needZoomToFit) 364 if (m_needZoomToFit)
378 { 365 {
379 m_needZoomToFit = false; 366 m_needZoomToFit = false;
380 zoomAllToFit(); 367 zoomAllToFit();
389 if (config::lighting() and not m_isDrawingSelectionScene) 376 if (config::lighting() and not m_isDrawingSelectionScene)
390 glEnable(GL_LIGHTING); 377 glEnable(GL_LIGHTING);
391 else 378 else
392 glDisable(GL_LIGHTING); 379 glDisable(GL_LIGHTING);
393 380
394 if (camera() != Camera::Free) 381 if (camera() != gl::FreeCamera)
395 { 382 {
396 glMatrixMode (GL_PROJECTION); 383 glMatrixMode (GL_PROJECTION);
397 glPushMatrix(); 384 glPushMatrix();
398 glLoadIdentity(); 385 glLoadIdentity();
399 glMultMatrixf(currentCamera().realMatrix().constData()); 386 glMultMatrixf(currentCamera().realMatrix().constData());
485 * 472 *
486 * Parameters: 473 * Parameters:
487 * - surface determines what kind of surface to draw (triangles, quadrilaterals, edges or conditional edges) 474 * - surface determines what kind of surface to draw (triangles, quadrilaterals, edges or conditional edges)
488 * - colors determines what VBO subclass to use for colors 475 * - colors determines what VBO subclass to use for colors
489 */ 476 */
490 void GLRenderer::drawVbos(VboClass surface, VboSubclass colors) 477 void gl::Renderer::drawVbos(VboClass surface, VboSubclass colors)
491 { 478 {
492 // Filter this through some configuration options 479 // Filter this through some configuration options
493 if ((isOneOf(surface, VboClass::Quads, VboClass::Triangles) and config::drawSurfaces() == false) 480 if ((isOneOf(surface, VboClass::Quads, VboClass::Triangles) and config::drawSurfaces() == false)
494 or (surface == VboClass::Lines and config::drawEdgeLines() == false) 481 or (surface == VboClass::Lines and config::drawEdgeLines() == false)
495 or (surface == VboClass::ConditionalLines and config::drawConditionalLines() == false)) 482 or (surface == VboClass::ConditionalLines and config::drawConditionalLines() == false))
548 glDrawArrays(type, 0, count); 535 glDrawArrays(type, 0, count);
549 CHECK_GL_ERROR(); 536 CHECK_GL_ERROR();
550 } 537 }
551 } 538 }
552 539
553 QPen GLRenderer::textPen() const 540 QPen gl::Renderer::textPen() const
554 { 541 {
555 return {m_useDarkBackground ? Qt::white : Qt::black}; 542 return {m_useDarkBackground ? Qt::white : Qt::black};
556 } 543 }
557 544
558 bool GLRenderer::freeCameraAllowed() const 545 bool gl::Renderer::freeCameraAllowed() const
559 { 546 {
560 return true; 547 return true;
561 } 548 }
562 549
563 void GLRenderer::paintEvent(QPaintEvent*) 550 void gl::Renderer::paintEvent(QPaintEvent*)
564 { 551 {
565 makeCurrent(); 552 makeCurrent();
566 initGLData(); 553 initGLData();
567 drawGLScene(); 554 drawGLScene();
568 555
572 QPainter painter {this}; 559 QPainter painter {this};
573 painter.setRenderHint(QPainter::Antialiasing); 560 painter.setRenderHint(QPainter::Antialiasing);
574 overpaint(painter); 561 overpaint(painter);
575 } 562 }
576 563
577 void GLRenderer::overpaint(QPainter &painter) 564 void gl::Renderer::overpaint(QPainter &painter)
578 { 565 {
579 // Draw a background for the selected camera 566 // Draw a background for the selected camera
580 painter.setPen(thinBorderPen); 567 painter.setPen(thinBorderPen);
581 painter.setBrush(QBrush {QColor {0, 128, 160, 128}}); 568 painter.setBrush(QBrush {QColor {0, 128, 160, 128}});
582 painter.drawRect(m_cameraIcons[static_cast<int>(camera())].hitRect); 569 painter.drawRect(m_cameraIcons[static_cast<int>(camera())].hitRect);
583 570
584 // Draw the camera icons 571 // Draw the camera icons
585 for (const CameraIcon& info : m_cameraIcons) 572 for (const CameraIcon& info : m_cameraIcons)
586 { 573 {
587 // Don't draw the free camera icon when we can't use the free camera 574 // Don't draw the free camera icon when we can't use the free camera
588 if (info.camera == Camera::Free and not freeCameraAllowed()) 575 if (info.camera == gl::FreeCamera and not freeCameraAllowed())
589 continue; 576 continue;
590 577
591 painter.drawPixmap(info.targetRect, info.image, info.sourceRect); 578 painter.drawPixmap(info.targetRect, info.image, info.sourceRect);
592 } 579 }
593 580
600 } 587 }
601 } 588 }
602 589
603 // ============================================================================= 590 // =============================================================================
604 // 591 //
605 void GLRenderer::mouseReleaseEvent(QMouseEvent* event) 592 void gl::Renderer::mouseReleaseEvent(QMouseEvent* event)
606 { 593 {
607 bool wasLeft = (m_lastButtons & Qt::LeftButton) and not (event->buttons() & Qt::LeftButton); 594 bool wasLeft = (m_lastButtons & Qt::LeftButton) and not (event->buttons() & Qt::LeftButton);
608 m_panning = false; 595 m_panning = false;
609 596
610 // Check if we selected a camera icon 597 // Check if we selected a camera icon
624 m_totalMouseMove = 0; 611 m_totalMouseMove = 0;
625 } 612 }
626 613
627 // ============================================================================= 614 // =============================================================================
628 // 615 //
629 void GLRenderer::mousePressEvent(QMouseEvent* event) 616 void gl::Renderer::mousePressEvent(QMouseEvent* event)
630 { 617 {
631 m_lastButtons = event->buttons(); 618 m_lastButtons = event->buttons();
632 m_totalMouseMove = 0; 619 m_totalMouseMove = 0;
633 } 620 }
634 621
635 // ============================================================================= 622 // =============================================================================
636 // 623 //
637 void GLRenderer::mouseMoveEvent(QMouseEvent* event) 624 void gl::Renderer::mouseMoveEvent(QMouseEvent* event)
638 { 625 {
639 int xMove = event->x() - m_mousePosition.x(); 626 int xMove = event->x() - m_mousePosition.x();
640 int yMove = event->y() - m_mousePosition.y(); 627 int yMove = event->y() - m_mousePosition.y();
641 m_totalMouseMove += qAbs(xMove) + qAbs(yMove); 628 m_totalMouseMove += qAbs(xMove) + qAbs(yMove);
642 m_isCameraMoving = false; 629 m_isCameraMoving = false;
649 { 636 {
650 currentCamera().pan(xMove, yMove); 637 currentCamera().pan(xMove, yMove);
651 m_panning = true; 638 m_panning = true;
652 m_isCameraMoving = true; 639 m_isCameraMoving = true;
653 } 640 }
654 else if (left and camera() == Camera::Free and (xMove != 0 or yMove != 0)) 641 else if (left and camera() == gl::FreeCamera and (xMove != 0 or yMove != 0))
655 { 642 {
656 QQuaternion versor = QQuaternion::fromAxisAndAngle(yMove, xMove, 0, 0.6 * hypot(xMove, yMove)); 643 QQuaternion versor = QQuaternion::fromAxisAndAngle(yMove, xMove, 0, 0.6 * hypot(xMove, yMove));
657 m_rotation = versor * m_rotation; 644 m_rotation = versor * m_rotation;
658 m_isCameraMoving = true; 645 m_isCameraMoving = true;
659 } 646 }
671 event->accept(); 658 event->accept();
672 } 659 }
673 660
674 // ============================================================================= 661 // =============================================================================
675 // 662 //
676 void GLRenderer::keyPressEvent(QKeyEvent* event) 663 void gl::Renderer::keyPressEvent(QKeyEvent* event)
677 { 664 {
678 m_currentKeyboardModifiers = event->modifiers(); 665 m_currentKeyboardModifiers = event->modifiers();
679 } 666 }
680 667
681 // ============================================================================= 668 // =============================================================================
682 // 669 //
683 void GLRenderer::keyReleaseEvent(QKeyEvent* event) 670 void gl::Renderer::keyReleaseEvent(QKeyEvent* event)
684 { 671 {
685 m_currentKeyboardModifiers = event->modifiers(); 672 m_currentKeyboardModifiers = event->modifiers();
686 update(); 673 update();
687 } 674 }
688 675
689 // ============================================================================= 676 // =============================================================================
690 // 677 //
691 void GLRenderer::wheelEvent(QWheelEvent* ev) 678 void gl::Renderer::wheelEvent(QWheelEvent* ev)
692 { 679 {
693 makeCurrent(); 680 makeCurrent();
694 currentCamera().zoomNotch(ev->delta() > 0); 681 currentCamera().zoomNotch(ev->delta() > 0);
695 m_isCameraMoving = true; 682 m_isCameraMoving = true;
696 update(); 683 update();
697 ev->accept(); 684 ev->accept();
698 } 685 }
699 686
700 // ============================================================================= 687 // =============================================================================
701 // 688 //
702 void GLRenderer::leaveEvent(QEvent*) 689 void gl::Renderer::leaveEvent(QEvent*)
703 { 690 {
704 m_toolTipTimer->stop(); 691 m_toolTipTimer->stop();
705 update(); 692 update();
706 } 693 }
707 694
708 // ============================================================================= 695 // =============================================================================
709 // 696 //
710 void GLRenderer::setCamera(Camera camera) 697 void gl::Renderer::setCamera(gl::CameraType camera)
711 { 698 {
712 // The edit mode may forbid the free camera. 699 // The edit mode may forbid the free camera.
713 if (freeCameraAllowed() or camera != Camera::Free) 700 if (freeCameraAllowed() or camera != gl::FreeCamera)
714 { 701 {
715 m_camera = camera; 702 m_camera = camera;
716 config::setCamera(static_cast<int>(camera)); 703 config::setCamera(static_cast<int>(camera));
717 } 704 }
718 } 705 }
727 } 714 }
728 715
729 /* 716 /*
730 * Returns the set of objects found in the specified pixel area. 717 * Returns the set of objects found in the specified pixel area.
731 */ 718 */
732 QItemSelection GLRenderer::pick(const QRect& range) 719 QItemSelection gl::Renderer::pick(const QRect& range)
733 { 720 {
734 makeCurrent(); 721 makeCurrent();
735 QItemSelection result; 722 QItemSelection result;
736 723
737 // Paint the picking scene 724 // Paint the picking scene
785 repaint(); 772 repaint();
786 return result; 773 return result;
787 } 774 }
788 775
789 /* 776 /*
790 * Simpler version of GLRenderer::pick which simply picks whatever object on the cursor 777 * Simpler version of gl::Renderer::pick which simply picks whatever object on the cursor
791 */ 778 */
792 QModelIndex GLRenderer::pick(int mouseX, int mouseY) 779 QModelIndex gl::Renderer::pick(int mouseX, int mouseY)
793 { 780 {
794 makeCurrent(); 781 makeCurrent();
795 setPicking(true); 782 setPicking(true);
796 drawGLScene(); 783 drawGLScene();
797 unsigned char pixel[4]; 784 unsigned char pixel[4];
802 return result; 789 return result;
803 } 790 }
804 791
805 // ============================================================================= 792 // =============================================================================
806 // 793 //
807 void GLRenderer::setPicking(bool picking) 794 void gl::Renderer::setPicking(bool picking)
808 { 795 {
809 m_isDrawingSelectionScene = picking; 796 m_isDrawingSelectionScene = picking;
810 setBackground(); 797 setBackground();
811 798
812 if (m_isDrawingSelectionScene) 799 if (m_isDrawingSelectionScene)
826 } 813 }
827 814
828 /* 815 /*
829 * Returns an image containing the current render of the scene. 816 * Returns an image containing the current render of the scene.
830 */ 817 */
831 QImage GLRenderer::screenCapture() 818 QImage gl::Renderer::screenCapture()
832 { 819 {
833 // Read the current render to a buffer of pixels. We use RGBA format even though the image should be fully opaque at all times. 820 // Read the current render to a buffer of pixels. We use RGBA format even though the image should be fully opaque at all times.
834 // This is because apparently GL_RGBA/GL_UNSIGNED_BYTE is the only setting pair that is guaranteed to actually work! 821 // This is because apparently GL_RGBA/GL_UNSIGNED_BYTE is the only setting pair that is guaranteed to actually work!
835 // ref: https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glReadPixels.xml 822 // ref: https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glReadPixels.xml
836 QVector<unsigned char> pixelData; 823 QVector<unsigned char> pixelData;
843 } 830 }
844 831
845 /* 832 /*
846 * Show a tooltip if the cursor is currently hovering over a camera icon. 833 * Show a tooltip if the cursor is currently hovering over a camera icon.
847 */ 834 */
848 void GLRenderer::showCameraIconTooltip() 835 void gl::Renderer::showCameraIconTooltip()
849 { 836 {
850 for (CameraIcon & icon : m_cameraIcons) 837 for (CameraIcon & icon : m_cameraIcons)
851 { 838 {
852 if (icon.targetRect.contains (m_mousePosition)) 839 if (icon.targetRect.contains (m_mousePosition))
853 { 840 {
858 } 845 }
859 } 846 }
860 847
861 // ============================================================================= 848 // =============================================================================
862 // 849 //
863 void GLRenderer::zoomToFit() 850 void gl::Renderer::zoomToFit()
864 { 851 {
865 currentCamera().setZoom(30.0f); 852 currentCamera().setZoom(30.0f);
866 bool lastfilled = false; 853 bool lastfilled = false;
867 bool firstrun = true; 854 bool firstrun = true;
868 bool inward = true; 855 bool inward = true;
943 setPicking (false); 930 setPicking (false);
944 } 931 }
945 932
946 // ============================================================================= 933 // =============================================================================
947 // 934 //
948 void GLRenderer::zoomAllToFit() 935 void gl::Renderer::zoomAllToFit()
949 { 936 {
950 zoomToFit(); 937 zoomToFit();
951 } 938 }
952 939
953 // ============================================================================= 940 // =============================================================================
954 // 941 //
955 void GLRenderer::highlightCursorObject() 942 void gl::Renderer::highlightCursorObject()
956 { 943 {
957 if (not config::highlightObjectBelowCursor() and not objectAtCursor().isValid()) 944 if (not config::highlightObjectBelowCursor() and not objectAtCursor().isValid())
958 return; 945 return;
959 946
960 QModelIndex newIndex; 947 QModelIndex newIndex;
985 } 972 }
986 973
987 update(); 974 update();
988 } 975 }
989 976
990 bool GLRenderer::mouseHasMoved() const 977 bool gl::Renderer::mouseHasMoved() const
991 { 978 {
992 return m_totalMouseMove >= 10; 979 return m_totalMouseMove >= 10;
993 } 980 }
994 981
995 QPoint const& GLRenderer::mousePosition() const 982 QPoint const& gl::Renderer::mousePosition() const
996 { 983 {
997 return m_mousePosition; 984 return m_mousePosition;
998 } 985 }
999 986
1000 QPointF const& GLRenderer::mousePositionF() const 987 QPointF const& gl::Renderer::mousePositionF() const
1001 { 988 {
1002 return m_mousePositionF; 989 return m_mousePositionF;
1003 } 990 }
1004 991
1005 Qt::KeyboardModifiers GLRenderer::keyboardModifiers() const 992 Qt::KeyboardModifiers gl::Renderer::keyboardModifiers() const
1006 { 993 {
1007 return m_currentKeyboardModifiers; 994 return m_currentKeyboardModifiers;
1008 } 995 }
1009 996
1010 Camera GLRenderer::camera() const 997 gl::CameraType gl::Renderer::camera() const
1011 { 998 {
1012 return m_camera; 999 return m_camera;
1013 } 1000 }
1014 1001
1015 double GLRenderer::panning (Axis ax) const 1002 double gl::Renderer::panning (Axis ax) const
1016 { 1003 {
1017 return (ax == X) ? currentCamera().panningX() : currentCamera().panningY(); 1004 return (ax == X) ? currentCamera().panningX() : currentCamera().panningY();
1018 } 1005 }
1019 1006
1020 double GLRenderer::zoom() 1007 double gl::Renderer::zoom()
1021 { 1008 {
1022 return currentCamera().zoom(); 1009 return currentCamera().zoom();
1023 } 1010 }
1024 1011
1025 bool GLRenderer::isDrawingSelectionScene() const 1012 bool gl::Renderer::isDrawingSelectionScene() const
1026 { 1013 {
1027 return m_isDrawingSelectionScene; 1014 return m_isDrawingSelectionScene;
1028 } 1015 }
1029 1016
1030 Qt::MouseButtons GLRenderer::lastButtons() const 1017 Qt::MouseButtons gl::Renderer::lastButtons() const
1031 { 1018 {
1032 return m_lastButtons; 1019 return m_lastButtons;
1033 } 1020 }
1034 1021
1035 const Model* GLRenderer::model() const 1022 const Model* gl::Renderer::model() const
1036 { 1023 {
1037 return m_model; 1024 return m_model;
1038 } 1025 }
1039 1026
1040 /* 1027 /*
1041 * This virtual function lets derivative classes render something to the fixed camera 1028 * This virtual function lets derivative classes render something to the fixed camera
1042 * before the main brick is rendered. 1029 * before the main brick is rendered.
1043 */ 1030 */
1044 void GLRenderer::drawFixedCameraBackdrop() {} 1031 void gl::Renderer::drawFixedCameraBackdrop() {}
1045 1032
1046 QItemSelectionModel* GLRenderer::selectionModel() const 1033 QItemSelectionModel* gl::Renderer::selectionModel() const
1047 { 1034 {
1048 return m_compiler->selectionModel(); 1035 return m_compiler->selectionModel();
1049 } 1036 }
1050 1037
1051 void GLRenderer::setSelectionModel(QItemSelectionModel* selectionModel) 1038 void gl::Renderer::setSelectionModel(QItemSelectionModel* selectionModel)
1052 { 1039 {
1053 this->m_compiler->setSelectionModel(selectionModel); 1040 this->m_compiler->setSelectionModel(selectionModel);
1054 } 1041 }
1055 1042
1056 void GLRenderer::fullUpdate() 1043 void gl::Renderer::fullUpdate()
1057 { 1044 {
1058 this->m_compiler->fullUpdate(); 1045 this->m_compiler->fullUpdate();
1059 update(); 1046 update();
1060 } 1047 }

mercurial