gpt4 book ai didi

c++ - Opengl ES 2.0-glDrawElements(GL_TRIANGLES)出现颜色不透明的问题

转载 作者:行者123 更新时间:2023-12-02 10:37:42 26 4
gpt4 key购买 nike

我创建了用于在opengl es 2.0中绘制正方形的简单c++类。它在具有特定颜色和不透明度的特定位置放置正方形。一切都很好,除了不透明度。我使用功能“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);

左上角的白色矩形

enter image description here

这是黑色,不透明度0.1

enter image description here

最佳答案

我发现了问题。代码正常工作,其他第3方库将覆盖glBlendFunc。修复它解决了这个问题

关于c++ - Opengl ES 2.0-glDrawElements(GL_TRIANGLES)出现颜色不透明的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com