coordinate mapping

I have one doubt over this opengl development. Consider that I draw a sphere or square in grid paper, it is easy to do so, I can take full control of which position to place my drawing objects such as sphere, etc, with ease.

But if I want to draw something “on the window screen” using opengl, how I actually can have full control to place the drawing objects as my wish? For example, I want to draw a square, but I can’t see the window’s grid, so how I going to know which (x,y) points are at the postion I want.

I really have difficulty in working with the coordinate system that is virtually invisible, do you guys have any idea in handling this kind of problems? Or what you all do is to compile and run the program to see whether the drawing objects are at the location wanted, otherwise modify the coordinate value for each object, then compile and run again, till the desired results. Is it the way how you all do?

I will appreaciate much if some one can clear my doubt.

Thanks.

Hi !

If you want it to look the same as what you have on paper (and it is 2D) then you can setup an orthographic projection and scale it to look the exact same as on paper, maybe that makes life easier for you.

Remember that you can scale the coordinate system anyway you want in OpenGL, if you for example want to map it to match the pixel resolution of your window this can be done (an example of this is available on the FAQ on this website).

Mikael

So, it means that each time before I program the graphics, I have to draft it in a piece of grid paper, and use the coordinates as refer from the grid paper into my program. Is it what you mean?

Using the projection matrix you can determine exactly the coordinate extents of the viewport. Your “invisible grid” is user defined, so while as yes you could do a guess and check method, all you really need to do is keep your coordinate extents and aspect ratio.

For example:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0, 4.0, -3.0, 3.0, 1.0, -1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
     glVertex2f(-2.0f, 2.0f);
     glVertex2f(-2.0f, 0.0f);
     glVertex2f(0.0f, 0.0f);
     glVertex2f(0.0f, 2.0f);
glEnd();

There’s no guesswork involved. I can tell you exactly that a rectangle will appear with it’s right and bottom edges justified vertically and horizontally with the center of the viewport, and the width will be one fourth of the viewport, and the height one third that of the viewport.

To “know where things will show up” is just to know what kind of projection you have set up, and also having a concrete grasp on transformations.

But how if I want to draw many, say more than 10 different kind of objects in the viewport? (Assume that I always set both the viewport and projection’s width and height exactly the same as the window client area)

If your projection does not change, for instance you are rendering what, say, a camera sees (and ONLY what the camera sees) then all you need to do is:

1.) Set projection matrix
2.) Set modelview matrix to proper camera orientation (gluLookAt, etc.)
3.) for each object, push modelview matrix, perform object transformations (translate to position, perform rotations, etc.), draw object, pop modelview matrix
4.) lather, rinse, repeat, always repeat

hmm… seems like I still have a long way to go to understand what you mean here, I think I need to do a lot of “homework” before I can understand all these… :eek:

Anyway, thanks for your reply! :slight_smile: