I'm in the middle of porting some code from OpenGL ES 1.x to OpenGL ES 2.0, and I'm trying my best to achieve transparency, as it was before; all my triangles are completely opaque.
My OpenGL setup has the following lines:
// Draw objects back to front glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthMask(false);
And my shaders look like this:
attribute vec4 Position; uniform highp mat4 mat; attribute vec4 SourceColor; varying vec4 DestinationColor; void main(void) { DestinationColor = SourceColor; gl_Position = Position * mat; }
and this:
varying lowp vec4 DestinationColor; void main(void) { gl_FragColor = DestinationColor; }
What could be wrong?
EDIT: If I set the alpha in the fragment shader manually to 0.5 in the fragment shader (or even in the vertex shader) as suggested by keaukraine below , then I'm all transparent. Also, if I change the color values, I go to OpenGL as a float instead of unsigned bytes, then the code works correctly.
So, there seems to be something wrong with the code conveying color information in OpenGL, and I would still like to know what the problem is.
My vertices were defined like this (no change from OpenGL ES 1.x code):
typedef struct { GLfloat x, y, z, rhw; GLubyte r, g, b, a; } Vertex;
And I used the following code to pass them to OpenGL (similar to OpenGL ES 1.x):
glBindBuffer(GL_ARRAY_BUFFER, glTriangleVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * nTriangleVertices, triangleVertices, GL_STATIC_DRAW); glUniformMatrix4fv(matLocation, 1, GL_FALSE, m); glVertexAttribPointer(positionSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x)); glVertexAttribPointer(colorSlot, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, r)); glDrawArrays(GL_TRIANGLES, 0, nTriangleVertices); glBindBuffer(GL_ARRAY_BUFFER, 0);
What is wrong with that?