src/glRenderer.cpp

changeset 1034
a05fc8256a3a
parent 1033
32153a1dbb15
child 1035
8a60250175ba
equal deleted inserted replaced
1033:32153a1dbb15 1034:a05fc8256a3a
41 #include "glCompiler.h" 41 #include "glCompiler.h"
42 #include "primitives.h" 42 #include "primitives.h"
43 #include "documentmanager.h" 43 #include "documentmanager.h"
44 #include "grid.h" 44 #include "grid.h"
45 45
46 const LDFixedCamera g_FixedCameras[6] = 46 const CameraInfo g_cameraInfo[NumCameras] =
47 { 47 {
48 {{ 1, 0, 0 }, X, Z, false, false, false }, // top 48 {{ 1, 0, 0 }, X, Z, false, false, false }, // top
49 {{ 0, 0, 0 }, X, Y, false, true, false }, // front 49 {{ 0, 0, 0 }, X, Y, false, true, false }, // front
50 {{ 0, 1, 0 }, Z, Y, true, true, false }, // left 50 {{ 0, 1, 0 }, Z, Y, true, true, false }, // left
51 {{ -1, 0, 0 }, X, Z, false, true, true }, // bottom 51 {{ -1, 0, 0 }, X, Z, false, true, true }, // bottom
52 {{ 0, 0, 0 }, X, Y, true, true, true }, // back 52 {{ 0, 0, 0 }, X, Y, true, true, true }, // back
53 {{ 0, -1, 0 }, Z, Y, false, true, true }, // right 53 {{ 0, -1, 0 }, Z, Y, false, true, true }, // right
54 {{ 1, 0, 0 }, X, Z, false, false, false }, // free (defensive dummy data)
54 }; 55 };
55 56
56 ConfigOption (QColor BackgroundColor = "#FFFFFF") 57 ConfigOption (QColor BackgroundColor = "#FFFFFF")
57 ConfigOption (QColor MainColor = "#A0A0A0") 58 ConfigOption (QColor MainColor = "#A0A0A0")
58 ConfigOption (float MainColorAlpha = 1.0) 59 ConfigOption (float MainColorAlpha = 1.0)
97 m_thinBorderPen.setWidth (1); 98 m_thinBorderPen.setWidth (1);
98 setAcceptDrops (true); 99 setAcceptDrops (true);
99 connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (slot_toolTipTimer())); 100 connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (slot_toolTipTimer()));
100 101
101 // Init camera icons 102 // Init camera icons
102 for (Camera cam = EFirstCamera; cam < ENumCameras; ++cam) 103 for (Camera cam = EFirstCamera; cam < NumCameras; ++cam)
103 { 104 {
104 const char* cameraIconNames[ENumCameras] = 105 const char* cameraIconNames[NumCameras] =
105 { 106 {
106 "camera-top", "camera-front", "camera-left", 107 "camera-top", "camera-front", "camera-left",
107 "camera-bottom", "camera-back", "camera-right", 108 "camera-bottom", "camera-back", "camera-right",
108 "camera-free" 109 "camera-free"
109 }; 110 };
139 int i = 0; 140 int i = 0;
140 141
141 for (CameraIcon& info : m_cameraIcons) 142 for (CameraIcon& info : m_cameraIcons)
142 { 143 {
143 // MATH 144 // MATH
144 int x1 = (m_width - (info.camera != EFreeCamera ? 48 : 16)) + ((i % 3) * 16) - 1; 145 int x1 = (m_width - (info.camera != FreeCamera ? 48 : 16)) + ((i % 3) * 16) - 1;
145 int y1 = ((i / 3) * 16) + 1; 146 int y1 = ((i / 3) * 16) + 1;
146 147
147 info.sourceRect = QRect (0, 0, 16, 16); 148 info.sourceRect = QRect (0, 0, 16, 16);
148 info.targetRect = QRect (x1, y1, 16, 16); 149 info.targetRect = QRect (x1, y1, 16, 16);
149 info.hitRect = QRect ( 150 info.hitRect = QRect (
391 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); 392 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
392 393
393 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 394 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
394 glEnable (GL_DEPTH_TEST); 395 glEnable (GL_DEPTH_TEST);
395 396
396 if (camera() != EFreeCamera) 397 if (camera() != FreeCamera)
397 { 398 {
398 glMatrixMode (GL_PROJECTION); 399 glMatrixMode (GL_PROJECTION);
399 glPushMatrix(); 400 glPushMatrix();
400 401
401 glLoadIdentity(); 402 glLoadIdentity();
402 glOrtho (-m_virtualWidth, m_virtualWidth, -m_virtualHeight, m_virtualHeight, -100.0f, 100.0f); 403 glOrtho (-m_virtualWidth, m_virtualWidth, -m_virtualHeight, m_virtualHeight, -100.0f, 100.0f);
403 glTranslatef (panning (X), panning (Y), 0.0f); 404 glTranslatef (panning (X), panning (Y), 0.0f);
404 405
405 if (camera() != EFrontCamera and camera() != EBackCamera) 406 if (camera() != FrontCamera and camera() != BackCamera)
406 { 407 {
407 glRotatef (90.0f, g_FixedCameras[camera()].glrotate[0], 408 glRotatef (90.0f, g_cameraInfo[camera()].glrotate[0],
408 g_FixedCameras[camera()].glrotate[1], 409 g_cameraInfo[camera()].glrotate[1],
409 g_FixedCameras[camera()].glrotate[2]); 410 g_cameraInfo[camera()].glrotate[2]);
410 } 411 }
411 412
412 // Back camera needs to be handled differently 413 // Back camera needs to be handled differently
413 if (camera() == EBackCamera) 414 if (camera() == BackCamera)
414 { 415 {
415 glRotatef (180.0f, 1.0f, 0.0f, 0.0f); 416 glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
416 glRotatef (180.0f, 0.0f, 0.0f, 1.0f); 417 glRotatef (180.0f, 0.0f, 0.0f, 1.0f);
417 } 418 }
418 } 419 }
529 // This converts a 2D point on the screen to a 3D point in the model. If 'snap' 530 // This converts a 2D point on the screen to a 3D point in the model. If 'snap'
530 // is true, the 3D point will snap to the current grid. 531 // is true, the 3D point will snap to the current grid.
531 // 532 //
532 Vertex GLRenderer::convert2dTo3d (const QPoint& pos2d, bool snap) const 533 Vertex GLRenderer::convert2dTo3d (const QPoint& pos2d, bool snap) const
533 { 534 {
534 if (camera() == EFreeCamera) 535 if (camera() == FreeCamera)
535 return Origin; 536 return Origin;
536 537
537 Vertex pos3d; 538 Vertex pos3d;
538 const LDFixedCamera* cam = &g_FixedCameras[camera()]; 539 const CameraInfo* cam = &g_cameraInfo[camera()];
539 Axis axisX = cam->localX; 540 Axis axisX = cam->localX;
540 Axis axisY = cam->localY; 541 Axis axisY = cam->localY;
541 int signX = cam->negatedX ? -1 : 1; 542 int signX = cam->negatedX ? -1 : 1;
542 int signY = cam->negatedY ? -1 : 1; 543 int signY = cam->negatedY ? -1 : 1;
543 544
569 // Inverse operation for the above - convert a 3D position to a 2D screen position. Don't ask me how this code manages 570 // Inverse operation for the above - convert a 3D position to a 2D screen position. Don't ask me how this code manages
570 // to work, I don't even know. 571 // to work, I don't even know.
571 // 572 //
572 QPoint GLRenderer::convert3dTo2d (const Vertex& pos3d) 573 QPoint GLRenderer::convert3dTo2d (const Vertex& pos3d)
573 { 574 {
574 if (camera() == EFreeCamera) 575 if (camera() == FreeCamera)
575 return QPoint (0, 0); 576 return QPoint (0, 0);
576 577
577 const LDFixedCamera* cam = &g_FixedCameras[camera()]; 578 const CameraInfo* cam = &g_cameraInfo[camera()];
578 const Axis axisX = cam->localX; 579 const Axis axisX = cam->localX;
579 const Axis axisY = cam->localY; 580 const Axis axisY = cam->localY;
580 const int negXFac = cam->negatedX ? -1 : 1; 581 const int negXFac = cam->negatedX ? -1 : 1;
581 const int negYFac = cam->negatedY ? -1 : 1; 582 const int negYFac = cam->negatedY ? -1 : 1;
582 GLfloat matrix[16]; 583 GLfloat matrix[16];
636 painter.drawText ((width() - textSize.width()) / 2, height() - textSize.height(), textSize.width(), 637 painter.drawText ((width() - textSize.width()) / 2, height() - textSize.height(), textSize.width(),
637 textSize.height(), Qt::AlignCenter, text); 638 textSize.height(), Qt::AlignCenter, text);
638 } 639 }
639 #endif 640 #endif
640 641
641 if (camera() != EFreeCamera and not isPicking()) 642 if (camera() != FreeCamera and not isPicking())
642 { 643 {
643 // Paint the overlay image if we have one 644 // Paint the overlay image if we have one
644 const LDGLOverlay& overlay = currentDocumentData().overlays[camera()]; 645 const LDGLOverlay& overlay = currentDocumentData().overlays[camera()];
645 646
646 if (overlay.image) 647 if (overlay.image)
673 674
674 // Draw the camera icons 675 // Draw the camera icons
675 for (CameraIcon& info : m_cameraIcons) 676 for (CameraIcon& info : m_cameraIcons)
676 { 677 {
677 // Don't draw the free camera icon when we can't use the free camera 678 // Don't draw the free camera icon when we can't use the free camera
678 if (&info == &m_cameraIcons[EFreeCamera] and not m_currentEditMode->allowFreeCamera()) 679 if (&info == &m_cameraIcons[FreeCamera] and not m_currentEditMode->allowFreeCamera())
679 continue; 680 continue;
680 681
681 painter.drawPixmap (info.targetRect, info.image, info.sourceRect); 682 painter.drawPixmap (info.targetRect, info.image, info.sourceRect);
682 } 683 }
683 684
827 panning (X) += 0.03f * dx * (zoom() / 7.5f); 828 panning (X) += 0.03f * dx * (zoom() / 7.5f);
828 panning (Y) -= 0.03f * dy * (zoom() / 7.5f); 829 panning (Y) -= 0.03f * dy * (zoom() / 7.5f);
829 m_panning = true; 830 m_panning = true;
830 m_isCameraMoving = true; 831 m_isCameraMoving = true;
831 } 832 }
832 else if (left and camera() == EFreeCamera) 833 else if (left and camera() == FreeCamera)
833 { 834 {
834 rotation (X) = rotation (X) + dy; 835 rotation (X) = rotation (X) + dy;
835 rotation (Y) = rotation (Y) + dx; 836 rotation (Y) = rotation (Y) + dx;
836 clampAngle (rotation (X)); 837 clampAngle (rotation (X));
837 clampAngle (rotation (Y)); 838 clampAngle (rotation (Y));
852 #else 853 #else
853 m_mousePositionF = ev->localPos(); 854 m_mousePositionF = ev->localPos();
854 #endif 855 #endif
855 856
856 // Calculate 3d position of the cursor 857 // Calculate 3d position of the cursor
857 m_position3D = (camera() != EFreeCamera) ? convert2dTo3d (m_mousePosition, true) : Origin; 858 m_position3D = (camera() != FreeCamera) ? convert2dTo3d (m_mousePosition, true) : Origin;
858 859
859 highlightCursorObject(); 860 highlightCursorObject();
860 update(); 861 update();
861 ev->accept(); 862 ev->accept();
862 } 863 }
908 // ============================================================================= 909 // =============================================================================
909 // 910 //
910 void GLRenderer::setCamera (const Camera camera) 911 void GLRenderer::setCamera (const Camera camera)
911 { 912 {
912 // The edit mode may forbid the free camera. 913 // The edit mode may forbid the free camera.
913 if (m_currentEditMode->allowFreeCamera() or camera != EFreeCamera) 914 if (m_currentEditMode->allowFreeCamera() or camera != FreeCamera)
914 { 915 {
915 m_camera = camera; 916 m_camera = camera;
916 m_config->setCamera((int) camera); 917 m_config->setCamera((int) camera);
917 m_window->updateEditModeActions(); 918 m_window->updateEditModeActions();
918 } 919 }
1041 1042
1042 delete m_currentEditMode; 1043 delete m_currentEditMode;
1043 m_currentEditMode = AbstractEditMode::createByType (this, a); 1044 m_currentEditMode = AbstractEditMode::createByType (this, a);
1044 1045
1045 // If we cannot use the free camera, use the top one instead. 1046 // If we cannot use the free camera, use the top one instead.
1046 if (camera() == EFreeCamera and not m_currentEditMode->allowFreeCamera()) 1047 if (camera() == FreeCamera and not m_currentEditMode->allowFreeCamera())
1047 setCamera (ETopCamera); 1048 setCamera (TopCamera);
1048 1049
1049 m_window->updateEditModeActions(); 1050 m_window->updateEditModeActions();
1050 update(); 1051 update();
1051 } 1052 }
1052 1053
1102 1103
1103 // ============================================================================= 1104 // =============================================================================
1104 // 1105 //
1105 void GLRenderer::getRelativeAxes (Axis& relX, Axis& relY) const 1106 void GLRenderer::getRelativeAxes (Axis& relX, Axis& relY) const
1106 { 1107 {
1107 const LDFixedCamera* cam = &g_FixedCameras[camera()]; 1108 const CameraInfo* cam = &g_cameraInfo[camera()];
1108 relX = cam->localX; 1109 relX = cam->localX;
1109 relY = cam->localY; 1110 relY = cam->localY;
1110 } 1111 }
1111 1112
1112 // ============================================================================= 1113 // =============================================================================
1113 // 1114 //
1114 Axis GLRenderer::getRelativeZ() const 1115 Axis GLRenderer::getRelativeZ() const
1115 { 1116 {
1116 const LDFixedCamera* cam = &g_FixedCameras[camera()]; 1117 const CameraInfo* cam = &g_cameraInfo[camera()];
1117 return (Axis) (3 - cam->localX - cam->localY); 1118 return (Axis) (3 - cam->localX - cam->localY);
1118 } 1119 }
1119 1120
1120 // ============================================================================= 1121 // =============================================================================
1121 // 1122 //
1172 Axis GLRenderer::getCameraAxis (bool y, Camera camid) 1173 Axis GLRenderer::getCameraAxis (bool y, Camera camid)
1173 { 1174 {
1174 if (camid == (Camera) -1) 1175 if (camid == (Camera) -1)
1175 camid = camera(); 1176 camid = camera();
1176 1177
1177 const LDFixedCamera* cam = &g_FixedCameras[camid]; 1178 const CameraInfo* cam = &g_cameraInfo[camid];
1178 return (y) ? cam->localY : cam->localX; 1179 return (y) ? cam->localY : cam->localX;
1179 } 1180 }
1180 1181
1181 // ============================================================================= 1182 // =============================================================================
1182 // 1183 //
1208 else if (info.height == 0) 1209 else if (info.height == 0)
1209 info.height = (info.width * image->height()) / image->width(); 1210 info.height = (info.width * image->height()) / image->width();
1210 1211
1211 Axis localX = getCameraAxis (false, camera); 1212 Axis localX = getCameraAxis (false, camera);
1212 Axis localY = getCameraAxis (true, camera); 1213 Axis localY = getCameraAxis (true, camera);
1213 int signX = g_FixedCameras[camera].negatedX ? -1 : 1; 1214 int signX = g_cameraInfo[camera].negatedX ? -1 : 1;
1214 int signY = g_FixedCameras[camera].negatedY ? -1 : 1; 1215 int signY = g_cameraInfo[camera].negatedY ? -1 : 1;
1215 1216
1216 info.v0 = info.v1 = Origin; 1217 info.v0 = info.v1 = Origin;
1217 info.v0.setCoordinate (localX, -(info.offsetX * info.width * signX) / image->width()); 1218 info.v0.setCoordinate (localX, -(info.offsetX * info.width * signX) / image->width());
1218 info.v0.setCoordinate (localY, (info.offsetY * info.height * signY) / image->height()); 1219 info.v0.setCoordinate (localY, (info.offsetY * info.height * signY) / image->height());
1219 info.v1.setCoordinate (localX, info.v0[localX] + info.width); 1220 info.v1.setCoordinate (localX, info.v0[localX] + info.width);
1233 1234
1234 // ============================================================================= 1235 // =============================================================================
1235 // 1236 //
1236 void GLRenderer::clearOverlay() 1237 void GLRenderer::clearOverlay()
1237 { 1238 {
1238 if (camera() == EFreeCamera) 1239 if (camera() == FreeCamera)
1239 return; 1240 return;
1240 1241
1241 LDGLOverlay& info = currentDocumentData().overlays[camera()]; 1242 LDGLOverlay& info = currentDocumentData().overlays[camera()];
1242 delete info.image; 1243 delete info.image;
1243 info.image = nullptr; 1244 info.image = nullptr;
1247 1248
1248 // ============================================================================= 1249 // =============================================================================
1249 // 1250 //
1250 void GLRenderer::setDepthValue (double depth) 1251 void GLRenderer::setDepthValue (double depth)
1251 { 1252 {
1252 if (camera() < EFreeCamera) 1253 if (camera() < FreeCamera)
1253 currentDocumentData().depthValues[camera()] = depth; 1254 currentDocumentData().depthValues[camera()] = depth;
1254 } 1255 }
1255 1256
1256 // ============================================================================= 1257 // =============================================================================
1257 // 1258 //
1258 double GLRenderer::getDepthValue() const 1259 double GLRenderer::getDepthValue() const
1259 { 1260 {
1260 if (camera() < EFreeCamera) 1261 if (camera() < FreeCamera)
1261 return currentDocumentData().depthValues[camera()]; 1262 return currentDocumentData().depthValues[camera()];
1262 else 1263 else
1263 return 0.0; 1264 return 0.0;
1264 } 1265 }
1265 1266
1267 // 1268 //
1268 QString GLRenderer::cameraName (Camera camera) const 1269 QString GLRenderer::cameraName (Camera camera) const
1269 { 1270 {
1270 switch (camera) 1271 switch (camera)
1271 { 1272 {
1272 case ETopCamera: return tr ("Top Camera"); 1273 case TopCamera: return tr ("Top Camera");
1273 case EFrontCamera: return tr ("Front Camera"); 1274 case FrontCamera: return tr ("Front Camera");
1274 case ELeftCamera: return tr ("Left Camera"); 1275 case LeftCamera: return tr ("Left Camera");
1275 case EBottomCamera: return tr ("Bottom Camera"); 1276 case BottomCamera: return tr ("Bottom Camera");
1276 case EBackCamera: return tr ("Back Camera"); 1277 case BackCamera: return tr ("Back Camera");
1277 case ERightCamera: return tr ("Right Camera"); 1278 case RightCamera: return tr ("Right Camera");
1278 case EFreeCamera: return tr ("Free Camera"); 1279 case FreeCamera: return tr ("Free Camera");
1279 default: break; 1280 default: break;
1280 } 1281 }
1281 1282
1282 return ""; 1283 return "";
1283 } 1284 }
1425 // 1426 //
1426 // Read in overlays from the current file and update overlay info accordingly. 1427 // Read in overlays from the current file and update overlay info accordingly.
1427 // 1428 //
1428 void GLRenderer::initOverlaysFromObjects() 1429 void GLRenderer::initOverlaysFromObjects()
1429 { 1430 {
1430 for (Camera camera = EFirstCamera; camera < ENumCameras; ++camera) 1431 for (Camera camera = EFirstCamera; camera < NumCameras; ++camera)
1431 { 1432 {
1432 if (camera == EFreeCamera) 1433 if (camera == FreeCamera)
1433 continue; 1434 continue;
1434 1435
1435 LDGLOverlay& meta = currentDocumentData().overlays[camera]; 1436 LDGLOverlay& meta = currentDocumentData().overlays[camera];
1436 LDOverlay* overlay = findOverlayObject (camera); 1437 LDOverlay* overlay = findOverlayObject (camera);
1437 1438
1454 1455
1455 // ============================================================================= 1456 // =============================================================================
1456 // 1457 //
1457 void GLRenderer::updateOverlayObjects() 1458 void GLRenderer::updateOverlayObjects()
1458 { 1459 {
1459 for (Camera cam = EFirstCamera; cam < ENumCameras; ++cam) 1460 for (Camera cam = EFirstCamera; cam < NumCameras; ++cam)
1460 { 1461 {
1461 if (cam == EFreeCamera) 1462 if (cam == FreeCamera)
1462 continue; 1463 continue;
1463 1464
1464 LDGLOverlay& meta = currentDocumentData().overlays[cam]; 1465 LDGLOverlay& meta = currentDocumentData().overlays[cam];
1465 LDOverlay* ovlobj = findOverlayObject (cam); 1466 LDOverlay* ovlobj = findOverlayObject (cam);
1466 1467
1602 Vertex const& GLRenderer::position3D() const 1603 Vertex const& GLRenderer::position3D() const
1603 { 1604 {
1604 return m_position3D; 1605 return m_position3D;
1605 } 1606 }
1606 1607
1607 const LDFixedCamera& GLRenderer::cameraInfo (Camera camera) const 1608 const CameraInfo& GLRenderer::cameraInfo (Camera camera) const
1608 { 1609 {
1609 if (camera >= EFirstCamera and camera <= ELastFixedCamera) 1610 if (camera >= EFirstCamera and camera <= ELastFixedCamera)
1610 return g_FixedCameras[camera]; 1611 return g_cameraInfo[camera];
1611 else 1612 else
1612 return g_FixedCameras[0]; 1613 return g_cameraInfo[0];
1613 } 1614 }
1614 1615
1615 bool GLRenderer::mouseHasMoved() const 1616 bool GLRenderer::mouseHasMoved() const
1616 { 1617 {
1617 return m_totalMouseMove >= 10; 1618 return m_totalMouseMove >= 10;
1633 initializeOpenGLFunctions(); 1634 initializeOpenGLFunctions();
1634 } 1635 }
1635 1636
1636 int GLRenderer::depthNegateFactor() const 1637 int GLRenderer::depthNegateFactor() const
1637 { 1638 {
1638 return g_FixedCameras[camera()].negatedDepth ? -1 : 1; 1639 return g_cameraInfo[camera()].negatedDepth ? -1 : 1;
1639 } 1640 }
1640 1641
1641 Qt::KeyboardModifiers GLRenderer::keyboardModifiers() const 1642 Qt::KeyboardModifiers GLRenderer::keyboardModifiers() const
1642 { 1643 {
1643 return m_currentKeyboardModifiers; 1644 return m_currentKeyboardModifiers;

mercurial