• Advertisement
Sign in to follow this  

SDL/OpenGL texture problem

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

so i'm working on loading 3D models from a text file using SDL/OpenGL. the problem is SDL_LoadBMP() flips the image so when I draw it using the texture coordinates it's upside down. I'm trying to figure out how to invert the texture coordinates as well but it's not coming out right. to make matters worse some of the texture coordinates are less than 0 or greater then 1 because the model was made using a modeling program.

Share this post


Link to post
Share on other sites
Advertisement
I would highly reccomend using an OpenGL based means of texture loading abilities instead of using your SDL_Surfaces, but if you still want to, here is some of my code/other code that does this:


// Created from Docs/Cone3D's code
void SetPixelU32(SDL_Surface *screen, int x, int y, Uint32 newcolor)
{
Uint8 *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color = newcolor;
char c1, c2, c3;

/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0)
return;
}

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x;
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);

if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{

c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
}
else
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen))
{
SDL_UnlockSurface(screen);
}
}

Uint32 GetPixelU32(SDL_Surface* surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
// Mine
SDL_Surface *MirrorSurfaceX( SDL_Surface *screen )
{
SDL_Surface* newimg = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);

for( int y = 0; y < screen->h; y++ )
{
for( int x = 0; x < screen->w; x++ )
{
SetPixelU32( newimg, x, y, GetPixelU32( screen, x, screen->h - y - 1 ) );
}
}

return newimg;
}

SDL_Surface *MirrorSurfaceY( SDL_Surface *screen )
{
SDL_Surface* newimg = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);

for( int y = 0; y < screen->h; y++ )
{
for( int x = 0; x < screen->w; x++ )
{
SetPixelU32( newimg, x, y, GetPixelU32( screen, screen->w - x - 1, y ) );
}
}

return newimg;
}



This is a commnon issue, so it's not you, what you can do is this:

// Your existing image that needs to be flipped
SDL_Surface* yourImage;

// Flip the image the correct way
SDL_Surface* finalImage = MirrorSurfaceX(yourImage);

// Your image is no longer needed
SDL_FreeSurface(yourImage);

Then you use the finalImage's pixels when you pass to the OpenGl function you use to create a texture from pixels. I'll also note that there's another method as shown here, but that will mess up any textures that you do not make with SDL, which if you don't use any, then you might want to use that instead. Use that to flip the surface, then see if you have other problems from the texture.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement