Faster transformation with VBOS

Hello, im attempting to use a transformation method to modify my VBOS however I would like to know what toher way I can edit the positions within the VBO without having to create it again. This is how I am doing it at the moment. I get 30fps doing 10000 transformations.

	public void translate(int x, int y) {
		verticies.clear();
		for (int i = 0; i < position.length; i++) {
			float cx = position[i].getx();
			float cy = position[i].gety();
			verticies.put(cx + x);
			verticies.put(cy + y);
			position[i].setcoordinates(cx + x, cy + y);
		}
		verticies.rewind();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticies, GL15.GL_DYNAMIC_DRAW);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
	}

EDIT:
Note is that position is an array of vertex2d objects (not OpenGL ones).

The easiest and probably fastest way to do this would be to do this in a vertex shader for constantly updating data.


// Modifies the matrix to translate
public void translate(int x, int y)
{
     transformMatrix.m03 -= x;
     transformMatrix.m13 -= y;
}

public void render()
{
     glBindBuffer(GL_ARRAY_BUFFER, vertid);
     glBufferData(GL_ARRAY_BUFFER, vertices, GL_DYNAMIC_DRAW);

     glEnableVertexAttribArray(0);
     glVertexAttribPointer(0, 2, GL_FLOAT, false, 2 * 4, 0);

     // Draw using glDrawArrays or glDrawElements

     glDisableVertexAttribArray(0);
}

Then you should be multiplying in your vertex shader


attribute vec2 position;

uniform mat4 transformation;

void main()
{
     gl_Position = transformation * vec4(position, 1.0, 1.0);
}

EDIT:
I should note that the transformation matrix should start as an identity with m00, m11, m22, and m33 having a value of 1.

Thanks for your help I will try that in a little while however, I am wandering if there is a way to still do it inside the class though. I saw at one point using glmatrixpush() worked but I didnt understand how you would get it to function.

Also in your post how would I bind the transformation object you passed into the shader?

I’d advise you to learning OpenGL properly. Here’s a link to a great tutorial on modern OpenGL 3.3 along with the ported Java+LWJGL code accompanying each example.

Thanks that has really helped.
So looking at it I would think that firstly when im using the shader I bind the object , I activate the shader variable then I point to it to assign the value , deactivate the shader variable and then render.

No. You want the shader active when you render or else it won’t be used!

Oh no not like that I mean activate the linking the attribute then disable it then render then cancel the shader.

So you’re saying [icode]glDisableVertexAttribArray(0);[/icode] before rendering?

yes because once you have bound the value I thought you could stop that and leave the shader active.

You have to leave those enabled or else nothing will get passed to the shader.

oh ok thank you

so basically ive setup a vertex shader


uniform vec2 transform;

void main()
{
	gl_Position =gl_Vertex+vec4(transform,0,0);
}

I then bind transform per VBO which is done through
int transloc = GL20.glGetUniformLocation(shaderprogram, transform);
GL20.glUniform2f(transloc,transformationx,transformationy);

going to have to test it tommorow though. thanks for the help

Watch out, you’re learning 2.0/2.1 OpenGL. gl_Vertex is deprecated in the core profile (GL 3.0+) and removed in 3.1+

so im required to bind it?

If you are using 2.0/2.1 or compatibility profile, then you can just keep using gl_Vertex and bind the data using glVertexPointer. If you are using core, then you create your own attributes and bind the data using glVertexAttribPointer.

If all of this is flying over your head, please do read the tutorials I linked :slight_smile:

i have been and there very good tutorials. I sort of understand, however I am using the lower version shader loading and binding.

I am wandering what the new form of modelviewprojectionmatrix is , when I try to multiply by it in my shader I get no result. I know that the code would work with it as I tested it in the shader IDE.

Modern OpenGL doesn’t take care of the matrices for you. You have to manage your matrices on your own and send them to the shaders and multiply them there.

ok so which matrix would I send to the shader?
EDIT: ive fixed it I passed in a vertical scalar which is the height divided by width, then the vertex.y is divided by it inside the shader.