Search code examples
openglglut

How Does OpenGL's Coordinate System Work?


this is probably a pretty basic question but I can't seem to get OpenGL to work with a point coordinate system. Instead it seems to be in percents, for example .99 puts me right at the edge of the screen. Is this how it is, or am I doing something wrong? Thanks!

code used:

#include <iostream>
#include <stdlib.h>
#include <GLUT/GLUT.h>

void RenderScene()
{
    glLoadIdentity (); 
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin( GL_POINTS );  

        glVertex3f( 0, 0, 0 );

    glEnd();


    glFlush();  
}

int main(int argc, char **argv)
{
    glutInit( &argc, argv );                
    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );        
    glutInitWindowSize( 600, 600 );        
    glutCreateWindow( "OpenGL" );   
    glutDisplayFunc( RenderScene );
    glutMainLoop();
    return 0;
}

Solution

  • OpenGL works in the following way: You start of a local coordinate system (of arbitrary units). This coordinate system is transformed to so called eye space coordinates by the model-view matrix (it is called model-view matrix, because it combines model and view transformations).

    The eye space is then transformed to clip space by the projection matrix, immediately followed by the perspective divide to obtain normalized device coordinates ( NDC{x,y,z} = Clip{x,y,z}/Clip_w ). The range [-1,1]^3 in NDC space is mapped to the viewport (x and y) and the set depth range (z).

    So if you leave your transformation matrices (modelview and projection) identity, then indeed the coordinate ranges [-1,1] will map to the Viewport. However by choosing appropriate transformation and projection you can map from Model Space units to Viewport units arbitrarily.