Results 1 to 6 of 6

Thread: glOrthof and coordinates.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    21

    glOrthof and coordinates.

    I am having trouble understanding something.

    I am working through several different tutorials. My favorite right now is NeHe. I am working through the 1-5 tutorials.

    THe problem I'm experiencing is around the coordinates provided for the shapes. All the examples show vertices with 0.0, 1.0 values etc.

    The only way I can get my polygons to show up is to use the actual coordinates of my view. My view is 320/480. So, I'm using 50, 75, etc. If I use translate, I can move the shape. I understand that. However, the sizes of my shapes are very small if I use the coordinates in the examples.

    Anyone know what I'm doing wrong...



    All the examples draw a polygon like this:
    glBegin(GL_TRIANGLES);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();

    Because, I'm learning on the iphone with OpenGL ES, I've translated this to:

    const GLfloat kTriangleVertices []= {
    0.0f, 0.0f, 0.0f,
    spriteWidth / 2, spriteHeight, 0.0f,
    spriteWidth, 0.0f, 0.0f
    };

    glEnableClientState (GL_VERTEX_ARRAY);
    glVertexPointer (3, GL_FLOAT , 0, kTriangleVertices);
    glDrawArrays (GL_TRIANGLES, 0, 3);

    I had to init my view with code like this before I could get anything:

    glViewport(0, 0, backingWidth, backingHeight);

    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity();
    glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity();

  2. #2
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    7

    Re: glOrthof and coordinates.

    The interesting point here is that the glOrtho command specifies the region that will be mapped on the viewport of your screen.

    "glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity();
    glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);"

    This will map all vertices whose x and y coordinate are within [0, width] and [0,height], respectively. The vertices that are out of these ranges are just clipped to the borders. Because you are using the same width and height of your viewport, the vertices won't be scaled when they are mapped.

    "glBegin(GL_TRIANGLES);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();"

    Since the size of this triangle is very small compared to your viewport (320x480 pixels), you will notice just a point on the screen. If you try it with glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f), the triangle will be shown largely, because its vertices will be mapped exactly on the viewport borders.

    You can read more about projection and viewport transformations in the red book.
    http://www.glprogramming.com/red/chapter03.html

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    21

    Re: glOrthof and coordinates.

    I suspected it was related to my glOrthof call.

    What happens if you do not make a call to glOrthof and you just set the viewport to the size of the window? The examples seem to take that approach unless I'm missing a key line.

    I can't get anything to show on my screen w/o a glOrthof call.

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    7

    Re: glOrthof and coordinates.

    It might be that the default projection matrix is the identitiy matrix, but I wouldn't rely on it. You should explicitly set it like this:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    This equals a orthogonal projection such as
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

    With a identity matrix for projection the vertices within the range [-1.0f, 1.0f] will be mapped on your viewport and will be automatically scaled. You will find the exact transformation matrix in the OpenGL Specs, e.g. section "2.11.2 Matrices" in http://www.opengl.org/documentation/...0/glspec20.pdf

  5. #5
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: glOrthof and coordinates.

    tkunert, after calling:
    glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

    the projection matrix is not the identity matrix though apparently, it does not change anything in the rendering. The matrix is:

    [[ 1 0 0 0]
    [ 0 1 0 0]
    [ 0 0 -1 0]
    [ 0 0 0 1]]

    So, using the identity will just affect the z coordinates that will not be reverted.

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    7

    Re: glOrthof and coordinates.

    dietozeun, you're right.

    The command that equals the identity matrix is then
    glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);

Similar Threads

  1. Texture Coordinates and Depth Buffer to obtain world coordinates
    By snkhan42 in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 04-27-2018, 11:52 AM
  2. Replies: 4
    Last Post: 06-08-2016, 07:01 PM
  3. Explanation of glOrthof()
    By Blakeasd in forum OpenGL ES
    Replies: 1
    Last Post: 12-30-2012, 08:30 PM
  4. Replies: 2
    Last Post: 06-02-2012, 08:10 AM
  5. Problems mapping 2d window coordinates to 3d system coordinates
    By stefboombastic in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 01-04-2003, 01:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean