PDA

View Full Version : shared info on a same triangle [gles2]?



billconan
08-05-2010, 01:29 PM
Hello guys,

I have a question about sharing information between vertices on a same triangle when writing GLES 2 shaders.

before, when we use Opengl, sharing an information between vertices on a same triangle is very easy.

for example, if we want the normal of the three vertices of the triangle be the same, we can simply do this:

glBegin(GL_TRIANGLES);
glNormal3f();
glVertex3f();
glVertex3f();
glVertex3f();
glEnd();

the three points will have the same normal inside the shader program.

but as for GLES2 (and i guess the new opengl standard), we are forced to use vertex attributes.

so how do i share an information between points on the same triangle.

the normal case is one example. another example is the texture coordinate case.

for example, if i want to texture map a ball. there must be some points that are on the seam of the texture. in other words, those points must have two sets of texture coordinates or even more.

depending on which triangle you are drawing, you assign difference texture coordinates to the point.

but with gles 2 shader, i don't know how to do this, because it seems that a vertex always maps to a single texture coordinate:

attribute vec4 vertex;
attribute vec4 texcoord;

of course, duplicating points is a solution, but in the facet normal case, you need to duplicate a lot of points in order to use different normals on different facet.

i would like to give another example:

suppose we have a geometry object formed by triangles.

now, i need to write a gles 2 shader that color different triangle with different color.

how do i do that?

DmitryM
08-05-2010, 01:40 PM
First: how I deal with face data.

During the export stage, each vertex is cloned for each face it belongs. A special key is constructed for each vertex, containing attributes that has to be unique (position, normal, texture coordinates, colors, etc). Then vertices with equal keys are merged into one that will be exported.

The case of facet normals will actually use the maximum number of vertices (one per original vertex per face), but it doesn't have a real use-case scenario with a big number of vertices. You may have a simple facet model (like a cube), but once the vertex number is relatively high, you generally want shading to be smooth.



now, i need to write a gles 2 shader that color different triangle with different color.

Another way is to use geometry shader's gl_PrimitiveID value. You can prepare some GPU storage of face colors in form of texture/uniform array/uniform buffer and sample from it using this coordinate.

kyle_
08-07-2010, 05:26 AM
I wonder if this use case is frequent enough to warrant attribute divisor like behavior for 'per face', or 'per n-vertices' attributes.

Ilian Dinev
08-07-2010, 08:57 AM
With GLES2 you can't avoid the vertex duplication, but with GL3.x you can - even without geometry shaders.
The key hardware feature required is "flat" varying interpolation:

http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=265872