Jump to content
  • Advertisement
Sign in to follow this  
Kerkhoff

OpenGL Texture problem..

This topic is 4591 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

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...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Some possible causes..
Are you sending the same ATextureBuffer for both textures??
Are you binding the corresponding texture when rendering?

Share this post


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

Share this post


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

    Share this post


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


    Share this post


    Link to post
    Share on other sites
    = (((

    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!!!!

    Share this post


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

    Share this post


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

    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!