Ok, so I’ve been struggling to get to grips with textures all day now. I’ve nearly got it sorted even though I don’t fully understand what is going on. I’ve followed one of NeHe’s tutorials and adapted it to try and get it working for me. I’ve now got the textures loading and displaying until the display refreshes. I’m using TGA files.
Here’s the relevant code from my initialisation function:
void init()
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
cout<< "Error Loading Textures"; // If Texture Didn't Load Return FALSE
}
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Do not calculate inside of jet
glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Black background
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Spot0Dir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, Spot0Cut);
glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight);
glPopMatrix();
}
Here’s my renderscene function:
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, -12, -40);
glRotated(90, -1, 0, 0);
//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, FloorAmbi);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f);glVertex2d(-50, -50);
glTexCoord2f(1.0f, 0.0f);glVertex2d(50, -50);
glTexCoord2f(1.0f, 1.0f);glVertex2d(50, 50);
glTexCoord2f(0.0f, 1.0f);glVertex2d(-50, 50);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glRotated(90, 1, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, 0, -100);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, BackGroundEmi);
glRectd (-100, -100, 100, 100);
DrawBase();
DisplayPegs(ChoosenPeg);
glutSwapBuffers();
}
And here’s the Texture loading function which I assume must be working correctly as the textures display at first.
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
// Load The Bitmap, Check For Errors.
if (LoadTGA(&texture[0], "Data/Compressed.tga") &&
LoadTGA(&texture[1], "Data/Compressed.tga"))
{
Status=TRUE; // Set The Status To TRUE
for (int loop=0; loop<2; loop++) // Loop Through Both Textures
{
// Typical Texture Generation Using Data From The TGA ( CHANGE )
glGenTextures(1, &texture[loop].texID); // Create The Texture ( CHANGE )
glBindTexture(GL_TEXTURE_2D, texture[loop].texID);
glTexImage2D(GL_TEXTURE_2D, 0, texture[loop].bpp / 8, texture[loop].width, texture[loop].height, 0, texture[loop].type, GL_UNSIGNED_BYTE, texture[loop].imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
if (texture[loop].imageData) // If Texture Image Exists ( CHANGE )
{
free(texture[loop].imageData); // Free The Texture Image Memory ( CHANGE )
}
}
}
return Status; // Return The Status
}
I hope this is enough for any of you to be able to help me. It’s very frustrating to have overcome so many other issues and get so close that it works for a split second!