GL coordinate setup

I’m just starting with GL, and I have this question-

When I create a viewport, I get a x maximum of 1 and a y maximum of 1 as well. That means If I am drawing something that has real-world coordinate data of say -128, 20 I have to devide by the with of the window in GUI terms, which makes the pixels pile up on each other. (I am using GL_POINTS to plot a bunch of colored points) I would appreciate it if someone could tell me how to set the coordinate system extreme values to an arbitrary number.



there are two things you need to set up:
the area in the window that you’re drawing in, the viewport. This can be set up by a call to glViewport, usually this is: glViewport(0,0,windowWidth,windowHeight);

the next thing you need to set up is the clipping plane… you can use glOrtho2d to set it up for you. Once, you’re past the beginning you can try gluPerspective or glFrustum… but it’s the clipping plane you forgot to set…

For example, if you had a window 640x480 you probably want the x clipping plane 4/3 (640/480) of the x clipping plane… so look up those functions in the refrence.

I tried using the gluOrtho2D command like:

gluOrtho2D(-600, 600, -600, 600);

Because I want a coordinate system with extreme values of (-600, -600) and (600, 600). However, doing this messed with my viewport. I have a viewport set as
glViewport(0, 150, cwidth / 2, cheight - 150);
and another positioned to the right of this one taking up the remainder of the window space to the right, and two more below. Setting glOrtho2D to (-1, 1, -1, 1) doesn’t do anything (I assume this is the default) but setting it to what I describe earlier somehow overwrites the other windows. I also tried setting scale as glScale(600, 600, 0) but that messed things up as well. My code is as follows:

int dvpWidth = cwidth / 2, dvpHeight = cheight - 150;

glViewport(0, 150, GLsizei(dvpWidth), GLsizei(dvpHeight));

gluOrtho2D(-600, 600, -600, 600);
glColor3f(0.0, 0.1, 0.0);
glColor3f(1.0, 0.0, 0.0);

glPointSize(600 / cwidth);
glVertex2f(0.0, 0.0);
glVertex2f(1.0, 0.0);
glVertex2f(0.9, 0.0);

cwidth is the client width and cheight is the client height of the window I am working with. I then go on to create three more viewports (although I haven’t set gluOrtho2D projections for them, they are in the default state). All I want to do right now is make my coordinate system have 600 units on each axis in each direction. Later I will want to “zoom” in on a specific range, say change the viewing area to (-20, -10) to (20, 10) in the coordinate system, but for now I just want a global view that shows 600 units.

There are two (mainly two) different matrices you use. The projectionmatrix and the modelviewmatrix. You must know how to use these before you start coding.

The modelviewmatrix is the matrix that transform a vertex from local coordinates to world coordinates.

The projectionmatrix is the matrix that transforms a vertex from worldcoordinates to screen coordinates.

It’s up to you do activate the correct matrix when calling functions. When you call functions like glRotate and glTranslate, you should activate the modelview matrix. When calling gl(u)Ortho*, glFrustum and gluPerspective, you should activate the projection matrix. This is now what is done in the code you posted.

Try do something like this:

// modelview matrix is active by default
glViewport(0,0,x,y); //x and y is size of window
glMatrixMode(GL_PROJECTION); //activate the projection matrix
gluOrtho2D(-600, 600, -600, 600); //the projection you wanted to use
glMatrixMode(GL_MODELVIEW); //reactivate the modelview matrix

Now you can pass vertices and do your own transformation.

And only call projectionfunctions once (unless you need to rebuild the projection, for example when changing field of view).