PDA

View Full Version : Textures work until display updates.



ekelly30
04-01-2011, 09:11 AM
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(&amp;texture[0], "Data/Compressed.tga") &amp;&amp;
LoadTGA(&amp;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, &amp;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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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!

Dan Bartlett
04-01-2011, 09:20 AM
Well, you have glDisable(GL_TEXTURE_2D) in your renderscene function, so after you have rendered it once with texturing, you are then disabling texturing (for GL_TEXTURE_2D on texture unit 0).
If you need to render something else without textures, then you will need to re-enable texturing, with glEnable(GL_TEXTURE_2D) before drawing - otherwise just leave it on.

ekelly30
04-01-2011, 09:47 AM
When I leave that out everything is then the same colour as the texture, as in the picture.

I've also shown what it's like textured and then after the redisplay.

Do I need to disable texturing before drawing shapes that aren't textured? The texturing is enabled at the start of the renderscene function, and it's this function thats used for redisplaying so surely it would be enabled again each time it updated? That's what I would of thought at least. Thanks for the reply.

Dan Bartlett
04-01-2011, 10:03 AM
With the code you provided, you only have glEnable(GL_TEXTURE_2D) at the start of the init() function - which I assume is only called once at start up, rather than per frame.

Yes, you would need glEnable(GL_TEXTURE_2D) before drawing textured objects, and glDisable(GL_TEXTURE_2D) before drawing shapes that aren't textured. Putting a glEnable(GL_TEXTURE_2D) at the start of renderscene() should achieve this.

note: glEnable/glDisable(GL_TEXTURE_2D) are not needed when using shaders.

ekelly30
04-01-2011, 10:08 AM
Thanks very much, that was such a simple fix. I didn't realise it was in the init function. Thanks again.

BTW regarding drawing untextured shapes I had just got a little confused as you said without as opposed to with, I think.

Thanks again!