I should elaborate a little bit on why I need to learn imaging. I’m actually working on a my first game as a way to practice (and maybe sell for a dollar) all the skills I’ve developed in C++ and Opengl. For those interested, the game is a type of final fantasy/zelda rpg. I can do texturing just fine and I started the game by using wood, grass, and stone textures, and applying these without problems to geometry. What would really help, however, is to learn how to load a bmp or jpeg and simply paste it the screen. I know this sounds simple, nonetheless, I failed on my attempts. Here is the a pic of the game…www.geocities.com/zadeh1979/town
We have written a class to load the bitmap textures:
//Bitmap.h
//Zehne Ziba Corporation
//All rights reserved
#ifndef __BITMAP_H
#define __BITMAP_H
class BMPTEXTURE
{
public:
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *bitmapImage; // bitmap image data
BMPTEXTURE();
~BMPTEXTURE();
bool Load( char *filename );
void Unload();
};
BMPTEXTURE::BMPTEXTURE()
{
bitmapImage = NULL;
memset( &bitmapFileHeader, 0, sizeof( BITMAPFILEHEADER ) );
memset( &bitmapInfoHeader, 0, sizeof( BITMAPINFOHEADER ) );
}
BMPTEXTURE::~BMPTEXTURE()
{
}
bool BMPTEXTURE::Load( char *filename)
{
FILE *filePtr; // the file pointer
int imageIdx = 0; // image index counter
// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return false;
// read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
// verify that this is a bitmap by checking for the universal bitmap id
if (bitmapFileHeader.bfType != 0x4D42 )
{
fclose(filePtr);
return false;
}
// read the bitmap information header
fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
// move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
// allocate enough memory for the bitmap image data
bitmapImage = new unsigned char[bitmapInfoHeader.biSizeImage];
// verify memory allocation
if (!bitmapImage)
{
delete[] bitmapImage;
fclose(filePtr);
return false;
}
// read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader.biSizeImage, filePtr);
// make sure bitmap image data was read
if (bitmapImage == NULL)
{
fclose(filePtr);
return false;
}
// close the file and return the bitmap image data
fclose(filePtr);
return true;
}
void BMPTEXTURE::Unload()
{
if( bitmapImage != NULL )
{
delete [] bitmapImage;
bitmapImage = NULL;
}
memset( &bitmapInfoHeader, 0, sizeof( bitmapInfoHeader ) );
memset( &bitmapFileHeader, 0, sizeof( bitmapFileHeader ) );
}
#endif
Important functions are
Load -Loads a file
Unload -Unloads data
So you can write a function to load data.As an example:
bool LoadBitmapTexture( char *fileName, unsigned int texID )
{
BMPTEXTURE bitmap_texture;
if( !bitmap_texture.Load( fileName ) )
return false;
// setup the texture objext
glBindTexture(GL_TEXTURE_2D, texID);
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bitmap_texture.bitmapInfoHeader.biWidth, bitmap_texture.bitmapInfoHeader.biHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, bitmap_texture.bitmapImage );
bitmap_texture.Unload();
return true;
}
and then you can load your file in your init file:
bool Init()
{
…
GLuint texture;
glGenTexture( 1, &texture );
if( !LoadBitmapTexture( “filename.bmp”, texture ) )
return false;
In your render function you can use from this texture:
glBindTexture( GL_TEXTURE_2D, texture );
And in your clean function you should delete the texture memory:
glDeleteTextures( 1, &texture );
But remember our copyright. Writing such classes wasn’t easy for me
Unfortunately, i have no idea about the JPEG files.
Regards
-Ehsan-
Ok, I am using a class called Glbmp that I found and is easy to use. But I am only successful with loading and applying an image as a “texture”. I dont want to display the image as a texture…If I apply the texture witht the texture functions to a very small geometry, will I see the image in whole or will it resemble a texture??
For small objects, you should use from the function gluBuild2DMipmaps(). It creates new textures based on your orignal texture and selects the best texture for your object.As an example if your texture is 6464 , textures created with gluBuild2DMipmaps() are 6464, 3232, 1616, 88, 44, 22 and 11. In this case if your object is too small, it will select small textures( as an example 44, 22 ) for you.
-Ehsan-
-Ehsan-