Advertisement

Loading Bitmaps for Textures, using Glut

Started by December 09, 2004 07:31 PM
3 comments, last by stormrunner 19 years, 11 months ago
Can someone give me a link to some tutorials / source code for loading bitmaps via Glut? I have some, but they dont explain it... at all, and the code isnt just simply loading a bitmap, they added a bunch of other junk in it. I am new to OpenGL, and c++ in general and id prefer to use Glut for loading my bitmaps at the moment, unless someone knows of a reason not to. So, can someone give links, source code, or explain the process to me? if source code, please give the most simplified code to just explain the bmp/texture loading aspect of it. And by simplified i mean no need to add lighting, ect... Big thanks to any helpful comments! :) (Glut 3.7, Dev C++ 4.9.9.0)
Rank: OpenGL & Glut "Nub", C++ Freshman.
glut has nothing to do with the loading
just load and create a texture using OpenGL only.
all bitmaps loading tutorials for opengl will work with glut. you dont use glut functions in the loading.
a tutorial can be found here
pex.
Advertisement
sigh :/, ok but am i missing something?

Everywhere i go i am told Glaux is old and obsolete. Even NeHe says that.

I have been to this site befor and i downloaded the tutorial, and i come to find it also uses glaux.


So for once and forall, how do i do this without glaux /cry.

I tried NeHe's Glaux replacement code, however it seems to be dependant on Win32 API, i included the file with no avail, as i get the error
Quote:
GL_BGR_EXT undeclared, first use of this function

(or something similar to that)
Which i assume is because i am using Glut to handle my windows and whatnot, NOT the full blown win32.


http://www.ultimategameprogramming.com/Tutorial.php?category=OpenGL&page=4
There is source for it but i have yet to fully figure it out as it is documented poorly for someone with my grasp of OpenGL & Glut.

Any help?
Rank: OpenGL & Glut "Nub", C++ Freshman.
GLFW - note you would have to convert to tga
alright, well, i'm assuming you're referring to the tutorial named "Teapot". even if not (more than likely ;) ), it contains the information you need. pex22 was correct that it isn't really a good idea to load an image with Glut; instead you should write your own or use a 3d party library. moving on, the CBMPLoader.h and CBMPLoader.cpp files are what you need. i'll provide the two files and explain what they do
/*   Class Name:      CBMPLoader.   Created by:      Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).   Description:      This class loads a .bmp texture into this object.*/#ifndef CTEXTURE_H#define CTEXTURE_H#include<windows.h>        // Windows header file.#include<stdio.h>          // Standard input/output.#include<gl/gl.h>          // OpenGl include.#include<gl/glu.h>         // OpenGl utility include.#define BITMAP_ID 0x4D42	// The universal bitmap IDclass CBMPLoader{   public:      CBMPLoader();   // Constructor.      ~CBMPLoader();  // Destructor.      bool LoadBMPFile(char *filename);   // Load a .bmp image file.      void FreeImage();                   // Delete a image.      unsigned int ID;                    // ID used for generating the textures in OpenGl.      int imageWidth;                     // Width of a texture.      int imageHeight;                    // Height of a texture.   protected:      void GenerateTexture();             // Generate a texture in OpenGL.      unsigned char* LoadBitmap(char *file,                                BITMAPINFOHEADER *bitmapInfoHeader);// Load a bitmap image.      unsigned char *image;               // Texture image.      bool textureExist;                  // This will be used if the image was loaded.      int type;                           // Image format.};#endif// Copyright September 2003// All Rights Reserved!// Allen Sherrod// ProgrammingAce@UltimateGameProgramming.com// www.UltimateGameProgramming.com

