// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Load a texture from a resource and return it’s index (0 on error); Setup
// the texture using the following default characteristics:
// Environment Mode = GL_MODULATE
// S Wrap Method = GL_REPEAT
// T Wrap Method = GL_REPEAT
// Magnification Filter=GL_LINEAR
// Minification Filter=GL_LINEAR_MIPMAP_LINEAR
// Perspective Correction Hint = GL_NICEST
size_t MgTextureManager::
loadTextureFromResource(
unsigned int resourceId,
const char* textureName,
bool mipMapped
)
{
if (!IsOpenGlInitialized())
{
m_LastErrorDescription =
“Attempted to load a texture prior to OpenGL Initialization”;
return 0;
}
// Load the image;
GLuint texIndx(0);
glGenTextures(1,&texIndx);
if (texIndx==0)
{
MgString msg(
"Failed generating texture OpenGL index for texture named ",
textureName
);
msg << “|” << GetOpenGLErrorMessage();
m_LastErrorDescription = msg;
return 0;
}
glBindTexture(GL_TEXTURE_2D,texIndx);
MgTextureOptions options;
options.apply();
HINSTANCE hInstance;
HANDLE hBitmap;
BITMAPINFO bmInfo;
void *pBitmapData;
// Find the bitmap resource
hInstance = GetModuleHandle(NULL);
hBitmap = LoadBitmap(hInstance,MAKEINTRESOURCE(resourceId));
if (hBitmap == NULL)
{
m_LastErrorDescription =
“Failed loading texture from bitmap resource”;
return 0;
}
GetObject(hBitmap,sizeof(BITMAPINFO),&bmInfo);
DeleteObject(hBitmap);
hBitmap =
LoadResource(
hInstance,
::FindResource(hInstance,MAKEINTRESOURCE(resourceId),RT_BITMAP)
);
if (hBitmap == NULL)
{
m_LastErrorDescription =
“Failed finding texture bitmap resource”;
return 0;
}
pBitmapData = LockResource(hBitmap);
int width = bmInfo.bmiHeader.biWidth;
int height = bmInfo.bmiHeader.biHeight;
// This is specific to the binary format of the data read in.
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
if (mipMapped)
{
gluBuild2DMipmaps(
GL_TEXTURE_2D,
3,
width,
height,
GL_BGR_EXT,
GL_UNSIGNED_BYTE,
(unsigned char *)pBitmapData+sizeof(bmInfo)+2
);
}
else
{
glTexImage2D(
GL_TEXTURE_2D,
0,
3,
width,
height,
0,
GL_BGR_EXT,
GL_UNSIGNED_BYTE,
(unsigned char *)pBitmapData+sizeof(bmInfo)+2
);
}
TextureInfo info(texIndx,width,height,24);
info.options = options;
(*p_Textures)[MgStlString(textureName)] = info;
// Done with bitmap
DeleteObject(hBitmap);
return 0;
}