Opengl cube map - different results for freeimage and stb image libs - opengl

I'm trying to understand cube maps. I have read the following tutorial: https://learnopengl.com/Advanced-OpenGL/Cubemaps My example program is very similar to the tutorial program but I use freeimage for loading textures. My cube map has inverted y view for each face. When I change texture library from stb image to freeimage in the tutorial program I also get inverted y view for each face. Does freeimage use some non-standard convention ? Why I get different results ?

I don't know if there is a standard for this, but I wouldn't say that FreeImage is mirroring the image vertically, it's just the way pixel data is stored.
If you want to read the data with an OpenGL generated texture, you may want to flip the images by code using FreeImage_FlipVertical before FreeImage_GetBits. I guess you are using FreeImage_GetBits to deliver the data to the texture generated in OpenGL, if you are reading the image with scan lines, you can load the data from the bottom up.
Heads-up: If you use multiple formats of images It is likely that you also have other problems regarding the way data is stored, so just to let you know, when you use GetBits and GetScanLine, in the official FreeImage documentation says "It is up to you to interpret these bytes correctly, according to the results of FreeImage_GetBPP, FreeImage_GetRedMask, FreeImage_GetGreenMask and FreeImage_GetBlueMask" so if you have other problems loading the image to OpenGL you may want to check the examples here for interpreting the data:
http://freeimage.sourceforge.net/documentation.html

Related

How to check if image should be flipped vertically before loading using stb_image

I'm writing a project using OpenGL, and loading the textures using stb_image.
Some of the textures are loaded flipped upside down (regarding the y-axis) so I use
"stbi flip image vertically on load" to load them properly.
The problem is that some of the textures I load require flipping, and some not,
But of course my code flipps them all.
how can I check (before loading, or at least before flipping) whether or not to flip the
image?
Short answer: always flip when loading an image from stb_image to an OpenGL texture. Longer answer: you can't know whether a user wants to flip the image themselves. As it was posed, I think your question is answered by the question Kai Burjack linked you to (Should I vertically flip the lines of an image loaded with stb_image to use in OpenGL?) because it clarifies the correct use of this feature of stb_image.
If you are going straight from an image file to an OpenGL texture, then you should always flip during import if you want the "up" of the imported texture to match what users see in their art programs. However, if you want to give users the option to load images upside down independent of how the image looks in the art program, you can totally do that, too. The catch is that the user has to tell you. There's no way to know what the user wants, and IMO artists who want their images upside-down are likely to just make them that way in their art programs anyways.

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

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.
Finally, my target platforms are PC, Android and iOS :)
EDIT: I answered my own question... see below.
I know this question's pretty old, but it's one of the first results on google when trying to solve this problem, so I thought I'd offer an updated solution.
Sometime after this question was originally asked stb_image.h added a function called "stbi_set_flip_vertically_on_load", simply passing true to this function will cause it to output images the way OpenGL expects - thus removing the need for manual flipping/texture-coordinate flipping.
Also, for those who don't know where to get the latest version, for whatever reason, you can find it at github being actively worked on:
https://github.com/nothings/stb
It's also worth noting that in stb_image's current implementation they flip the image pixel-by-pixel, which isn't exactly performant. This may change at a later date as they've already flagged it for optimsation. Edit: It appears that they've swapped to memcpy, which should be a good bit faster.
Ok, I will answer my own question... I went thru the documentation for both libs (stb_image and OpenGL).
Here are the appropriate bits with reference:
glTexImage2D says the following about the data pointer parameter: "The first element corresponds to the lower left corner of the texture image. Subsequent elements progress left-to-right through the remaining texels in the lowest row of the texture image, and then in successively higher rows of the texture image. The final element corresponds to the upper right corner of the texture image." From http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
The stb_image lib says this about the loaded image pixel: "The return value from an image loader is an 'unsigned char *' which points to the pixel data. The pixel data consists of *y scanlines of *x pixels, with each pixel consisting of N interleaved 8-bit components; the first pixel pointed to is top-left-most in the image." From http://nothings.org/stb_image.c‎
So, the issue is related the pixel storage difference between the image loading lib and OpenGL. It wouldn't matter if I loaded other file formats than PNG because stb_image returns the same data pointer for all formats it loads.
So I decided I'll just swap in place the pixel data returned by stb_image in my OglTextureFactory. This way, I keep my approach platform-independent. If load time becomes an issue down the road, I'll remove the flipping at load time and do something on the GPU instead.
Hope this helps someone else in the future.
Yes, you should. This can be easily accomplished by simply calling this STBI function before loading the image:
stbi_set_flip_vertically_on_load(true);
Since this is a matter of opposite assumptions between image libraries in general and OpenGL, Id say the best way is to manipulate the vertical UV-coord. This takes minimal effort and is always relevant when loading images using any image library and passing it to OpenGL.
Either feed tex-coords with 1.0f-uv.y in vertex-population OR reverse in shader.
fcol = texture2D( tex, vec2(uv.x,1.-uv.y) );

