Jump to content
  • Advertisement
Sign in to follow this  
Lode

OpenGL glTexSubImage2D

This topic is 3326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have pixels in an unsigned char buffer. Initially, I can bring it to the video RAM with
glTexImage2D(GL_TEXTURE_2D, 0, 4, u2, v2, 0, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
If a part of the pixels in the unsigned char buffer change, then I'd like, for faster pixel transfer, not upload the whole thing to video RAM anymore, but use something that only sends the rectangular area in which pixels changed. I found that glTexSubImage2D can do that, except one thing: The last parameter of it is const GLvoid *pixels, but those pixels need all to be contiguous in memory. However, my pixels of a small rectangular area aren't in memory like that, they're in the original buffer which is a larger rectangle. So each row of pixels is contiguous, but between that is always a gap (the rest of the line of pixels, which isn't part of the smaller rectangle). Is there a similar function in OpenGL that takes a small rectangle both of your pixel buffer and the video RAM buffer instead of only in the video RAM buffer? I didn't find any. Alternatively, would it be efficient, or not, to call glTexSubImage2D multiple times, namely once for each row of pixels?

Share this post


Link to post
Share on other sites
Advertisement
I'm not certain, as I haven't had to use it before, but looking at the documentation, I think glPixelStore and GL_UNPACK_ROW_LENGTH will give you that functionality.

http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/pixelstore.html

Share this post


Link to post
Share on other sites
Look up the pixel unpack parameters. More specifically, glPixelStore and the parameters GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_PIXELS and GL_UNPACK_SKIP_ROWS. Set these parameters to the width of the image, the X and the Y-offset, respectively, and pass the pointer to the image.

Share this post


Link to post
Share on other sites
Thanks! That behaves exactly as I wished!

More precisely:

  //indicate how we'll read from buffer
glPixelStorei( GL_UNPACK_ROW_LENGTH, u2);
glPixelStorei( GL_UNPACK_SKIP_PIXELS, x0);
glPixelStorei( GL_UNPACK_SKIP_ROWS, y0);

glTexSubImage2D(GL_TEXTURE_2D, 0, x0, y0, x1 - x0, y1 - y0, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0] );

//set back to default so that the rest behaves normally
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei( GL_UNPACK_SKIP_ROWS, 0);

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Look up the pixel unpack parameters. More specifically, glPixelStore and the parameters GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_PIXELS and GL_UNPACK_SKIP_ROWS. Set these parameters to the width of the image, the X and the Y-offset, respectively, and pass the pointer to the image.


Hey man, a bit of a later follow up question.

This stuff kind of rules, I didn't know about this yet until now and now that I'm using it, it also allows me to do other things. Namely, I'm now having a buffer that is SMALLER than the OpenGL texture (OpenGL texture is power of two size, my buffer not, e.g. 50x50 while the GL texture is 64x64). I used GL_UNPACK_ROW_LENGTH, set it to the smaller size (50), and this also appears to work!

This is the inverse of the post above because there GL_UNPACK_ROW_LENGTH is used to set it to larger instead of smaller size.

Is it safe to use GL_UNPACK_ROW_LENGTH for smaller size too or did it just work "by accident" on my PC? (I'm starting to think so after some more experimentation...)

And another question, is there an equivalent of this in Direct3D, if so how?

[Edited by - Lode on November 1, 2009 5:30:17 PM]

Share this post


Link to post
Share on other sites
The definition of the row length unpack parameter is the number of pixels to advance the read pointer for each row. Thus, if you set the row length to 50 and loads the pixel rectangle as a 64 pixels wide texture, the first 50 pixels of each row will be as desired. The remaining 14 pixels, which is effectively padding to make the texture a power of two, will be whatever is next in memory (the first 14 pixels of the following row, most likely). This should work fine according to the specification.

But consider this; what will the padding be for the last row? What follows the 50:th pixel of the last row of your buffer?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!