and this creates the texture:
There are many things wrong there.
glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB);
This should have given you an OpenGL error. UNPACK_ALIGNMENT takes integer values, not enumerators. And GL_RGB is certainly not 1, 2, 4, or 8, which are the only allowed values of the UNPACK_ALIGNMENT.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
As previously mentioned, if you want to get exact values from your texture, you don’t want LINEAR filtering. Just NEAREST.
I suspect however that your real trouble is this:
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData);
I’m guessing your copy-and-pasted this code from some website. So that’s what you get for regurgitating code without knowing where it came from or what it does. At all times be aware of what functions are and what they do. If you don’t know, look up the information online; don’t just copy some code that works and poke at it, hoping that it’ll work for you.
In any case, there are two problems with using this function. The first is conceptual: if you want texel-accurate results (no filtering), why are you building mipmaps? That kinda defeats the whole point of texel-accurate results; you’re wasting precious texture space on mipmaps that you don’t need and will never use.
But that’s minor compared to the real problem. If “Width” and “Height” are not powers of two (and I’m guessing that they aren’t, in your case), then gluBuild2DMipmaps
will scale your image up to the nearest power of 2. This scaling will necessarily blur the various pixels in the image.
The silly part of course, is that most GL implementations will handle arbitrarily sized textures just fine. But because you’re using gluBuild2DMipmaps
to load your texture, you can’t use that feature. It will automatically tell OpenGL that the texture is bigger than it really is, and it will scale up your data accordingly.
So, the lesson here (besides not using code you don’t understand) is: avoid gluBuild2DMipmaps
unless you have no other choice. If you need mipmaps, then either generate them offline (perhaps in a DDS file) and load them with glTex(Sub)Image2D
, or use glTex(Sub)Image2D
to load the base layer and use glGenerateMipmap
(which requires GL 3.0 or above, or ARB_framebuffer_object) to generate the rest.