Search code examples
opengltransformationvertex-buffer

OpenGL - How to transform a subset of a vertex in a vertex buffer?


float linePos[6]={0.0f,5.0f,0.0f,0.0f,30.0f,0.0f};    
...

glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2, linePos, GL_STATIC_DRAW);

...
glUniformMatrix4fv(UniformColor.modelToWorldMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
//this just transforms all vertices

glLineWidth(3);
glDrawArrays(GL_LINES,0,2);

I want to make some animation changing the position (ex.translate) of one end the line(linePos[0]) but not the other. How can I do that?


Solution

  • You have basically 2 options: Update vertex position in the linePos array and re-upload into the vertex buffer object.You can use glBufferSubData to update the data.Second ,and by my opinion is better (faster) approach to transform your line in vertex shader.Anyways , if you need to modify individual vertex position the first approach is what you need.Here is a popular tutorial on how it is done using programmable pipeline.

    Btw , I want to extend on shader based solution.I suggested it because your line has only 2 points:start and end.So changing position of any of them is like transforming the whole line by a matrix.That is why I think it is better to do it inside a vertex shader.If you had a line defined by at least 3 vectors , then to modify position of a specific vertex would requre modifying vertex array on CPU .Well,you can do it in geometry shader but this is advanced stuff.