Witam. Próbuje napisać małą grę przy której pisaniu uczę się OpenGL. Niestety jak to czasem bywa napotkałem na pewien problem .
Nie mogę załadować tekstury do programu i jej wyświetlić:
GLuint cubeTexture;
GLbyte *gltReadTGABits(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat, GLbyte *pData = NULL);
...
void SkyBox()
{
glPushMatrix();
glTranslatef( Player.getX(), Player.getY(), Player.getZ() );
glEnable( GL_TEXTURE_CUBE_MAP );
glBindTexture( GL_TEXTURE_CUBE_MAP, cubeTexture );
MakeCube( 5.0f );
glDisable( GL_TEXTURE_CUBE_MAP );
glPopMatrix();
}
...
void SkyInit()
{
const char *szCubeFaces[6] = { "pos_x.tga",
"neg_x.tga",
"pos_y.tga",
"neg_y.tga",
"pos_z.tga",
"neg_z.tga" };
GLenum cube[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
int i;
// Usuwanie tylnych œcian wielok¹tów
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glGenTextures(1, &cubeTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexture);
// Tworzenie map tekstur
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// £adowanie obrazów tekstury szeœciennej
for(i = 0; i < 6; i++){
// £adowanie tej mapy tekstury
pBytes = gltReadTGABits(szCubeFaces[i], &iWidth, &iHeight, &iComponents, &eFormat);
glTexImage2D(cube[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
}
}
typedef struct
{
GLbyte identsize; // Size of ID field that follows header (0)
GLbyte colorMapType; // 0 = None, 1 = paletted
GLbyte imageType; // 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8=rle
unsigned short colorMapStart; // First colour map entry
unsigned short colorMapLength; // Number of colors
unsigned char colorMapBits; // bits per palette entry
unsigned short xstart; // image x origin
unsigned short ystart; // image y origin
unsigned short width; // width in pixels
unsigned short height; // height in pixels
GLbyte bits; // bits per pixel (8 16, 24, 32)
GLbyte descriptor; // image descriptor
} TGAHEADER;
GLbyte *gltReadTGABits(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat, GLbyte *pData)
{
FILE *pFile; // File pointer
TGAHEADER tgaHeader; // TGA file header
unsigned long lImageSize; // Size in bytes of image
short sDepth; // Pixel depth;
GLbyte *pBits = NULL; // Pointer to bits
// Default/Failed values
*iWidth = 0;
*iHeight = 0;
*eFormat = GL_RGB;
*iComponents = GL_RGB;
// Attempt to open the file
pFile = fopen(szFileName, "rb");
if(pFile == NULL) {
cout << "Blad 1\n";
return NULL;
}
// Read in header (binary)
fread(&tgaHeader, 18/* sizeof(TGAHEADER)*/, 1, pFile);
// Get width, height, and depth of texture
*iWidth = tgaHeader.width;
*iHeight = tgaHeader.height;
sDepth = tgaHeader.bits / 8;
// Put some validity checks here. Very simply, I only understand
// or care about 8, 24, or 32 bit targa's.
if( tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32 ) {
cout << "Blad 2\n";
return NULL;
}
// Calculate size of image buffer
lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
// Allocate memory and check for success
if(pData == NULL)
pBits = (GLbyte*)malloc(lImageSize * sizeof(GLbyte));
else
pBits = pData;
// Read in the bits
// Check for read error. This should catch RLE or other
// weird formats that I don't want to recognize
if(fread(pBits, lImageSize, 1, pFile) != 1){
if(pBits != NULL)
free(pBits);
cout << "Blad 3\n";
return NULL;
}
// Set OpenGL format expected
switch(sDepth)
{
case 3:
*eFormat = GL_BGR;
*iComponents = GL_RGB;
break;
case 4:
*eFormat = GL_BGRA;
*iComponents = GL_RGBA;
break;
case 1:
*eFormat = GL_LUMINANCE;
*iComponents = GL_LUMINANCE;
break;
default:
break;
}
// Done with File
fclose(pFile);
// Return pointer to image data
cout << " ok ";
return pBits;
}
Błąd w czytywaniu plików występuje w miejscu :
...
if( tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32 ) {
cout << "Blad 2\n";
return NULL;
}
...
Może mi ktoś powiedzieć co jest nie tak? Kod wczytujący tekstury pochodzi z książki OpenGl księga eksperta wyd. V.
Z góry dziękuje za pomoc :)