Jump to content

  • Log In with Google      Sign In   
  • Create Account

b1narez

Member Since 05 Nov 2013
Offline Last Active Jan 25 2014 11:45 AM

Topics I've Started

Should I vertically flip the lines of an image loaded with stb_image to use in OpenGL?

05 November 2013 - 07:05 AM

Hi everybody,

 

I'm working on an OpenGL-powered 2d engine.

 

I'm using stb_image to load image data so I can create OpenGL textures. I know that the UV origin for OpenGL is bottom-left and I also intend to work in that space for my screen-space 2d vertices i.e. I'm using glm::ortho( 0, width, 0, height, -1, 1 ), not inverting 0 and height.

 

You probably guessed it, my texturing is vertically flipped but I'm 100% sure that my UV are specified correctly.

So: is this caused by stbi_load's storage of pixel data? I'm currently loading PNG files only so I don't know if it would cause this problem if I was using another file format. Would it? (I can't test right now, I'm not at home).

 

I really want to keep the screen coords in the "standard" OpenGL space... I know I could just invert the orthogonal projection to fix it but I would really rather not.

 

I can see two sane options:

 

1- If this is caused by stbi_load storage of pixel data, I could invert it at loading time. I'm a little worried about that for performance reason and because I'm using texture arrays (glTexture3d) for sprite animations meaning I would need to invert texture tiles individually which seems painful and not a general solution.

 

2- I could use a texture coordinate transformation to vertically flip the UVs on the GPU (in my GLSL shaders).

 

A possible 3rd option would be to use glPixelStore to specify the input data... but I can't find a way to tell it that the incoming pixels are vertically flipped.

 

What are your recommendations for handling my problem? I figured I can't be the only one using stbi_load + OpenGL and having that problem.

 

Just to be extra clear: I know that (0,0) is the bottom-left of the texture map. My problem is precisely that when I specify (0,0) I get the top-left corner texel... not what I expect on OpenGL That's why I'm asking if stbi_load pixel storage and OpenGL pixel storage differ and what I can do about it if that's the case.

 

Finally, my target platforms are PC, Android and iOS smile.png Thanks!

 


PARTNERS