00001
00002
00004
#include "StdAfx.h"
00005
#include "OpenGLTexture.h"
00006
00008
00010
00011 COpenGLTexture::COpenGLTexture()
00012 {
00013
SetEntityType(Entity_OpenGLTexture);
00014 m_uiGLTextureID = m_uiGLFormat = m_uiGLInternalFormat = m_uiPixelStride = 0;
00015
m_Mask = tRGBA(-1, -1, -1, -1);
00016 }
00017
00018 COpenGLTexture::~COpenGLTexture()
00019 {
00020
Release();
00021 }
00022
00023 void COpenGLTexture::Release()
00024 {
00025
if(glIsTexture(m_uiGLTextureID))
00026 glDeleteTextures(1, &m_uiGLTextureID);
00027
00028 m_uiGLTextureID = 0;
00029 }
00030
00031 void COpenGLTexture::Bind()
00032 {
00033
if(m_uiGLTextureID == 0)
00034 {
00035 glDisable(GL_TEXTURE_2D);
00036
return;
00037 }
00038
else
00039 {
00040
if(glIsTexture(m_uiGLTextureID))
00041 {
00042 glEnable(GL_TEXTURE_2D);
00043 glBindTexture(GL_TEXTURE_2D, m_uiGLTextureID);
00044 }
00045
else
00046 glDisable(GL_TEXTURE_2D);
00047 }
00048 }
00049
00050 int COpenGLTexture::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00051 {
00052
TiXmlElement* pXMLElement = NULL;
00053
const char *pcValue = NULL;
00054
00055
int iRetValue =
CTexture::LoadXML(pDataNode, strFilename);
00056
if(iRetValue !=1)
00057
return iRetValue;
00058
00059
return Create();
00060 }
00061
00062 int COpenGLTexture::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00063 {
00064
return CTexture::SaveXML(pDataNode, strFilename);
00065 }
00066
00067 UINT
COpenGLTexture::Create()
00068 {
00069 UINT ImageWidth = 0, ImageHeight = 0, ImageDepth = 0;
00070 BYTE* ImageData = NULL;
00071
00072 ImageWidth = FreeImage_GetWidth(m_pBitmap);
00073 ImageHeight = FreeImage_GetHeight(m_pBitmap);
00074 ImageDepth = FreeImage_GetBPP(m_pBitmap);
00075
00076
if(!
IsMasked())
00077 FreeImage_ConvertTo24Bits(m_pBitmap);
00078
else
00079 FreeImage_ConvertTo32Bits(m_pBitmap);
00080
00081 ImageData = FreeImage_GetBits(m_pBitmap);
00082
00083
00084
00085 glGenTextures(1, &m_uiGLTextureID);
00086 glBindTexture(GL_TEXTURE_2D, m_uiGLTextureID);
00087
00088
00089 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00090 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00091
00092 BYTE *MaskedTexture = NULL;
00093 UINT uiIndex = 0;
00094
00095
if(
IsMasked())
00096 {
00097 MaskedTexture =
new BYTE[ImageWidth * ImageHeight * 4];
00098
00099 UINT uiIndex = 0;
00100
for (UINT i=0; i<ImageWidth * ImageHeight * (ImageDepth / 8); i+=(ImageDepth / 8))
00101 {
00102 MaskedTexture[uiIndex] = ImageData[i];
00103 MaskedTexture[uiIndex+1] = ImageData[i+1];
00104 MaskedTexture[uiIndex+2] = ImageData[i+2];
00105
00106
if((ImageData[i] == (BYTE)(m_Mask.B)) && (ImageData[i+1] == (BYTE)(m_Mask.G)) && (ImageData[i+2] == (BYTE)(m_Mask.R)))
00107 MaskedTexture[uiIndex + 3] = 0;
00108
else
00109 MaskedTexture[uiIndex + 3] = 255;
00110
00111 uiIndex+=4;
00112 }
00113
00114 glTexImage2D(GL_TEXTURE_2D, 0, 4, ImageWidth, ImageHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, MaskedTexture);
00115
delete[] MaskedTexture;
00116 }
00117
else
00118 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, ImageWidth, ImageHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, ImageData);
00119
00120
00121
if( (!CheckSize(
GetWidth())) && (!CheckSize(
GetHeight())))
00122 {
00123
00124 gluBuild2DMipmaps(GL_TEXTURE_2D,
00125
GetDepth(),
00126
GetWidth(),
00127
GetHeight(),
00128 GL_BGR_EXT,
00129 GL_UNSIGNED_BYTE,
00130 m_pBitmap);
00131 }
00132
00133
return 1;
00134 }
00135
00136 void COpenGLTexture::Destroy()
00137 {
00138
CTexture::Destroy();
00139 }
00140
00141 bool COpenGLTexture::IsOfType(eEntityType eType)
00142 {
00143
if(eType == Entity_OpenGLTexture)
00144
return true;
00145
00146
return CTexture::IsOfType(eType);
00147 }
00148
00149
00150
00151
00152
bool CheckSize(UINT Size)
00153 {
00154
return ( (Size & ((~Size)+1)) == Size) ?
true :
false;
00155 }