Legion
Legion

Reputation: 3457

Vertex Coordinates in OpenGL

How come vertex coordinates in OpenGL range from -1 to 1? Is there a way to be able to specify vertex coordinates using the same coordinates as the screen?

So instead of:

    float triangleCoords[] = {
        // X, Y, Z
        -0.5f, -0.25f, 0,
         0.5f, -0.25f, 0,
         0.0f,  0.559016994f, 0
    }; 

I could have

    float triangleCoords[] = {
        // X, Y, Z
         80, 60, 0,
         240, 60, 0,
         0,  375, 0
    }; 

Just seems a bit much that I need to get out a calculator just so I can hard code in some vertex coordinates. It's not like I'm gonna be trying to lay things out and think "yeah, that should go right around (0, 0.559016994), that'll look perfect..."

Upvotes: 2

Views: 3639

Answers (1)

Tim
Tim

Reputation: 35943

This is what projection matrices are for. They project from your coordinate frame to the normalized coordinates.

You're free to setup vertices in pixels if you want, but then you just have to pair it with a proper projection matrix to tell opengl how to transform those coordinates to the screen.

Given your example:

float triangleCoords[] = {
    // X, Y, Z
     80, 60, 0,
     240, 60, 0,
     0,  375, 0
}; 

If you pair this with an orthographic projection matrix (similar to one generated by glOrtho(0, width, 0, height, -1, 1), then your triangle will draw at the pixel coordinates described.

Upvotes: 4

Related Questions