PDA

View Full Version : How do I use Shaders correctly?



Limas
03-02-2011, 07:17 AM
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.

Alfonse Reinheart
03-02-2011, 01:44 PM
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.

Limas
03-03-2011, 06:05 AM
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.

ZbuffeR
03-03-2011, 06:49 AM
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

Limas
03-08-2011, 11:06 AM
Ok I think I understand now but one thing remains.

Here (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=293060&Searchpa ge=1&Main=56510&Words=load+shader+from+file&Search =true#Post293002) 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.

Alfonse Reinheart
03-08-2011, 12:02 PM
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.