OpenGL Texture in relation to GL orientation, and... glOrtho help.

Jones
Unregistered
 
Post: #1
I've noticed that when grabbing texture tops, bottoms left and rights before vertex drawing operations, most example code I've seen uses measurements like this (0.0, 1.0) to grab, in this case (<-) the top left. And yet, when drawing (were talking about pixel perfect mode) they use pixel values, like (0, 240) for the top left (for example). I know that glOrtho sets up that stuff, but does it not apply to textures?

Not only that, but I know glOrtho defines where top left, top right (etc etc) are, but in relation to what? Annoyed

In accordance with my first question, this is what I mean:

Code:
glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex2i(0,   0);
    glTexCoord2i(0, 1); glVertex2i(0,   480);
    glTexCoord2i(1, 1); glVertex2i(640, 480);
    glTexCoord2i(1, 0); glVertex2i(640, 0);
    glEnd();

Thanks!
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
It sounds like your confusion is coming from the fact that you're working with multiple coordinate systems. Texture space is, in general, a completely separate coordinate system from vertex space.

When using GL_TEXTURE_2D, {0, 0} represents the lower left corner of the texture image, and {1, 1} represents the upper right corner of it. Coordinates outside of this range will give different results depending on your GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T settings. You can use the GL_TEXTURE matrix mode to alter your texture space coordinate system if you wish.

The GL_PROJECTION and GL_MODELVIEW matrices affect your vertex space coordinate system. You can think of glOrtho as defining the coordinates at the edges of your viewport.

Specifying a texture coordinate for a vertex tells OpenGL to use look up the color at that position in the texture image, and use it as the vertex's color. When you specify different texture coordinates for multiple vertices in the same primitive, the color lookup position is interpolated over the texture image between the specified vertices.

To give you a physical analogy, think of a texture image as a stretchy material. When you apply it to a triangle, you pick up the texture by the specified texture coordinates, and stretch it over the triangle. Each point in the texture image specified by the texture coordinates goes directly over its corresponding vertex.
Quote this message in a reply
Jones
Unregistered
 
Post: #3
So, for example, if I have a texture that's 100 * 100, and I draw (in pixel perfect mode) with a texture point of 50,50 I'm starting with the middle of the image?

EDIT: And how is the viewport defined by glViewport, different from what glOrtho does? :/
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
Well, for one thing, if you have a texture image that's 100 * 100, you won't be able to use it with GL_TEXTURE_2D. Smile

If you're using GL_TEXTURE_2D and don't mess with the texture matrix, texture coordinates run from 0 to 1 on both axes regardless of the dimensions of your texture image. {0.5, 0.5} on a 128 * 64 image is the pixel at {64, 32}. As I understand, with GL_TEXTURE_RECTANGLE_EXT, texture coordinates are specified in pixels, but I haven't used the extension myself, so I'm not 100% sure on that. I also don't know how the texture matrix affects it.

The viewport is simply the rectangular portion of the window or screen in which your OpenGL context lives, relative to pixels on your screen. The projection matrix (commonly set up with glOrtho, gluOrtho2D, glFrustum, or gluPerspective) is the coordinate system relative to your viewport. The modelview matrix (commonly altered with glTranslate, glRotate, glScale, etc.) is the coordinate system relative to your projection. The texture matrix is the coordinate system for texture images.

This isn't completely accurate, but for the purpose of this discussion, you can think of a hierarchical coordinate system that looks something like this:

Viewport
Projection
Modelview transformation
-------
Texture

Each coordinate system is relative to the one before it. Texture is separated because it's not directly relative to the other three; it's associated to vertices in the modelview coordinate system by glTexCoord calls.

I hope that made at least some amount of sense... OpenGL experts above my level please correct me if I'm posting misinformation.
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #5
Edit: I was too slow posting this…

No. The dimension of your image in pixels doesn't matter. 1.0 is always top or right of the texture. To start with the middle of the texture, you'd use 0.5. That, at least, is valid for GL_TEXTURE_2D. If you prefer pixel coordinates, you use GL_TEXTURE_RECTANGLE, then you specify the coordinates as pixel coordinates. It also allows you to use textures with any size.

glViewport defines what part of the window or drawing area you are drawing in. It is useful for cases where you want to draw two different scenes side by side, for example, or any other case where you feel like you don't need all the pixels you have available. Usually, you set this to just the size of the drawing area and forget about it.

glOrtho is very different, it specifies how your model coordinates are mapped to window coordinates. Actually, it is more complicated than this, but if you set the glViewport in a usual way, you need not care about this. The top, left, right and bottom values you specify are in relation to the origin of the scene (the point where all coordinates are 0). You specify with top how far the y axis goes up, with left how far the x axis goes to the left, with right how far it goes to the right and with bottom how far the y axis goes down. A point that has the coordinates (top, right) will be at the top right corner of your OpenGL drawing area, and one with (- bottom, - left) will be at the bottom left corner.
Quote this message in a reply
Jones
Unregistered
 
Post: #6
Oh I see now. It always helps me when somebody tells me how a command applies to the video games I see every day. In this case, glViewport could be for making a splitscreen on a two player game or something.

Thank you both!
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
Cochrane Wrote:glViewport defines what part of the window or drawing area you are drawing in.
Technically it does not. The scissor rect defines that.

To recap all of this, it goes:

[Your vertices] * model view projection matrix = [Your vertices] in normalized device coordinates (-1 to 1 unit cube.)

Now clipping is performed in normalized coordinates.

[Clipped NDC vertices] * viewport scale = [Your clipped vertices] in window coordinates.

Then during rasterization, the scissor rect determines what area to draw. The viewport rect does not scissor, it just scales.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Texture Masking airfire 6 14,706 Mar 17, 2014 07:07 PM
Last Post: baioses
  OpenGL ES Texture Compression ajrs84 9 4,081 May 7, 2013 03:36 PM
Last Post: ajrs84
  OpenGL ES Texture Masking dalasjoe sin 0 3,833 Apr 13, 2012 12:17 AM
Last Post: dalasjoe sin
  Reflect a vector in relation to another vector ipeku 4 5,251 Dec 12, 2011 07:17 AM
Last Post: ipeku
  Texture in OpenGL ES 2 looks pixelated vunterslaush 18 23,079 Aug 30, 2011 09:44 PM
Last Post: Frogblast