Reading Depth map using OpenGL

I have extracted the depth map of 2 images and stored them as .tif file
now I would like to use openGL to join these two images depending on their depth
so I want to read the depth for each image from the .tif file and then use that depth to draw the pixel with the higher depth
to make it more clear the depth map are two images like this
link
so say I have the pervious image and I want to join it with this image
link
my question is how to read this depth from the .tif file
Ok, I'll have a go ;-)
I see the images are just grayscale, so if the "depth" information is just the intensity of the pixel, "joining" them may be just a matter of adding the pixels. This is generally referred to as "blending", but I don't know what else you could mean.
So, you need to;
Read the 2 images into memory
For each pixel (assuming both images the same size):
read the intensity from image A[row,col]
read the intensity from image B[row,col]
write max(A[row,col],B[row,col]) to C[row,col]
Save image C - this is your new "joined" image.
Now OpenGL doesn't have any built-in support for loading/saving images, so you'll need to find a 3rd party library, like FreeImage or similar.
So, that's a lot of work. I wonder if you really want an OpenGL solution or are just assuming OpenGL would be good for graphics work. If the algorithm above is really what you want, you could do it in something like C# in a matter of minutes. It has built-in support for loading (some formats) of image file, and accessing pixels using the Bitmap class. And since your created this images yourself, you may not be bound the the TIFF format.

reading indexed palette image in C++

My platform is Windows. I didn't expect reading indexed palette image to be this difficult in C++. In case you are not familiar with it, it's single channel image but expresses its pixel color with 256 indexed colors called palette.
I was using OpenCV but its imread just converts the file to a 3 channel image so I have no way to save it back to indexed palette image or compare it with another indexed palette image.
I tried to use Bitmap but for some reason, it does not read correct pixel values.
So right now, I am looking for a light library or code to read pixels from indexed palette file.
Using OpenCV to read or write a image from real cameras will lose and change the image information, so I prefer to use gdi+, which is more powerful in dealing with image format problems to solve your problem.
As comments on the question shows, I decided to have two methods, OpenCV for non-indexed-palette images and Bitmap (GDI+) for indexed palette images. Now everything is working perfect.

Texture Mapping C++ OpenGL

I have read around on this, including Nehe and here for solutions, but I cant find a specific answer.
I am trying to load a a photo, called stars.jpg. I want to make this my background of the scene, by mapping it using uv coordinates, doing it by
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
However I am just very confused about how to load the actual textures in, all the calls for
glActiveTexture();
glEnable(GL_TEXTURE_2d);
glBindTexture(GL_TEXTURE);
All they do is confuse me, what do all these mean/do, and in what order am I suppose to put these in, in order to get the stars.jpg to be my background?
Your number-one tool for loading textures in OpenGL is the Simple OpenGL Image Loader (SOIL) library. You just need to pass the filename and some flags and you'll get your texture ID.
Also, you're learning very old and outdated version of OpenGL now - you might want to have a google for newer tutorials or browse the specs whenever you feel ready.
Here's a step by step tutorial on loading textures
http://www.nullterminator.net/gltexture.html
Its important to remember that OpenGL is a state machine, so you have to tell it "I'm going to talk about textures now" that's where the glActiveTexture(); comes in.
Also keep in mind that you will have to load in pixel by pixel the colors from your .jpg (compressed) to your texture array, so either you will need to find a library that will give you bitmap values of your .jpg file or you will need to pre-convert it into a .ppm or a .bmp which will make reading in the values easier.