Skip to main content
GameDev.net gamedev.net
🔒 Locked

glGenTextures returns odd stuff...

Started by GamerX Dec 20, 2004 at 2:40 AM 4 replies 1.1k views
Original Post
GamerX
GamerX
I've just hit a brick wall here... Ok so I have a GUI class with an array size 5 holding CGUIBtn objects:
class CGUIBtn
{
public:
	CGUIBtn(void);
	CGUIBtn(char *file);
	~CGUIBtn(void);
	
	CTexture m_TexOff;
	CTexture m_TexOver;
	
	int m_Height;
	int m_Width;

	bool LoadTexture(int state, char * file);
	void Render(float yPos);
};

and GUI calls LoadTexture to set each texture in each of the 5 buttons. Rigth after loading all those I load another texture. The problem is: all the polygons are textured with the very last texture which I've loaded... In CTexture the function
void CTexture::GenerateTexture()
{	
	// Generate the texture and text the id to the images id.
	glGenTextures(1, &ID);
   
	// Here we bind the texture and set up the filtering.
	glBindTexture(GL_TEXTURE_2D, ID);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexImage2D(GL_TEXTURE_2D, 0, type, imageWidth, imageHeight, 0, type, GL_UNSIGNED_BYTE, image);
	gluBuild2DMipmaps(GL_TEXTURE_2D, type, imageWidth, imageHeight, type, GL_UNSIGNED_BYTE, image);
}

seems to be the source of the problem because glGenTextures ALWAYS sets ID to 1... What could possibly be causing it to do this??? I'm using GLUT and here's my init function:
void CFGapp::Init(int argc, char *argv[])
{
    ///////////////////////////////////////////////////////////////////////////
    // GLUT Initialisation
    ///////////////////////////////////////////////////////////////////////////

    glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT); // Size the window
        
    // Center Window
    glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN)-SCREEN_WIDTH)/2, 
                          (GetSystemMetrics(SM_CYSCREEN)-SCREEN_HEIGHT)/2);
    glutInitWindowPosition(0,0);

    glutInit(&argc, argv); // Initialise GLUT
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); // Set display mode
    
    // Create a normal window if we're debuging
    glutCreateWindow("Space Combat Arena");

    char dispModeStr[64];
    sprintf(dispModeStr, "%dx%d:%d@%d", SCREEN_WIDTH, SCREEN_HEIGHT, 16, 60);
    glutGameModeString(dispModeStr);
    glutEnterGameMode();
        
    ///////////////////////////////////////////////////////////////////////////
    // GLUT now ready...
    // Some of our own GL setup
    ///////////////////////////////////////////////////////////////////////////
        
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  // Clear the screen to the specified color.

    glShadeModel(GL_SMOOTH);         // Smooth shading in our scenes.
    glEnable(GL_DEPTH_TEST);         // Enable desth testing for hidden surface removal.
    glEnable(GL_TEXTURE_2D);         // Enable texture mapping.
    
    // Light properties for the diffuse light, specular light, and light position.
	float diffuseLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
	float specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
	float LightPosition[] = {0.0f, 0.0f, 8.0f, 1.0f};
	
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0f);
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 75.0f);

    // Set up the material information for our objects.  Again this is just for show.
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
    glMateriali(GL_FRONT, GL_SHININESS, 128);
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_SRC_ALPHA);
    
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // resets the viewport to new dimensions.
    glMatrixMode(GL_PROJECTION);     // Sets the projection matrix.
    glLoadIdentity();                // Reset the modelview matrix.

            
    // Calculate the aspect ratio of the window.
    gluPerspective(45.0f, (GLfloat)SCREEN_WIDTH/(GLfloat)SCREEN_HEIGHT, 0.1f, 1000.0f);

	glutDisplayFunc(RenderGLUT_CB);	 // Our display function...

    glutSetCursor(GLUT_CURSOR_NONE);
    
    glMatrixMode(GL_MODELVIEW);      // Sets the projection matrix.
    glLoadIdentity();                // Reset the modelview matrix.
	
	glGUI.LoadTextures();
}

glGUI.LoadTextures() loads all the textures, i put it at the very end so everything should be enabled right? PS Sorry if I'm asking so many questions.
Enigma
Enigma
I can't see anything wrong there (except for the fact that you're using char* instead of std::string and have public data members). Can you post your full Texture class definition plus the function which makes you conclude that "glGenTextures ALWAYS sets ID to 1" (i.e. the function where you print out ID or assert it or whatever).

Enigma
GamerX
GamerX
To AP: It's not my rendering functions, the glBindTexture is called. But the ID ends up being the same for all of them hence everything is rendered with the same texture.

Enigma, I used VS.NET Debuger to determine that. I put a breakpoint at glGenTextures and every time the function is called ID is 1 (before the call it's 0) I know they are different objects because the height/width, which are read from the file, differ (and yes they are suposed to).


And I think I just solved it! In my CTexture DESTRUCTOR I had

glDeleteTextures(1, &ID);


After I commented out that line it worked... I have no idea why on earth the destructor would be called then but anyway, at least it works.
_the_phantom_
_the_phantom_
at a guess, you've got a copy happening somewhere which is causing the orignal object to be deleted, thus it removes the reference to a texture
GamerX
GamerX
Ofcourse! It's because I'm using this method for setting up GLUT callbacks to the member functions:

class TMain
{
void DoStuff(void)
{
// do stuff here
}

static TMain * inst;

static void DoStuffCB(void)
{
inst->DoStuff();
}
};

glutIdleFunc(TMain::DoStuffCB);

Thanks you guys are great!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.