View Full Version : glVertex Parameter's limit
08-07-2008, 01:29 AM
I am new to OpenGL and is trying to understand the very basics of it. I obssrved that in most of the examples, glVertex API parameters take the value from 0 to 1? I have the following question about glVertex:
- How does the parameters relate to the screen location? Suppose the screen(or the viewport) is 800 by 600 pixels, does glVertex(0.5,0.5,0.0) correspond to 400 pixels on X and 300 pixels on Y?
08-07-2008, 02:00 AM
Actually glVertex parameters don't relate to a screen position, but a vertex position in object space.
For instance, you want to draw a cube and its vertices coordinates are defined from its center. To do this, you move the center of the cube in world space where you want and then draw the cube giving its vertices coordinates with glVertex.
Now, in 2D, you have to set the viewport and projection matrix for orthographic projection and then draw vertices on screen with glVertex with only 2 coordinates. These coordinates are in the 2D space you set and can be in any range and not bound to [0 1].
See gluOrtho2D function reference page here (http://www.opengl.org/sdk/docs/man/).
08-07-2008, 02:09 AM
If I take your example, this the code you need, to obtain this behaviour:
// set a 2D orthographic projection
gluOrtho2D(0, 800, 0, 600);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -600, 0);
// then draw all you want, for instance:
// this vertexx is at (400,300) pixel coordinates
08-07-2008, 02:22 AM
Thanks for your quick reply. So the glVertex parameters value depend on the orthographic viewing volume you set? I am pasting a program from the book 'OpenGL programming book' for my easy understanding.
In this program, the 'glOrtho' parameters are limited to either -1 or 1. Does it mean that the object space is limited to these numbers and so the glVertex() is limited to these numbers?
And the window size is set to 250,250 which i guess the whole window is now the viewport. So when the object space coordinates are mapped to screen coordinates, -1 and +1 are related to the window size (250 by 250)? I mean how do i know where the rectangle would be displayed on the window or how do i draw at the required position on the screen?
/* clear all pixels */
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
glColor3f (1.0, 1.0, 1.0);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
/* don't wait!
* start processing buffered OpenGL routines
void init (void)
/* select clearing color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
int main(int argc, char** argv)
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
return 0; /* ANSI C requires main to return int. */
08-07-2008, 03:08 AM
When you set an orthographic or parallel projection you set a clipping volume which sides are defined by left, right, top, bottom, near and far. All vertices outside of this volume are clipped. This volume is not limited to [-1 1]x[-1 1]x[-1 1], this depends on what you need and you can set it bigger.
After projection transformation, vertices coordinates are divided by their fourth coordinate w, this is alled perspective division. And then all remaining vertices coordinates are in [-1 1] range and are called normalized device coordinates (ndc).
Finally only after all this, ndc are mapped to transformed into coordinates and this is configured with the glViewport function which sets screen bottom left corner position, width and height in pixels. See the reference page I gave you above.
08-07-2008, 08:11 AM
To clarify your problem: there are no APi limits on values passed to the Vertex command (besides the natural precision limits of the data type). The coordinates are then transfoprmed via the modelview and projection matrices into the clipping space (like dletouzen described). So, the length units only depend on how you set up your projection/modelview matrices. I recomemnds that you look at the OpenGL specificatioin, where this all is explained in detail and experiement with different matrix setups, till you get a hang of it.
08-08-2008, 12:22 AM
I got it. Thanks to dletozeun and Zenger. I changed 'glOrtho' parameters to 'glOrtho(0.0, 600.0, 0.0, 800.0, 0.0, 200.0)' and set window size to 'glutInitWindowSize (600, 800);'
and could see glVertex parameter values not restricted to 1. This sure gave me the idea to place the object in the space at the required location and when the object was rendered on the screen, it was mapped exactly where i wanted it.
1)Isn't it advantageous to have the viewing volume same as your screen size instead of -1,1 so that we can position the object at the required pixel? If -1 and 1 are used, the exact pixel position will have to be corelated with 600,800 (ie screen size) and the parameters will be in decimels? For example the glVertex(300,300,0) would put the vertex exactly on 300,300,0 location instead of mentioning the same in decimels.
2)If 'glOrtho' or glFrustum() or glPerspective() are NOT used, what is the default viewing volume and which projection is in effect(ortho or perspective)?
08-08-2008, 01:30 AM
1) It depends on your needs. What you do is useful to place 2D elements on screen like a user interface. Yet, I'd rather use [0 1]x[0 1] screen space since it doesn't depend anymore on the screen resolution.
This way if you want to draw something at the horizontal pixel position 300, you just have to set 300/window_width in glVertex.
2) I don't know exactly, I would risk myself to let Opengl default viewing. But this is something you would find in the opengl specification I think.
08-08-2008, 07:23 AM
1) There is a function called glWindowPos exactly for this reason.
Consult the GL 2.1 specification.
2) The default is the identity matrix.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.