PDA

View Full Version : Grayscale Shader: Combining color and texture



speedbird25
09-12-2011, 02:31 AM
Hi!

We have the following problem: We want to implement a grayscale fragment shader for the entire scene. The scene consists of a 2D graphics that uses

* non-textured primitives (e.g. GL_POLYGON) and
* textured primitives (GL_QUADS).

Rendering is done in an external library and some is done in our library.

The fragment shader code (very simple conversion to grayscale):

uniform sampler2D tex;

void main()
{
gl_FragColor = gl_Color * texture2D(tex, gl_TexCoord[0].st);
gl_FragColor = glFragColor.rrra;
}

Now, the following problem arises: When there is no texture used, the pixels are still combined with a texel color and look strange. The question is: Is there any way to check if a texture is currently bound? We know that we can use a uniform variable and set it from outside, but as some parts of the scene come from an external library, we don't know when a texture is used.

This post at stackoverflow is related:

http://stackoverflow.com/questions/6686741/fragment-shader-glsl-for-texture-color-and-texture-color

Thanks,
Stephan

Alfonse Reinheart
09-12-2011, 02:43 AM
You need a separate shader for each case. You could do some #define weirdness if you want, but for your simple case, it'd be easier to just have multiple shaders for each case.

speedbird25
09-12-2011, 02:56 AM
We had that idea too, but the problem is, we don't know when to use the correct shader. The external library may draw a textured primitive or draw a non-textured primitive...

Alfonse Reinheart
09-12-2011, 03:26 AM
If you have no control over what the user is doing, or no way to know what the user will be doing, then there isn't anything you can do. If all you can do is bind a program and see what happens, then there are no options for you.

OpenGL is not meant to be used in parts, where one guy sets up one thing and another sets up something else independently.

Either the external library needs to tell you more about what it is going to render, or you need to impose what the external library will be rendering.

_arts_
09-12-2011, 03:36 AM
I've had quite a similar problem.

I didn't want to bind different shaders for textured and not textured primitives. What I did first is to check the texture color fragment: if it is (0,0,0,0) then I assumed there was no texture bound.

However, I found it weird, and we suggested me to always bind texture and use 1x1 pixel full white texture for untextured geometry.

I don't know if you can do the last suggestion in your case, but maybe at least the first one could help you (as long as you don't have full black and full transparent pixels in your textures...).

speedbird25
09-12-2011, 03:47 AM
First, thanks for your quick answers!

@_arts_ We'll try your first suggestion. That's an compromise since we have control which textures are loaded.

I wonder about how the fixed functionality pipeline does the combination of color and texture...

BionicBytes
09-12-2011, 07:13 AM
Fixed function manages colour and textures by way of GL state and texture environment/ parameters.
In the most basic case, the last glColor specified will be modulated against the texture colour components. If you don't want texturing to affect the output colour, then disable texturing! The OpenGL specification describes the various effects of texturing and environment colour combinations.