Transforming model vertices after loading to vao [solved]

Greetings earthlings of JGO.
I’ve been i little stuck on this problem for a while now.
I want to know if there is there a way to transform the individual vertices of the model without needing to change the vertices array and then reload it to the vao. Also whenever i do a transformation it applies it to the whole model.
I am also aware that there might be a way to do this in the vertex shader.

vertices and indices:


// defines a triangle
private static float[] vertices = {
		-0.5f, 0.5f, 0, 
		-0.5f, -0.5f, 0, 
		0.5f,-0.5f, 0, 
	};
	private static int[] indices = {
		0,1,2
	};

and i load them to a vbo and vao, after which i load to shader, render etc.


private List<Integer> vaos = new ArrayList<Integer>();
	private List<Integer> vbos = new ArrayList<Integer>();

	public Shape loadToVao(float[] positions, int[] indices) {
		int vaoId = glGenVertexArrays();
		vaos.add(vaoId);
		glBindVertexArray(vaoId);

		bindIndicesBuffer(indices);
		dataInAttribList(0, positions);

		glBindVertexArray(0);
		return new Shape(vaoId, indices.length);
	}

	private void dataInAttribList(int attribNum, float[] d) {
		int vboId = glGenBuffers();
		vbos.add(vboId);
		glBindBuffer(GL_ARRAY_BUFFER, vboId);
		FloatBuffer b = BufferUtils.createFloatBuffer(d.length);
		b.put(d);
		b.flip();
		//GL_DYNAMIC_DRAW the vertices will change
		glBufferData(GL_ARRAY_BUFFER, b, GL_DYNAMIC_DRAW);
		glVertexAttribPointer(attribNum, 3, GL_FLOAT, false, 0, 0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	private void bindIndicesBuffer(int[] indices) {
		int vboID = glGenBuffers();
		vbos.add(vboID);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
		IntBuffer b = BufferUtils.createIntBuffer(indices.length);
		b.put(indices);
		b.flip();
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, b, GL_STATIC_DRAW);
 	}

also i want to note that i have seen this thread: http://www.java-gaming.org/topics/applying-transformations-to-vertices/34070/view.html

Whenever you want to change the actual vertices, you need to re-upload the data to reflect the changes. See my last point for the solution to this problem.

This is the point of a transformation. If you want part of your model to move differently, then you either need to use a different transformation matrix or change the vertex data.

This is the solution you are looking for. Essentially, you can send all the transformation data the the vertex shader in a little [icode]mat4[/icode]. Then, each vertex is multiplied by this matrix to result in the final transformation. This way, you can upload the vertices once at model creation, then continuously update the transformation matrix to change the transformation of the entire model.

[quote]his is the solution you are looking for. Essentially, you can send all the transformation data the the vertex shader in a little mat4. Then, each vertex is multiplied by this matrix to result in the final transformation. This way, you can upload the vertices once at model creation, then continuously update the transformation matrix to change the transformation of the entire model.
[/quote]
i understand this part but how can i make the transformation only apply to one vertex not all of them, my current shader code looks like this:


#version 330 core

in vec3 position;

uniform mat4 projMat;
uniform mat4 transMat;
uniform mat4 viewMat;

void main() {
	gl_Position = projMat * viewMat * transMat * vec4(position,1.0);
}

Sorry, I did not understand that you only wanted to transform one vertex. In that case, the easiest solution is to probably just change the data for that one vertex, and re-upload it (you can use glBufferSubData()) to OpenGL.

You could pass in a custom flag with your vertex data then upload 2 transformation matrix to the shader and then use the flag to switch between which one you use (a very basic form of skinning).

SirSoltex, what you really want is uniform buffer objects. The reason being that you will be limited to roughly 256 4x4 matrices and then you can’t pass in any more uniforms. There is a limit to something like 4096 uniforms (this is per float, not matrix), but I’m not sure if this is a gpu limit or a glsl limit. I highly recommend going the UBO route now before you have to go back and rewrite.

Also, to access the correct matrix in the UBO you will want to pass a float argument in as an attribute on a per vertex level. Then you’ll know which matrix to use. You could go with an integer, but then you’ll need to bind another vbo and it might be more hassle than it’s worth.

Longarmx was already right, glBufferSubData was everything necessary to answer the question.

thanks a ton. I was originally having some trouble with rendering but turns out i was using the vao id not the vbo id to bind the arraybuffer. but it works great now.
the code:


private FloatBuffer b = BufferUtils.createFloatBuffer(vertices.length);
	public void changePos(Vector3f p0,Vector3f p1,Vector3f p2) {
		vertices[0] = p0.x;
		vertices[1] = p0.y;
		vertices[2] = p0.z;
		
		vertices[3] = p1.x;
		vertices[4] = p1.y;
		vertices[5] = p1.z;
		
		vertices[6] = p2.x;
		vertices[7] = p2.y;
		vertices[8] = p2.z;
		
		glBindBuffer(GL_ARRAY_BUFFER,s.vboId);
		b.rewind();
		b.put(vertices);
		b.flip();
		glBufferSubData(GL_ARRAY_BUFFER, 0, b);
		
		glBindBuffer(GL_ARRAY_BUFFER,0);
	}