Jump to content
  • Advertisement

Archived

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

Loading bmp's in Unix

This topic is 5803 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 have a question about loading .bmp files with OpenGL on a unix platform. i have tried running the code from lesson6, but i can''t seem to load the image files. has anyone else tried this for a unix platform? do i need to get some other code to load the bmp for me, or am i possibly doing something wrong? any adivce would be greatly appreciated

Share this post


Link to post
Share on other sites
Advertisement
Lesson 6 is evil (because of its teaching the use of GLaux). Have you tried the GLX or SDL ports of it? They should work (I''ll try them even). I use my own Windows Bitmap loading code, and so can you, so that''s a solution even if nothing else is.

Share this post


Link to post
Share on other sites
I run Linux so I know what your talking about. You gotta create the bmp loader your self. I did some research and figured out how to do it. Heres the code.


  
struct PICTURE
{
unsigned long x;
unsigned long y;
char* data;
};

//0 failed

//1 everything is kool

//must be an rbg(color) bmp not b&w(black and white)

int LoadBMP(char *filename, PICTURE* pic)
{
FILE* file;
unsigned long size;
unsigned long a;
unsigned short int planes;
unsigned short int bpp;
char temp;

file = fopen(filename, "rb");

if(file == NULL)
{
fclose(file);
return 0;
}

fseek(file, 18, SEEK_CUR);

a = fread(&pic->x, 4, 1, file);

if(a != 1)
{
fclose(file);
return 0;
}

a = fread(&pic->y, 4, 1, file);

if(a != 1)
{
fclose(file);
return 0;
}

size = pic->x * pic->y * 3;

if(fread(&planes, 2, 1, file) != 1)
{
fclose(file);
return 0;
}

if(planes != 1)
{
fclose(file);
return 0;
}

a = fread(&bpp, 2, 1, file);

if(a != 1)
{
fclose(file);
return 0;
}

if(bpp != 24)
{
fclose(file);
return 0;
}

fseek(file, 24, SEEK_CUR);

pic->data = (char *) malloc(size);

if(pic->data == NULL)
{
fclose(file);
return 0;
}

a = fread(pic->data, size, 1, file);

if(a != 1)
{
fclose(file);
return 0;
}

for(a = 0; a < size; a += 3)
{
temp = pic->data[a];
pic->data[a] = pic->data[a + 2];
pic->data[a + 2] = temp;
}

return 1;
}

/*
"Both width and height must have the form 2^m + 2b, where m is a non-negative integer (which can have a
different value for width than for height) and b is the value of border. The maximum size of a texture
map depends on the implementation of OpenGL, but it must be at least 64 x 64 (or 66 x 66 with borders)."
--OpenGL Programming Guide

Meaning:

You can use width or height of 2, 4, 8, 16, 32, 64, (on new video cards) 128, 256, 512 (They dont have
to be the same)

If you have a texture border you add 2 to the size.
*/

int LoadTexture(char* FileName, GLuint Texture[], int num)
{
PICTURE* pic;

pic = (PICTURE *)malloc(sizeof(PICTURE));

if(pic == NULL)
{
return 0;
}

if(LoadBMP(FileName, pic) == 0)
{
return 0;
}

glGenTextures(1, &Texture[num]);
glBindTexture(GL_TEXTURE_2D, Texture[num]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, pic->x, pic->y, 0, GL_RGB, GL_UNSIGNED_BYTE, pic->data);

delete pic;

return 1;
}


The way you use the code I gave you is simpal.


  
GLuint Texture[1]; /*Make the array size how many Textures you want*/

if(LoadTexture("blah.bmp", Texture, 0) == 0)
{
//failed

}


I hope this helps you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What are you talking about?
Use SDL and you can easily load your bitmaps (see the SDL Port).
IMHO is SDL way better then GLX since you could probably compile the same code on windows w/o much modification.

Share this post


Link to post
Share on other sites
I was just showing another way. If you want to use SDL go ahead but I like GLUT, and GLUT can be converted to windows easily.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!