简体   繁体   中英

Opengl ES 2.0 - Problem with color opacity by glDrawElements(GL_TRIANGLES)

I created simple c++ class for drawing square in opengl es 2.0. It puts square on specific place with specific color and opacity. All is fine, except opacity. I set color and opacity with function "setColor". I expect 0.0f full transparent and 1.0 full visible. Looks like, for black color it works, but white color is still full visible, does not matter what I set to opacity. For other colors is opacity weird too.

At beginning I set

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

My class :

GLSquare::GLSquare() {
    vecs = new std::vector<GLfloat>();
    indices = new std::vector<GLshort>();
    colors = new std::vector<GLfloat>();
    indicesCount = 6;

    for (int i = 0; i < 12; i++) {
        vecs->push_back(0.0f);
    }

    for (int i=0; i<16; i+=4) {
        colors->push_back(0.0f);
        colors->push_back(0.0f);
        colors->push_back(0.0f);
        colors->push_back(1.0f);
    }

    GLshort ind[] = { 0, 1, 2, 0, 2, 3 };
    for (int i = 0; i < indicesCount; i++) {
        indices->push_back(ind[i]);
    }
}

GLSquare::~GLSquare() {
    delete vecs;
    delete colors;
    delete indices;
}

void GLSquare::draw(Matrix *matrix) {
    glUseProgram(program->id);

    glEnableVertexAttribArray(program->positionHandle);

    glVertexAttribPointer(program->positionHandle, 3, GL_FLOAT, false, 12, &vecs->front());

    glEnableVertexAttribArray(program->colorHandle);

    // Prepare the background coordinate data
    glVertexAttribPointer(program->colorHandle, 4, GL_FLOAT, false, 0, &colors->front());

    glUniformMatrix4fv(program->matrixHandle, 1, false, matrix->projectionAndView);
    glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_SHORT, &indices->front());

    glDisableVertexAttribArray(program->positionHandle);
    glDisableVertexAttribArray(program->colorHandle);
}

void GLSquare::set(float left, float top, float width, float height) {
    position[0] = left;
    position[1] = top;
    size[0] = width;
    size[1] = height;

    vecs->at(0) = left;
    vecs->at(1) = top;

    vecs->at(3) = left;
    vecs->at(4) = top + height;

    vecs->at(6) = left + width;
    vecs->at(7) = top + height;

    vecs->at(9) = left + width;
    vecs->at(10) = top;
}

void GLSquare::setColor(Color color, GLfloat opacity) {
    for (int i=0; i<16; i+=4) {
        colors->at(i) = color.r;
        colors->at(i + 1) = color.g;
        colors->at(i + 2) = color.b;
        colors->at(i + 3) = opacity;
    }
}

my fragment shader is simple :

precision mediump float; 

varying vec4 v_Color;

void main() 
{ 
    gl_FragColor = v_Color;
}

My vertex shader :

uniform   mat4 uMVPMatrix;

attribute vec4 vPosition;
attribute vec4 a_Color;

varying vec4 v_Color;

void main() 
{ 
   gl_Position = uMVPMatrix * vPosition;
   v_Color = a_Color;
}

Gettings colorHandle from shader:

program->colorHandle = glGetAttribLocation(programID, "a_Color");

Here is how it looks with :

square->setColor(Color(1.0f,1.0f,1.0f), 0.1f);

white rectangle in left top corner

在此处输入图片说明

and here is with black color, opacity 0.1

在此处输入图片说明

I found the problem. Code works correctly, other 3rd party library overrides glBlendFunc. Fixing it solved the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM