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 3 of 3

Thread: Automatically identify color source

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    1

    Question Automatically identify color source

    Hi everyone!

    I want to write a simple fragment shader which multiplies an extra alpha value to each drawn object in a collection, no matter the type of the object. However, as I am new to shaders, my solution is rather clumsy:

    Code :
    uniform sampler2D testTexture;
     
    // alpha value to use
    uniform float alpha;
     
    // to decide where the color of the fragment comes from
    uniform int textured = 0;
     
    void main(void) {
     if (gl_TexCoord[0].s < 0.0 || gl_TexCoord[0].s > 1.0) discard;
     if (gl_TexCoord[0].t < 0.0 || gl_TexCoord[0].t > 1.0) discard;
     
     if (textured > 0) {
      gl_FragColor = texture2D(testTexture, gl_TexCoord[0].xy);
      gl_FragColor = gl_FragColor * alpha;
     } else {
      gl_FragColor = gl_Color * alpha;
     }
    }

    As there seem to be two sources for the color (textured and gl_Color, depending on the renderable object) and I do not know how to automatically tell which one to take, I have this extra field "textured" which I have to set manually for each object.

    Is there a way to automatically identify the correct source of the fragment color?

    Thanks a lot!

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,098
    There is no correct source. It is you who decidse whether to render an object with a texture or a solid colour; so you have to indicate to the fragment shader what to choose. How you do this is up to you. The reason for shaders is to remove
    an resriction on how a mesh is rendered. The up side is you can do what every you like - chose between texture or colour or use both combined; the down side is you have to code every thing for yourself.

  3. #3
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,165
    Is there a way to automatically identify the correct source of the fragment color?
    Perhaps a separate shader for each 'class' of object?

Tags for this Thread

Posting Permissions

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