Advertisement

Loading bmp's in Unix

Started by June 29, 2002 04:52 PM
4 comments, last by ThorMan 22 years, 7 months ago
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
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.

Advertisement
Yes, both the SDL and GLX ports compiled and ran perfectly for me.

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.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
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.
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.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind

This topic is closed to new replies.

Advertisement