diff --git a/src/ViewCube.cpp b/src/ViewCube.cpp index 18530e4..723dbf7 100644 --- a/src/ViewCube.cpp +++ b/src/ViewCube.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace { @@ -74,6 +75,35 @@ void ViewCube::paintGL(QOpenGLShaderProgram* simpleShader, int simpleShaderColor drawViewCube(viewCubeProjection, viewMatrix, opacity); drawAxes(simpleShader, simpleShaderColorLoc, viewCubeProjection, viewMatrix); + QMatrix4x4 mvp = viewCubeProjection * viewMatrix; + const float axisLen = 1.7f; + + QVector3D xAxisEnd(axisLen, 0.0f, 0.0f); + QVector3D yAxisEnd(0.0f, 0.0f, axisLen); + QVector3D zAxisEnd(0.0f, axisLen, 0.0f); + + QVector3D xAxisNdc = mvp.map(xAxisEnd); + QVector3D yAxisNdc = mvp.map(yAxisEnd); + QVector3D zAxisNdc = mvp.map(zAxisEnd); + + const int vpX = width - viewCubeSize; + const int vpY = height - viewCubeSize; + const int vpW = viewCubeSize; + const int vpH = viewCubeSize; + const float dpr = QGuiApplication::primaryScreen()->devicePixelRatio(); + + m_xAxisLabelPos.setX( (xAxisNdc.x() + 1.0f) * vpW / 2.0f + vpX ); + m_xAxisLabelPos.setY( (1.0f - xAxisNdc.y()) * vpH / 2.0f + vpY ); + m_xAxisLabelPos /= dpr; + + m_yAxisLabelPos.setX( (yAxisNdc.x() + 1.0f) * vpW / 2.0f + vpX ); + m_yAxisLabelPos.setY( (1.0f - yAxisNdc.y()) * vpH / 2.0f + vpY ); + m_yAxisLabelPos /= dpr; + + m_zAxisLabelPos.setX( (zAxisNdc.x() + 1.0f) * vpW / 2.0f + vpX ); + m_zAxisLabelPos.setY( (1.0f - zAxisNdc.y()) * vpH / 2.0f + vpY ); + m_zAxisLabelPos /= dpr; + glDisable(GL_BLEND); } @@ -229,6 +259,23 @@ void ViewCube::drawAxes(QOpenGLShaderProgram* simpleShader, int colorLoc, const void ViewCube::paint2D(QPainter& painter, int widgetWidth, int widgetHeight) { + QFont font("Arial", 10, QFont::Bold); + painter.setFont(font); + painter.setPen(Qt::black); + QFontMetrics fm(font); + + QRect xRect = fm.boundingRect("X"); + xRect.moveCenter(m_xAxisLabelPos); + painter.drawText(xRect, Qt::AlignCenter, "X"); + + QRect yRect = fm.boundingRect("Y"); + yRect.moveCenter(m_yAxisLabelPos); + painter.drawText(yRect, Qt::AlignCenter, "Y"); + + QRect zRect = fm.boundingRect("Z"); + zRect.moveCenter(m_zAxisLabelPos); + painter.drawText(zRect, Qt::AlignCenter, "Z"); + if (!m_isHovered) { return; } diff --git a/src/ViewCube.h b/src/ViewCube.h index db1ba61..8d5d20b 100644 --- a/src/ViewCube.h +++ b/src/ViewCube.h @@ -34,6 +34,10 @@ private: QRect m_homeButtonRect; QSvgRenderer* m_homeButtonRenderer = nullptr; + QPoint m_xAxisLabelPos; + QPoint m_yAxisLabelPos; + QPoint m_zAxisLabelPos; + QOpenGLTexture* m_faceTextures[6]; QOpenGLShaderProgram* m_textureShaderProgram = nullptr;