Clarification on Texturing in OpenGL

Nibbie
Posts: 4
Joined: 2009.09
Post: #1
hi,

The following is a code snippet from the OpenGL redbook, chapter 9,
Texture Mapping.

Could someone provide a little clarification on how the glTexCoord2f
and glVertex3f work in this example? Or point me to a good explanation?


glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);

I am interested in an explanation of the correspondance between the
two forms of coordinates. Just trying to understand what the code is doing.

thanks,

Mike Ninja


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Assuming that you have the same edition of the book as I do (1.4), then look on page 361. The picture on the left is the texture with the texture coordinates (glTexCoord) that are used superimposed on top. The picture on the left is the the final drawn object using glVertex. It is a different shape than the coordinates taken from the texture so the texture must be stretched to fit.

If that didn't make sense, think of it this way, the glVertex3f gives the position of the vertex in the world, and glTexCoord tells opengl what place in the texture to pin onto it. (textures are very stretchy) In this case, the 3f on glVertex3f means that you are defining a vertex in 3D space using a float parameter, and the 2f on glTexCoord2f means that you are using a float to specify a 2D position on the texture.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.09
Post: #3
hi,

Thanks! That makes sense.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #4
Skorche Wrote:(textures are very stretchy)

LOL

That's a great quote.

- Alex Diener
Quote this message in a reply
Vertizor
Unregistered
 
Post: #5
I've only done a little bit of texture coding in OpenGL. If you didn't already know, the texture coords used in glTexCoord2f() are in a range of 0.0f and 1.0f. So if you have a 256x256 texture (that's the dimensions in pixels) 1.0f, 1.0f would be the lower right corner of the image.

I have found that: 1 / x * n gives me the right value to pass to glTexCoord2f().
x = the width or height of the texture in pixels.
n = the 2D pixel coord you want.

The result is the texture coord in OpenGL space.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
Vertizor Wrote:I've only done a little bit of texture coding in OpenGL. If you didn't already know, the texture coords used in glTexCoord2f() are in a range of 0.0f and 1.0f. So if you have a 256x256 texture (that's the dimensions in pixels) 1.0f, 1.0f would be the lower right corner of the image.

I have found that: 1 / x * n gives me the right value to pass to glTexCoord2f().
x = the width or height of the texture in pixels.
n = the 2D pixel coord you want.

The result is the texture coord in OpenGL space.

If you use the integer versions of glTexCoordi*() it will do the same thing with less effort. Very convenient for 2D graphics. However, if you want sub-pixel coordinates (not very useful in most cases) you'll still have to use your version.
Quote this message in a reply
tigakub
Unregistered
 
Post: #7
Skorche Wrote:If you use the integer versions of glTexCoordi*() it will do the same thing with less effort. Very convenient for 2D graphics. However, if you want sub-pixel coordinates (not very useful in most cases) you'll still have to use your version.

Actually, I think I read somewhere that the PowerPC does floating point just as fast as integer so I'm not sure it would actually make that much of a difference.

Also there is an extension, GL_TEXTURE_RECTANGLE_EXT, that allows you to use non-square, and non-power-of-2 textures. But in this mode, instead of using texels between 0.0 and 1.0, you actually have to specify texture coordinates. Therefore, if you were trying to map a 450 x 321 pixel texture map on to a unit square you'd have to specify

Code:
glBegin(GL_TRIANGLE_STRIP);
glTexCoordf(0,0); glVertex3f(0,0,0);
glTexCoordf(450,0); glVertex3f(1,0,0);
glTexCoordf(0,321); glVertex3f(0,1,0);
glTexCoordf(450,321); glVertex3f(1,1,0);
glEnd();
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Texturing problem in OpenGL vinodpatel2006 2 2,437 Oct 3, 2007 11:25 AM
Last Post: Fenris
  Tex Internal Formats - Clarification Jones 2 3,124 Nov 17, 2006 10:25 PM
Last Post: Jones
  SDL_GL_DEPTH_SIZE clarification... sealfin 2 4,762 Oct 25, 2006 03:51 AM
Last Post: sealfin
  Texturing with OpenGL & Cocoa (link) Taxxodium 0 2,495 Dec 2, 2004 06:31 AM
Last Post: Taxxodium
  glTexImage2D formats clarification WhatMeWorry 1 4,783 Nov 17, 2004 01:31 AM
Last Post: arekkusu