Reputation: 1480
I'm currently trying to learn OpenGL, and I'm having trouble with texture transparency.
I have 2 planes/rectangles that are textured, the first is closest to the camera, the second is further back, behind the first one. Both are textured with an image that has a background at 50% opacity.
I should be able to see the texture of the second plane behind the first since the first is transparent to some degree - but I don't. The images I'm using do in fact appear on their own (can see them if I rotate the camera), and they have words on them (which I would expect to kind of bleed through the semi-transparent background of the image in front).
Is there a way to correct this? Here is my initialization and paintGL methods (they are probably horribly optimized)
void oglWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
qglClearColor(QColor(Qt::white));
shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
shaderProgram.link();
// vertices[1] << QVector3D(-xAmt, -1, 0) << QVector3D( xAmt, -1, 0) << QVector3D( xAmt, 1, 0) // Front
// << QVector3D( xAmt, 1, 0) << QVector3D(-xAmt, 1, 0) << QVector3D(-xAmt, -1, 0);
float xAmt = 16.00/9.00;
vertices << QVector3D(-xAmt, -1, 0) << QVector3D( xAmt, -1, 0) << QVector3D( xAmt, 1, 0) // Midground
/*<< QVector3D( xAmt, 1, 0) << QVector3D(-xAmt, 1, 0) << QVector3D(-xAmt, -1, 0)*/
<< QVector3D(-xAmt, -1, -1) << QVector3D( xAmt, -1, -1) << QVector3D( xAmt, 1, -1) // Background
<< QVector3D( xAmt, 1, -1) << QVector3D(-xAmt, 1, -1) << QVector3D(-xAmt, -1, -1);
textureCoordinates << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Front
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Front
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0);
}
void oglWidget::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);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shaderProgram.setUniformValue("texture", 0);
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
shaderProgram.enableAttributeArray("textureCoordinate");
glGenTextures(0, &texture); glBindTexture(GL_TEXTURE_2D, texture);
QImage tempTexture = QGLWidget::convertToGLFormat(QImage(ImgManager::getImg("/Textures/Editor/m.png")));
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tempTexture.width(), tempTexture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tempTexture.bits() );
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shaderProgram.release();
}
Upvotes: 0
Views: 1617
Reputation: 2455
Even though blending is enabled new geometry will perform a depth test to check if the current fragment is visible and populate the depth buffer with new values.
A simple way to fix it is to disable depth testing completly with glDisable(GL_DEPTH_TEST);
, but the better way is the following:
glDepthMask(GL_TRUE);
glDepthMask(GL_FALSE);
glDepthMask(GL_FALSE);
will disable the writing to the depth buffer while the depth test itself is still enabled. As a result opaque objects will occlude transparent objects only if they are really in front of them.
Upvotes: 3