feat: Add translucent view cube with hover opacity effect

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-17 16:49:49 -07:00
parent 34ecee0fa2
commit 5e20822df4
4 changed files with 25 additions and 7 deletions

View File

@@ -34,17 +34,31 @@ void ViewCube::initializeGL()
setupBuffers(); setupBuffers();
} }
void ViewCube::paintGL(QOpenGLShaderProgram* simpleShader, int simpleShaderColorLoc, const QMatrix4x4& viewMatrix, int width, int height) void ViewCube::paintGL(QOpenGLShaderProgram* simpleShader, int simpleShaderColorLoc, const QMatrix4x4& viewMatrix, int width, int height, const QPoint& mousePos)
{ {
int viewCubeSize = 150 * QGuiApplication::primaryScreen()->devicePixelRatio(); int viewCubeSize = 150 * QGuiApplication::primaryScreen()->devicePixelRatio();
glViewport(width - viewCubeSize, height - viewCubeSize, viewCubeSize, viewCubeSize); glViewport(width - viewCubeSize, height - viewCubeSize, viewCubeSize, viewCubeSize);
QRect viewCubeRect(width - viewCubeSize, 0, viewCubeSize, viewCubeSize);
QPoint physicalMousePos = mousePos * QGuiApplication::primaryScreen()->devicePixelRatio();
float opacity = 0.25f;
if (viewCubeRect.contains(physicalMousePos)) {
opacity = 1.0f;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
QMatrix4x4 viewCubeProjection; QMatrix4x4 viewCubeProjection;
viewCubeProjection.ortho(-2, 2, -2, 2, -10, 10); viewCubeProjection.ortho(-2, 2, -2, 2, -10, 10);
drawViewCube(viewCubeProjection, viewMatrix); drawViewCube(viewCubeProjection, viewMatrix, opacity);
drawAxes(simpleShader, simpleShaderColorLoc, viewCubeProjection, viewMatrix); drawAxes(simpleShader, simpleShaderColorLoc, viewCubeProjection, viewMatrix);
glDisable(GL_BLEND);
} }
void ViewCube::createFaceTextures() void ViewCube::createFaceTextures()
@@ -146,7 +160,7 @@ void ViewCube::setupBuffers()
m_axesVbo.release(); m_axesVbo.release();
} }
void ViewCube::drawViewCube(const QMatrix4x4& projection, const QMatrix4x4& view) void ViewCube::drawViewCube(const QMatrix4x4& projection, const QMatrix4x4& view, float opacity)
{ {
if (!m_textureShaderProgram || !m_textureShaderProgram->isLinked()) return; if (!m_textureShaderProgram || !m_textureShaderProgram->isLinked()) return;
@@ -155,6 +169,7 @@ void ViewCube::drawViewCube(const QMatrix4x4& projection, const QMatrix4x4& view
m_textureShaderProgram->setUniformValue("projectionMatrix", projection); m_textureShaderProgram->setUniformValue("projectionMatrix", projection);
m_textureShaderProgram->setUniformValue("modelViewMatrix", view); m_textureShaderProgram->setUniformValue("modelViewMatrix", view);
m_textureShaderProgram->setUniformValue("texture_diffuse1", 0); m_textureShaderProgram->setUniformValue("texture_diffuse1", 0);
m_textureShaderProgram->setUniformValue("opacity", opacity);
QOpenGLVertexArrayObject::Binder vaoBinder(&m_cubeVao); QOpenGLVertexArrayObject::Binder vaoBinder(&m_cubeVao);
for (int i = 0; i < 6; ++i) { for (int i = 0; i < 6; ++i) {

View File

@@ -5,6 +5,7 @@
#include <QMatrix4x4> #include <QMatrix4x4>
#include <QOpenGLVertexArrayObject> #include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
#include <QPoint>
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class QOpenGLTexture; class QOpenGLTexture;
@@ -16,13 +17,13 @@ public:
~ViewCube(); ~ViewCube();
void initializeGL(); void initializeGL();
void paintGL(QOpenGLShaderProgram* simpleShader, int simpleShaderColorLoc, const QMatrix4x4& viewMatrix, int width, int height); void paintGL(QOpenGLShaderProgram* simpleShader, int simpleShaderColorLoc, const QMatrix4x4& viewMatrix, int width, int height, const QPoint& mousePos);
private: private:
void createFaceTextures(); void createFaceTextures();
void initShaders(); void initShaders();
void setupBuffers(); void setupBuffers();
void drawViewCube(const QMatrix4x4& projection, const QMatrix4x4& view); void drawViewCube(const QMatrix4x4& projection, const QMatrix4x4& view, float opacity);
void drawAxes(QOpenGLShaderProgram* simpleShader, int colorLoc, const QMatrix4x4& projection, const QMatrix4x4& view); void drawAxes(QOpenGLShaderProgram* simpleShader, int colorLoc, const QMatrix4x4& projection, const QMatrix4x4& view);
QOpenGLTexture* m_faceTextures[6]; QOpenGLTexture* m_faceTextures[6];

View File

@@ -191,7 +191,7 @@ void ViewportWidget::paintGL()
QMatrix4x4 viewCubeModel; QMatrix4x4 viewCubeModel;
viewCubeModel.rotate(m_camera->xRotation() / 16.0f, 1, 0, 0); viewCubeModel.rotate(m_camera->xRotation() / 16.0f, 1, 0, 0);
viewCubeModel.rotate(m_camera->yRotation() / 16.0f, 0, 1, 0); viewCubeModel.rotate(m_camera->yRotation() / 16.0f, 0, 1, 0);
m_viewCube->paintGL(m_shaderProgram, m_colorLoc, viewCubeModel, width() * retinaScale, height() * retinaScale); m_viewCube->paintGL(m_shaderProgram, m_colorLoc, viewCubeModel, width() * retinaScale, height() * retinaScale, m_currentMousePos);
glViewport(0, 0, width() * retinaScale, height() * retinaScale); glViewport(0, 0, width() * retinaScale, height() * retinaScale);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);

View File

@@ -5,8 +5,10 @@ out vec4 FragColor;
in vec2 TexCoord; in vec2 TexCoord;
uniform sampler2D texture_diffuse1; uniform sampler2D texture_diffuse1;
uniform float opacity;
void main() void main()
{ {
FragColor = texture(texture_diffuse1, TexCoord); vec4 texColor = texture(texture_diffuse1, TexCoord);
FragColor = vec4(texColor.rgb, texColor.a * opacity);
} }