diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-04-15 08:18:09 +0300 |
---|---|---|
committer | Mika Salmela <mika.salmela@digia.com> | 2013-04-15 09:16:42 +0300 |
commit | 28e8ada5e636a4c897358dfd5bad392135fd7f59 (patch) | |
tree | 413a9b875f0e3cc7d8fcdcb58cdcbbfa95221931 /src/datavis3d | |
parent | de431be4a3da6de758a08e1bc63f5b152a66395d (diff) |
Added drawing the label of selected bar in non-QPainter version
Moved label drawing into it's own function.
Change-Id: I4408e077b28f5d05de2c80adb2a97208271a9c3c
Change-Id: I4408e077b28f5d05de2c80adb2a97208271a9c3c
Reviewed-by: Mika Salmela <mika.salmela@digia.com>
Diffstat (limited to 'src/datavis3d')
-rw-r--r-- | src/datavis3d/engine/q3dbars.cpp | 214 | ||||
-rw-r--r-- | src/datavis3d/engine/q3dbars.h | 3 | ||||
-rw-r--r-- | src/datavis3d/engine/qdataitem.cpp | 20 | ||||
-rw-r--r-- | src/datavis3d/engine/qdataitem_p.h | 12 | ||||
-rw-r--r-- | src/datavis3d/utils/camerahelper.cpp | 6 | ||||
-rw-r--r-- | src/datavis3d/utils/camerahelper_p.h | 2 | ||||
-rw-r--r-- | src/datavis3d/utils/utils.cpp | 20 | ||||
-rw-r--r-- | src/datavis3d/utils/utils_p.h | 2 |
8 files changed, 190 insertions, 89 deletions
diff --git a/src/datavis3d/engine/q3dbars.cpp b/src/datavis3d/engine/q3dbars.cpp index 2fdd5d9c..ac7f8dc9 100644 --- a/src/datavis3d/engine/q3dbars.cpp +++ b/src/datavis3d/engine/q3dbars.cpp @@ -183,7 +183,7 @@ void Q3DBars::render(QPainter *painter) QDataItem *data = d_ptr->m_selectedBar; if (d_ptr->m_selectionMode < ZoomRow && data) { glDisable(GL_DEPTH_TEST); - Utils::printText(painter, data->d_ptr->valueStr(), data->d_ptr->position()); + Utils::printText(painter, data->d_ptr->valueStr(), data->d_ptr->labelSize()); // use size for screen position; this way we don't need 2 member variables in qdataitem } else if (d_ptr->m_zoomActivated) { glDisable(GL_DEPTH_TEST); float scale = 1.0f; @@ -213,7 +213,7 @@ void Q3DBars::render(QPainter *painter) //float coordY = ((1.0f - item->d_ptr->translation().y()) // * d_ptr->m_zoomViewPort.height()) / 2.0f; // Use a fixed label distance from the bottom of the screen - QPoint screenCoords(coordX, 150.0f); // use coord Y for reducing from painter window height to avoid unwanted transformations + QSize screenCoords(coordX, 150.0f); // use coord Y for reducing from painter window height to avoid unwanted transformations Utils::printText(painter, item->d_ptr->valueStr(), screenCoords, false, 60.0f, scale); //QPoint screenCoords(coordX, d_ptr->m_zoomViewPort.height() - 100.0f); // Use a label distance from the bottom of the screen based on bar height @@ -455,10 +455,11 @@ void Q3DBars::drawZoomScene() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } for (int col = 0; col < d_ptr->m_zoomSelection->d_ptr->row().size(); col++) { + QDataItem *item = d_ptr->m_zoomSelection->d_ptr->getItem(col); // // TODO: Optimize! Create textures only when zoomselection changes! Store the texture id's into zoomselection // - QDataItem *item = d_ptr->m_zoomSelection->d_ptr->getItem(col); + // ie. move this segment elsewhere... // Create labels // Print label into a QImage using QPainter QImage label = Utils::printTextToImage(d_ptr->m_font @@ -467,75 +468,19 @@ void Q3DBars::drawZoomScene() , d_ptr->m_theme->m_textColor , d_ptr->m_labelTransparency); + // Set label size + item->d_ptr->setLabelSize(label.size()); // Insert text texture into label - GLuint labelTexture = d_ptr->m_textureHelper->create2DTexture(label, true, true); - - // TODO: Fix drawing of labels when in ZoomColumn -mode - - // Draw label - QMatrix4x4 modelMatrix; - QMatrix4x4 MVPMatrix; - if (ZoomColumn == d_ptr->m_selectionMode) { - modelMatrix.translate(-(item->d_ptr->translation().z()) - zComp - , -1.5f//item->d_ptr->translation().y() - , zComp); - } else { - modelMatrix.translate(item->d_ptr->translation().x() - , -1.5f//item->d_ptr->translation().y() - , zComp); - } - - // Rotate - modelMatrix.rotate(-45.0f, 0.0f, 0.0f, 1.0f); + item->d_ptr->setTextureId(d_ptr->m_textureHelper->create2DTexture(label, true, true)); + // ...ie. move this segment elsewhere - // Calculate scale factor to get uniform font size - float scaledFontSize = 0.05f + d_ptr->m_fontSize / 500.0f; - float scaleFactor = scaledFontSize / (float)label.height(); - - // Scale label based on text size - modelMatrix.scale(QVector3D((float)label.width() * scaleFactor - , scaledFontSize - , 0.0f)); - - MVPMatrix = projectionMatrix * viewMatrix * modelMatrix; - - d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->MVP() - , MVPMatrix); - - // Activate texture - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, labelTexture); - d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->texture() - , 0); - - // 1st attribute buffer : vertices - glEnableVertexAttribArray(d_ptr->m_labelShader->posAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_labelObj->vertexBuf()); - glVertexAttribPointer(d_ptr->m_labelShader->posAtt() - , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + drawLabel(*item, viewMatrix, projectionMatrix, false, -45.0f); - // 2nd attribute buffer : UVs - glEnableVertexAttribArray(d_ptr->m_labelShader->uvAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_labelObj->uvBuf()); - glVertexAttribPointer(d_ptr->m_labelShader->uvAtt() - , 2, GL_FLOAT, GL_FALSE, 0, (void*)0); - - // Index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_labelObj->elementBuf()); - - // Draw the triangles - glDrawElements(GL_TRIANGLES, d_ptr->m_labelObj->indexCount() - , GL_UNSIGNED_SHORT, (void*)0); - - // Free buffers - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - glDisableVertexAttribArray(d_ptr->m_labelShader->uvAtt()); - glDisableVertexAttribArray(d_ptr->m_labelShader->posAtt()); - - glBindTexture(GL_TEXTURE_2D, 0); + // ie. move this segment elsewhere... + GLuint labelTexture = item->d_ptr->textureId(); glDeleteTextures(1, &labelTexture); + item->d_ptr->setTextureId(0); + // ...ie. move this segment elsewhere } glDisable(GL_TEXTURE_2D); @@ -860,7 +805,13 @@ void Q3DBars::drawScene() // Insert data to QDataItem. We have no ownership, don't delete the previous one if (!d_ptr->m_zoomActivated) { d_ptr->m_selectedBar = item; - d_ptr->m_selectedBar->d_ptr->setPosition(d_ptr->m_mousePos); +#ifdef USE_PAINTER_TEXT + QSize mousePositionAsSize = QSize(d_ptr->m_mousePos.x() + , d_ptr->m_mousePos.y()); + d_ptr->m_selectedBar->d_ptr->setLabelSize(mousePositionAsSize); +#else + item->d_ptr->setTranslation(modelMatrix.column(3).toVector3D()); +#endif barSelectionFound = true; if (d_ptr->m_selectionMode >= ZoomRow) { item->d_ptr->setTranslation(modelMatrix.column(3).toVector3D()); @@ -954,15 +905,140 @@ void Q3DBars::drawScene() } } else if (d_ptr->m_selectionMode >= ZoomRow && Q3DBarsPrivate::MouseOnScene == d_ptr->m_mousePressed) { + // Activate zoom mode d_ptr->m_zoomActivated = true; d_ptr->m_sceneViewPort = QRect(0, height() - height() / 5 , width() / 5, height() / 5); + } else { + // Print value of selected bar +#ifndef USE_PAINTER_TEXT + d_ptr->m_labelShader->bind(); + glDisable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); + if (d_ptr->m_labelTransparency > TransparencyNone) { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + // Create label + // Print label into a QImage using QPainter + QImage label = Utils::printTextToImage(d_ptr->m_font + , d_ptr->m_selectedBar->d_ptr->valueStr() + , d_ptr->m_theme->m_textBackgroundColor + , d_ptr->m_theme->m_textColor + , d_ptr->m_labelTransparency); + + // Set label size + d_ptr->m_selectedBar->d_ptr->setLabelSize(label.size()); + // Insert text texture into label + d_ptr->m_selectedBar->d_ptr->setTextureId(d_ptr->m_textureHelper->create2DTexture(label + , true + , true)); + + drawLabel(*d_ptr->m_selectedBar, viewMatrix, projectionMatrix, true); + + GLuint labelTexture = d_ptr->m_selectedBar->d_ptr->textureId(); + glDeleteTextures(1, &labelTexture); + d_ptr->m_selectedBar->d_ptr->setTextureId(0); + + glDisable(GL_TEXTURE_2D); + if (d_ptr->m_labelTransparency > TransparencyNone) + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + + // Release label shader + d_ptr->m_labelShader->release(); +#endif } // Release bar shader d_ptr->m_barShader->release(); } +// TODO: Move to a separate class, so that it can be used by other vis types as well (will need a lot more parameters..) +void Q3DBars::drawLabel(const QDataItem &item, const QMatrix4x4 &viewmatrix + , const QMatrix4x4 &projectionmatrix, bool useDepth, qreal rotation) // TODO: Add enum? for label position +{ + // TODO: Fix drawing of labels when in ZoomColumn -mode + + // Draw label + QMatrix4x4 modelMatrix; + QMatrix4x4 MVPMatrix; + qreal yPosition = -1.5f; + if (useDepth) // replace with enum? or adjustable height + yPosition = item.d_ptr->translation().y() + + (item.d_ptr->value() / d_ptr->m_heightNormalizer);// / 2.0f; + qreal zPosition = zComp; + if (useDepth) + zPosition = item.d_ptr->translation().z(); + if (ZoomColumn == d_ptr->m_selectionMode) { + modelMatrix.translate(-(item.d_ptr->translation().z()) - zComp + , yPosition + , zPosition); + } else { + modelMatrix.translate(item.d_ptr->translation().x() + , yPosition + , zPosition); + } + + // Rotate + modelMatrix.rotate(rotation, 0.0f, 0.0f, 1.0f); + + if (useDepth) { + // Apply negative camera rotations to keep labels facing camera + QPointF rotations = CameraHelper::getCameraRotations(); + modelMatrix.rotate(-rotations.x(), 0.0f, 1.0f, 0.0f); + modelMatrix.rotate(-rotations.y(), 1.0f, 0.0f, 0.0f); + } + + // Calculate scale factor to get uniform font size + float scaledFontSize = 0.05f + d_ptr->m_fontSize / 500.0f; + float scaleFactor = scaledFontSize / (float)item.d_ptr->labelSize().height(); + + // Scale label based on text size + modelMatrix.scale(QVector3D((float)item.d_ptr->labelSize().width() * scaleFactor + , scaledFontSize + , 0.0f)); + + MVPMatrix = projectionmatrix * viewmatrix * modelMatrix; + + d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->MVP() + , MVPMatrix); + + // Activate texture + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, item.d_ptr->textureId()); + d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->texture() + , 0); + + // 1st attribute buffer : vertices + glEnableVertexAttribArray(d_ptr->m_labelShader->posAtt()); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_labelObj->vertexBuf()); + glVertexAttribPointer(d_ptr->m_labelShader->posAtt() + , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + + // 2nd attribute buffer : UVs + glEnableVertexAttribArray(d_ptr->m_labelShader->uvAtt()); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_labelObj->uvBuf()); + glVertexAttribPointer(d_ptr->m_labelShader->uvAtt() + , 2, GL_FLOAT, GL_FALSE, 0, (void*)0); + + // Index buffer + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_labelObj->elementBuf()); + + // Draw the triangles + glDrawElements(GL_TRIANGLES, d_ptr->m_labelObj->indexCount() + , GL_UNSIGNED_SHORT, (void*)0); + + // Free buffers + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glDisableVertexAttribArray(d_ptr->m_labelShader->uvAtt()); + glDisableVertexAttribArray(d_ptr->m_labelShader->posAtt()); + + glBindTexture(GL_TEXTURE_2D, 0); +} + void Q3DBars::mousePressEvent(QMouseEvent *event) { // TODO: for testing shaders diff --git a/src/datavis3d/engine/q3dbars.h b/src/datavis3d/engine/q3dbars.h index df02e0e8..73856813 100644 --- a/src/datavis3d/engine/q3dbars.h +++ b/src/datavis3d/engine/q3dbars.h @@ -208,6 +208,9 @@ protected: private: void drawZoomScene(); void drawScene(); + void drawLabel(const QDataItem &item, const QMatrix4x4 &viewmatrix + , const QMatrix4x4 &projectionmatrix, bool useDepth = false + , qreal rotation = 0.0f); QScopedPointer<Q3DBarsPrivate> d_ptr; Q_DISABLE_COPY(Q3DBars) }; diff --git a/src/datavis3d/engine/qdataitem.cpp b/src/datavis3d/engine/qdataitem.cpp index a1ae0123..1c295cd8 100644 --- a/src/datavis3d/engine/qdataitem.cpp +++ b/src/datavis3d/engine/qdataitem.cpp @@ -74,7 +74,7 @@ QDataItemPrivate::QDataItemPrivate(QDataItem *q, float value, const QString &lab , m_value(value) , m_label(label) , m_prependLabel(false) - , m_position(QPoint(0, 0)) + , m_size(QSize(0, 0)) , m_translation(QVector3D(0, 0, 0)) { } @@ -83,14 +83,14 @@ QDataItemPrivate::~QDataItemPrivate() { } -void QDataItemPrivate::setPosition(const QPoint &position) +void QDataItemPrivate::setLabelSize(const QSize &size) { - m_position = position; + m_size = size; } -QPoint QDataItemPrivate::position() +QSize QDataItemPrivate::labelSize() { - return m_position; + return m_size; } void QDataItemPrivate::setTranslation(const QVector3D &translation) @@ -122,4 +122,14 @@ QString QDataItemPrivate::valueStr() return strVal; } +void QDataItemPrivate::setTextureId(GLuint textureId) +{ + m_textureId = textureId; +} + +GLuint QDataItemPrivate::textureId() +{ + return m_textureId; +} + QTCOMMERCIALDATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/engine/qdataitem_p.h b/src/datavis3d/engine/qdataitem_p.h index d3dec0d6..69dd1a17 100644 --- a/src/datavis3d/engine/qdataitem_p.h +++ b/src/datavis3d/engine/qdataitem_p.h @@ -44,7 +44,8 @@ #include "qdatavis3dglobal.h" #include "qdataitem.h" -#include <QPoint> +#include <QOpenGLFunctions> +#include <QSize> #include <QString> #include <QVector3D> @@ -57,20 +58,23 @@ class QDataItemPrivate , const QString &label = QString()); ~QDataItemPrivate(); - void setPosition(const QPoint &position); - QPoint position(); + void setLabelSize(const QSize &size); + QSize labelSize(); void setTranslation(const QVector3D &translation); + void setTextureId(GLuint textureId); QVector3D translation(); float value(); QString valueStr(); // append value and label. If label has prepend -flag set, append label and value + GLuint textureId(); private: QDataItem *q_ptr; float m_value; QString m_label; bool m_prependLabel; - QPoint m_position; + QSize m_size; QVector3D m_translation; + GLuint m_textureId; friend class QDataItem; }; diff --git a/src/datavis3d/utils/camerahelper.cpp b/src/datavis3d/utils/camerahelper.cpp index 8637da9d..b18c3ffa 100644 --- a/src/datavis3d/utils/camerahelper.cpp +++ b/src/datavis3d/utils/camerahelper.cpp @@ -171,4 +171,10 @@ void CameraHelper::updateMousePos(const QPoint &mousePos) } } +QPointF CameraHelper::getCameraRotations() +{ + QPointF rotations(m_xRotation, m_yRotation); + return rotations; +} + QTCOMMERCIALDATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/utils/camerahelper_p.h b/src/datavis3d/utils/camerahelper_p.h index d5b1e206..fba2c780 100644 --- a/src/datavis3d/utils/camerahelper_p.h +++ b/src/datavis3d/utils/camerahelper_p.h @@ -58,6 +58,8 @@ public: static void setRotationSpeed(int speed); // Set camera rotation in degrees static void setCameraRotation(const QPointF &rotation); + // Get camera rotations + static QPointF getCameraRotations(); // Set default camera orientation. Position's x and y should be 0. static void setDefaultCameraOrientation(const QVector3D &defaultPosition , const QVector3D &defaultTarget diff --git a/src/datavis3d/utils/utils.cpp b/src/datavis3d/utils/utils.cpp index cf59699c..00d27be0 100644 --- a/src/datavis3d/utils/utils.cpp +++ b/src/datavis3d/utils/utils.cpp @@ -60,7 +60,7 @@ QVector3D Utils::vectorFromColor(const QColor &color) return QVector3D(color.redF(), color.greenF(), color.blueF()); } -void Utils::printText(QPainter *painter, const QString &text, const QPoint &position +void Utils::printText(QPainter *painter, const QString &text, const QSize &position , bool absoluteCoords, qreal rotation, qreal scale) { painter->save(); @@ -91,16 +91,16 @@ void Utils::printText(QPainter *painter, const QString &text, const QPoint &posi // , bgrLen, 30, 10.0, 10.0); #if 0 // Hack solution, as drawRect doesn't work - painter->drawText(position.x() - (bgrStrLen / 2) - , position.y() - bgrHeight + painter->drawText(position.width() - (bgrStrLen / 2) + , position.height() - bgrHeight , bgrStrLen, bgrHeight , Qt::AlignCenter | Qt::AlignVCenter , bgrStr); //painter->setPen(d_ptr->m_textColor); painter->setPen(Qt::lightGray); // TODO: Use lightGray, as nothing works painter->setFont(valueFont); - painter->drawText(position.x() - (valueStrLen / 2) - , position.y() - bgrHeight + painter->drawText(position.width() - (valueStrLen / 2) + , position.height() - bgrHeight , valueStrLen, bgrHeight , Qt::AlignCenter | Qt::AlignVCenter , text); @@ -109,20 +109,20 @@ void Utils::printText(QPainter *painter, const QString &text, const QPoint &posi painter->scale(scale, scale); if (absoluteCoords) { // This assumes absolute screen coordinates - painter->translate(position.x() - (((float)bgrStrLen / 2.0f) * cos(rotation * m_pi / 180.0f)) + painter->translate(position.width() - (((float)bgrStrLen / 2.0f) * cos(rotation * m_pi / 180.0f)) + (((float)bgrHeight / 2.0f) * sin(rotation * m_pi / 180.0f)) - , position.y() + , position.height() - ((((float)bgrHeight / 2.0f) * cos(rotation * m_pi / 180.0f)) + (((float)bgrStrLen / 2.0f) * sin(rotation * m_pi / 180.0f)))); } else { // This calculates y as a distance from screen bottom - painter->translate(position.x() - (((float)bgrStrLen / 2) * cos(rotation * m_pi / 180.0f)) + painter->translate(position.width() - (((float)bgrStrLen / 2) * cos(rotation * m_pi / 180.0f)) + (((float)bgrHeight / 2.0f) * sin(rotation * m_pi / 180.0f)) - , painter->window().height() - position.y() + , painter->window().height() - position.height() - ((((float)bgrHeight / 2.0f) * cos(rotation * m_pi / 180.0f)) + (((float)bgrStrLen / 2.0f) * sin(rotation * m_pi / 180.0f)))); } - //qDebug() << painter->window().height() - position.y() + //qDebug() << painter->window().height() - position.height() // - ((((float)bgrHeight / 2.0f) * cos(rotation * m_pi / 180.0f)) // + (((float)bgrStrLen / 2.0f) * sin(rotation * m_pi / 180.0f))); painter->rotate(rotation); diff --git a/src/datavis3d/utils/utils_p.h b/src/datavis3d/utils/utils_p.h index 506944ab..41871d75 100644 --- a/src/datavis3d/utils/utils_p.h +++ b/src/datavis3d/utils/utils_p.h @@ -58,7 +58,7 @@ class Utils { public: static QVector3D vectorFromColor(const QColor &color); - static void printText(QPainter *painter, const QString &text, const QPoint &position + static void printText(QPainter *painter, const QString &text, const QSize &position , bool absoluteCoords = true, qreal rotation = 0, qreal scale = 1.0f); static QImage printTextToImage(const QFont &font , const QString &text |