Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access to other vertex in a vertex shader program in opengl es 2?

I a android application , I want to calcul the normal of a surface depanding on other vertex of this surface. I don't want to do it in "master" programm because it take to much time. Actually for each vertex i pass 4 float array for each vextex :

attribute vec3 a_bottom;
attribute vec3 a_left;
attribute vec3 a_right;
attribute vec3 a_top;

vec3 calculNormal( ) {
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

it is very very very dirty code i know, so instead of passing 4 arrays , i want to do that :

vec3 calculNormal( ) {
    vec3 a_left = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - 1 ];
    vec3 a_bottom = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - X ];
    ...
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

So is it possible in a vertex shader programm to acess to the current float buffer ? Is there a special keywords like currentFloat ? Or is there another possibility that i miss ?

like image 268
pixcon Avatar asked Jan 31 '12 12:01

pixcon


2 Answers

This is indeed not possible. A vertex shader only has access to the currently processed vertex and its attributes. And since OpenGL ES doesn't have a texture_buffer_object extension, you cannot access the data of a VBO inside a shader. So the only way to access a vertex' neighbours is by explicitly putting them in as vertex attributes, like in your first example.

But since it looks like your geometry is a rectangular pattern, you might also store it in a texture (or copy it to one, a pixel_buffer_object extension might help with that if supported in ES). In this case you could just use a classic GPGPU fragment shader, that computes the normals for each "pixel" (in this case a vertex) of the output image (in this case the normal data of the rectangular geometry) based on the values of its neighbours (accessed by simple texture accesses).

But I guess neither the first one nor the second one would really by you anything, considering the additional prgramming overhead and/or memory copy operations, since computing vertex normals is already pretty fast. You don't have to do it every frame anyway and if you really do, then it is because you're updating your vertex positions, too, in which case you can use the same update routine for the normal data (be it CPU or GPGPU).

like image 135
Christian Rau Avatar answered Sep 28 '22 18:09

Christian Rau


I want to calcul the normal of a surface depanding on other vertex of this surface.

Wrong approach. Calculate the normals and store them together with the vertices.

I don't want to do it in "master" programm because it take to much time.

You should not recalculate the normals for each rendering pass. Just calculate them one and store them. Calculations in the vertex shader are not for free. And calculating normals in the VS is just waste of processing power.

Calculate them, store them.

like image 40
datenwolf Avatar answered Sep 28 '22 17:09

datenwolf