Best way to modify incorrect textures?

Started by
7 comments, last by Drew_Benton 18 years, 7 months ago
Ok, so I'm using this tutorial on using SDL_Surface's as OpenGL textures. Everything works fine and dandy except for the fact that the texture is mirrored and flipped. They suggest to simply add in this code:

//change to texture matrix and do the trick
glMatrixMode(GL_TEXTURE);
glRotatef(180.0f,0.0f,0.0f,1.0f);
glScalef(-1.0f,1.0f,1.0f);
While that works great for that, I am thinking if that is the best solution in the long run (which I don't think it is). If I were to use a regular OpenGL texture, then it would then be messed up and then all chaos will break lose. Now since I am using SDL, I'm thinking I'll pretty much only use the IMG_Load function (SDL_Image) and this technique of converting from SDL_Surface to an OpenGL texture all thoughout, but I still need to take into account the case of if I were to need to not use this technique later on. Right now I'm thinking my two main choices are either to use the OpenGL code to mirror and flip, or I can add in code to my texture loading function to mirror and flip the image then and have it all correct. Of coursethe problem then is all the extra overhead and resource comsumption if a lot of textures are used. So you can see my delima. Does anyone have any other possible suggestions/ideas as to what I can do to remedy this? Thanks for your time.
Advertisement
FLIP IT WHEN U LOAD IT
dont know if IMG_Load function (SDL_Image) can do this or not (check the header)
else u can flip the texture coordinates
FLIP IT WHEN U LOAD IT
dont know if IMG_Load function (SDL_Image) can do this or not (check the header)
else u can flip the texture coordinates
Like zedzeek says, the best way would be to mirror and flip your pixel data during loading of images. It's far more efficient that altering the texture matrix each time you want to flip your textures.

Another soultion could be - simply - changing texture coordinates of a primitive you are rendering.

Hope it helps!
___Quote:Know where basis goes, know where rest goes.
Clapton has the right idea, I think. As it sounds to me - the thing that is causing your problem is that in SDL, the positive y-axis is downwards, ie 0,0 is at the top left of your screen. In OpenGL, (0,0) is the bottom left. y-axis is positive in the upward direction. So the texture coordinate (0,0) is the bottom left of the texture. Are you sure the texture coords are alright?
Quote:Original post by deavik
Are you sure the texture coords are alright?


Well, I was just using NeHe code from texturing a cube, but on the tutorial site, it tells that the texture will be mirrored and flipped. But just to show you guys:
glBegin(GL_QUADS);			// Front Face			glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad			glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad			glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad			glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad			// Back Face			glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad			glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad			glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad			glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad			// Top Face			glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad			glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad			glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad			glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad			// Bottom Face			glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad			glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad			glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad			glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad			// Right face			glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad			glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad			glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad			glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad			// Left Face			glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad			glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad			glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad			glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad		glEnd();


Quote:Original post by clapton
Another soultion could be - simply - changing texture coordinates of a primitive you are rendering.


Eeek! I don't think I'd want to do that since my overall goal is not to have to do any modifications outside of normal OpenGL operations. Thanks for that suggestion though.

I guess I will end up just fixing the texture data when I load it. I'll post some code of that routine when I've got it done to see what you guys can say about it after it's done. I have a feeling it may require too much processing for what it's going to do, but I guess we will see! Thanks for the advice everyone! I will post again when the other function is done.
Well, that went a lot easier than I thought it would. It took me less than 1 minute to fix this problem. It so happens that I took a look at some old code I posted on GameDev about mirroring in SDL. I just called the MirrorSurfaceX function, and everything is working great! Thanks all for the advice, this is a lot more convient then switching the OpenGL texture rendering code.
It would be more interesting if you looked into what is causing the problem. This could either be a borked SDL function for loading the image (some formats have some bits that specify the origin of the axes) or you have wrong texture coordinates.

Basically try loading the same image with 2 or 3 different libraries and displaying it with OpenGL. For example TGAs have a bit that specifies the orientation, maybe other formats do too, and the loader doesn't check it.

Another thing, IIRC, is that BMPs are stored upside down and usually need to be inverted. Again you could both write your own loader and use several libraries.
Quote:Original post by Ilici


Oh yea, I should take a look into what was causing the problem. It seems to be a problem with any images, tga, jpg, bmp all alike, when I use the pixel data from a SDL surface into the buildmipmaps function, it causes it to be all wrong. I'll try to do some more research into how the SDL_Surface pixels are held, but for now everything is working great. I have used other texturing functions that work fine with other libraries (well not using pure SDL though) and it's worked fine, but I wanted a SDL specific solution that would not require adding in other stuff outside of the SDL realm [wink]

I've finally made a single function that loads an image into an OpenGL texture. It can easily be modified if you have a SDL_Surface and want to use it as a texture as well. I've put the snippet on ym webpage, but here it is as well:

// Function that will load an image file and create an OpenGL texture from itbool LoadTexture( const char* fileName, GLuint *texture ){	// Load the image file	#ifdef _SDL_IMAGE_H		// Use SDL_Image if avaliable		SDL_Surface* imgFile1 = IMG_Load(fileName);	#else		// Otherwise use the standard SDL BMP loading		SDL_Surface* imgFile1 = BMP_Load(fileName);	#endif	// If the image could not be loaded	if( !imgFile1 )		return false;	// This is the final image	SDL_Surface* imgFile = SDL_CreateRGBSurface( SDL_HWSURFACE, imgFile1->w, imgFile1->h, imgFile1->format->BitsPerPixel, imgFile1->format->Rmask, imgFile1->format->Gmask, imgFile1->format->Bmask, imgFile1->format->Amask);	// If the final image's space could not be allcoated	if( !imgFile )		return false;	// Loop though and fix the pixels	for( int y = 0; y < imgFile1->h; y++ )	{		for( int x = 0; x < imgFile1->w; x++ )		{			// Don't worry too much about trying to understand all of what follows, just know it works ;)			// This is the code that gets a pixel on a surface			Uint32 curPixel = 0;			int FinalX = x;			int FinalY = imgFile1->h - y - 1;			// Store the surface BPP			int bpp = imgFile1->format->BytesPerPixel;			// Here p is the address to the curPixel we want to retrieve			Uint8 *p = (Uint8 *)imgFile1->pixels + FinalY * imgFile1->pitch + FinalX * bpp;			switch(bpp) 			{				case 1:					curPixel = (*p);				case 2:					curPixel = (*(Uint16 *)p);				case 3:					if(SDL_BYTEORDER == SDL_BIG_ENDIAN)						curPixel = (p[0] << 16 | p[1] << 8 | p[2]);					else						curPixel = (p[0] | p[1] << 8 | p[2] << 16);				case 4:					curPixel = (*(Uint32 *)p);			}			// This is the code that will draw pixels			Uint8 *ubuff8;			Uint16 *ubuff16;			Uint32 *ubuff32;			Uint32 color = curPixel;			char c1, c2, c3;			// Lock the imgFile, if needed			if(SDL_MUSTLOCK(imgFile)) 			{				if(SDL_LockSurface(imgFile) < 0) 					return false;			}			// How we draw the pixel depends on the bitdepth			switch(imgFile->format->BytesPerPixel) 			{				case 1: 					ubuff8 = (Uint8*) imgFile->pixels;					ubuff8 += (y * imgFile->pitch) + x; 					*ubuff8 = (Uint8) color;				break;				case 2:					ubuff8 = (Uint8*) imgFile->pixels;					ubuff8 += (y * imgFile->pitch) + (x*2);					ubuff16 = (Uint16*) ubuff8;					*ubuff16 = (Uint16) color; 				break;  				case 3:					ubuff8 = (Uint8*) imgFile->pixels;					ubuff8 += (y * imgFile->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*) imgFile->pixels;					ubuff8 += (y*imgFile->pitch) + (x*4);					ubuff32 = (Uint32*)ubuff8;					*ubuff32 = color;				break;				default:					fprintf(stderr, "Error: Unknown bitdepth!\n");					return false;			}			/* Unlock the imgFile if needed */			if(SDL_MUSTLOCK(imgFile)) 				SDL_UnlockSurface(imgFile);		}	}	// Free this temporary surface	SDL_FreeSurface( imgFile1 );	// Create the texture	glGenTextures( 1, texture );	// Bind the texture	glBindTexture( GL_TEXTURE_2D, *texture );	// Build the texture now	gluBuild2DMipmaps( GL_TEXTURE_2D, 3, imgFile->w, imgFile->h, GL_BGR, GL_UNSIGNED_BYTE, imgFile->pixels );	// Free the final image since we are done with it now	SDL_FreeSurface( imgFile );	// Return success	return true;}


I'm sure it's just a matter of how the data is stored in the SDL_Surface and how it should be for the mipmaps though. Thanks for the time! If I do find out what the problem was and have some links that explain better, I'll be sure to post back here.

This topic is closed to new replies.

Advertisement