Skip to main content
gamedev.net
🔒 Locked

Texturing 2D surfaces

Started by Christen Nov 30, 2004 at 9:39 AM 5 replies 750+ views
Original Post
Christen
Christen
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;
}

Enigma
Enigma
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
CoreMeltdown
CoreMeltdown
Texture images are mapped in the range [0..1].
So, your glTexCoord2d(0, 640) must be glTexCoord2d(0.0, 1.0);
master of void
master of void
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)
Christen
Christen
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?
Enigma
Enigma
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.