Jump to content
  • Advertisement
Sign in to follow this  
Fella

OpenGL glGenTextures() problem

This topic is 4493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!