[osg-users] gl_vertexID and OpenSceneGraph question

Eon Strife eon_strife at yahoo.com
Wed Mar 21 06:53:24 PDT 2018


Hi,
This question is similar to my question in other thread about Texture Buffer Object. I figure this might be a different problem, i.e. on gl_vertexID instead of Texture Buffer Object, that's why I create this thread.

I need to attach additional information to each vertex of a loaded 3D model (in FBX format). In order to make it easier to understand, I will try to make it a very simple case, just say I want to attach additional RGBA color to each vertex (in my actual application, it's more than that).

The additional information is contained in a separate file in JSON format. So, in my application I load the FBX 3D model and the JSON file. The JSON file contains a bunch of RGBA values, with ordering according to the vertices list in the FBX file (as they are output from the same application). Just say first RGBA corresponds to vertex #1, second RGBA corresponds to vertex #2, and so on.

Now the problem is that, in vertex shader we cannot know the current vertex should get which RGBA value. So, I try to use gl_vertexID, as it contains vertex index. 

In order to test whether gl_vertexID works or not, I try to test assigning grayscale value to each vertex according to its gl_vertexID :


Code:

vec3 FixColor = vec3(float(gl_VertexID)/382.0);
out_vColor1 = vec4(FixColor, 1.0);




Note that I divide it by 382 since the test 3D object is a sphere with 382 vertices. However when I run it, I see that the spiral grayscale color becomes fully white only after only one-quarter of the sphere (Figure A in the attached image). However if I divide it by 2282.0 (which is 382 x 6) :


Code:

vec3 FixColor = vec3(float(gl_VertexID)/2282.0);
out_vColor1 = vec4(FixColor, 1.0);




Then we can see the spiral grayscale color nicely becomes fully white in another end (Figure B in the attached image).

In the FBX file, the sphere only uses 382 unique vertices and it uses indexing. That is what I expect when I use gl_vertexID. 

However, I find out what the OpenSceneGraph does is actually duplicating the vertices. Most of the vertices are duplicated five times because they are shared by six triangles (Figure C in the attached image).

According to OpenGL reference :


> gl_VertexID is a vertex language input variable that holds an integer index for the vertex. The index is implicitly generated by glDrawArrays and other commands that do not reference the content of the GL_ELEMENT_ARRAY_BUFFER, or explicitly generated from the content of the GL_ELEMENT_ARRAY_BUFFER by commands such as glDrawElements. For glDrawElements forms that take a basevertex, gl_VertexID will have this value added to the index from the buffer. 


So, it turns out that maye OpenSceneGraph is using glDrawArrays instead of glDrawElements..that's why the gl_vertexID is the implicitly generated, with automatic increment. So, the vertices are duplicated, and they have different indices, even though they should be identical.

So, my question is that, is there a way for me to force OpenSceneGraph (or maybe the FBX loader plugin) to use indexing as defined in the 3D object file, so that I can use gl_vertexID to attach correct RGBA information to each vertex ?


... 

Thank you!

Cheerrs
Eon

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=73144#73144




Attachments: 
http://forum.openscenegraph.org//files/osg_160.png




More information about the osg-users mailing list