0
votes

I am leaning GLSL and in general some OpenGL and I am having some trouble with vertex movement and management. I am good with camera rotations and translation but now I need to move a few vertices and have them stay in their new positions.

What I would like to do is move them using the vertex shader but also not keep track of their new positions trough matrices (as I need to move them around independently and it would be very pricey in terms of memory and computing power to store that many matrices).

If there were a way to change their position values in the VBO directly from the vertex shader, that would be optimal.

Is there a way to do that? What other ways do you suggest? Thanks in advance.

PS I am using GLSL version 1.30

3

3 Answers

2
votes

While it's possible to write values from a shader into a buffer and later read it from the CPU-client side (i.e., by using glReadPixels()) I don't think it is your case.

You can move a group of vertices, all with the same movement, with a single matrix. Why don't you do it with the CPU and store the results, updating their gl-buffer when needed? (VAO remains unchanged if you just update the glBuffer) Once they are moved, you don't need that matrix anymore, right? Or if you want to undo the movement, then, yes, yo need to store also the matrix.

0
votes

It seems that transform feedback is exactly what you need.

0
votes

What I would like to do is move them using the vertex shader but also not keep track of their new positions trough matrices

If I understand you correctly then what you want is to send some vertices to the GPU. Then having the vertex shader moving them. You can't because a vertex shader is only able to read from the vertex buffer, it isn't able to write back to it.

it would be very pricey in terms of memory and computing power to store that many matrices.

Considering:

I am good with camera rotations and translation

Then in wouldn't be expensive at all. Considering that you already have a view and projection matrix for the camera and viewport. Then having a model matrix contain the translation, rotation and scaling of each object isn't anywhere near a bottleneck.

In the vertex shader you'd simply have:

uniform mat4 mvp; // model view projection matrix
...
gl_Position = mvp * vec4(position, 1.0);

On the CPU side of things you'd do:

mvp = projection * view * model;
GLint mvpLocation​ = glGetUniformLocation(shaderGeometryPass, "mvp")
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, (const GLfloat*)&mvp);

If this gives you performance issues then the problem lies elsewhere.

If you really want to "save" which ever changes you make on the GPU side of things, then you'd have to look into Shader Storage Buffer Object and/or Transform Feedback