Loading the texture:
GLuint loadImage(const char* theFileName)
{
ILuint imageID; // Create an image ID as a ULuint
GLuint textureID = 0; // Create a texture ID as a GLuint
ILboolean success; // Create a flag to keep track of success/failure
ILenum error; // Create a flag to keep track of the IL error state
ilGenImages(1, &imageID); // Generate the image ID
ilBindImage(imageID); // Bind the image
success = ilLoadImage((ILstring)theFileName); // Load the image file
// If we managed to load the image, then we can start to do things with it...
if (success)
{
// If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
ILinfo ImageInfo;
iluGetImageInfo(&ImageInfo);
if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
{
iluFlipImage();
}
// Convert the image into a suitable format to work with
// NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA
success = ilConvertImage(ilGetInteger(IL_IMAGE_FORMAT), IL_UNSIGNED_BYTE);
// Quit out if we failed the conversion
if (!success)
{
error = ilGetError();
std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
//exit(-1);
}
// Generate a new texture
glGenTextures(1, &textureID);
// Bind the texture to a name
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Set texture interpolation method to use linear interpolation (no MIPMAPS)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Specify the texture specification
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
ilGetInteger(IL_IMAGE_BPP), // Image colour depth
ilGetInteger(IL_IMAGE_WIDTH), // Image width
ilGetInteger(IL_IMAGE_HEIGHT), // Image height
0, // Border width in pixels (can either be 1 or 0)
ilGetInteger(IL_IMAGE_FORMAT), // Image format (i.e. RGB, RGBA, BGR etc.)
IL_UNSIGNED_BYTE, // Image data type
ilGetData()); // The actual image data itself
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
else // If we failed to open the image file in the first place...
{
error = ilGetError();
std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
std::cout << theFileName << std::endl;
exit(-1);
}
ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image.
//std::cout << "Texture creation successful." << std::endl;
return textureID; // Return the GLuint to the texture so you can use it!
}
Draw the scene:
void RenderScene()
{
std::sort (RenderList.begin(), RenderList.end(), SortRenderList);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
Material *TempMat = NULL;
MyVertex *TempVerts = NULL;
glDisable(GL_TEXTURE_2D);
for (RenderListIT = RenderList.begin(); RenderListIT != RenderList.end(); RenderListIT++)
{
glLoadIdentity();
glTranslatef((*RenderListIT)->Position->x, (*RenderListIT)->Position->y, (*RenderListIT)->Position->z);
glScalef ((*RenderListIT)->Scale->x, (*RenderListIT)->Scale->y, (*RenderListIT)->Scale->z);
glRotatef ((*RenderListIT)->Rotation->x, 1.0f, 0.0f, 0.0f);
glRotatef ((*RenderListIT)->Rotation->y, 0.0f, 1.0f, 0.0f);
glRotatef ((*RenderListIT)->Rotation->z, 0.0f, 0.0f, 1.0f);
if (TempMat == NULL || TempMat != (*RenderListIT)->RenderMesh->Materials)
{
TempMat = (*RenderListIT)->RenderMesh->Materials;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (*RenderListIT)->RenderMesh->Materials->Diffuse );
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (*RenderListIT)->RenderMesh->Materials->Specular );
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (*RenderListIT)->RenderMesh->Materials->Ambient );
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (*RenderListIT)->RenderMesh->Materials->Emission );
}
if (TempVerts == NULL || TempVerts != (*RenderListIT)->RenderMesh->VertexData)
{
TempVerts = (*RenderListIT)->RenderMesh->VertexData;
if ((*RenderListIT)->RenderMesh->HasVertexs == true)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData );
}
else
{
glDisableClientState(GL_VERTEX_ARRAY);
}
if ((*RenderListIT)->RenderMesh->HasNormals == true)
{
glEnable(GL_LIGHTING);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer( GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData );
}
else
{
glDisable(GL_LIGHTING);
glDisableClientState(GL_NORMAL_ARRAY);
}
if ((*RenderListIT)->RenderMesh->HasTexcoords == true)
{
if ((*RenderListIT)->RenderMesh->HasTexture == true)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, (*RenderListIT)->RenderMesh->Active->TextureHandle);
}
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData );
}
else
{
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
if ((*RenderListIT)->RenderMesh->HasColors)
{
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData );
}
else
{
glDisableClientState(GL_COLOR_ARRAY);
}
}
glDrawArrays(GL_TRIANGLES, 0, (*RenderListIT)->RenderMesh->NumVerticies);
}
//RenderList.clear();
glutSwapBuffers();
}
the model
http://www.turbosquid.com/FullPreview/Index.cfm/ID/526437