Archived

This topic is now archived and is closed to further replies.

if Not Glaux, hot to load bmps ..... ??????

This topic is 5510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I heard that using GLaux is bad and I heard you all guys saying that you made your own bitmap loaders and stuff. Can i have a link of where to download. Or tutorials on how to make one. How is the bmp file format structured ? How to convert it to the one that OpenGL works with to create texures ?

Share this post


Link to post
Share on other sites
I have never writen a bitmap loader, however if you go to filespecs.com they have descriptions of most popular file formats including bmp. To convert it to OpenGL form, you could read NeHe''s Targa image loading tutorial (number 24) and just make appropriate changes for bitmap type.

Share this post


Link to post
Share on other sites
basically you read it in according to the file format spec and then convert it into an array of bytes. then you draw it to the screen using the glDrawPixels(...) function. look it up in the red book for details: clicky

-me

Share this post


Link to post
Share on other sites
I have a bitmap loader class (C++) that works pretty well. It works best if the dimensions are powers of 2, which is what OpenGL needs form textures. Anyway, since bitmap parsers are so prevalent, I certainly don''t mind sharing mine if you don''t want to write your own. Email me at avariant@hotmail.com and I''ll send it to you.

Share this post


Link to post
Share on other sites
I use this to load 24 Bits BMPs (dimensions are assumed to be correct) in Windows:


  
HBITMAP l_hBMP=(HBITMAP)LoadImage(0, "filename.bmp" , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILELR_CREATEDIBSECTION);
BITMAP l_BMP;
if (l_hBMP)
{
GetObject(l_hBMP, sizeof(l_BMP), &l_BMP);
// now do glTexImage2D

glTexImage2D(GL_TEXTURE_2D, 0, 3 /* color channels */, l_BMP.bmWidth, l_BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, l_BMP.bmBits);
DeleteObject(l_hBMP);
//}



I haven''t tested it, so there might be typos but if this doesn''t work refer to nehe.''s last new tutorial (37 I think).

Share this post


Link to post
Share on other sites
This is the 3rd time I post this, on this very forum :D

Give me some credit if you use it in your projects, if possible

You feed it with a 8 BPP, uncompressed, power of 2, on x and y BMP, and it returns you a texture_id, or -1 in case of any error.
You can also specify some alpha, value, if you want color blending, etc.


//load a bmp texture, with the specified global alpha
GLuint load_bmp8_fixed_alpha(char * FileName, Uint8 a)
{
int i,x,y,x_padding,x_size,y_size,colors_no,r,g,b,current_pallete_entry;
Uint8 * file_mem;
Uint8 * file_mem_start;
Uint8 * texture_mem;
Uint8 * read_buffer;
Uint8 * color_pallete;
FILE *f = NULL;
int file_lenght;
GLuint texture;

f = fopen (FileName, "rb");
if (!f) return;
file_mem = (Uint8 *) calloc ( 20000, sizeof(Uint8));
file_mem_start=file_mem;
fread (file_mem, 1, 50, f);//header only
//now, check to see if our bmp file is indeed a bmp file, and if it is 8 bits, uncompressed
if(*((short *) file_mem)!=19778)//BM (the identifier)
{
free(file_mem_start);
fclose (f);
return -1;
}
file_mem+=18;
x_size=*((int *) file_mem);
file_mem+=4;
y_size=*((int *) file_mem);
file_mem+=6;
if(*((short *)file_mem)!=8)//8 bit/pixel?
{
free(file_mem_start);
fclose (f);
return -1;
}

file_mem+=2;
if(*((int *)file_mem)!=0)//any compression?
{
free(file_mem_start);
fclose (f);
return -1;
}
file_mem+=16;

colors_no=*((int *)file_mem);
if(!colors_no)colors_no=256;
file_mem+=8;//here comes the pallete

color_pallete=file_mem+4;
fread (file_mem, 1, colors_no*4+4, f);//header only
file_mem+=colors_no*4;

x_padding=x_size%4;
if(x_padding)x_padding=4-x_padding;

//now, allocate the memory for the file
texture_mem = (Uint8 *) calloc ( x_size*y_size*4, sizeof(Uint8));
read_buffer = (Uint8 *) calloc ( 2000, sizeof(Uint8));


for(y=0;y {
//fread (texture_mem+y*x_size, 1, x_size-x_padding, f);
fread (read_buffer, 1, x_size-x_padding, f);

for(x=0;x {
current_pallete_entry=*(read_buffer+x);
b=*(color_pallete+current_pallete_entry*4);
g=*(color_pallete+current_pallete_entry*4+1);
r=*(color_pallete+current_pallete_entry*4+2);
*(texture_mem+(y*x_size+x)*4)=r;
*(texture_mem+(y*x_size+x)*4+1)=g;
*(texture_mem+(y*x_size+x)*4+2)=b;
*(texture_mem+(y*x_size+x)*4+3)=a;
}

}

free(file_mem_start);
free(read_buffer);
fclose (f);
//ok, now, hopefully, the file is loaded and converted...
//so, assign the texture, and such

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
x_size, y_size,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
texture_mem);

free(texture_mem);
return texture;
}

Share this post


Link to post
Share on other sites
For goodness sake just use DevIL! Its free and supports just about every picture file format, has a bunch of extra image minipulation algoriths and such and is so easy to use. Here is how you load a bitmap:

ilInit();
ilGenImages(1, &m_ImageName);
ilBindImage(m_ImageName);
ilLoadImage("i.bmp");

if you want to make it a texture just add this:

ilutInit();
ilutRenderer(ILUT_OPENGL);
m_TextureID = ilutGLBindTexImage();

thats it, and it works for EVERYTHING. Don''t reinvent the wheel!

Share this post


Link to post
Share on other sites
Ya, you need to download the libraries and headers. Just search for "DevIL" on google, it should find their page. All you need are three DLL''s, three libs, and some headers.

Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?
Pres. Bush - I assure you I am not stoned.

Share this post


Link to post
Share on other sites