Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.