2

I'm working on OpenGL 2.1 and have some problems with alpha value on gl_FragColor.

Whole code:

uniform sampler2D texture_0;
uniform vec3 uColor;
varying vec2 varTexCoords;

void main(void)
{
    //vec4 col = texture2D(texture_0, varTexCoords);
    vec4 col = vec4(0.0, 0.0, 0.0, 0.5);
    gl_FragColor = col;
}

Can someone explain to me why:

Works:

vec4 col = texture2D(texture_0, varTexCoords);
//vec4 col = vec4(0.0, 0.0, 0.0, 0.5);
gl_FragColor = col;

Doesn't work:

//vec4 col = texture2D(texture_0, varTexCoords);
vec4 col = vec4(0.0, 0.0, 0.0, 0.5);
gl_FragColor = col;

Works:

vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
//col.a = 0.5;
gl_FragColor = col;

Also works:

vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
col.a *= 0.5;
gl_FragColor = col;

Doesn't work:

vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
col.a = 0.5;
gl_FragColor = col;

And this one dosen't work even though many examples seem to use it:

gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);

Error occurence in code is here:

glEnableVertexAttribArray(textureCoords);
        CHECK_GL_ERROR("glEnableVertexAttribArrayCheck");

All code related to the shader:

inline void Renderer::renderText(float x, float y, string msg) {
    mat4 proj;
    Matrix::projection2D(proj,
        (float) nScreenWidth_, (float) nScreenHeight_, 0.0f);
    mat4 res, restmp;
    mat4 pos;
    mat4 rot;
    mat4 scale;
    //Vector3D p(72.0f, 88.0f, 1.0f);
    //Vector3D p(20.0f, 20, 1.0f);
    Vector3D r(0.0f, 0.0f, 0.0f);
    Vector3D s(1.0f, nScreenWidth_ / nScreenHeight_, 1.0f);
    //Matrix::translate(pos, p.getX(), p.getY(), p.getZ());
    //Matrix::rotateZ(rot, r.getZ());

    float widthMod = nScreenWidth_ / 100.0f;
    float heightMod = nScreenHeight_ / 100.0f;

    Matrix::translate(pos, x * widthMod, y * heightMod, 1.0f);
    Matrix::rotateZ(rot, r.getZ());
    //Matrix::scale(scale, s.getX() * widthMod, s.getY() * heightMod, 1.0f);
    Matrix::scale(scale, 16.0f, 16.0f, 1.0f);

    Matrix::multiply(proj, pos, res);
    Matrix::multiply(res, rot, restmp);
    Matrix::multiply(restmp, scale, res);

    // Select shader program to use.
    int shaderId = features_->getText()->getShaderId();
    glUseProgram(shaderId);
    CHECK_GL_ERROR("glUseProgram");

    int matrix = glGetUniformLocation(shaderId, "uWVP");
    int color = glGetUniformLocation(shaderId, "uColor");
    int texture = glGetUniformLocation(shaderId, "texture_0");
    CHECK_GL_ERROR("glGetUniformLocation");
    int textureCoords = glGetAttribLocation(shaderId, "attrTexCoords");
    int vertices = glGetAttribLocation(shaderId, "attrPos");
    CHECK_GL_ERROR("glGetAttribLocation");

    // Specify WVP matrix.
    glUniformMatrix4fv(matrix, 1, false, res);
    CHECK_GL_ERROR("glUniformMatrix4fv");

    // Bind the texture.
    glActiveTexture(GL_TEXTURE0);
    CHECK_GL_ERROR("glActiveTexture");
    glBindTexture(GL_TEXTURE_2D, features_->getText()->getFontMapId());
    CHECK_GL_ERROR("glBindTexture");
    glUniform1i(texture, 0);
    CHECK_GL_ERROR("glUniform1i");


    glEnableVertexAttribArray(vertices);
    CHECK_GL_ERROR("glEnableVertexAttribArray");

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    CHECK_GL_ERROR("glBindBuffer");

    glEnable(GL_BLEND);
    CHECK_GL_ERROR("glEnable");
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    CHECK_GL_ERROR("glBlendFunc");

    //string text = output_;
    for (unsigned int i = 0; i < msg.length(); i++) {
        unsigned short l = static_cast<unsigned short>(msg[i]) - 32;
        mat4 delta, resmove;
        Matrix::translate(delta, 1.6f, 0.0f, 0.0f);
        Matrix::multiply(res, delta, resmove);
        Matrix::copy(resmove, res);
        glUniformMatrix4fv(matrix, 1, false, res);
        CHECK_GL_ERROR("glUniformMatrix4fv");
        float col[] = {0.0f, 1.0f, 0.0f};
        glUniform3fv(color, 1, col);
        CHECK_GL_ERROR("glUniform3fv");
        glVertexAttribPointer(vertices, 3, GL_FLOAT, GL_FALSE, 0,
            features_->getText()->vertices_);
        CHECK_GL_ERROR("glVertexAttribPointer");
        glEnableVertexAttribArray(textureCoords);
        CHECK_GL_ERROR("glEnableVertexAttribArrayCheck");
        glVertexAttribPointer(textureCoords, 2, GL_FLOAT, GL_FALSE, 0,
            features_->getText()->getSymbol(l));
        CHECK_GL_ERROR("glVertexAttribPointer");
        glDrawArrays(GL_TRIANGLES, 0, 18 / 3);
        CHECK_GL_ERROR("glDrawArrays");
    }

    glDisable(GL_BLEND);
    CHECK_GL_ERROR("glDisable");
}

