Results 1 to 3 of 3

Thread: I'm getting lost in the details..please help

  1. #1
    Member Contributor
    Join Date
    Mar 2000

    I'm getting lost in the details..please help

    I have been struggling for quite some time in learning(and understanding) how to load a bitmap in OpenGL (Just get it on the screen)
    I'm getting really confused in all the "red tape" involved so I thought somebody could answer these questions:

    1-Does OpenGl have support for loading bitmaps? or is this a Windows only thing(embedded in an Opengl program)

    2- Every tutorial I see on the net about this uses glaux. Why? I never see tutorials(of any kind)using glutCreateWindow().

    3-Do I really have to study windows programming to understand how loading the bitmap works?

    I don't want to copy/use anyone else's code to do this. I want to understand (by looking at someone's code) and then write the code myself. So far every tutorial I look at (this one is from NeHe) goes something like this:

    HWND hWnd=NULL;
    HINSTANCE hInstance

    bool keys[256];
    bool active=TRUE;
    bool fullscreen=TRUE;

    GLfloat xrot;
    GLfloat yrot;
    GLfloat zrot;

    GLuint texture[1];


    AUX_RGBImageRec *LoadBMP(char *Filename
    FILE *File=NULL;
    if (!Filename)
    return NULL;

    if (File)
    return auxDIBImageLoad(Filename);

    return NULL;

    int LoadGLTextures()
    int Status=FALSE;
    AUX_RGBImageRec *TextureImage[1];
    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    if (TextureImage[0]=LoadBMP("Data/NeHe.bmp"))
    Status=TRUE; // Set The Status To TRUE

    glGenTextures(1, &texture[0]); // Create The Texture

    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    if (TextureImage[0]) // If Texture Exists
    if (TextureImage[0]->data) // If Texture Image Exists
    free(TextureImage[0]->data); // Free The Texture Image Memory

    free(TextureImage[0]); // Free The Image Structure

    return Status; // Return The Status

    5-Does it really have to be this complicated? I am not an advanced OpenGl/C programmer but this code makes zero sense to me...I'm guessing I can't just call some function with the name of my file and load it into memory...then call another function to display it....Is any of the above code OpenGl at all? Or is all that Windows95/98 stuff....

    Any clarification would be great..Thanks so much....

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Feb 2000

    Re: I'm getting lost in the details..please help

    To show a bitmap on the screen, you can use glDrawPixels(slow on PCs) or you can just make a texture out of it(like nehe does) and apply this texture to a polygon.

    Now to load the bitmap in memory, you can use auxloadbitmap or something like that or you can make your own function.

    This is a basic C function for loading a bitmap using some Win32 data structures :

    bool loadBitmap(char* filename, unsigned char alpha, GLubyte* color)
    FILE* bmpfile;
    BITMAPFILEHEADER bmfileheader;
    BITMAPINFOHEADER bminfoheader;
    RGBTRIPLE rgb;

    int texpos = 0;

    bmpfile = fopen(filename, "rb");

    if (!bmpfile)
    return false;

    fread(&bmfileheader, sizeof(BITMAPFILEHEADER), 1, bmpfile);
    fread(&bminfoheader, sizeof(BITMAPINFOHEADER), 1, bmpfile);

    iWidth = bminfoheader.biWidth;
    iHeight = bminfoheader.biHeight;

    colors = (GLubyte*)malloc(tex->width * tex->height *4);

    // read color
    for (int i = 0; i < iWidth; i++)
    for(int j = iHeight - 1; j>=0 ; j--) //start with the height because the first data you read in the color array of the bitmap
    { // is coord (1.0).
    fread(&rgb, sizeof(RGBTRIPLE),1,bmpfile);
    colors[texpos] = rgb.rgbtRed ;
    colors[texpos+1] = rgb.rgbtGreen;
    colors[texpos+2] = rgb.rgbtBlue;
    colors[texpos+3] = alpha

    texpos += 4;


    return true;

    It is not complicated to load a bitmap, it is only that the documentation is bad.

    a bitmap is composed of 3 things:
    you can get the description of those things at You have to look in platformSDK, win32, references and then look for Bitmap. There is actually two types of bitmap, you can check wich type it is by looking at the type parameter of the info header. But from experience, this methods works in 99.9% of the time

    1.Bitmap File Header

    2.Bitmap Info Header

    3.Colors values wich are RBGTRIPLE. Wich is just a structure of 3 floats. One for red, one for green, one for blue.

    So what you do is :

    read the file header,
    read the info header, get the height and width.

    allocate memory for data.

    then get all the RGB triplets.

    Then use opengl calls to load it in a texture.

    The function above also have an alpha parameter to specify a default alpha value if you d'like to use RGBA instead of RGB

    Of course, you'll still have to learn how to do texture mapping with opengl.

    to learn that, you can buy the red book, and/or check tutorials.

    Nehe tutorials are really good.
    Romka also have good tutorials. he often write on the forum on update on his site.

    [This message has been edited by Gorg (edited 05-10-2000).]

  3. #3
    Member Contributor
    Join Date
    Mar 2000

    Re: I'm getting lost in the details..please help

    Thanks for the speedy (and great reply) It makes a lot more sense now...

Similar Threads

  1. Level of Details (LOD)
    By pramod in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-21-2007, 06:54 AM
  2. Multisample Details
    By Rodan in forum OpenGL: Advanced Coding
    Replies: 24
    Last Post: 08-26-2006, 05:20 PM
  3. more details on Collada FX?
    By Alkiem in forum COLLADA
    Replies: 1
    Last Post: 01-19-2006, 01:39 PM
  4. details about glColorMaterial
    By adrien in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 09-20-2005, 04:44 AM
  5. GL_ARB_vertex_buffer_object details
    By GeorgeSarkisyan in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 05-20-2003, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean