Jump to content
  • Advertisement
Sign in to follow this  
Thunderstrike

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);
    if(!img) 
     {
      printf("IMG_Load_RW failed: %s\n", IMG_GetError());
      return -1;
     } 
    image=img;
    framenum=rectnum;    
    for(int i=0;i<framenum;i++)
    {
        framepos=surfrects;
    }
    glGenTextures(1,&texname);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    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;
    src=spr->getsrcrect();
    image=spr->getimage();
    glBindTexture(GL_TEXTURE_2D, spr->gettexname());
           
    glBegin(GL_QUADS);
    glTexCoord2f(((float)src.x/(float)image->w),((float)src.y/(float)image->h));
    glVertex3f((float)x, (float)y, 0.0f);
    glTexCoord2f((((float)src.x+(float)src.w)/(float)image->w),((float)src.y/(float)image->h));
    glVertex3f(float(x+src.w),float(y) , 0.0f);
    glTexCoord2f((((float)src.x+(float)src.w)/(float)image->w),(((float)src.y+(float)src.h)/(float)image->h));
    glVertex3f(float(x+src.w),float(y+src.h), 0.0f);
    glTexCoord2f(((float)src.x/(float)image->w),(((float)src.y+(float)src.h)/(float)image->h));
    glVertex3f(float(x),float(y+src.h), 0.0f);
    glEnd();
}

Thanks in advance, Thunder

Share this post


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

Enigma

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.

Thunder

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!