Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Help needed with texturing

This topic is 4850 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've got some trouble with OpenGL recently. I am writing a Tetris clone, and it is working fine with Direct3D and SDL rendering. However, when using OpenGL, the _last_ loaded texture is not displayed. Its containing rectangle is there, but pure white. I tried changing the order, but every time, the texture in the last "loadfile" line is missing. FYI, texname and image are properties of the sprite_gl class. The loading routine:
int sprite_gl::loadfile(char *picfile, SDL_Rect *surfrects, int rectnum)
    SDL_Surface *img=IMG_Load(picfile);
      printf("IMG_Load_RW failed: %s\n", IMG_GetError());
      return -1;
    for(int i=0;i<framenum;i++)
    glBindTexture(GL_TEXTURE_2D, &texname);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
    return 0;

The drawing routine:
int blitter_gl::draw(sprite *spr, int x, int y)
    SDL_Rect src,dest;
    SDL_Surface *image;
    glBindTexture(GL_TEXTURE_2D, spr->gettexname());
    glVertex3f((float)x, (float)y, 0.0f);
    glVertex3f(float(x+src.w),float(y) , 0.0f);
    glVertex3f(float(x+src.w),float(y+src.h), 0.0f);
    glVertex3f(float(x),float(y+src.h), 0.0f);

Thanks in advance, Thunder

Share this post

Link to post
Share on other sites
glTexParameter{if} affects the currently bound texture object. The first time a texture name is bound using glBindTexture it acquires the default texture state. The default texture state for GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. So each time you load a texture you apply a state change to the previously bound texture object (the default texture in the case of your first loaded texture) and then create a new texture object (via glBindTexture) with default state. So you leave the default texture and all but the last of your textures with state GL_TEXTURE_MIN_FILTER set to GL_NEAREST, while your last loaded texture retains the default setting of GL_NEAREST_MIPMAP_LINEAR. Since your texture is not mipmap complete the result is as if texturing were disabled for that texture object.

To solve the problem simply move glBindTexture(GL_TEXTURE_2D, texname); above your glTexParameteri calls. (Btw. I assume the & in that line is a typo?).


Share this post

Link to post
Share on other sites
Yes, the ampersand was a typo, I've removed several lines from the source to only post the main part; it somehow got there :)

Thanks very much for the advice, I would've had a really hard time figuring that out.


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!