and now CBMPLoader.cpp
/*   Class Name:      CBMPLoader.   Created by:      Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).   Description:      This class loads a .bmp texture into this object.*/#include"CBMPLoader.h"CBMPLoader::CBMPLoader(){   // Give everything default values.  image = 0;  textureExist = false;  type = 0;}CBMPLoader::~CBMPLoader(){   FreeImage();                  // Delete all images and dynamic memory.   glDeleteTextures(1, &ID);     // Release the image from OpenGL memory.}bool CBMPLoader::LoadBMPFile(char *file){	BITMAPINFOHEADER biHeader;    // This will hold the info header of the bitmap.   if(textureExist)      FreeImage();	// Load the image.	image = LoadBitmap(file, &biHeader);   // Make sure the image loaded.	if(image == 0)	   {         return false;	   }   imageWidth = biHeader.biWidth;   imageHeight = biHeader.biHeight;   GenerateTexture();   textureExist = true;   return true;}void CBMPLoader::GenerateTexture(){   // Generate the texture and text the id to the images id.	glGenTextures(1, &ID);      // Here we bind the texture and set up the filtering.   glBindTexture(GL_TEXTURE_2D, ID);   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	gluBuild2DMipmaps(GL_TEXTURE_2D, type, imageWidth, imageHeight,                     type, GL_UNSIGNED_BYTE, image);   glTexImage2D(GL_TEXTURE_2D, 0, type, imageWidth, imageHeight,                 0, type, GL_UNSIGNED_BYTE, image);}unsigned char* CBMPLoader::LoadBitmap(char *file, BITMAPINFOHEADER *bitmapInfoHeader){	FILE *pFile = 0;              // Need this to open a file.	BITMAPFILEHEADER header;      // This will hold the bitmap header information.	unsigned char *textureData = 0;  // This will hold the bitmap image itself.   // This will be used to swap the image colors from BGR to RGB.	unsigned char textureColors = 0;	pFile = fopen(file, "rb");    // Open the file.	if(pFile == 0) return 0;      // Check and make sure there are no errors.	// Read in the bitmap header info into the BITMAPFILEHEADER variable.	fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);		// Make sure this is a real bitmap by checking the ID.	if(header.bfType != BITMAP_ID)	   {		   fclose(pFile);		   return 0;	   }	// Read in the second header info into the BITMAPINFOHEADER variable.	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);   if(bitmapInfoHeader->biSizeImage == 0)      bitmapInfoHeader->biSizeImage = bitmapInfoHeader->biWidth * bitmapInfoHeader->biHeight * 3;	// Place the pointer in front of where the image data starts.	fseek(pFile, header.bfOffBits, SEEK_SET);	// Dynamically create enough memory for the image.	textureData = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);	// Error checking.  Make sure the memory was allocated.	if(!textureData)	   {		   free(textureData);		   fclose(pFile);		   return 0;	   }	// Read in the image.	fread(textureData, 1, bitmapInfoHeader->biSizeImage, pFile);	// Error checking.  Make sure an image was loaded.	if(textureData == 0)	   {		   fclose(pFile);		   return 0;	   }	// Bitmaps are saved in BGR format so we will make the image RGB by...	for(int index = 0; index < (int)bitmapInfoHeader->biSizeImage; index+=3)	   {		   textureColors = textureData[index];		   textureData[index] = textureData[index + 2];		   textureData[index + 2] = textureColors;	   }   type = GL_RGB;	fclose(pFile);     // We are done with the file so close it.	return textureData;  // Send the image to the function that called this.}void CBMPLoader::FreeImage(){   // When the application is done delete all dynamically allocated memory.   if(image)      {         free(image);         image = 0;         textureExist = false;         type = 0;      }}// Copyright September 2003// All Rights Reserved!// Allen Sherrod// ProgrammingAce@UltimateGameProgramming.com// www.UltimateGameProgramming.com

the image itself is just an array of unsigned chars read from a file, and the header contains the information on how it is encoded (i'll leave it at that, as google will enlighten you on the finer points). all you're doing is taking that data (specifically from the function CBMPLoader::LoadBitmap) and sending it to opengl to create a texture (which that class does). glut doesn't need to do anything. for reference, you can write you're own loader for .tga and .pcx files (and many more, assuming you know the file format).

hope that helped.
cheers.

<edit :: and you can eliminate the windows dependency by making your own bitmap header - use the same contents, just name it something different.
- stormrunner

This topic is closed to new replies.

Advertisement