When I run the following code, it just shows a plain color, like Pink or Orange. The picture which I'm trying to load is a 24-bit BMP which should work 100%. (Size 256x256)
BmpLoader.h
#ifndef BMPLOADER_H
#define BMPLOADER_H
#include<stdio.h>
#include<Windows.h>
#include<glut.h>
struct RGB
{
char red,green,blue;
};
class BmpLoader
{
public:
BmpLoader(char*,int,int);
~BmpLoader();
int width,height;
char* pixels;
};
BmpLoader* loadBmp(const char*);
GLuint loadTexture(BmpLoader*);
#endif
BmpLoader.cpp
#include "BmpLoader.h"
BmpLoader::BmpLoader(char* px,int w,int h):pixels(px),width(w),height(h)
{
}
BmpLoader::~BmpLoader()
{
delete[] pixels;
}
BmpLoader* loadBmp(const char* filename)
{
BITMAPFILEHEADER header;
BITMAPINFOHEADER info;
FILE *file;
file=fopen(filename,"rb");
fread(&header,sizeof(header),1,file);
fread(&info,sizeof(info),1,file);
char *px;
px=new char[info.biHeight*info.biWidth*3+1];
fseek(file,header.bfOffBits,0);
for(int i=0;i<info.biHeight;i++)
for(int j=0;j<info.biWidth;j+=3)
{
RGB rbg;
fread(&rbg,sizeof(rbg),1,file);
px[(i*info.biWidth)+j]=rbg.blue;
px[(i*info.biWidth)+j+1]=rbg.green;
px[(i*info.biWidth)+j+2]=rbg.red;
}
return new BmpLoader(px,info.biWidth,info.biHeight);
}
GLuint loadTexture(BmpLoader *image)
{
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,image->width,image->height,0,GL_RGB,GL_BYTE,image->pixels);
return textureId;
}