GLSL 中的纹理和颜色在一起?

发布于 2024-10-17 12:49:46 字数 1046 浏览 2 评论 0原文

我不知道如何使用 OpenGL ES 2.0 获得与 OpenGL ES 1.1 类似的结果。我实际上想使用 Sampler2D(将我的纹理与 Alpha 通道混合到帧缓冲区)并设置颜色。纹理应该用颜色绘制 - 就像 OpenGL ES 1.1 中那样 我的 FragmentShader 看起来像这样:

varying lowp vec4 colorVarying;
varying mediump vec2 texcoordVarying;
uniform sampler2D texture;

void main(){
    gl_FragColor = texture2D(texture, texcoordVarying) + colorVarying;
}

但是“+ colorVarying”部分用黑色破坏了我的 alpha 通道(因为我还添加了 colorVarying,如果 AlphaValue 为 0)并产生奇怪的渐变效果......纹理和颜色如何?颜色通道合并在固定功能管道中?我对 glColor4f 的替换是:

void gl2Color4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a){
    const GLfloat pointer[] = {r, g, b, a};
    glVertexAttribPointer(ATTRIB_COLOR, 2, GL_FLOAT, 0, 0, pointer);
    glEnableVertexAttribArray(ATTRIB_COLOR);
}

我正在使用 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);如果这在某种程度上相关...

对于颜色 1.0, 0.0, 1.0, 1.0 这是我现在得到的: Now

我想得到:

我想看到什么

实现这一目标的一些想法? 任何帮助将不胜感激。

I cant figure out, how to get with OpenGL ES 2.0 similiar Results to OpenGL ES 1.1. I want to use actually a Sampler2D (to blend my texture with Alpha Channel to the Framebuffer) and also set an Color. The texture should be painted in the color - like in OpenGL ES 1.1
My FragmentShader looks like this:

varying lowp vec4 colorVarying;
varying mediump vec2 texcoordVarying;
uniform sampler2D texture;

void main(){
    gl_FragColor = texture2D(texture, texcoordVarying) + colorVarying;
}

But the part "+ colorVarying" destroys my alphachannel with black (because I also add colorVarying, if the AlphaValue is 0) and makes an strange gradient effect... How is the texture & color channel combined in the fixed function pipeline? My Replacement for glColor4f is:

void gl2Color4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a){
    const GLfloat pointer[] = {r, g, b, a};
    glVertexAttribPointer(ATTRIB_COLOR, 2, GL_FLOAT, 0, 0, pointer);
    glEnableVertexAttribArray(ATTRIB_COLOR);
}

And I'm using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if this is somehow relevant...

For the color 1.0, 0.0, 1.0, 1.0 here is what I get now:
Now

And I want to get:

What I want to see

Some ideas to accomplish this?
Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-10-24 12:49:46

要按照 OpenGL ES 1.1 默认情况下的方式将顶点颜色与纹理结合起来,您需要片段着色器为:

varying lowp vec4 colorVarying;
varying mediump vec2 texcoordVarying;
uniform sampler2D texture;

void main(){
    gl_FragColor = texture2D(texture, texcoordVarying) * colorVarying;
}

请注意,GL_MODULATE 将纹理乘以颜色,而不是相加。

您在图像中看到了渐变,因为将步长 0 传递给 OpenGL ES(1.1 和 2.0)中的顶点数组规范函数不会导致步长为 0,而是 OpenGL ES 会为您计算步长,假设您指定的格式/类型的紧密包装元素。因此,您实际上是在将数组末尾读入随机内存中。如果您希望所有顶点具有相同的值,则应该设置当前属性值并禁用关联的数组:

void gl2Color4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a){
    const GLfloat pointer[] = {r, g, b, a};
    glVertexAttrib4fv(ATTRIB_COLOR, pointer);
    glDisableVertexAttribArray(ATTRIB_COLOR);
}

To combine your vertex color with your texture the way OpenGL ES 1.1 does by default, you’ll want your fragment shader to be:

varying lowp vec4 colorVarying;
varying mediump vec2 texcoordVarying;
uniform sampler2D texture;

void main(){
    gl_FragColor = texture2D(texture, texcoordVarying) * colorVarying;
}

Note that GL_MODULATE multiplies the texture by the color, rather than adding to it.

You’re seeing a gradient in your image because passing a stride of 0 to vertex array specification functions in OpenGL ES (both 1.1 and 2.0) doesn’t result in a stride of 0—rather, OpenGL ES calculates the stride for you, assuming tightly packed elements of the format/type you specified. As a result, you’re actually reading past the end of your array into random memory. If you want the same value across all vertices, you should set the current attribute value and disable the associated array:

void gl2Color4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a){
    const GLfloat pointer[] = {r, g, b, a};
    glVertexAttrib4fv(ATTRIB_COLOR, pointer);
    glDisableVertexAttribArray(ATTRIB_COLOR);
}
吃素的狼 2024-10-24 12:49:46

枢轴答案对我来说非常有效。我只是想添加新版本的 GLSL (3.3+) 的外观:

in vec2 passedUvCoords;
in vec4 passedColor;

out vec4 outColor;

uniform sampler2D textureSampler;

void main(void) {
    outColor = texture(textureSampler, passedUvCoords) * passedColor;
}

这只是片段着色器。

Pivots answer is working great for me. I just wanted to add how it would look like with a newer version of GLSL (3.3+):

in vec2 passedUvCoords;
in vec4 passedColor;

out vec4 outColor;

uniform sampler2D textureSampler;

void main(void) {
    outColor = texture(textureSampler, passedUvCoords) * passedColor;
}

This is just the fragment shader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文