Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES 2.0 multiple meshes? (just Real World Examples)

I`m a little confused about this point.

Everything that I found in books, blogs, forums and even in OpenGl specs just talk about a very abstract techniques. Nothing about real world examples.

And I`m going crazy with this: How to put and manage multiple objects (meshes) with OpenGL ES 2.x?

In theory seems simple. You have a Vertex Shader (vsh) and Fragment Shader (fsh), then you bind the both to one Program(glGenProgram, glUseProgram, ...). In the every cycle of render, that Program will perform its VSH by each Vertex and after this will perform FSH on every "pixel" of that 3d object and finally send the final result to the buffer (obviously without talk about the per-vertex, rasterization, and other steps in the pipeline).

OK, seems simple...

All this is fired by a call to the draw function (glDrawArrays or glDrawElements).

OK again.

Now the things comes confused to me. And If you have several objects to render?

Let's talk about a real world example. Imagine that you have a landscape with trees, and a character. The grass of the landscape have one texture, the trees have texture to the trunk and leaves (Texture Atlas) and finally the character has another texture (Texture Atlas) and is animated too.

After imagine this scene, my question is simple: How you organize this?

You create a separated Program (with one VSH and FSH) for each element on the scene? Like a Program to the grass and soil's relief, a Program to the trees and a Program to the character?

I've tried it, but... when I create multiple Programs and try to use glVertexAttribPointer() the textures and colors of the objects enter in conflicts with each others. Because the location of the attributes, the indexes, of the first Program repeat in the second Program.

Let me explain, I used glGetAttribLocation() in one class that controls the floor of the scene, so the OpenGL core returned to me the index of 0,1 and 2 for the vertexes attributes. After, in the class of trees I created another Program, anothers shaders, and after used again the glGetAttribLocation() at this time the OpenGL core return with indexes of 0, 1, 2 and 3.

After in the render cycle, I started setting the first Program with glUseProgram() and I've made changes to its vertexes attributes with glVertexAttribPointer() and finally a call to glDrawElements(). After this, call again glUseProgram() to the second Program and use glVertexAttribPointer() again and finally glDrawElements(). But at this point, the things enter in conflicts, because the indexes of vertexes attributes of the second Program affects the vertexes of the first Program too.

I'm tried a lot of thing, searched a lot, asked a lot... I'm exhausted. I can't find what is wrong.

So I started to think that I'm doing everything wrong!

Now I repeat my question again: How to work with multiple meshes (with different textures and behavior) in OpenGL ES 2.x? Using multiple Programs? How?

like image 719
user464230 Avatar asked Oct 25 '10 18:10

user464230


1 Answers

To draw multiple meshes, just call glDrawElements/glDrawArrays multiple times. If those meshes require different shaders, just set them. ONE, and only ONE shader program is active.

So each time you change your shader program (Specifically the VS), you need to reset all vertex attributes and pointers.

Just simple as that.

like image 61
Dr. Snoopy Avatar answered Nov 10 '22 04:11

Dr. Snoopy