| SDL - upper-down image - very important thing in OpenGL |
|
Jesse A.
|
Are you using SDL 1.3?
If so, are you using SDL's built-in hardware-accelerated rendering, or are you just creating an OpenGL context using SDL and then doing the rendering yourself? Also, how are you loading the texture images? |
|||||||||||
|
|
||||||||||||
|
badday
|
Yes, I use SDL 1.3 - here is my code - this is function to load texture from path
|
|||||||||||||
|
|
||||||||||||||
|
Jesse A.
|
Unless I'm missing something, the problem you're seeing doesn't really have anything to do with SDL.
As far as images go, SDL doesn't really put the origin in the upper left, nor does OpenGL put the origin in the lower left. Most image formats store the image data in row-major order (that is, row 0, then row 1, then row 2, etc.). Furthermore, most image editing programs (IMX, at least) interpret images as being stored 'top-down'; that is, the 0'th row is the top row, and on down from there. So if you create a texture in an image editing program that has a clear 'up' and 'down', it's likely that the first row will be the top row rather than the bottom row. OpenGL itself doesn't have any concept of how an image is oriented; how the image is oriented is determined by the texture coordinates, which in turn are determined by the data that you send to the API. So in other words, you determine how the image is oriented in OpenGL; OpenGL just does what you tell it to do. That said, when specifying texture coordinates for, say, a quad in OpenGL, people do tend to place v = 0 at the bottom rather than the top. If the image hasn't been adjusted in any way, this will result in the image appearing to be 'upside-down' relative to what you expect. There's at least a couple of solutions to the problem. One solution is to adjust the v components of all your texture coordinates appropriately (e.g. v = 1 - v). Another is to flip your image vertically before uploading it to OpenGL. Unless I'm mistaken, IMG_Load() just loads the image as-is; it doesn't impose any sort of coordinate system or orientation on it, so in that sense the issue has nothing to do with SDL or SDL_image. Rather, it's just an issue of convention, which can be addressed (as mentioned above) by flipping the image vertically or modifying the texture coordinates appropriately. |
|||||||||||
|
|
||||||||||||
|
badday
|
For example I have square - here is link to nehe opengl tutorial with textures: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06 Citation from this site:
"To properly map a texture onto a quad, you have to make sure the top right of the texture is mapped to the top right of the quad. The top left of the texture is mapped to the top left of the quad, the bottom right of the texture is mapped to the bottom right of the quad, and finally, the bottom left of the texture is mapped to the bottom left of the quad. If the corners of the texture do not match the same corners of the quad, the image may appear upside down, sideways, or not at all. The first value of glTexCoord2f is the X coordinate. 0.0f is the left side of the texture. 0.5f is the middle of the texture, and 1.0f is the right side of the texture. The second value of glTexCoord2f is the Y coordinate. 0.0f is the bottom of the texture. 0.5f is the middle of the texture, and 1.0f is the top of the texture. So now we know the top left coordinate of a texture is 0.0f on X and 1.0f on Y, and the top left vertex of a quad is -1.0f on X, and 1.0f on Y. Now all you have to do is match the other three texture coordinates up with the remaining three corners of the quad. Try playing around with the x and y values of glTexCoord2f. Changing 1.0f to 0.5f will only draw the left half of a texture from 0.0f (left) to 0.5f (middle of the texture). Changing 0.0f to 0.5f will only draw the right half of a texture from 0.5f (middle) to 1.0f (right). " So when I copy code from this tutorial:
I have upper-down image because for example: glTexCoord2f(0.0f, 0.0f); in opengl it is left down corner image but in SDL it is left upper image. |
|||||||||||||
|
|
||||||||||||||
|
Jesse A.
|
You might give my previous post another read. Again, OpenGL doesn't specify how textures are oriented; rather, you determine how the texture is oriented by specifying appropriate texture coordinates. I can't really comment directly on the NeHe tutorial (among other things, it uses a bitmap-loading function that I'm not familiar with and about which there doesn't seem to be much info online). I can tell you though that it's fairly common to flip images vertically on load in order to compensate for the effect you're describing, and that it's also fairly common to 'invert' the v components of texture coordinates for the same reason. Again, it's not because SDL does it one way and OpenGL does it another way. SDL_image is just loading the image as it's stored in the file, and OpenGL is just rendering what you tell it to render. Unless I'm mistaken, the issue has nothing to do with SDL; if you were to use another image loader, you'd likely run into the same problem. (It's worth noting that some image loaders offer a 'vertical flip' option to compensate for this effect.) One solution to the problem would be to flip the image after receiving it from SDL_image, and before uploading it to OpenGL. |
|||||||||||||||
|
|
||||||||||||||||
|
badday
|
"OpenGL doesn't specify how textures are oriented; rather, you determine how the texture is oriented by specifying appropriate texture coordinates. " - other example - in opengl I can draw teapot or sphere without writing vertex coordinates using functions glutSolidTeapot() and glutSolidSphere() - in that case I CAN'T set textures coordinates so textures are always upside-down. This isn't only my problem:
http://www.gamedev.net/topic/576552-sdl-inverted-image/ http://boardreader.com/thread/SDL_Image_OpenGL_Inverted_f4pkX3zsj.html http://www.gamedev.net/topic/188170-opengl-texture-mapping-question-sdl/ http://bhsphd.spaces.live.com/blog/cns!1844003DFD14BA7D!312.entry This is SDL problem - why ? Because I can write my own function to load textures:
and this problem with upper-down picture doesn't exist. Isn't it really possible in some function in SDL change settings these coordinates ? |
|||||||||||||
|
|
||||||||||||||
|
badday
|
I HAVE FOUND SOLUTION HERE: http://www.gribblegames.com/articles/game_programming/sdlgl/invert_sdl_surfaces.html :D
But somebody should add this solution to SDL library - it is very important for OpenGL. So the solution is to invert image:
|
|||||||||||||
|
|
||||||||||||||
|
Jesse A.
|
I'm glad you solved your problem :)
However, it seems you didn't read any of my posts. The threads you linked to basically said (in one form or another) exactly what I've said here, and in fact the exact same solutions were proposed that I've proposed here. Note:
So you basically ended up doing exactly what I suggested :) Still, it's important to understand that this isn't really an SDL issue; you could easily run into the same problem using another image loader. Furthermore, the problem isn't that SDL puts the origin in the 'upper left' or that OpenGL puts it in the 'lower left', but rather that the conventions most typically used for image layout and texture coordinate layout happen to differ, meaning that in general, one or the other will need to be adjusted. This issue is independent of any particular image loading library, more or less.
Well, more features are always nice I don't know if SDL_image is undergoing active development right now, but you could always submit a patch or a feature request. But, since flipping an image is a fairly straightforward process, it's probably just as easy to use a custom function for this purpose (just as you're doing currently). |
|||||||||||||||||
|
|
||||||||||||||||||
|
badday
|
thx for answer Jesse
|
|||||||||||
|
|
||||||||||||

