[SDL] OpenGL Textures

Lloyd Dupont lloyd at galador.net
Mon Aug 13 00:56:00 PDT 2001


allow: post

SL_image is the right way...
you sould look at nehe tutoril lesson06:
(here is the relevant code from lesson06)
---------------------------------------------------------
SDL_Surface *LoadBMP(char *filename)
{
    Uint8 *rowhi, *rowlo;
    Uint8 *tmpbuf, tmpch;
    SDL_Surface *image;
    int i, j;

    image = SDL_LoadBMP(filename);
    if ( image == NULL ) {
        fprintf(stderr, "Unable to load %s: %s\n", filename,  
SDL_GetError());
        return(NULL);
    }

    /* GL surfaces are upsidedown and RGB, not BGR :-) */
    tmpbuf = (Uint8 *)malloc(image->pitch);
    if ( tmpbuf == NULL ) {
        fprintf(stderr, "Out of memory\n");
        return(NULL);
    }
    rowhi = (Uint8 *)image->pixels;
    rowlo = rowhi + (image->h * image->pitch) - image->pitch;
    for ( i=0; i<image->h/2; ++i ) {
        for ( j=0; j<image->w; ++j ) {
            tmpch = rowhi[j*3];
            rowhi[j*3] = rowhi[j*3+2];
            rowhi[j*3+2] = tmpch;
            tmpch = rowlo[j*3];
            rowlo[j*3] = rowlo[j*3+2];
            rowlo[j*3+2] = tmpch;
        }
        memcpy(tmpbuf, rowhi, image->pitch);
        memcpy(rowhi, rowlo, image->pitch);
        memcpy(rowlo, tmpbuf, image->pitch);
        rowhi += image->pitch;
        rowlo -= image->pitch;
    }
    free(tmpbuf);
    return(image);
}

// Load Bitmaps And Convert To Textures
void LoadGLTextures(void)
{	
    // Load Texture
    SDL_Surface *image1;

    image1 = LoadBMP("Data06/NeHe.bmp");
    if (!image1) {
        SDL_Quit();
        exit(1);
    }

    // Create Texture	
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and  
y size)

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);  
// scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);  
// scale linearly when image smalled than texture

    // 2d texture, level of detail 0 (normal), 3 components (red,  
green, blue), x size from image, y size from image,
    // border 0 (normal), rgb color data, unsigned byte data, and  
finally the data itself.
    glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->w, image1->h, 0,  
GL_RGB, GL_UNSIGNED_BYTE, image1->pixels);
};




More information about the SDL mailing list