Loading a bitmap (Without using glaux)
Okay, so I got this bit of code from NeHe, it works alright, havn't had any problems with it so far..
Code:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
AUX_RGBImageRec *LoadBMP(const char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
GLuint LoadGLTexture( const char *filename ) // Load Bitmaps And Convert To Textures
{
AUX_RGBImageRec *pImage; // Create Storage Space For The Texture
GLuint texture = 0; // Texture ID
pImage = LoadBMP( filename ); // Loads The Bitmap Specified By filename
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ( pImage != NULL && pImage->data != NULL ) // If Texture Image Exists
{
glGenTextures(1, &texture); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->sizeX, pImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
free(pImage->data); // Free The Texture Image Memory
free(pImage); // Free The Image Structure
}
return texture; // Return The Status
}
I notice that it uses auxDIBImageLoad to actually load the bitmap... The way my program is set up, I want to create a texture object with its own loading function..
Something like
Code:
class BMPTexture
{
// storage structs
// Loading Code
};
I kinda want to mirror this setup (snipped from my MS3DModel definition)
Code:
class MS3DModel
{
public:
MS3DModel();
virtual ~MS3DModel();
struct Vertex
{
char BoneID;
float Location[3];
};
int NumVertices;
Vertex *Vertices;
struct Triangle
{
float VertexNormals[3][3];
float Textures1[3], Textures2[3];
int VertexIndices[3];
};
int NumTriangles;
Triangle *Triangles;
struct Mesh
{
int MaterialIndex;
int NumTriangles;
int *TriangleIndices;
};
int NumMeshes;
Mesh *Meshes;
struct Material
{
float Ambient[4], Diffuse[4], Specular[4], Emissive[4];
float Shininess;
GLuint Texture;
char *TextureFilename;
};
int NumMaterials;
Material *Materials;
bool Load( const std::string & name );
void ReloadTextures();
void Draw();
};
Aside from the drawing function and reloadTextures function, and of course different storage structures, I want to mirror this setup, I find it works quite nicely with the resource manager I've created, shown here:
Code:
template< typename T_ >
class Resource_Manager
{
public:
typedef T_ value_type; // std library convention
typedef boost::shared_ptr<T_> Resource_Ptr;
typedef boost::weak_ptr<T_> Resource_Observer;
typedef std::map< std::string, Resource_Ptr > Resource_Map;
Resource_Manager<T_>() {};
~Resource_Manager<T_>() {};
Resource_Observer Request_Resource(const std::string & name)
{
Resource_Map::iterator it = mResources.find(name);
if (it == mResources.end())
{
Resource_Ptr Raw_Resource(new T_);
Raw_Resource->Load(name);
mResources.insert(std::make_pair(name, Raw_Resource));
Resource_Observer Resource(Raw_Resource);
return Resource;
}
else
{
return Resource_Observer(it->second);
}
}
void Request_Resource_Removal(const std::string & name)
{
Resource_Map::iterator it = mResources.find(name);
if (it != mResources.end())
{
mResources.erase(it);
}
}
private:
Resource_Map mResources;
};
As you can see, it returns a resource observer weak pointer, but the actual loading of the bitmap returns a texture ID. While applying textures via ID is what I want to be able to do, you kinda have to in opengl. HMmmm...
Looking at the loading code more closely, LoadBMP actually returns a pointer to the actual bitmap, while the GLLoadTexture func creates the texID.. The reason I want to use my manager to do all this stuff is, not to sound boastful, basically that my resource manager can do a cleaner job than the auxDIBImageLoad function. I don't really have to worry about deleting stuff with my resource manager, it is a nice way to do things IMO..
I guess my dilemma is this..
My resource manager handles pointers, it doesn't handle ID's, thus I can't mix ID creation in with my texture manager, unless maybe I'm returning an observer with a tex ID in it..... Ah yes, that's it.
If I mirror the MS3D setup I can easily add an ID in the info structs and then access the ID of the texture object with methods in that class..
So I'm guessing when I get down to it, it looks like this:
Resource_Manager<BMPTexture> Textures;
Textures.Request_Resource("texture name here");
Then I can access the ID via the return value of the request resource method, which is a resource observer..
Okay, is this making sense to you guys? Am I in rightful reason to want to avoid the auxDIBImageLoad function? Is what I'm proposing making sense to you guys?
Thanks.
EDIT: Really I just need to know how to load a bitmap without using auxDIBImageLoad..