GLSL 中的纹理和颜色在一起?
我不知道如何使用 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 这是我现在得到的:
我想得到:
实现这一目标的一些想法? 任何帮助将不胜感激。
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:
And I want to get:
Some ideas to accomplish this?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要按照 OpenGL ES 1.1 默认情况下的方式将顶点颜色与纹理结合起来,您需要片段着色器为:
请注意,
GL_MODULATE
将纹理乘以颜色,而不是相加。您在图像中看到了渐变,因为将步长 0 传递给 OpenGL ES(1.1 和 2.0)中的顶点数组规范函数不会导致步长为 0,而是 OpenGL ES 会为您计算步长,假设您指定的格式/类型的紧密包装元素。因此,您实际上是在将数组末尾读入随机内存中。如果您希望所有顶点具有相同的值,则应该设置当前属性值并禁用关联的数组:
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:
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:
枢轴答案对我来说非常有效。我只是想添加新版本的 GLSL (3.3+) 的外观:
这只是片段着色器。
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+):
This is just the fragment shader.