How to call textCoord and Colors separately


I have a model that contains 2 meshes (a ball and a cube); the ball is textured and for the cube it’s colored by vertex. Now I want to render them using OpenGL but how can I render them in their correct data (textures for ball and Colors for the cube?
In my fragment shader I have this:
FragColor=texture(textures, textCoord) * vs_color;

This will call both Colors and textures but not what I’m looking for I want to separate them.
How to do it?

You have two options:

  1. Use different shaders for textured objects and vertex-coloured objects.

  2. Use a solid white texture for the cube (a 1x1 texture will suffice) and set the vertex colours to white for the sphere. Note that you don’t need to use an attribute array for the sphere; you can use glDisableVertexAttribArray and set a constant value for the attribute with glVertexAttrib.

Neither of these approaches is more “correct” than the other. For non-trivial programs, determining which solution is faster may require trying both and measuring. A shader which only uses one source of colour information will be slightly faster, but the cost of switching shaders may outweigh the gain.

So for the first approach I have to call glUseProgram() with the correct program using switch case or if for instance? Is it right?
What do you think if I use alpha=0 that’s way the color will be transparent and not opaque?
For the second approach it’s not very clear, can you give me a doc or a link so I can explore more?

You don’t have to have two shaders. I’ve got a single texture-shader that draws ‘bare’ surfaces by simply bypassing the texture(…) function. I use a uniform Color-value instead … and obviously a uniform switch-value to signal when to use what.

I found this:
This is what I’m looking for(if statement with uUseTexture) but how can I declare the uUseTexture variable in my code (not in the vertex and fragment shader) so I can bind it with the vertex and then fragment shader?

Use glGetUniformLocation and glUniform1i.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.