Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Grayscale Shader: Combining color and texture

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Grayscale Shader: Combining color and texture

    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/6...-texture-color

    Thanks,
    Stephan

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,793

    Re: Grayscale Shader: Combining color and texture

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Re: Grayscale Shader: Combining color and texture

    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...

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,793

    Re: Grayscale Shader: Combining color and texture

    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.

  5. #5
    Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    466

    Re: Grayscale Shader: Combining color and texture

    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...).

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Re: Grayscale Shader: Combining color and texture

    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...

  7. #7
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Grayscale Shader: Combining color and texture

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •