M
Michael Sgier
Hello
I want to replace a windows bitmap load function through an equivalent
SDL function.
I should extract the img height etc. and assign it to the terrainTex
array. I've also
three beginner questions:
-Concerning that array why is texID used? terrainTex[0] doesn't state
the first element?
What does terrainTex[0] represent? Especially the "0".
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.
-Why does glGenTextures(1, &terrainTex[0].texID) use &terrainTex and
not *terrainTex?
The declaration is:
CTexture terrainTex[5];
and the CTextureclass:
class CTexture
{
private:
long int scaledWidth;
long int scaledHeight;
unsigned char *palette;
public:
texTypes_t textureType;
unsigned char tgaImageCode; // 0 = not TGA image, 2 = color, 3 =
greyscale
int width;
int height;
int bitDepth;
unsigned int texID;
unsigned char *data;
CTexture() { data = NULL; palette = NULL; }
~CTexture() { Unload(); }
// void LoadTexture(char *filename); // That's the original function
i want to replace
//unsigned char *LoadTex(char* filename, CTexture terrainTex[0]);
//int LoadTex(char* filename);
void Unload()
{
glDeleteTextures(1, &texID);
if (data != NULL)
free(data);
if (palette != NULL)
free(palette);
data = NULL;
palette = NULL;
}
};
// load texture here instead of calling the LoadTex function
// terrainTex[0].LoadTex("pics/ground1.bmp", &terrainTex[0]);
//terrainTex[0].LoadTex("pics/ground.bmp");
SDL_Surface* img;
img = SDL_LoadBMP("pics/ground.bmp");
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &terrainTex[0].texID);
glBindTexture(GL_TEXTURE_2D, terrainTex[0].texID);
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);
//or GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//or GL_CLAMP
//
// // switch (terrainTex[0].textureType)
// // {
// // case BMP:
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, terrainTex[0].width,
terrainTex[0].height,
GL_RGB, GL_UNSIGNED_BYTE, terrainTex[0].data);
The following is the original windows bmp functions:
#include "texture.h"
// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *CTexture::LoadBitmapFile(char *filename, BITMAPINFOHEADER
*bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable
// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;
// 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 != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}
// 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 = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}
// 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 NULL;
}
// swap the R and B values to get RGB since the bitmap color format is
in BGR
for (imageIdx = 0; imageIdx < (int)bitmapInfoHeader->biSizeImage;
imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
// close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}
/*****************************************************************************
LoadBitmapFileWithAlpha
Loads a bitmap file normally, and then adds an alpha component to use for
blending
*****************************************************************************/
unsigned char *CTexture::LoadBitmapFileWithAlpha(char *filename,
BITMAPINFOHEADER *bitmapInfoHeader)
{
unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
unsigned char *bitmapWithAlpha = (unsigned char
*)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);
// loop through the bitmap data
for (int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src
+=3, dst +=4)
{
// if the pixel is black, set the alpha to 0. Otherwise, set it to
255.
if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 &&
bitmapImage[src+2] == 0)
bitmapWithAlpha[dst+3] = 0;
else
bitmapWithAlpha[dst+3] = 0xFF;
// copy pixel data over
bitmapWithAlpha[dst] = bitmapImage[src];
bitmapWithAlpha[dst+1] = bitmapImage[src+1];
bitmapWithAlpha[dst+2] = bitmapImage[src+2];
}
free(bitmapImage);
return bitmapWithAlpha;
} // end LoadBitmapFileWithAlpha()
// LoadBMPTexture()
// desc: loads a texture of the BMP format
void CTexture::LoadBMPTexture(char *filename)
{
BITMAPINFOHEADER texInfo; // BMP header
// store BMP data in texture
data = LoadBitmapFileWithAlpha(filename, &texInfo);
if (data == NULL)
{
free(data);
}
// store texture information
width = texInfo.biWidth;
height = texInfo.biHeight;
palette = NULL;
scaledHeight = 0;
scaledWidth = 0;
textureType = BMP;
}
// LoadTexture()
// desc: loads a texture given the filename
void CTexture::LoadTexture(char *filename)
{
char *extStr;
// get extension from filename
extStr = strchr(filename, '.');
extStr++;
// set the texture type based on extension of filename
if ((strcmpi(extStr, "BMP") == 0) || (strcmpi(extStr, "bmp") == 0))
LoadBMPTexture(filename);
else if ((strcmpi(extStr, "PCX") == 0) || (strcmpi(extStr, "pcx") == 0) )
LoadPCXTexture(filename);
else if ((strcmpi(extStr, "TGA") == 0) || (strcmpi(extStr, "tga") == 0) )
LoadTGATexture(filename);
}
THANKS and regards
Michael
I want to replace a windows bitmap load function through an equivalent
SDL function.
I should extract the img height etc. and assign it to the terrainTex
array. I've also
three beginner questions:
-Concerning that array why is texID used? terrainTex[0] doesn't state
the first element?
What does terrainTex[0] represent? Especially the "0".
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.
-Why does glGenTextures(1, &terrainTex[0].texID) use &terrainTex and
not *terrainTex?
The declaration is:
CTexture terrainTex[5];
and the CTextureclass:
class CTexture
{
private:
long int scaledWidth;
long int scaledHeight;
unsigned char *palette;
public:
texTypes_t textureType;
unsigned char tgaImageCode; // 0 = not TGA image, 2 = color, 3 =
greyscale
int width;
int height;
int bitDepth;
unsigned int texID;
unsigned char *data;
CTexture() { data = NULL; palette = NULL; }
~CTexture() { Unload(); }
// void LoadTexture(char *filename); // That's the original function
i want to replace
//unsigned char *LoadTex(char* filename, CTexture terrainTex[0]);
//int LoadTex(char* filename);
void Unload()
{
glDeleteTextures(1, &texID);
if (data != NULL)
free(data);
if (palette != NULL)
free(palette);
data = NULL;
palette = NULL;
}
};
// load texture here instead of calling the LoadTex function
// terrainTex[0].LoadTex("pics/ground1.bmp", &terrainTex[0]);
//terrainTex[0].LoadTex("pics/ground.bmp");
SDL_Surface* img;
img = SDL_LoadBMP("pics/ground.bmp");
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &terrainTex[0].texID);
glBindTexture(GL_TEXTURE_2D, terrainTex[0].texID);
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);
//or GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//or GL_CLAMP
//
// // switch (terrainTex[0].textureType)
// // {
// // case BMP:
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, terrainTex[0].width,
terrainTex[0].height,
GL_RGB, GL_UNSIGNED_BYTE, terrainTex[0].data);
The following is the original windows bmp functions:
#include "texture.h"
// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *CTexture::LoadBitmapFile(char *filename, BITMAPINFOHEADER
*bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable
// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;
// 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 != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}
// 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 = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}
// 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 NULL;
}
// swap the R and B values to get RGB since the bitmap color format is
in BGR
for (imageIdx = 0; imageIdx < (int)bitmapInfoHeader->biSizeImage;
imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
// close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}
/*****************************************************************************
LoadBitmapFileWithAlpha
Loads a bitmap file normally, and then adds an alpha component to use for
blending
*****************************************************************************/
unsigned char *CTexture::LoadBitmapFileWithAlpha(char *filename,
BITMAPINFOHEADER *bitmapInfoHeader)
{
unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
unsigned char *bitmapWithAlpha = (unsigned char
*)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);
// loop through the bitmap data
for (int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src
+=3, dst +=4)
{
// if the pixel is black, set the alpha to 0. Otherwise, set it to
255.
if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 &&
bitmapImage[src+2] == 0)
bitmapWithAlpha[dst+3] = 0;
else
bitmapWithAlpha[dst+3] = 0xFF;
// copy pixel data over
bitmapWithAlpha[dst] = bitmapImage[src];
bitmapWithAlpha[dst+1] = bitmapImage[src+1];
bitmapWithAlpha[dst+2] = bitmapImage[src+2];
}
free(bitmapImage);
return bitmapWithAlpha;
} // end LoadBitmapFileWithAlpha()
// LoadBMPTexture()
// desc: loads a texture of the BMP format
void CTexture::LoadBMPTexture(char *filename)
{
BITMAPINFOHEADER texInfo; // BMP header
// store BMP data in texture
data = LoadBitmapFileWithAlpha(filename, &texInfo);
if (data == NULL)
{
free(data);
}
// store texture information
width = texInfo.biWidth;
height = texInfo.biHeight;
palette = NULL;
scaledHeight = 0;
scaledWidth = 0;
textureType = BMP;
}
// LoadTexture()
// desc: loads a texture given the filename
void CTexture::LoadTexture(char *filename)
{
char *extStr;
// get extension from filename
extStr = strchr(filename, '.');
extStr++;
// set the texture type based on extension of filename
if ((strcmpi(extStr, "BMP") == 0) || (strcmpi(extStr, "bmp") == 0))
LoadBMPTexture(filename);
else if ((strcmpi(extStr, "PCX") == 0) || (strcmpi(extStr, "pcx") == 0) )
LoadPCXTexture(filename);
else if ((strcmpi(extStr, "TGA") == 0) || (strcmpi(extStr, "tga") == 0) )
LoadTGATexture(filename);
}
THANKS and regards
Michael