0

I have textured a sphere with a .bmp format image. The problem is, when the image is mapped on the sphere the colour of the image looks inverted. Like the RED becomes BLUE and BLUE becomes RED. I have tried using GL_BGR instead of GL_RGB but its no use. Do I have to change the code for loading the image. Because It produces warning for the use of fopen() function and also I don't think its relevant of what I am asking. The image what I am getting after mapping istexured sphere with inverted colors

This is what I have tried for loading the image and applied some texture rendering stuff.

   GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;

//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );

glGenTextures( 1, &texture ); //generate the texturewith the loaded data
glBindTexture( GL_TEXTURE_2D, texture ); //bind thetexture to it’s array
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE ); //set        texture environment parameters




// better quality
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR );

//Here we are setting the parameter to repeat the textureinstead of clamping the texture
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );   
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );

//Generate the texture with mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data ); //free the texture
return texture; //return whether it was successfull
}

   void FreeTexture( GLuint texture )
   {
    glDeleteTextures( 1, &texture );
   }
saurabh
  • 17
  • 9
  • Possible duplicate of [How to load a bmp on GLUT to use it as a texture?](http://stackoverflow.com/questions/12518111/how-to-load-a-bmp-on-glut-to-use-it-as-a-texture) – Teivaz Apr 29 '16 at 11:57

2 Answers2

0

A BMP file starts with a BITMAPFILEHEADER struct, which contains (amongst other things) the offset to the actual start of the bits in the file.

So you could do something like this to get to the bits of the BMP file.

BITMAPFILEHEADER bmpFileHeader;
fread(&bmpFileHeader,sizeof(bmpFileHeader),1,file);
fsetpos(file,bmpFileHeader->bfOffBits,SEEK_SET);
size_t bufferSize = width * height * 3;
fread(data,bufferSize,1,file);

Of course, this is dangerous as you are expecting a properly sized and formatted BMP file. So you really need to read the BITMAPINFO too.

BITMAPFILEHEADER bmpFileHeader;
fread(&bmpFileHeader,sizeof(bmpFileHeader),1,file);
BITMAPINFO bmpInfo;
fread(&bmpInfo,sizeof(bmpInfo),1,file)
fsetpos(file,bmpFileHeader->bfOffBits,SEEK_SET);
int width = bmpInfo->bmiHeader.biWidth;
int height = bmpInfo->bmiHeader.biHeight;
assert(bmpInfo->bmiHeader.biCompression == BI_RGB);
assert(bmpInfo->bmiHeader.biBitCount == 24;
size_t bufferSize = width * height * 3;
fread(data,bufferSize,1,file);

You can obviously make this increasingly sophisticated in mapping bmp formats to allowed opengl formats.

Additional complications will be:

  • the bmp data is bottom up so will appear upsidedown unless you correct for that.
  • the pixel data in a bmp file is padded to ensure each row is a multiple of 4 bytes wide which can cause stride issues if your image is not either 32bits per pixel, or a multiple of 4 pixels wide (which is practice is usually true).
  • Just because you can feed a constant to OpenGL does not mean the format is actually supported. Sometimes you just have to get in there and re-order the bytes yourself.
Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • As I am new in this field tell me is BITMAPFILEHEADER is avaliable in glut.h or i have to add some specific header file for this. – saurabh Apr 29 '16 at 12:13
  • Oh I see - you are not doing this on windows then? If you are, just #include , if not, you can find the BMP file structures documented here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx – Chris Becke Apr 29 '16 at 12:18
  • Ok. I have corrected that upside down image thing, by editing it. – saurabh Apr 29 '16 at 12:19
  • tell me, if I invert the color of the source image ,with adobe Photoshop software, will it work @chris – saurabh Apr 30 '16 at 06:13
0

Finally I got what i needed. I was using an wrong argument GL_RGB instead I have replaced it with GL_BRG_EXT in this function

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_BGR_EXT, GL_UNSIGNED_BYTE, data ); 

previously I was getting this.textured mapped sphere with inverted colors

now I am getting this after the change I have made above.textured mapped sphere with true colors

saurabh
  • 17
  • 9