I have some Mesh
-Objects like a flat rectangle:
private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) {
float size = size_mesh/2;
mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0), VertexAttribute.Normal());
mesh.setVertices(new float[]
// Position XYZ Color RGBA Texture Coordinates UV Normal XYZ
//|-,-,-,| |-,-,-,-,| |-,-,| |-,-,-|
{
-size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 1, 0, 0, -1,
size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 1, 0, 0, 1,
size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 0, 0, 0, 1,
-size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 0, 0, 0, -1
});
mesh.setIndices(new short[] {0, 1, 2,2, 3, 0});
}
My task is to create a vertex shader which let such an object wave in some way.
My idea:
First I deliver the vertex shader a uniform
variable which changes every second from 0 to 1 and vice versa:
Date endDate = new Date();
float numSeconds = (float)((endDate.getTime() - startDate.getTime()) / 1000);
float even = (numSeconds % 2);
...
shader.setUniformMatrix("u_worldView", cam.combined);//WVP-Matrix
shader.setUniformi("u_texture", 0);
shader.setUniformf("u_even", even);
Each vertex of the rectangle object has an Normal
vector, like you in see above in the generateMesh
method. I can access and use this vector inside the vertex shader:
uniform float u_even;
uniform mat4 u_worldView;
attribute vec4 a_color;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_normal;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
if(u_even > 0.5) gl_Position = u_worldView * a_position + a_normal;
else gl_Position = u_worldView * a_position - a_normal;
}
I expected that the object would change every second, which it does in some way, but I also manipulate the camera position everytime. The view position also goes back and forth if the values of the Normal
vector are all 0.
I have also tried:
if(u_even > 0.5) gl_Position = u_worldView * (a_position + a_normal);
else gl_Position = u_worldView * (a_position - a_normal);
But its even worse...
So how can I change the position of a vertex without changing the whole point of view?
I appreciate your help!