Texture and color together in GLSL?

I cannot figure out how to get similar results with OpenGL ES 2.0 in OpenGL ES 1.1. I want to use Sampler2D (to mix my texture with Alpha Channel with Framebuffer) and also set the color. The texture should be painted in color - for example, 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 "+ colorVarying" part destroys my alpha channel in black (because I also add colorVarying if AlphaValue is 0) and creates a weird gradient effect ... How is the texture and color channel combined in the pipeline of a fixed function? My replacement for 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); } 

And I use glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if it is so important ...

For colors 1.0, 0.0, 1.0, 1.0 here I get the following: Now

And I want to get:

What I want to see

Some ideas for this? Any help would be appreciated.

+8
alpha fragment-shader blending
source share
2 answers

To combine your vertex color with a texture, like the default OpenGL ES 1.1, you 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 color rather than adding to it.

You see the gradient in your image, because passing the step from 0 to the vertex functions of the array specification in OpenGL ES (like 1.1 and 2.0) does not lead to step 0, rather, OpenGL ES calculates the step for you, taking densely packed elements of the format you specify / type. As a result, you are actually reading the end of your array into random memory. If you want to have the same value at all vertices, you must set the current value of the attribute 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); } 
+15
source share

Reply to summary messages is great for me. I just wanted to add what it would look like with the 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 a fragmentary shader.

0
source share

All Articles