#include "PreCompile.h"
#include "ImageTarga.h"
#include "TargaReader.h"
ImageTarga::ImageTarga()
{
}
ImageTarga::~ImageTarga()
{
}
void ImageTarga::Load(const string& fileName, bool transparent)
{
m_name = fileName;
TargaReader targaReader;
targaReader.Load(fileName);
if (targaReader.GetBitDepth() != 32 && targaReader.GetBitDepth() != 16
&& targaReader.GetBitDepth() != 24)
{
throw RacException("bit depth of tga file must be 32, 24, or 16");
}
const GLubyte* pTextureData = targaReader.GetRawData();
m_width = targaReader.GetWidth();
m_height = targaReader.GetHeight();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &m_textureID);
glBindTexture(GL_TEXTURE_2D, m_textureID);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
if (targaReader.GetBitDepth() == 32)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height,
0, GL_RGBA, GL_UNSIGNED_BYTE, pTextureData);
}
else if (targaReader.GetBitDepth() == 24)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height,
0, GL_RGB, GL_UNSIGNED_BYTE, pTextureData);
}
else
{
if (transparent)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height,
0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pTextureData);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height,
0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pTextureData);
}
int err = glGetError();
if (err != GL_NO_ERROR)
{
throw RacException("Error in glTexImage2D: ", err);
}
}