# Inverting textures[solved, but discussion open]

This topic is 4254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The textures I load are appearing inverted ( 180 degree counter-clockwise rotation about Y axis, to be more specific). Why does it happen? What can I do about it? Any help is welcome. [Edited by - horizon981 on June 30, 2006 6:57:50 AM]

##### Share on other sites
Are you using the correct texture co-ords? Some code would be nice. [smile]

##### Share on other sites
Are you using a library to load your textures? Some libs require you to set the default orientation, or else they appear upside down.

##### Share on other sites
Try

glMatrixMode(GL_TEXTURE);glScalef(1.0f, -1.0f, 1.0f);glMatrixMode(GL_MODELVIEW);

##### Share on other sites
not sure but try this

for (int i=0;i<Size/2;i++)
swap(image,image[(Size-1)-i]);

##### Share on other sites
THANK YOU MARS_999!

##### Share on other sites
Quote:
 Original post by horizon981THANK YOU MARS_999!

Are you doing anything to the texture matrix at any other point?

If not, then that's probably not the original problem, and when you have textures that load properly, they'll appear inverted.

I reccommend that you give us some code and further explain the problem so you can fix the root of the cause instead of fixing the problem that results from the problem.

##### Share on other sites
Here's the code
static void draw_screen( void ){    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();        glColor3f(1.0f,1.0f,1.0f);    glTranslatef(0.0f,0.0f,-2.0f);               glBegin( GL_QUADS );     glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);     glTexCoord2d(1.0,0.0); glVertex2d(1.0,0.0);     glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);     glTexCoord2d(0.0,1.0); glVertex2d(0.0,1.0);    glEnd();     SDL_GL_SwapBuffers( );}

Here's the function to load the bitmaps:

void LoadTextures(){  glGenTextures( 1, &tex_floor );  glBindTexture( GL_TEXTURE_2D, tex_floor );    SDL_Surface *surf = SDL_LoadBMP( "grass.bmp" );  SDL_LockSurface( surf );  glTexImage2D( GL_TEXTURE_2D, 0, 3, surf->w, surf->h, 0, GL_BGR,       GL_UNSIGNED_BYTE, surf->pixels );  SDL_UnlockSurface( surf );  SDL_FreeSurface( surf );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );  glBindTexture( GL_TEXTURE_2D, tex_floor );  }

and to make it work properly, I have to add:

static void setup_opengl( int width, int height ){    float ratio = (float) width / (float) height;    /* Our shading model--Gouraud (smooth). */    glShadeModel( GL_SMOOTH );    /* Culling. */    glCullFace( GL_BACK );    glFrontFace( GL_CCW );    glEnable( GL_CULL_FACE );    glEnable( GL_DEPTH_TEST );    glEnable(GL_TEXTURE_2D);    /* Set the clear color. */    glClearColor( 0, 0, 0, 0 );    /* Setup our viewport. */    glViewport( 0, 0, width, height );        ///////////// Texture Inversion ///////////////    glMatrixMode(GL_TEXTURE);    glScalef(1.0f, -1.0f, 1.0f);    glMatrixMode(GL_MODELVIEW);   -------- -------- etc etc.}

Yes, im using SDL as nothing else has worked so far.
If i don't manipulate the Texture Matrix (swear to GOD I didn't know it existed), the textures appear inverted. Don't go by the name "grass.bmp", it actually has my photo and name on it : that's how I can tell if it is inverted. :)
Does this help?

##### Share on other sites
I wouldn't be surprised if SDL gives you an image stored top down. Meaning, top row first, and then second row, and so on. OpenGL assumes the image passed to be stored bottom up, so to solve the problem (scaling the texture matrix is not a solution, it's a way around the problem) you need to flip the image before calling glTexImage.

I suggest you look into some library that gives you more control over the images being loaded. You should make sure the images are loaded bottom up, because that's the game OpenGL wants to play, and you should play it too. Maybe DevIL is a better solution to loading images.

And you thought the problem was solved just because it happened to look right? Reminds me of a recent post at these forums. Clicky [rolleyes]

##### Share on other sites
I tried DevIL, but it gave me linker errors. I remember having read in its documentation that it requires your OS to be in C: drive, otherwise some really scary settings had to be configured. Although I liked DevIL a lot (documentation, support, neatness), I left it for that reason. I posted it on Sourceforge forums but nobody replied. I posted it here(Gamedev)
http://www.gamedev.net/community/forums/topic.asp?topic_id=399340
but no solution came. By the way, all the DLLs and library files had been properly added.

See if you can help. I was really frustrated with DevIL, but if you tell me it doesn't need the OS to be in C:, i willing to give it one more try. ;)

##### Share on other sites
Please let me know if I'm wrong, but in OpenGL, don't the texture coordinates start at (0,0) in the bottom right corner of the image? So that would make the top left corner of the image have the coords (1,1).

I believe that could be the problem.

Again, I'm not sure, but that was how I set up my cube, and the textures on all sides are fine.

Edit:: Hmmm, perhaps not. I just tested your stuff and the tex coords are fine.

[Edited by - Endar on June 30, 2006 9:19:23 AM]

##### Share on other sites
Quote:
 Original post by EndarEdit:: Hmmm, perhaps not. I just tested your stuff and the tex coords are fine.

Did you try that on SDL?

##### Share on other sites
No, I don't have SDL. Anyway, the only thing that I can see that SDL would have an effect on is how the bitmap was loaded, and since hundreds of people use SDL every day, I'm assuming that the loading function is fine.