Skip to main content
gamedev.net
🔒 Locked

glGenTextures() problem

Started by Fella 3 replies 1.4k views
Original Post
Fella
Fella
I am trying to load a texture on a quad and if I do not use glGenTextures() in the following code it works fine while if do use glGenTextures() the texture does not get mapped on the quad and I just get a red quad. Why does it work if I remove the glGenTextures()?

const int TEX_NUM = 1; // number of textures
const int GROUND_TEX = 0; // ground texture
GLuint textures[ TEX_NUM ]; // holds allocated names of texture objects
const char* textureFileNames[ TEX_NUM ] = { "texture.tga" };

// initializes OpenGL state
void setupRC()
{
        glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); // set clearing color
	
	GLubyte* pLoadedTGA; // pointer to loaded TGA texture
	GLint width, height; // number of color components in the texture
	GLint internalFormat; // texture propreties
	GLenum format; // format of the texture ( RGB etc.)

	glEnable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	glGenTextures( TEX_NUM, textures );
	for ( int texObj = 0; texObj < TEX_NUM; texObj++ )
        {
		glBindTexture( GL_TEXTURE_2D, textures[ texObj ] );
                pLoadedTGA = gltLoadTGA( textureFileNames[ texObj ], &width, &height, &internalFormat, &format ); 
		gluBuild2DMipmaps( GL_TEXTURE_2D, internalFormat, width, height, format, GL_UNSIGNED_BYTE, pLoadedTGA  ); 
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

		free( pLoadedTGA );
	}

	glEnable( GL_DEPTH_TEST );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 45.0f, 800/600, 1, 500 );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
}

void render()
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glPushMatrix();
		glColor3fv( 1.0f, 0.0f, 0.0f );
		glTranslatef( 0.0f, 0.0f, -30.0f );
		glRotatef( 10.0f, 1.0f, 0.0f, 0.0f );

        glEnable( GL_TEXTURE_2D );
	        glBindTexture( GL_TEXTURE_2D, GROUND_TEX );
	        glBegin( GL_QUADS );
		      glTexCoord2f( 0.0f, 0.0f );
                      glVertex3f( -2000.0f, 0.0f, 2000.0f );
		      glTexCoord2f( 1.0f, 0.0f ); 
                      glVertex3f( 2000.0f, 0.0f, 2000.0f );
		      glTexCoord2f( 1.0f, 1.0f ); 
                      glVertex3f( 2000.0f, 0.0f, -2000.0f);
		      glTexCoord2f( 0.0f, 1.0f ); 
                      glVertex3f( -2000.0f, 0.0f, -2000.0f);
	        glEnd();
	glDisable( GL_TEXTURE_2D );
	glPopMatrix();

	glutSwapBuffers();
}




[Edited by - Fella on April 30, 2006 7:24:16 AM]
People of the Earth unite!
DrakeSlayer
DrakeSlayer
I don't know why it works with no glGenTextures, but I may have found your problem.

You are calling glDisable(GL_TEXTURE_2D) at the end of your render function, but I don't see any glEnable(GL_TEXTURE_2D). This would explain why you don't see your texture.
Fella
Fella
Sorry I forgot to add glEnable(GL_TEXTURE_2D) while copying and pasting the code in forum. That is not the issue. Any other clues?
People of the Earth unite!
_the_phantom_
_the_phantom_
The problem is, on loading you ask OGL to provide a texture ID, however when you come to display it you always try to bind texture ID 0 (GROUND_TEX) instead of using the one generated.
Fella
Fella
Yep that was it. Thank you.
People of the Earth unite!

Topic Locked

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

Sign in to reply to this topic.