我正在研究 OpenGL 2.1,并且在 gl_FragColor 上的 alpha 值存在一些问题。
整个代码:
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;
}
有人可以向我解释为什么:
作品:
vec4 col = texture2D(texture_0, varTexCoords);
//vec4 col = vec4(0.0, 0.0, 0.0, 0.5);
gl_FragColor = col;
不起作用:
//vec4 col = texture2D(texture_0, varTexCoords);
vec4 col = vec4(0.0, 0.0, 0.0, 0.5);
gl_FragColor = col;
作品:
vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
//col.a = 0.5;
gl_FragColor = col;
也有效:
vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
col.a *= 0.5;
gl_FragColor = col;
不起作用:
vec4 col = texture2D(texture_0, varTexCoords);
col.rgb = uColor;
col.a = 0.5;
gl_FragColor = col;
即使许多示例似乎都使用它,这个也不起作用:
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
代码中的错误发生在这里:
glEnableVertexAttribArray(textureCoords);
CHECK_GL_ERROR("glEnableVertexAttribArrayCheck");
与着色器相关的所有代码:
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");
}
错误是 GL_INVALID_VALUE 并且仅在执行代码后发生,而不是在编译和链接着色器后发生。