Texture class, free code comments...

I have problems when chenging res, maybe because I dont clean after myself…

Tehture.h

#ifndef TEXTURE_H
#define TEXTURE_H

#include “main.h”

#define BITMAP_ID 0x4D42
#define TGA_BGR 0x0002
#define TGA_BW 0x0003
#define TGA_BGR_RLE 0x000A
#define TGA_BW_RLE 0x000B
#define TGA_32 32
#define TGA_24 24
#define TGA_16 16

extern bool SGIS_generate_mipmap;

class CTexture
{
private:
unsigned char* data;
GLenum components;
GLenum type;

public:
GLuint id;
unsigned int width, height;

CTexture()
{
data = NULL;
width = 0;
height = 0;
}

~CTexture()
{
if(glIsTexture(id))
glDeleteTextures(1, &id);

  if(data)
  	free(data);

}

void Load(const char* name, const char* extension);

void LoadDataFromBMP(const char* filename);

void LoadDataFromTGA(const char* filename);

void LoadDataFromJPG(const char* filename);

void Create(void);

void Recreate(void);

inline void BindPrimary(void);

inline void BindSecondary(void);
};

#endif

Texture.cpp

#include “TextureCM.h”

void CTextureCM::Load(const char* extension)
{
if(!strcmp(“bmp”,extension))
LoadDataFromBMP();
else if(!strcmp(“tga”,extension))
LoadDataFromTGA();
else if(!strcmp(“jpg”,extension))
LoadDataFromJPG();
else
cout<<“Unsupported texture format!”<<endl;
}

void CTextureCM::LoadDataFromBMP()
{
FILE *file;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
file = fopen(“Textures/CubeMaps/posx.bmp”, “rb”);
if(!file)
{
cout<<"

Files messed up (posx)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

“<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
posx = (unsigned char*)malloc(bih.biSizeImage);
fread(posx, 1, bih.biSizeImage, file);
fclose(file);
//posx
file = fopen(“Textures/CubeMaps/negx.bmp”, “rb”);
if(!file)
{
cout<<”

Files messed up (negx)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

“<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
negx = (unsigned char*)malloc(bih.biSizeImage);
fread(negx, 1, bih.biSizeImage, file);
fclose(file);
//negx
file = fopen(“Textures/CubeMaps/posy.bmp”, “rb”);
if(!file)
{
cout<<”

Files messed up (posy)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

“<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
posy = (unsigned char*)malloc(bih.biSizeImage);
fread(posy, 1, bih.biSizeImage, file);
fclose(file);
//posy
file = fopen(“Textures/CubeMaps/negy.bmp”, “rb”);
if(!file)
{
cout<<”

Files messed up (negy)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

“<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
negy = (unsigned char*)malloc(bih.biSizeImage);
fread(negy, 1, bih.biSizeImage, file);
fclose(file);
//negz
file = fopen(“Textures/CubeMaps/posz.bmp”, “rb”);
if(!file)
{
cout<<”

Files messed up (posz)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

“<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
posz = (unsigned char*)malloc(bih.biSizeImage);
fread(posz, 1, bih.biSizeImage, file);
fclose(file);
//posz
file = fopen(“Textures/CubeMaps/negz.bmp”, “rb”);
if(!file)
{
cout<<”

Files messed up (negz)!

“<<endl;
exit(0);
}
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<”

Not a bitmap?

"<<endl;
exit(0);
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (widthheight3);
negz = (unsigned char*)malloc(bih.biSizeImage);
fread(negz, 1, bih.biSizeImage, file);
fclose(file);
//negz

components = GL_RGB8;
type = GL_BGR;

Create();
}

void CTextureCM::LoadDataFromTGA()
{
}

void CTextureCM::LoadDataFromJPG()
{
}

void CTextureCM::Create()
{
if(SGIS_generate_mipmap)
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, id);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posx);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negx);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posy);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negy);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posz);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negz);
}
else
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, id);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posz);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negz);
}
}

void CTextureCM::Recreate()
{
if(SGIS_generate_mipmap)
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, id);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posx);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negx);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posy);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negy);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, posz);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, components, width, height, 0, type, GL_UNSIGNED_BYTE, negz);
}
else
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, id);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, components, width, height, type, GL_UNSIGNED_BYTE, posz);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, components, width, height, type, GL_UNSIGNED_BYTE, negz);
}
}

Any comments, sugestions

Please give more details.

What kind of problems, how do you change resolution, what operating system, …

WXPpro. Textures are switching when I change res, for ex. wall texture goes to ceiling.