Jump to content
  • Advertisement
Sign in to follow this  
Trillian

OpenGL Uploading texture data with stride

This topic is 3491 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! Up to recently, I thought that OpenGL didn't provide any means of specifying the stride (distance in bytes between the first pixel of two adjacent rows) of texture data because there was no such parameter in glTexImage2D. However, I recently found a thread talking about glPixelStore, which could maybe allow this. I've looked at the documentation but because of crappy inlined ASCII art for algebraic formulas, I couldn't figure out if it was what I needed. Does glPixelStore allow this? If so, could someone post a sample chunk of code calling that function? Also, should I care about GL_UNPACK_ALIGNMENT if I'm not doing anything fancy? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
To set the stride, call
glPixelStorei(GL_UNPACK_ROW_LENGTH,stride);
before calling glTexImage2D (or similar functions), stride is the length of a row in bytes although the default value of 0 means the data is tightly packed. When done, it's a good idea to set the value back to zero to avoid any problems.

Regarding GL_UNPACK_ALIGNMENT, read the documentation here (no ASCII art). I don't think you need to touch the value.

An example of calling the function:

void update_texture(GLuint tex, unsigned char* buf,int w,int h, int stride) {
glPixelStorei(GL_UNPACK_ROW_LENGTH,stride);
glBindTexture(GL_TEXTURE_2D,tex);
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,w,h,GL_LUMINANCE,GL_UNSIGNED_BYTE,buf);
// set GL_UNPACK_ROW_LENGTH back to 0 to avoid bugs
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
}




(To see the entire source code download liboggplayer (my project) and open examples/ogl_glsl.cpp)

Share this post


Link to post
Share on other sites
Cool, thanks Kambiz!

I was pretty sure that GL_UNPACK_ROW_LENGTH was what I needed to look at because of its name but I found the documentation somewhat misleading:

Quote:
If greater than 0, GL_UNPACK_ROW_LENGTH defines the number of pixels in a row.


Anyways, I'm trying it now!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!