Thread: OpenGL -- Bitmaps

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    35

    OpenGL -- Bitmaps

    How do I load a bitmap without using glaux?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Read up about the fileformat at wotsit and read it all in using binary i/o stream.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Don't get this wrong, I'm not too lazy to read it... I just learn better/faster from examples. So, got any?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, first off, BMPs store their color as GBR and not RGB, ogl expects RGB soooooo, you will have to convert it from GBR to RGB. Second, BMPs has their origin at the top left corner, ogl expects an origin of bottom left corner. Also you will want to look up BITMAPINFOHEADER, BITMAPFILEHEADER and BMPQUAD. This should be enough for you to start *some*. I will not give you any code that does that job for you, that is for you to do

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah you will kick yourself later for not figuring out how to load it yourself.

    But you pick... your destiny is in your hands.

    nehe.gamedev.net

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    No, I did it the good way, not the easy one
    After ~5hrs of coding I managed to make one

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Ok, I need a little help here.. .How do I convert a bitmap to a texture?

    This doesn't work:

    Code:
    glGenTextures(1, &_pSlides[0]);
    Error:

    Code:
    Main.cpp:266: error: cannot convert `Bitmap*' to `GLuint*' for argument `2' to 
       `void glGenTextures(int, GLuint*)'

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I strongly suggest you have a look at the nehe tutorials for loading in textures once again.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Actually, I was wondering why their glaux replacement code doesn't work for me

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, read what the error says. The function arguments takes an integer and a pointer to an unsigned integer. What you are trying to send in is a Bitmap pointer. If you post the whole function or a bigger part of the function I can help you more.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Header:

    Code:
    //-----------------------------------------------------------------
    // Bitmap Object
    // C++ Header - Bitmap.h
    //-----------------------------------------------------------------
    
    //-----------------------------------------------------------------
    // Include Files
    //-----------------------------------------------------------------
    #include <windows.h>
    
    //-----------------------------------------------------------------
    // Custom Data Types
    //-----------------------------------------------------------------
    struct BITMAPINFO_256
    {
      BITMAPINFOHEADER  bmiHeader;
      RGBQUAD           bmiColors[256];
    };
    
    //-----------------------------------------------------------------
    // Bitmap Class
    //-----------------------------------------------------------------
    class Bitmap
    {
    protected:
      // Member Variables
      HBITMAP m_hBitmap;
      int     m_iWidth, m_iHeight;
    
      // Helper Methods
      void Free();
    
    public:
      // Constructor(s)/Destructor
      Bitmap();
      Bitmap(HDC hDC, LPTSTR szFileName);
      Bitmap(HDC hDC, UINT uiResID, HINSTANCE hInstance);
      Bitmap(HDC hDC, int iWidth, int iHeight, COLORREF crColor = RGB(0, 0, 0));
      virtual ~Bitmap();
    
      // General Methods
      BOOL Create(HDC hDC, LPTSTR szFileName);
      BOOL Create(HDC hDC, UINT uiResID, HINSTANCE hInstance);
      BOOL Create(HDC hDC, int iWidth, int iHeight, COLORREF crColor);
      void Draw(HDC hDC, int x, int y);
      int  GetWidth() { return m_iWidth; };
      int  GetHeight() { return m_iHeight; };
    };
    Cpp file;

    Code:
    //-----------------------------------------------------------------
    // Bitmap Object
    // C++ Source - Bitmap.cpp
    //-----------------------------------------------------------------
    
    //-----------------------------------------------------------------
    // Include Files
    //-----------------------------------------------------------------
    #include "Bitmap.h"
    
    //-----------------------------------------------------------------
    // Bitmap Constructor(s)/Destructor
    //-----------------------------------------------------------------
    Bitmap::Bitmap()
      : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
    }
    
    // Create a bitmap from a file
    Bitmap::Bitmap(HDC hDC, LPTSTR szFileName)
      : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
      Create(hDC, szFileName);
    }
    
    // Create a bitmap from a resource
    Bitmap::Bitmap(HDC hDC, UINT uiResID, HINSTANCE hInstance)
      : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
      Create(hDC, uiResID, hInstance);
    }
    
    // Create a blank bitmap from scratch
    Bitmap::Bitmap(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
      : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
      Create(hDC, iWidth, iHeight, crColor);
    }
    
    Bitmap::~Bitmap()
    {
      Free();
    }
    
    //-----------------------------------------------------------------
    // Bitmap Helper Methods
    //-----------------------------------------------------------------
    void Bitmap::Free()
    {
      // Delete the bitmap graphics object
      if (m_hBitmap != NULL)
      {
        DeleteObject(m_hBitmap);
        m_hBitmap = NULL;
      }
    }
    
    //-----------------------------------------------------------------
    // Bitmap General Methods
    //-----------------------------------------------------------------
    BOOL Bitmap::Create(HDC hDC, LPTSTR szFileName)
    {
      // Free any previous bitmap info
      Free();
    
      // Open the bitmap file
      HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (hFile == INVALID_HANDLE_VALUE)
        return FALSE;
    
      // Read the bitmap file header
      BITMAPFILEHEADER  bmfHeader;
      DWORD             dwBytesRead;
      BOOL bOK = ReadFile(hFile, &bmfHeader, sizeof(BITMAPFILEHEADER),
        &dwBytesRead, NULL);
      if ((!bOK) || (dwBytesRead != sizeof(BITMAPFILEHEADER)) ||
        (bmfHeader.bfType != 0x4D42))
      {
        CloseHandle(hFile);
        return FALSE;
      }
    
      BITMAPINFO* pBitmapInfo = (BITMAPINFO*)(new BITMAPINFO_256);
      if (pBitmapInfo != NULL)
      {
        // Read the bitmap info header
        bOK = ReadFile(hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER),
          &dwBytesRead, NULL);
        if ((!bOK) || (dwBytesRead != sizeof(BITMAPINFOHEADER)))
        {
          CloseHandle(hFile);
          Free();
          return FALSE;
        }
    
        // Store the width and height of the bitmap
        m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
        m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
    
        // Skip (forward or backward) to the color info, if necessary
        if (pBitmapInfo->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
          SetFilePointer(hFile, pBitmapInfo->bmiHeader.biSize - sizeof
            (BITMAPINFOHEADER), NULL, FILE_CURRENT);
    
        // Read the color info
        bOK = ReadFile(hFile, pBitmapInfo->bmiColors,
          pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD), &dwBytesRead,
          NULL);
    
        // Get a handle to the bitmap and copy the image bits
        PBYTE pBitmapBits;
        m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
          (PVOID*)&pBitmapBits, NULL, 0);
        if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
        {
          SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
          bOK = ReadFile(hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
            &dwBytesRead, NULL);
          if (bOK)
            return TRUE;
        }
      }
    
      // Something went wrong, so cleanup everything
      Free();
      return FALSE;
    }
    
    BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance)
    {
      // Free any previous DIB info
      Free();
    
      // Find the bitmap resource
      HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
      if (hResInfo == NULL)
        return FALSE;
    
      // Load the bitmap resource
      HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
      if (hMemBitmap == NULL)
        return FALSE;
    
      // Lock the resource and access the entire bitmap image
      PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
      if (pBitmapImage == NULL)
      {
        FreeResource(hMemBitmap);
        return FALSE;
      }
    
      // Store the width and height of the bitmap
      BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
      m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
      m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
    
      // Get a handle to the bitmap and copy the image bits
      PBYTE pBitmapBits;
      m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
        (PVOID*)&pBitmapBits, NULL, 0);
      if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
      {
        const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
          pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
        CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);
    
        // Unlock and free the bitmap graphics object
        UnlockResource(hMemBitmap);
        FreeResource(hMemBitmap);
        return TRUE;
      }
    
      // Something went wrong, so cleanup everything
      UnlockResource(hMemBitmap);
      FreeResource(hMemBitmap);
      Free();
      return FALSE;
    }
    
    BOOL Bitmap::Create(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
    {
      // Create a blank bitmap
      m_hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
      if (m_hBitmap == NULL)
        return FALSE;
    
      // Set the width and height
      m_iWidth = iWidth;
      m_iHeight = iHeight;
    
      // Create a memory device context to draw on the bitmap
      HDC hMemDC = CreateCompatibleDC(hDC);
    
      // Create a solid brush to fill the bitmap
      HBRUSH hBrush = CreateSolidBrush(crColor);
    
      // Select the bitmap into the device context
      HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
    
      // Fill the bitmap with a solid color
      RECT rcBitmap = { 0, 0, m_iWidth, m_iHeight };
      FillRect(hMemDC, &rcBitmap, hBrush);
    
      // Cleanup
      SelectObject(hMemDC, hOldBitmap);
      DeleteDC(hMemDC);
      DeleteObject(hBrush);
    
      return TRUE;
    }
    
    void Bitmap::Draw(HDC hDC, int x, int y)
    {
      if (m_hBitmap != NULL)
      {
        // Create a memory device context for the bitmap
        HDC hMemDC = CreateCompatibleDC(hDC);
    
        // Select the bitmap into the device context
        HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
    
        // Draw the bitmap to the destination device context
        BitBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);
    
        // Restore and delete the memory device context
        SelectObject(hMemDC, hOldBitmap);
        DeleteDC(hMemDC);
      }
    }

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    That is not ogl code. That is win32 API code and this means nothing to me when it comes to loading in bitmap textures to use with ogl. I will stick with what I said in a post above, go to nehe.gamedev.net and read the tutorials about loading bmps to use as textures. There is also a tutorial how to load tga (targa) files (no glaux used there).

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Targa files huh? That's great, I've got this file converter and it handles BMP -> TGA

    Thanks for the help

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What the hell is that? Go ahead and use that code and your game will crawl. Don't use the GDI.......please everyone read this.

    Don't use GDI for graphics, games, sounds, etc.

  15. #15
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Not to mention it will also be limited to one operating system.

    Overall it'd most likely be easier and better to just load the bitmap in via file access, process it, and pop the information into an array. I may be wrong but I think OpenGL just needs the colors in the correct order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM