Jump to content
  • Advertisement
Sign in to follow this  
vHaB

OpenGL textures upside down and reversed?

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

ATTN MODS: not sure if this is the right forum. By all means move it if it's not. I am using SDLs image loading routines to load textures, which I am then converting to OpenGL textures. I have two problems when they are drawn on the screen. 1 - They are upside down and mirrored. 2 - They are appearing twice the size as the actual texture image on disk. So if it's 64x64, it's showing up as 128x128 on screen. The "game" I'm working on here is actually 2D, so I'm not doing any weird transformations at all. I'm just testing the sprite engine, so the rendering code is really simplistic. Here's the code I'm using to convert from SDL Surface to Open GL texture:
SDL_Surface *glPixels = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y, 32, rmask, gmask, bmask, amask);
			SDL_SetAlpha(temp, 0, 0);
			if(r > 0)
				SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));
			SDL_BlitSurface(temp, NULL, glPixels, NULL);

			//convert to OpenGL
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
			glGenTextures(1, &texName);
			glBindTexture(GL_TEXTURE_2D, texName);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glPixels->w, glPixels->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, glPixels->pixels);
and here is the code I'm drawing it to the screen with:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, anim.frames[curFrame].texture);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0);  glVertex2i(64, 64);
		glTexCoord2f(0.0, 1.0);  glVertex2i(64, 128);
		glTexCoord2f(1.0, 1.0);  glVertex2i(128, 128);
		glTexCoord2f(1.0, 0.0);  glVertex2i(128, 64);
	glEnd();
	glDisable(GL_TEXTURE_2D);

	SDL_GL_SwapBuffers();
Suggestions are greatly appreciated. If this needs to be moved to Alternative Game Libs, that's fine, but when I was SDL only, I never had this problem, so I am assuming it's OpenGL related. Some difference in the way each lib stores pixel data or something?

Share this post


Link to post
Share on other sites
Advertisement
Alright... I'm not THAT familiar with OpenGL, but I think it's the order in which you're drawing those quad vertices.

If I remember correctly, you should be drawing them in counter-clockwise order. So, with those coordinates, try this instead:

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2i(64, 64);
glTexCoord2f(1.0, 0.0); glVertex2i(128, 64);
glTexCoord2f(1.0, 1.0); glVertex2i(128, 128);
glTexCoord2f(0.0, 1.0); glVertex2i(64, 128);
glEnd();

Share this post


Link to post
Share on other sites
This should work:

glTexCoord2f(0.0, 1.0); glVertex2i(64, 64);
glTexCoord2f(0.0, 0.0); glVertex2i(64, 128);
glTexCoord2f(1.0, 0.0); glVertex2i(128, 128);
glTexCoord2f(1.0, 1.0); glVertex2i(128, 64);

simply swap the texture's y coordinate...

Share this post


Link to post
Share on other sites
Quote:
Original post by master of void
This should work:

glTexCoord2f(0.0, 1.0); glVertex2i(64, 64);
glTexCoord2f(0.0, 0.0); glVertex2i(64, 128);
glTexCoord2f(1.0, 0.0); glVertex2i(128, 128);
glTexCoord2f(1.0, 1.0); glVertex2i(128, 64);

simply swap the texture's y coordinate...


That fixed it. Thanks.

Just realized the reason for it being double sized is because I made the quad that big. Oops.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!