Skip to main content
gamedev.net
🔒 Locked

Help needed with texturing

Started by Thunderstrike Aug 5, 2005 at 10:00 AM 1 replies 650+ views
Original Post
Thunderstrike
Thunderstrike
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
Enigma
Enigma
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
Thunderstrike
Thunderstrike
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.