I wrote my entire Texture loader by hand. I'm not using any Libraries. From what I gather the loading part is done correctly and stored in the texture class:
Texture* TGATextureLoader::create(const char *filename) {
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
GLint iWidth = 0;
GLint iHeight = 0;
GLenum eFormat = GL_BGR_EXT;
GLint iComponents = GL_RGB8;
// Attempt to open the fil
pFile = fopen(filename, "rb");
if(pFile == NULL)
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){
return NULL;
}
// Calculate size of image buffer
lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
// Allocate memory and check for success
pBits = (GLbyte*)malloc(lImageSize * sizeof(GLbyte));
if(pBits == NULL)
return NULL;
// 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)
{
free(pBits);
return NULL;
}
// Set OpenGL format expected
switch(sDepth)
{
case 3: // Most likely case
eFormat = GL_BGR_EXT;
iComponents = GL_RGB8;
break;
case 4:
eFormat = GL_BGRA_EXT;
iComponents = GL_RGBA8;
break;
case 1:
eFormat = GL_LUMINANCE;
iComponents = GL_LUMINANCE8;
break;
};
// Done with File
fclose(pFile);
Texture* texture = new Texture(pBits);
texture->setWidth(iWidth);
texture->setHeight(iHeight);
texture->setComponents(iComponents);
texture->setFormat(eFormat);
// Return pointer to image data
return texture;
}
Then when Im done, I call textureName->generateTexture() Method from the texture class:
void Texture::generateTexture(){
GLuint id;
glGenTextures(1, &id);
this->textureID = id;
std::cout << "Binding Texture\n";
glBindTexture(GL_TEXTURE_2D, this->textureID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, this->width, this->height, 0, GL_RGB, GL_UNSIGNED_BYTE, this->data);
glBindTexture(GL_TEXTURE_2D, 0);
std::cout << this->textureID;
}
Then in my initialization of the resources, I call;
textureName = Engine::TGATextureLoader::create("filePath/fileName.tga");
textureName->generateTexture();
lastly in my render method, I bind the texture again before I use it:
glBindTexture(GL_TEXTURE_2D, cubeTexture->getTextureID());
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glTexCoord2f(0.0f, 0.0f);
//glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glTexCoord2f(1.0f, 0.0f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glTexCoord2f(1.0f, 1.0f);
//glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
The texture doesn't render though. Is there something wrong with this code?