Results 1 to 3 of 3

Thread: vertex shader with color attribute question

  1. #1
    Member Contributor
    Join Date
    May 2010
    Posts
    66

    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
    Senior Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    892
    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
    Member Contributor
    Join Date
    May 2010
    Posts
    66
    Awesome! Thanks very much
    Bob

Similar Threads

  1. Replies: 1
    Last Post: 06-20-2018, 05:40 AM
  2. Running a vertex shader without any per-vertex attribute
    By mobeen in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-11-2013, 06:12 AM
  3. attribute in vertex shader
    By Mick Derevnin in forum OpenGL: GLSL
    Replies: 3
    Last Post: 04-05-2011, 05:28 PM
  4. Cannot pass color value to shader vertex attribute
    By Phinehas in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 10-05-2010, 09:16 AM
  5. color value in shader vertex attribute
    By Phinehas in forum OpenGL: GLSL
    Replies: 1
    Last Post: 10-04-2010, 12:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean