PDA

View Full Version : Issue draw object transparency using QGLShaderProgram



andreaQt
15th October 2020, 10:54
I can't control the transparency by change the value on alpha, this is my code (its s part of a large project that i tray to build:
glwidget.h


#include <QGLWidget>
#include <QGLShaderProgram>
#include <QVector>
#include <QVector3D>
#include <QCheckBox>
#include <QMenuBar>
#include <QList>

class GlWidget : public QGLWidget
{
//! [0]
Q_OBJECT
public:
GlWidget(QWidget *parent = 0);
~GlWidget();
QSize sizeHint() const;
....
private:
QGLShaderProgram shaderProgram;
....
};

glwidget.cpp


#include "glwidget.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>
#include <math.h>

GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(/* Additional format options */), parent)
{
initializeWindow(); // storede some data
}
//------------------------------------------------------------------------------------------------------------------------
GlWidget::~GlWidget()
{
}
//------------------------------------------------------------------------------------------------------------------------
QSize GlWidget::sizeHint() const
{
return QSize(1024,768);
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
qglClearColor(QColor(Qt::black));
shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, ":/fragmentShader.fsh");
shaderProgram.link();
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;

QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);

QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);

vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);

shaderProgram.bind();

shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

// X_axes_BODY
//shaderProgram.setUniformValue("color", QColor(Qt::red));
shaderProgram.setUniformValue("color", QColor(255, 0, 0, 127));
shaderProgram.setAttributeArray("vertex", x_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDepthMask(GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, x_axes_body.size());
glDepthMask(GL_TRUE);
// Y_axes_BODY
shaderProgram.setUniformValue("color", QColor(Qt::white));
shaderProgram.setAttributeArray("vertex", y_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, y_axes_body.size());
// Z_axes_BODY
shaderProgram.setUniformValue("color", QColor(Qt::green));
shaderProgram.setAttributeArray("vertex", z_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, z_axes_body.size());

shaderProgram.disableAttributeArray("vertex");

shaderProgram.release();
}

fragmentShader.fsh


#version 130

uniform vec4 color;

out vec4 fragColor;

void main(void)
{
fragColor = color;
}

vertexShader.vsh


#version 130

uniform mat4 mvpMatrix;

in vec4 vertex;

void main(void)
{
gl_Position = mvpMatrix * vertex;
}

by calling "glDepthMask(GL_FALSE)" before drawing the element to be transparent, and by calling "glDepthMask(GL_TRUE)" to restore the state of NO transparency, i can draw just the first element transparent (is a test to draw some element transparent & some no), but if in QColor(255, 0, 0, ???) i change the value of alpha data the behavior of the transaprency is the same -> the element is drawed in full transparency.
I have miss something, or what wrong i do?