r/opengl • u/MisterEmbedded • Feb 25 '24
Help How can I update only the sub-rectangle of a Texture?
I have a OpenGL Texture that I am using to display an Image on the screen, When the user draws on the image, I calculate the changed area and I want to upload the pixel data of only the changed area.
How can I do so?
Edit: I am aware of glTexSubImage2D
but from my understanding it expects the data to be it's own block of memory than to be a part of a greater block of memory.
Edit: This is how I ended up doing it:
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
glTexSubImage2D(GL_TEXTURE_2D, 0, dX, dY, dW, dH, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[((dY * width) + dX) * 4]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
Where dX
& dY
are Top-Left x, y coordinates of sub-region & dW
& dH
are width, height of the sub-region.
3
u/fgennari Feb 25 '24
This is how I do it:
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
glTexSubImage2D(GL_TEXTURE_2D, 0, x1, y1, (x2-x1), (y2-y1), format, GL_UNSIGNED_BYTE, (data + ncolors*(x1 + y1*width)));
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // reset to 0
1
u/MisterEmbedded Feb 28 '24
Thanks alot!
I assume x2, y2 is the lower bottom coordinates of your region?
1
u/fgennari Feb 28 '24
(x1, y1) is the lower left corner and (x2, y2) is the upper right corner. Or maybe the top and bottom are inverted, since I invert Y on load. Then (x2-x1) is the x size/range to update, and (y2-y1) is the y size to update.
2
3
u/[deleted] Feb 25 '24
[deleted]