Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture and color together in GLSL?

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.

like image 938
Constantin Avatar asked Feb 14 '11 23:02

Constantin


People also ask

What is blending in OpenGL?

Blending is the stage of OpenGL rendering pipeline that takes the fragment color outputs from the Fragment Shader and combines them with the colors in the color buffers that these outputs map to. Blending parameters can allow the source and destination colors for each output to be combined in various ways.

What is a uniform in GLSL?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.

What is texture2D GLSL?

The texture2D function returns a texel, i.e. the (color) value of the texture for the given coordinates. The function has one input parameter of the type sampler2D and one input parameter of the type vec2 : sampler, the uniform the texture is bound to, and coord, the 2-dimensional coordinates of the texel to look up.

What is the difference between GLSL and HLSL?

In GLSL, you apply modifiers (qualifiers) to a global shader variable declaration to give that variable a specific behavior in your shaders. In HLSL, you don't need these modifiers because you define the flow of the shader with the arguments that you pass to your shader and that you return from your shader.


2 Answers

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);
}
like image 117
Pivot Avatar answered Sep 28 '22 17:09

Pivot


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.

like image 25
Roovy Avatar answered Sep 28 '22 18:09

Roovy