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: vertex shader with color attribute question

  1. #1
    Intern Newbie
    Join Date
    Nov 2010
    Posts
    40

    vertex shader with color attribute question

    Hello,

    I am drawing a bunch of 2D objects using OpenGLES 2.0, so I have vertex and fragment shaders. Some of the objects have color data at each vertex, and some of the objects are all one color.

    Do I need to switch shader programs whenever I switch from drawing a single-color object to a multi-color one, and vice-versa? Obviously the multi-color objects will need to use a shader which uses the color-per-vertex data as an attribute. But can I render the single-color objects using the same shader? I was thinking that a uniform variable could indicate whether to look at the color-per-vertex data or whether to use a global color.

    So, for monochrome objects I would not want to pass any color-per-vertex data. But my question is -- will there be problems if the colorattribute is not bound to valid data, even if I don't use it?

    Does that make sense? I'm wondering if it is possible to draw both types of objects without switching shaders, and without having to supply all that redundant color vertex data for the monochrome objects.

    Thanks
    Bob

  2. #2
    Advanced Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    781
    This is very easy. If the vertex shader reads an attribute that is not enabled (glDisableVertexAttribArray) then the shader reads the attribute from the "current vertex" instead of an array.

    And you can set a constant attribute in the "current vertex" with glVertexAttrib.

    So yes, you can use exactly the same shader and simply change the vertex array state. No need to write an ubershader with flow control via a uniform.

  3. #3
    Intern Newbie
    Join Date
    Nov 2010
    Posts
    40
    Awesome! Thanks very much
    Bob

Posting Permissions

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