The error is GL_INVALID_VALUE and only occurs after executing code, not after compiling and linking shader.

7
  • 3
    What does "works" and "doesn't work" mean here? How do you determine this? Commented Oct 22, 2012 at 10:58
  • Doesn't work means I get opengl error after glEnableVertexAttribArray call.I pick up error with GLint error = glGetError(). Commented Oct 22, 2012 at 12:38
  • Before error occurs only vertex coordinates are passed, vertex array looks like this: GLfloat vertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f }; Commented Oct 22, 2012 at 13:09
  • GL_INVALID_VALUE is generated if index is greater than or equal to GL_MAX_VERTEX_ATTRIBS. Please check exactly what values you are passing in glEnableVertexAttribArray(..) Commented Oct 22, 2012 at 13:20
  • glEnableVertexAttribArray(vertices) ... where is this one? Commented Oct 22, 2012 at 13:25

1 Answer 1

4

This is probably what is happening : (I say "compiler" here, but it's probably the linker that does the actual purging)

The shader compliler drops this one :

varying vec2 varTexCoords;

If the compiler determines that a variable is not used, it will be discarded.

The last example is good :

vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
col.a = 0.5;
gl_FragColor = col;

The compiler understands that the original value in col is overwritten by the uColor uniform and the 0.5 constant. The texture read is dropped, so the varying is also dropped.

Then your attrTexCoords will also most likely be dropped, so your textureCoords variable containing the attrib locations is -1.

Here on the other hand, the compiler cannot remove the texture read because col.bg will contain values from the texture.

vec4 col = texture2D(texture_0, varTexCoords);
col.r = uColor.r;
col.a = 0.5;
gl_FragColor = col;
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, just figured this out, apparently it changes textureCoords to -1, when it is not used and this causes my code throw an error when passing -1. It's a little strange because when first testing values, I could swear I got 1 on textureCoords. Probably messed up something else while checking. The last example isn't good, because apparently the compiler again optimizes varTexCoords out, but changing second line to col.r = uColor.r; works. I'll be more careful in the future and keep in mind that some unused values might be removed. Thanks.
The last example is very good in fact and should explain why it works when you change the second line to : col.r = uColor.r
I meant not good that with it, the code throws error, but yes as an example it is indeed very good and shows the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.