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
Qt Code:
  1. #include <QGLWidget>
  2. #include <QGLShaderProgram>
  3. #include <QVector>
  4. #include <QVector3D>
  5. #include <QCheckBox>
  6. #include <QMenuBar>
  7. #include <QList>
  8.  
  9. class GlWidget : public QGLWidget
  10. {
  11. //! [0]
  12. Q_OBJECT
  13. public:
  14. GlWidget(QWidget *parent = 0);
  15. ~GlWidget();
  16. QSize sizeHint() const;
  17. ....
  18. private:
  19. QGLShaderProgram shaderProgram;
  20. ....
  21. };
To copy to clipboard, switch view to plain text mode 
glwidget.cpp
Qt Code:
  1. #include "glwidget.h"
  2. #include <QMouseEvent>
  3. #include <QWheelEvent>
  4. #include <QDebug>
  5. #include <math.h>
  6.  
  7. GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(/* Additional format options */), parent)
  8. {
  9. initializeWindow(); // storede some data
  10. }
  11. //------------------------------------------------------------------------------------------------------------------------
  12. GlWidget::~GlWidget()
  13. {
  14. }
  15. //------------------------------------------------------------------------------------------------------------------------
  16. QSize GlWidget::sizeHint() const
  17. {
  18. return QSize(1024,768);
  19. }
  20. //------------------------------------------------------------------------------------------------------------------------
  21. void GlWidget::initializeGL()
  22. {
  23. glEnable(GL_DEPTH_TEST);
  24. qglClearColor(QColor(Qt::black));
  25. shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
  26. shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
  27. shaderProgram.link();
  28. }
  29. //------------------------------------------------------------------------------------------------------------------------
  30. void GlWidget::paintGL()
  31. {
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33.  
  34. QMatrix4x4 mMatrix;
  35. QMatrix4x4 vMatrix;
  36.  
  37. QMatrix4x4 cameraTransformation;
  38. cameraTransformation.rotate(alpha, 0, 1, 0);
  39. cameraTransformation.rotate(beta, 1, 0, 0);
  40.  
  41. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
  42. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
  43.  
  44. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
  45.  
  46. shaderProgram.bind();
  47.  
  48. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
  49.  
  50. // X_axes_BODY
  51. //shaderProgram.setUniformValue("color", QColor(Qt::red));
  52. shaderProgram.setUniformValue("color", QColor(255, 0, 0, 127));
  53. shaderProgram.setAttributeArray("vertex", x_axes_body.constData());
  54. shaderProgram.enableAttributeArray("vertex");
  55. glDepthMask(GL_FALSE);
  56. glDrawArrays(GL_TRIANGLE_STRIP, 0, x_axes_body.size());
  57. glDepthMask(GL_TRUE);
  58. // Y_axes_BODY
  59. shaderProgram.setUniformValue("color", QColor(Qt::white));
  60. shaderProgram.setAttributeArray("vertex", y_axes_body.constData());
  61. shaderProgram.enableAttributeArray("vertex");
  62. glDrawArrays(GL_TRIANGLE_STRIP, 0, y_axes_body.size());
  63. // Z_axes_BODY
  64. shaderProgram.setUniformValue("color", QColor(Qt::green));
  65. shaderProgram.setAttributeArray("vertex", z_axes_body.constData());
  66. shaderProgram.enableAttributeArray("vertex");
  67. glDrawArrays(GL_TRIANGLE_STRIP, 0, z_axes_body.size());
  68.  
  69. shaderProgram.disableAttributeArray("vertex");
  70.  
  71. shaderProgram.release();
  72. }
To copy to clipboard, switch view to plain text mode 
fragmentShader.fsh
Qt Code:
  1. #version 130
  2.  
  3. uniform vec4 color;
  4.  
  5. out vec4 fragColor;
  6.  
  7. void main(void)
  8. {
  9. fragColor = color;
  10. }
To copy to clipboard, switch view to plain text mode 
vertexShader.vsh
Qt Code:
  1. #version 130
  2.  
  3. uniform mat4 mvpMatrix;
  4.  
  5. in vec4 vertex;
  6.  
  7. void main(void)
  8. {
  9. gl_Position = mvpMatrix * vertex;
  10. }
To copy to clipboard, switch view to plain text mode 
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?