OpenGL Coordinate System

Started by
3 comments, last by antiHUMANDesigns 11 years, 9 months ago
Hi All,

How does Opengl coordinate system relate to texture coordinates ?
what does affect quad and texutre coordinates calculations from the point of initilizing opengl ?
Advertisement
Well, I'm not entirely sure I've understood your question 100% but here goes for a good answer

at the basics for OpenGL when you load an image as a texture the edges for that texture are:
(0.0f,0.0f)
(0.0f,1.0f)
(1.0f,1.0f)
(1.0f,0.0f)

soo Lets say you made a QUAD like such

glBegin(GL_QUAD);
glVertex3f(-1.0f, 1.0f,0.0f); // top left
glVertex3f( 1.0f, 1.0f,0.0f); // top right
glVertex3f( 1.0f,-1.0f,0.0f); // bottom right
glVertex3f(-1.0f,-1.0f,0.0f); // bottom left
glEnd();

Lets assume you have set everything up and this is at the draw scene stage of things
that draws a 2x2 unit square relative to your view and any translation

now lets apply a texture to that quad


glTextureBind(GL_TEXTURE_2D, texture[0]) //whatever texture you've loaded into this GLuint variable
glBegin(GL_QUAD);
glVertex3f(-1.0f, 1.0f,0.0f); glTexCoord2f(0.0f,0.0f);
glVertex3f( 1.0f, 1.0f,0.0f); glTexCoord2f(1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,0.0f); glTexCoord2f(1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f); glTexCoord2f(0.0f,1.0f);
glEnd();



usually I invert the Y when loading the image thus why 1.0f in the Y coord makes it go down.

but basically you can select any portion of the texture within the 1.0-0.0 constraints and that associates that point on the texture with that point of the quad. you can also select outside the 1.0 constraints and depending on the texture settings you can have multiple images tiled onto the quad or just a smaller image that doesn't fill the whole quad.

I'm sure there are many more things but right now that's a simple run down of textures, hope this helps
The codesampler has a nice texture in the texture filtering demo that shows you how texture coordinates are oriented in both OpenGL and D3D: http://www.codesampler.com/oglsrc/oglsrc_2.htm#ogl_texture

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Notice that there is no relation between quad texture coordinates and screen coordinates. This entirely depends on how an object is transformed before it is being shown (translated, rotated, scaled). For an up-to-date description about how 3D works, based on OpenGL, see Learning Modern 3D Graphics Programming.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Hmm, I think you need to specify the texture coordinate (and normals) before the call to glVertexf, not after like Vero wrote, but I'm assume it was just a silly mistake.

There's also the texture matrix, usually used for projecting textures, but I've personally never used it because I'm not an advanced openGL user. The texture matrix is accessed the same was as projection and modelview matrices. I think the basic idea of the texture matrix is to change the way the texture coordinate system applies to quads, so it will change the idea of 0.0f to 1.0f meaning the sides/corners to meaning something else based on the state of the texture matrix.

But as for normal use, the coordinates (0.0->1.0) specify a point on a texture that should be positioned at that vertex, and then the texture is "interpolated" (may be the wrong way of seeing it) between each vertex... based on their texture coordinate. (Ye, that sounds tricky...)

But yes, a texture coordinate of 0,0 means the corner of the texture. If you want the corner of the texture to be in the corner of the quad, then that's what you need to do. But you can slide the texture over a quad by moving the texture coordinates. What happens to the parts that are outside the texture (< 0 or > 1) depends on whether you've set the texture environment to clamp or repeat the texture.

This topic is closed to new replies.

Advertisement