Texture problem..

Started by
9 comments, last by Skeleton_V@T 18 years, 2 months ago
Hi, I'm having trouble with textures in opengl... I'm loading some texture files (.bmp) but the last file is used to all objects in the scene = (( here's my loadtexture code

void POpenGLDriver::LoadTexture(std::string AFileName, unsigned int &ATextureBuffer, int ATextureType)
{
  AUX_RGBImageRec *TextureImage = null;

  if (TextureImage = LoadBMPRec(AFileName))
  {
    glGenTextures(1, &ATextureBuffer);
    glBindTexture(GL_TEXTURE_2D, ATextureBuffer);
		
    switch (ATextureType)
    {
      case TT_TEXT:
      {
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	    
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
      }break;
      case TT_RECT:
      {
	glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      }break;
    }
  }
  
  if (TextureImage)
    if (TextureImage->data)
      free(TextureImage->data);
  
  free(TextureImage);
}

any suggestion? Thanx...
Advertisement
Some possible causes..
Are you sending the same ATextureBuffer for both textures??
Are you binding the corresponding texture when rendering?
Hi, thanx...but..

which methods of opengl I need to call everytime I draw the objects?

if I call my LoadTexture method every time, it works..

but...I think that I don't need to load the bitmap everytime...right?

so...which methods?

thanx
You need to call glTexImage*D () and the like just once, after saving the Texture ID returned by glGenTextures (), each time you want to use the texture, you call glBindTexture () with the desired texture ID. Also note these things:

  • Don't forget to call glDeleteTextures () after having done with the texture, preferably at the uninitialize phase of the program.
  • The glBindTexture () appears from OpenGL 1.1, but don't worry about it, almost every modern graphics card support it.
  • With OpenGL 1.0, you will need to call glTexImage*D () each time you want to use the new texture, this makes texture switching expensive. glBindTexture () has largely reduced the overhead, but try not to manipulate the OpenGL states unnecessarily multiple times as well, it has overhead too.
  • --> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
    You need to call glBindTexture() every time you want to draw an object with a different textures.

    If you do not call this function ,every object will stay bind to the last texture loaded because it only "unbind" when you call the glBindTexture again.

    Why are are you freeing the textures in your load method???


    = (((

    first...thanx to everybody...

    I have tested my code in two video cards:

    Geforce fx5200 128mb
    Mobile intel 915GM/GMS (notebook =P)

    both doesn't work..

    look my codes...

    where is my mistake???

    this is the code that loads my texture and keep the ID into a varible ATextureBuffer

    void POpenGLDriver::LoadTexture(std::string AFileName, unsigned int &ATextureBuffer, int ATextureType){  AUX_RGBImageRec *TextureImage = null;  if (TextureImage = LoadBMPRec(AFileName))  {    glGenTextures(1, &ATextureBuffer);        glBindTexture(GL_TEXTURE_2D, ATextureBuffer);		    switch (ATextureType)    {      case TT_TEXT:      {	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);		    	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);      }break;      case TT_RECT:      {	glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);					glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);      }break;    }  }    if (TextureImage)    if (TextureImage->data)      free(TextureImage->data);		  free(TextureImage);}


    this code is call every time I draw the objects

    void POpenGLDriver::BindTexture(unsigned int ATextureBuffer){  glPushAttrib(GL_TEXTURE_BIT);    glEnable(GL_TEXTURE_2D);    glBindTexture(GL_TEXTURE_2D, ATextureBuffer);  glPopAttrib();}


    it doesn't work = ((

    my class have an unsigned int FTexture variable..that stores the TextureID, the texture ID is right when I debug this last method, but only the last texture file loaded matters = (

    Help me please!!!!
    Remove the glPushAttrib(GL_TEXTURE_BIT) and glPopAttrib() calls from around the binding of the texture in your BindTexture routine. That interferes with the setting of the texture.

    hth
    F451
    Quote:void POpenGLDriver::BindTexture(unsigned int ATextureBuffer)
    {
    glPushAttrib(GL_TEXTURE_BIT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, ATextureBuffer);
    glPopAttrib();
    }

    Your BindTexture () method has done a good job, but it is probably not what you intended: It saves the current texture mapping state, set a new state then restore it back to the original state then return. In other words, your method does nothing useful.

    glBindTexture(GL_TEXTURE_2D, ATextureBuffer);
    You will need this line each time you want to draw a geometry using a new texture object, the line glEnable () should be called when necessary (this is not the case as you never disable 2D texture mapping in your program (yet), so it need to be called just once somewhere in the initialization phase).

    I'm reviewing your texture creation code now.
    --> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
    yeahhh!!

    thanx to everyone..

    I just removed the glPushAttrib and it works great = D

    thanx a lot = D
    one more doubt..

    How can I repeat a texture inside a Rect???

    currently, I'm stretching it...

    thanx

    This topic is closed to new replies.

    Advertisement