PDA

View Full Version : Texture class, free code comments...



M/\dm/\n
01-23-2004, 05:56 AM
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<<"\n\nFiles messed up (posx)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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<<"\n\nFiles messed up (negx)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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<<"\n\nFiles messed up (posy)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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<<"\n\nFiles messed up (negy)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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<<"\n\nFiles messed up (posz)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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<<"\n\nFiles messed up (negz)!\n\n"<<endl;
exit(0);
}
fread(&amp;bfh, sizeof(BITMAPFILEHEADER), 1, file);
if(bfh.bfType != BITMAP_ID)
{
fclose(file);
cout<<"\n\nNot a bitmap?\n\n"<<endl;
exit(0);
}
fread(&amp;bih, sizeof(BITMAPINFOHEADER), 1, file);
width = bih.biWidth;
height = bih.biHeight;
if(!bih.biSizeImage)
bih.biSizeImage = (width*height*3);
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, &amp;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, &amp;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_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posz);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negz);
}
}

void CTextureCM::Recreate()
{
if(SGIS_generate_mipmap)
{
glGenTextures(1, &amp;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, &amp;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_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negx);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negy);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_A RB, components, width, height, type, GL_UNSIGNED_BYTE, posz);
gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_A RB, components, width, height, type, GL_UNSIGNED_BYTE, negz);
}
}


Any comments, sugestions http://www.opengl.org/discussion_boards/ubb/frown.gif

Overmind
01-23-2004, 10:15 AM
Please give more details.

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

M/\dm/\n
01-24-2004, 12:26 AM
WXPpro. Textures are switching when I change res, for ex. wall texture goes to ceiling.