src/glRenderer.cc

changeset 861
83426c5fa732
parent 859
ebc7a186699c
child 866
4951b737f8cb
equal deleted inserted replaced
860:a496e72af069 861:83426c5fa732
108 m_panning = false; 108 m_panning = false;
109 m_compiler = new GLCompiler (this); 109 m_compiler = new GLCompiler (this);
110 setDrawOnly (false); 110 setDrawOnly (false);
111 setMessageLog (null); 111 setMessageLog (null);
112 m_width = m_height = -1; 112 m_width = m_height = -1;
113 m_position3D = g_origin; 113 m_position3D = Origin;
114 m_toolTipTimer = new QTimer (this); 114 m_toolTipTimer = new QTimer (this);
115 m_toolTipTimer->setSingleShot (true); 115 m_toolTipTimer->setSingleShot (true);
116 m_isCameraMoving = false; 116 m_isCameraMoving = false;
117 m_thinBorderPen = QPen (QColor (0, 0, 0, 208), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); 117 m_thinBorderPen = QPen (QColor (0, 0, 0, 208), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
118 m_thinBorderPen.setWidth (1); 118 m_thinBorderPen.setWidth (1);
122 // Init camera icons 122 // Init camera icons
123 for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam) 123 for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam)
124 { 124 {
125 QString iconname = format ("camera-%1", tr (g_CameraNames[cam]).toLower()); 125 QString iconname = format ("camera-%1", tr (g_CameraNames[cam]).toLower());
126 CameraIcon* info = &m_cameraIcons[cam]; 126 CameraIcon* info = &m_cameraIcons[cam];
127 info->img = new QPixmap (getIcon (iconname)); 127 info->img = new QPixmap (GetIcon (iconname));
128 info->cam = cam; 128 info->cam = cam;
129 } 129 }
130 130
131 calcCameraIcons(); 131 calcCameraIcons();
132 } 132 }
146 146
147 m_compiler->setRenderer (null); 147 m_compiler->setRenderer (null);
148 delete m_compiler; 148 delete m_compiler;
149 delete m_editmode; 149 delete m_editmode;
150 150
151 glDeleteBuffers (1, &_axesVBO); 151 glDeleteBuffers (1, &m_axesVBO);
152 glDeleteBuffers (1, &_axesColorVBO); 152 glDeleteBuffers (1, &m_axesColorVBO);
153 } 153 }
154 154
155 // ============================================================================= 155 // =============================================================================
156 // Calculates the "hitboxes" of the camera icons so that we can tell when the 156 // Calculates the "hitboxes" of the camera icons so that we can tell when the
157 // cursor is pointing at the camera icon. 157 // cursor is pointing at the camera icon.
278 colordata[(i * 6) + (j * 3) + 1] = g_GLAxes[i].col.green(); 278 colordata[(i * 6) + (j * 3) + 1] = g_GLAxes[i].col.green();
279 colordata[(i * 6) + (j * 3) + 2] = g_GLAxes[i].col.blue(); 279 colordata[(i * 6) + (j * 3) + 2] = g_GLAxes[i].col.blue();
280 } 280 }
281 } 281 }
282 282
283 glGenBuffers (1, &_axesVBO); 283 glGenBuffers (1, &m_axesVBO);
284 glBindBuffer (GL_ARRAY_BUFFER, _axesVBO); 284 glBindBuffer (GL_ARRAY_BUFFER, m_axesVBO);
285 glBufferData (GL_ARRAY_BUFFER, sizeof axesdata, axesdata, GL_STATIC_DRAW); 285 glBufferData (GL_ARRAY_BUFFER, sizeof axesdata, axesdata, GL_STATIC_DRAW);
286 glGenBuffers (1, &_axesColorVBO); 286 glGenBuffers (1, &m_axesColorVBO);
287 glBindBuffer (GL_ARRAY_BUFFER, _axesColorVBO); 287 glBindBuffer (GL_ARRAY_BUFFER, m_axesColorVBO);
288 glBufferData (GL_ARRAY_BUFFER, sizeof colordata, colordata, GL_STATIC_DRAW); 288 glBufferData (GL_ARRAY_BUFFER, sizeof colordata, colordata, GL_STATIC_DRAW);
289 glBindBuffer (GL_ARRAY_BUFFER, 0); 289 glBindBuffer (GL_ARRAY_BUFFER, 0);
290 } 290 }
291 291
292 // ============================================================================= 292 // =============================================================================
317 if (not col.isValid()) 317 if (not col.isValid())
318 return; 318 return;
319 319
320 col.setAlpha (255); 320 col.setAlpha (255);
321 321
322 m_darkbg = luma (col) < 80; 322 m_darkbg = Luma (col) < 80;
323 m_bgcolor = col; 323 m_bgcolor = col;
324 qglClearColor (col); 324 qglClearColor (col);
325 } 325 }
326 326
327 // ============================================================================= 327 // =============================================================================
339 void GLRenderer::hardRefresh() 339 void GLRenderer::hardRefresh()
340 { 340 {
341 if (not RendererInitialized) 341 if (not RendererInitialized)
342 return; 342 return;
343 343
344 compiler()->compileDocument (getCurrentDocument()); 344 compiler()->compileDocument (CurrentDocument());
345 refresh(); 345 refresh();
346 } 346 }
347 347
348 // ============================================================================= 348 // =============================================================================
349 // 349 //
458 drawVBOs (VBOSF_CondLines, VBOCM_NormalColors, GL_LINES); 458 drawVBOs (VBOSF_CondLines, VBOCM_NormalColors, GL_LINES);
459 glDisable (GL_LINE_STIPPLE); 459 glDisable (GL_LINE_STIPPLE);
460 460
461 if (cfg::DrawAxes) 461 if (cfg::DrawAxes)
462 { 462 {
463 glBindBuffer (GL_ARRAY_BUFFER, _axesVBO); 463 glBindBuffer (GL_ARRAY_BUFFER, m_axesVBO);
464 glVertexPointer (3, GL_FLOAT, 0, NULL); 464 glVertexPointer (3, GL_FLOAT, 0, NULL);
465 glBindBuffer (GL_ARRAY_BUFFER, _axesVBO); 465 glBindBuffer (GL_ARRAY_BUFFER, m_axesVBO);
466 glColorPointer (3, GL_FLOAT, 0, NULL); 466 glColorPointer (3, GL_FLOAT, 0, NULL);
467 glDrawArrays (GL_LINES, 0, 6); 467 glDrawArrays (GL_LINES, 0, 6);
468 checkGLError(); 468 CHECK_GL_ERROR();
469 } 469 }
470 } 470 }
471 471
472 glPopMatrix(); 472 glPopMatrix();
473 glBindBuffer (GL_ARRAY_BUFFER, 0); 473 glBindBuffer (GL_ARRAY_BUFFER, 0);
474 glDisableClientState (GL_VERTEX_ARRAY); 474 glDisableClientState (GL_VERTEX_ARRAY);
475 glDisableClientState (GL_COLOR_ARRAY); 475 glDisableClientState (GL_COLOR_ARRAY);
476 checkGLError(); 476 CHECK_GL_ERROR();
477 glDisable (GL_CULL_FACE); 477 glDisable (GL_CULL_FACE);
478 glMatrixMode (GL_MODELVIEW); 478 glMatrixMode (GL_MODELVIEW);
479 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); 479 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
480 } 480 }
481 481
482 // ============================================================================= 482 // =============================================================================
483 // 483 //
484 void GLRenderer::drawVBOs (EVBOSurface surface, EVBOComplement colors, GLenum type) 484 void GLRenderer::drawVBOs (EVBOSurface surface, EVBOComplement colors, GLenum type)
485 { 485 {
486 // Filter this through some configuration options 486 // Filter this through some configuration options
487 if ((eq (surface, VBOSF_Quads, VBOSF_Triangles) and cfg::DrawSurfaces == false) or 487 if ((Eq (surface, VBOSF_Quads, VBOSF_Triangles) and cfg::DrawSurfaces == false) or
488 (surface == VBOSF_Lines and cfg::DrawEdgeLines == false) or 488 (surface == VBOSF_Lines and cfg::DrawEdgeLines == false) or
489 (surface == VBOSF_CondLines and cfg::DrawConditionalLines == false)) 489 (surface == VBOSF_CondLines and cfg::DrawConditionalLines == false))
490 { 490 {
491 return; 491 return;
492 } 492 }
501 501
502 if (count > 0) 502 if (count > 0)
503 { 503 {
504 glBindBuffer (GL_ARRAY_BUFFER, surfacevbo); 504 glBindBuffer (GL_ARRAY_BUFFER, surfacevbo);
505 glVertexPointer (3, GL_FLOAT, 0, null); 505 glVertexPointer (3, GL_FLOAT, 0, null);
506 checkGLError(); 506 CHECK_GL_ERROR();
507 glBindBuffer (GL_ARRAY_BUFFER, colorvbo); 507 glBindBuffer (GL_ARRAY_BUFFER, colorvbo);
508 glColorPointer (4, GL_FLOAT, 0, null); 508 glColorPointer (4, GL_FLOAT, 0, null);
509 checkGLError(); 509 CHECK_GL_ERROR();
510 glDrawArrays (type, 0, count); 510 glDrawArrays (type, 0, count);
511 checkGLError(); 511 CHECK_GL_ERROR();
512 } 512 }
513 } 513 }
514 514
515 // ============================================================================= 515 // =============================================================================
516 // This converts a 2D point on the screen to a 3D point in the model. If 'snap' 516 // This converts a 2D point on the screen to a 3D point in the model. If 'snap'
531 double cx = (-m_virtWidth + ((2 * pos2d.x() * m_virtWidth) / m_width) - pan (X)); 531 double cx = (-m_virtWidth + ((2 * pos2d.x() * m_virtWidth) / m_width) - pan (X));
532 double cy = (m_virtHeight - ((2 * pos2d.y() * m_virtHeight) / m_height) - pan (Y)); 532 double cy = (m_virtHeight - ((2 * pos2d.y() * m_virtHeight) / m_height) - pan (Y));
533 533
534 if (snap) 534 if (snap)
535 { 535 {
536 cx = Grid::snap (cx, Grid::Coordinate); 536 cx = Grid::Snap (cx, Grid::Coordinate);
537 cy = Grid::snap (cy, Grid::Coordinate); 537 cy = Grid::Snap (cy, Grid::Coordinate);
538 } 538 }
539 539
540 cx *= negXFac; 540 cx *= negXFac;
541 cy *= negYFac; 541 cy *= negYFac;
542 542
588 588
589 QPen GLRenderer::linePen() const 589 QPen GLRenderer::linePen() const
590 { 590 {
591 QPen linepen (m_thinBorderPen); 591 QPen linepen (m_thinBorderPen);
592 linepen.setWidth (2); 592 linepen.setWidth (2);
593 linepen.setColor (luma (m_bgcolor) < 40 ? Qt::white : Qt::black); 593 linepen.setColor (Luma (m_bgcolor) < 40 ? Qt::white : Qt::black);
594 return linepen; 594 return linepen;
595 } 595 }
596 596
597 // ============================================================================= 597 // =============================================================================
598 // 598 //
632 if (overlay.img != null) 632 if (overlay.img != null)
633 { 633 {
634 QPoint v0 = coordconv3_2 (currentDocumentData().overlays[camera()].v0), 634 QPoint v0 = coordconv3_2 (currentDocumentData().overlays[camera()].v0),
635 v1 = coordconv3_2 (currentDocumentData().overlays[camera()].v1); 635 v1 = coordconv3_2 (currentDocumentData().overlays[camera()].v1);
636 636
637 QRect targRect (v0.x(), v0.y(), abs (v1.x() - v0.x()), abs (v1.y() - v0.y())), 637 QRect targRect (v0.x(), v0.y(), Abs (v1.x() - v0.x()), Abs (v1.y() - v0.y())),
638 srcRect (0, 0, overlay.img->width(), overlay.img->height()); 638 srcRect (0, 0, overlay.img->width(), overlay.img->height());
639 paint.drawImage (targRect, *overlay.img, srcRect); 639 paint.drawImage (targRect, *overlay.img, srcRect);
640 } 640 }
641 641
642 // Paint the coordinates onto the screen. 642 // Paint the coordinates onto the screen.
792 // 792 //
793 void GLRenderer::mouseMoveEvent (QMouseEvent* ev) 793 void GLRenderer::mouseMoveEvent (QMouseEvent* ev)
794 { 794 {
795 int dx = ev->x() - m_mousePosition.x(); 795 int dx = ev->x() - m_mousePosition.x();
796 int dy = ev->y() - m_mousePosition.y(); 796 int dy = ev->y() - m_mousePosition.y();
797 m_totalmove += abs (dx) + abs (dy); 797 m_totalmove += Abs (dx) + Abs (dy);
798 setCameraMoving (false); 798 setCameraMoving (false);
799 799
800 if (not m_editmode->mouseMoved (ev)) 800 if (not m_editmode->mouseMoved (ev))
801 { 801 {
802 const bool left = ev->buttons() & Qt::LeftButton, 802 const bool left = ev->buttons() & Qt::LeftButton,
828 // Update 2d position 828 // Update 2d position
829 m_mousePosition = ev->pos(); 829 m_mousePosition = ev->pos();
830 m_globalpos = ev->globalPos(); 830 m_globalpos = ev->globalPos();
831 831
832 // Calculate 3d position of the cursor 832 // Calculate 3d position of the cursor
833 m_position3D = (camera() != EFreeCamera) ? coordconv2_3 (m_mousePosition, true) : g_origin; 833 m_position3D = (camera() != EFreeCamera) ? coordconv2_3 (m_mousePosition, true) : Origin;
834 834
835 highlightCursorObject(); 835 highlightCursorObject();
836 update(); 836 update();
837 ev->accept(); 837 ev->accept();
838 } 838 }
856 void GLRenderer::wheelEvent (QWheelEvent* ev) 856 void GLRenderer::wheelEvent (QWheelEvent* ev)
857 { 857 {
858 doMakeCurrent(); 858 doMakeCurrent();
859 859
860 zoomNotch (ev->delta() > 0); 860 zoomNotch (ev->delta() > 0);
861 zoom() = clamp (zoom(), 0.01, 10000.0); 861 zoom() = Clamp (zoom(), 0.01, 10000.0);
862 setCameraMoving (true); 862 setCameraMoving (true);
863 update(); 863 update();
864 ev->accept(); 864 ev->accept();
865 } 865 }
866 866
908 doMakeCurrent(); 908 doMakeCurrent();
909 909
910 // Clear the selection if we do not wish to add to it. 910 // Clear the selection if we do not wish to add to it.
911 if (not additive) 911 if (not additive)
912 { 912 {
913 LDObjectList oldsel = selection(); 913 LDObjectList oldsel = Selection();
914 getCurrentDocument()->clearSelection(); 914 CurrentDocument()->clearSelection();
915 915
916 for (LDObjectPtr obj : oldsel) 916 for (LDObjectPtr obj : oldsel)
917 compileObject (obj); 917 compileObject (obj);
918 } 918 }
919 919
925 int y0 = range.top(); 925 int y0 = range.top();
926 int x1 = x0 + range.width(); 926 int x1 = x0 + range.width();
927 int y1 = y0 + range.height(); 927 int y1 = y0 + range.height();
928 928
929 // Clamp the values to ensure they're within bounds 929 // Clamp the values to ensure they're within bounds
930 x0 = max (0, x0); 930 x0 = Max (0, x0);
931 y0 = max (0, y0); 931 y0 = Max (0, y0);
932 x1 = min (x1, m_width); 932 x1 = Min (x1, m_width);
933 y1 = min (y1, m_height); 933 y1 = Min (y1, m_height);
934 const int areawidth = (x1 - x0); 934 const int areawidth = (x1 - x0);
935 const int areaheight = (y1 - y0); 935 const int areaheight = (y1 - y0);
936 const qint32 numpixels = areawidth * areaheight; 936 const qint32 numpixels = areawidth * areaheight;
937 937
938 // Allocate space for the pixel data. 938 // Allocate space for the pixel data.
957 957
958 if (idx != 0) 958 if (idx != 0)
959 indices << idx; 959 indices << idx;
960 } 960 }
961 961
962 removeDuplicates (indices); 962 RemoveDuplicates (indices);
963 963
964 for (qint32 idx : indices) 964 for (qint32 idx : indices)
965 { 965 {
966 LDObjectPtr obj = LDObject::fromID (idx); 966 LDObjectPtr obj = LDObject::fromID (idx);
967 assert (obj != null); 967 assert (obj != null);
985 985
986 // Update everything now. 986 // Update everything now.
987 g_win->updateSelection(); 987 g_win->updateSelection();
988 988
989 // Recompile the objects now to update their color 989 // Recompile the objects now to update their color
990 for (LDObjectPtr obj : selection()) 990 for (LDObjectPtr obj : Selection())
991 compileObject (obj); 991 compileObject (obj);
992 992
993 if (removedObj) 993 if (removedObj)
994 compileObject (removedObj); 994 compileObject (removedObj);
995 995
1068 if (isPicking()) 1068 if (isPicking())
1069 { 1069 {
1070 glDisable (GL_DITHER); 1070 glDisable (GL_DITHER);
1071 1071
1072 // Use particularly thick lines while picking ease up selecting lines. 1072 // Use particularly thick lines while picking ease up selecting lines.
1073 glLineWidth (max<double> (cfg::LineThickness, 6.5)); 1073 glLineWidth (Max<double> (cfg::LineThickness, 6.5));
1074 } 1074 }
1075 else 1075 else
1076 { 1076 {
1077 glEnable (GL_DITHER); 1077 glEnable (GL_DITHER);
1078 1078
1098 return (Axis) (3 - cam->axisX - cam->axisY); 1098 return (Axis) (3 - cam->axisX - cam->axisY);
1099 } 1099 }
1100 1100
1101 // ============================================================================= 1101 // =============================================================================
1102 // 1102 //
1103 static QList<Vertex> getVertices (LDObjectPtr obj) 1103 static QList<Vertex> GetVerticesOf (LDObjectPtr obj)
1104 { 1104 {
1105 QList<Vertex> verts; 1105 QList<Vertex> verts;
1106 1106
1107 if (obj->numVertices() >= 2) 1107 if (obj->numVertices() >= 2)
1108 { 1108 {
1114 LDSubfilePtr ref = obj.staticCast<LDSubfile>(); 1114 LDSubfilePtr ref = obj.staticCast<LDSubfile>();
1115 LDObjectList objs = ref->inlineContents (true, false); 1115 LDObjectList objs = ref->inlineContents (true, false);
1116 1116
1117 for (LDObjectPtr obj : objs) 1117 for (LDObjectPtr obj : objs)
1118 { 1118 {
1119 verts << getVertices (obj); 1119 verts << GetVerticesOf (obj);
1120 obj->destroy(); 1120 obj->destroy();
1121 } 1121 }
1122 } 1122 }
1123 1123
1124 return verts; 1124 return verts;
1194 QImage* img = new QImage (QImage (file).convertToFormat (QImage::Format_ARGB32)); 1194 QImage* img = new QImage (QImage (file).convertToFormat (QImage::Format_ARGB32));
1195 LDGLOverlay& info = getOverlay (cam); 1195 LDGLOverlay& info = getOverlay (cam);
1196 1196
1197 if (img->isNull()) 1197 if (img->isNull())
1198 { 1198 {
1199 critical (tr ("Failed to load overlay image!")); 1199 CriticalError (tr ("Failed to load overlay image!"));
1200 currentDocumentData().overlays[cam].invalid = true; 1200 currentDocumentData().overlays[cam].invalid = true;
1201 delete img; 1201 delete img;
1202 return false; 1202 return false;
1203 } 1203 }
1204 1204
1220 const Axis x2d = getCameraAxis (false, cam), 1220 const Axis x2d = getCameraAxis (false, cam),
1221 y2d = getCameraAxis (true, cam); 1221 y2d = getCameraAxis (true, cam);
1222 const double negXFac = g_FixedCameras[cam].negX ? -1 : 1, 1222 const double negXFac = g_FixedCameras[cam].negX ? -1 : 1,
1223 negYFac = g_FixedCameras[cam].negY ? -1 : 1; 1223 negYFac = g_FixedCameras[cam].negY ? -1 : 1;
1224 1224
1225 info.v0 = info.v1 = g_origin; 1225 info.v0 = info.v1 = Origin;
1226 info.v0.setCoordinate (x2d, -(info.ox * info.lw * negXFac) / img->width()); 1226 info.v0.setCoordinate (x2d, -(info.ox * info.lw * negXFac) / img->width());
1227 info.v0.setCoordinate (y2d, (info.oy * info.lh * negYFac) / img->height()); 1227 info.v0.setCoordinate (y2d, (info.oy * info.lh * negYFac) / img->height());
1228 info.v1.setCoordinate (x2d, info.v0[x2d] + info.lw); 1228 info.v1.setCoordinate (x2d, info.v0[x2d] + info.lw);
1229 info.v1.setCoordinate (y2d, info.v0[y2d] + info.lh); 1229 info.v1.setCoordinate (y2d, info.v0[y2d] + info.lh);
1230 1230
1486 } 1486 }
1487 elif (meta.img != null and ovlobj == null) 1487 elif (meta.img != null and ovlobj == null)
1488 { 1488 {
1489 // Inverse case: image is there but the overlay object is 1489 // Inverse case: image is there but the overlay object is
1490 // not, thus create the object. 1490 // not, thus create the object.
1491 ovlobj = spawn<LDOverlay>(); 1491 ovlobj = LDSpawn<LDOverlay>();
1492 1492
1493 // Find a suitable position to place this object. We want to place 1493 // Find a suitable position to place this object. We want to place
1494 // this into the header, which is everything up to the first scemantic 1494 // this into the header, which is everything up to the first scemantic
1495 // object. If we find another overlay object, place this object after 1495 // object. If we find another overlay object, place this object after
1496 // the last one found. Otherwise, place it before the first schemantic 1496 // the last one found. Otherwise, place it before the first schemantic
1518 else 1518 else
1519 { 1519 {
1520 document()->insertObj (i, ovlobj); 1520 document()->insertObj (i, ovlobj);
1521 1521
1522 if (found) 1522 if (found)
1523 document()->insertObj (i + 1, spawn<LDEmpty>()); 1523 document()->insertObj (i + 1, LDSpawn<LDEmpty>());
1524 } 1524 }
1525 } 1525 }
1526 1526
1527 if (meta.img != null and ovlobj != null) 1527 if (meta.img != null and ovlobj != null)
1528 { 1528 {
1591 void GLRenderer::dropEvent (QDropEvent* ev) 1591 void GLRenderer::dropEvent (QDropEvent* ev)
1592 { 1592 {
1593 if (g_win != null and ev->source() == g_win->getPrimitivesTree()) 1593 if (g_win != null and ev->source() == g_win->getPrimitivesTree())
1594 { 1594 {
1595 QString primName = static_cast<SubfileListItem*> (g_win->getPrimitivesTree()->currentItem())->primitive()->name; 1595 QString primName = static_cast<SubfileListItem*> (g_win->getPrimitivesTree()->currentItem())->primitive()->name;
1596 LDSubfilePtr ref = spawn<LDSubfile>(); 1596 LDSubfilePtr ref = LDSpawn<LDSubfile>();
1597 ref->setColor (maincolor()); 1597 ref->setColor (MainColor());
1598 ref->setFileInfo (getDocument (primName)); 1598 ref->setFileInfo (GetDocument (primName));
1599 ref->setPosition (g_origin); 1599 ref->setPosition (Origin);
1600 ref->setTransform (g_identity); 1600 ref->setTransform (IdentityMatrix);
1601 LDDocument::current()->insertObj (g_win->getInsertionPoint(), ref); 1601 LDDocument::current()->insertObj (g_win->getInsertionPoint(), ref);
1602 ref->select(); 1602 ref->select();
1603 g_win->buildObjList(); 1603 g_win->buildObjList();
1604 g_win->R()->refresh(); 1604 g_win->R()->refresh();
1605 ev->acceptProposedAction(); 1605 ev->acceptProposedAction();

mercurial