Sign in to follow this  
Christen

OpenGL Texturing 2D surfaces

Recommended Posts

Hi! I'm a newbie here so please go easy on me. Anyway, I just browsed how to create 2d surfaces on OpenGL and now my problem is, I can't seem to put textures in these surfaces. Oh, and I've used the famous "2D in OpenGL code"
int DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glEnable2D();
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2d(0, 0); glVertex2d(0, 0);
		glTexCoord2d(640, 0); glVertex2d(640,0);
		glTexCoord2d(640, 480); glVertex2d(640, 480);
		glTexCoord2d(0, 480); glVertex2d(0, 480);
	glEnd();
	glDisable2D();

	return TRUE;
}

Share this post


Link to post
Share on other sites
A few common mistakes:
  • Is your texture power-of-two sized (e.g. 256 by 64)?

  • Do you have a valid OpenGL context when you create the texture


Also, texture coordinates should be in the range [0..1] if you don't want your texture to tile.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by PnP Bios
glEnable2D is not a valid function.

but i think he has defined the function himself (someone wrote some 2d enabling code that used a function with that name some time ago)

Share this post


Link to post
Share on other sites
OK, I've solved the problem by changing the texcoords, and resizing my image to 512x512.

Anyway, is there any method to display textures without sticking to that size limitation?

Share this post


Link to post
Share on other sites
You can get around the power-of-two restrication by:
  • Using gluBuild2DMipmaps to resize your image for you

  • Resizing your image to the nearest power of 2

  • Padding your image to the nearest power of 2 and changing the texture coordinates to only reference the portion of the texture that contains your image (tiling is not longer possible in this case)

  • Using a texture rectangle extention (i.e. GL_ARB_texture_rectangle). Texture coordinates then must be changed to lie in [0..dimension] to reference the full texture instead of [0..1]

  • Using an NPOT extention (i.e. GL_ARB_texture_non_power_of_two). Texture coordinates remain normalised as [0..1] in this case

  • Using OpenGL 2.0 - requires drivers that at time of writing have not been publicly released for most (any?) graphics cards


Enigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this