How do I use Shaders correctly?

Hello,

I am just getting started developing Games with OpenGL ES 2.0 on Android and I have a Problem. I want to know how I can manage my Shaders. Therefor I have a little example:

Let’s say I want to render two primitives, a square and a triangle. Beside I have two Shader Programs each consists of one Fragment and one Vertex Shader. These are all compiled and installed.
I already know that I have to use Vertex Attributes and in OpenGL ES 2.0 there are at least 8 Attributes avaliable.
Now I want that the Square will go through the two Shader Programs but the Triangle should only passed through the first one.
What is the best way to do that?

Thank you very much.

Now I want that the Square will go through the two Shader Programs but the Triangle should only passed through the first one.

That doesn’t make sense. When you render an object, it goes to the screen. You can render it again (multipass), but that’s just rendering it again. It works just the same for shaders as it did before (with the possible exception for the need to use the “invariant” keyword). You render it once, change the appropriate state (in this case, the current program) and render it again.

Ok maybe I just don’t understand how this works. OpenGL ES is shader-based. When I want to render a primitive I need at least one Shader-Program. The Vertex-Data will be passed through the Vertex Attributes. But let’s say I want to render more than one primitives. How do I specify the Data for these two Primitives and send it to the Shader Program. Can anyone please give me an example? Sorry but I really don’t understand how this worḱs.

A values for an attribute is specified per-vertex.
So each primitive will get different values.
Read the tutorial in Alfonse signature, especially this section :
http://www.arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html

Ok I think I understand now but one thing remains.

Here it say that I have to use glUseProgram for enbling Shaders as follows:

glUseProgram(program1);
//render stuff.
glUseProgram(program2);
//render other stuff.

But does that mean that I can only use one Shader Program a time or can I use more than one and if it is possible, how do I do this? Just use a Loop for that.
And also little Question about OpenGL ES. OpenGL ES 2.0 is something like OpenGL 2.0 for mobile Devices. Does that mean that I can use hardly any function from OpenGL 2.0 in OpenGL ES 2.0 like the functions for Lighting etc.? in the official OpenGL ES 2.0 Book there isn’t much about lighting.

But does that mean that I can only use one Shader Program a time

Yes. Using ARB_separate_program_objects, you can use multiple programs, each one for different stages. But it has some… quirks.

OpenGL ES 2.0 is something like OpenGL 2.0 for mobile Devices.

No. OpenGL ES 2.0 is OpenGL ES 2.0. It’s a separate specification that, while based on various OpenGL versions and tries to retain compatibility with OpenGL where possible, is not OpenGL. It’s OpenGL ES.

Specifically, ES 2.0 lacks all of the fixed-function pipeline from ES 1.1. So you do everything with shaders.

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