簡體   English   中英

Opengl ES 2.0 - glDrawElements(GL_TRIANGLES) 的顏色不透明度問題

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

我創建了簡單的 C++ 類,用於在 opengl es 2.0 中繪制正方形。 它將正方形放在具有特定顏色和不透明度的特定位置。 一切都很好,除了不透明度。 我使用函數“setColor”設置顏色和不透明度。 我期望 0.0f 完全透明和 1.0 完全可見。 看起來,對於黑色它有效,但白色仍然完全可見,與我設置的不透明度無關。 對於其他顏色,不透明度也很奇怪。

一開始我設置

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

我的課 :

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;
    }
}

我的片段着色器很簡單:

precision mediump float; 

varying vec4 v_Color;

void main() 
{ 
    gl_FragColor = v_Color;
}

我的頂點着色器:

uniform   mat4 uMVPMatrix;

attribute vec4 vPosition;
attribute vec4 a_Color;

varying vec4 v_Color;

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

從着色器獲取 colorHandle:

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

這是它的外觀:

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

左上角的白色矩形

在此處輸入圖片說明

這是黑色,不透明度 0.1

在此處輸入圖片說明

我發現了問題。 代碼工作正常,其他 3rd 方庫覆蓋 glBlendFunc。 修復它解決了問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM