onurmhb onurmhb - 7 days ago 5
C++ Question

OpenGL - Adding new data to texture

I am trying to create a spectogram efficently. Currently I am doing everything on CPU using a texture buffer by looping through the whole texture buffer and pushing the new data to the "queue". However, this costs me alot of CPU time. I want to add new column of pixel data to the texture, move old data to right, so the new data appear on left side while the old data moves to right. This will create a waterfall/sidescrolling effect if I do it each frame.

I am using

glTexSubImage2D()
to add new data, but this will not advance old data to right. How can I achieve this by using OpenGL?

Answer

I don't see a need to move any data around. Simply treat the texture as circular in the horizontal direction, basically a circular buffer of columns. Then take scare of the scrolling during rendering by choosing the texture coordinates accordingly.

Say you want to display n columns at a time. Create a texture of width n, and in each step k store the data in column k % n of the texture:

glTexSubImage(GL_TEXTURE_2D, 0, k % n, 0, 1, height, ...);

Then use texture coordinates in the range 1 + (k % n) / n to (k % n) / n in the horizontal direction, with the texture wrap mode set to GL_REPEAT. Or pass an offset to your shader, and add it to the texture coordinates in the GLSL code.

And of course, if you have all data ahead of time, and it's not very large, you can simply store all of it in a texture right from the start, and scroll through it by shifting the texture coordinates.

Comments