glMatrixMode and 2D display

I have searched and found some information about the various matrix modes, but am unable to determine what each really does. From the khronos.org site I see that GL_MODELVIEW specifies that subsequent operations are applied to the modelview stack, but no explanation of the modelview itself. The same applies to the other three modes. I found a stackoverflow thread but am not able to understand it.

Departed predecessors who created some strip chart code keep switching between the two Matrix modes, GL_MODELVIEW and GL_PROJECTION and repeatedly load the identity matrix. They left no comments as to why presuming I should know that. Not unreasonable, but I don’t know. This does run real time so I want to eliminate code that is not necessary.

Note: This might be a factor. I am creating a strip chart. Each point arrives as a pair of doubles, time and value. glOrtho( left_clip, right_clip, …) is used to create a moving time window such that only the points between time current_time and (current_time – time_span_of_chart) are displayed. For each update interval I do a glClear() and redraw all the points for each display interval. I can draw a sin(x) and a tan(x) static display so am getting close.

Here are my questions:

When displaying in 2D only, can, should, the mode be set to GL_MODELVIEW or GL_PROJECTION and leave it there?

I suspect that since there are no transformations the identity matrix is not needed. That is easy to test, but I always worry that without an understanding my tests may look good, but then the users may behave differently and have it crash and burn for them.

Unless you are using lighting (or certain texture coordinate generation modes via glTexGen), you don’t need to use two separate matrices. All vertex coordinates are transformed by both the model-view matrix and the projection matrix to obtain screen coordinates.

Most matrix operations append (right-multiply) a transformation with the current matrix, so if you want to set a matrix to a specific transformation regardless of any prior state, you need to call glLoadIdentity() before applying any other transformations.

I have arrived at the following conclusions about the glMatrixMode opetions: GL_MODELVIEW and GL_PROJECTION. Pease tell me where I am wrong:

The GL_MODELVIEW mode sets open GL to work with the modeling matrices. These are the matrices that represent the object which is being modeled. Changes while this mode is set change the model that represents some entity being modeled.

The GL_PROJECTION mode provides access to the matrices that control what is projected to the display, seen by the camera that becomes the 2D display. The camera can be moved to any position with respect to the model, changing the view, but not changing anything within or concerning the entity that is being modeled.

Your description corresponds to typical usage. But ultimately vertices are transformed by both matrices to obtain screen coordinates, so it doesn’t matter which one you use unless you’re using some feature of the pipeline which specifically uses eye-space coordinates, i.e. lighting or texture-coordinate generation.

The reason for the separation is that the projection matrix often includes a projective transformation (perspective projection), which interferes with the lighting calculations. So OpenGL allows this to be handled separately.

For your application, you can use either matrix. It makes no difference.

Thank you for that post. I have played around a bit with

glMatrixMode( GL_MODELVIEW);
code_to_plot_points();
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
update();  // the overridden function to update the display

The results are confusing me.
When the glMatrixMode calls have the argument reversed, I see no change. I think that is no big deal. Conceptually, within my head, it seems more proper to use MODELVIEW first then PROJECTION. Any problems there?

However, if I don’t use one then the other, the display does not update properly. Both must be used.
Why is that?

glLoadIdentity() must be called after the points are plotted. Calling it before appears to be optional. Remember that this is 2D display only, is there any harm to omitting the call before plotting the points? Any advantage to calling it then?

[QUOTE=mbkelly;1293700]Thank you for that post. I have played around a bit with

glMatrixMode( GL_MODELVIEW);
code_to_plot_points();
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
update();  // the overridden function to update the display

The results are confusing me.
When the glMatrixMode calls have the argument reversed, I see no change. I think that is no big deal. Conceptually, within my head, it seems more proper to use MODELVIEW first then PROJECTION. Any problems there?
[/QUOTE]
If one is the identity matrix, the order doesn’t matter. IM=MI=M.

Perhaps something else in the code is changing the matrix?

[QUOTE=mbkelly;1293700]
glLoadIdentity() must be called after the points are plotted. Calling it before appears to be optional. Remember that this is 2D display only, is there any harm to omitting the call before plotting the points? Any advantage to calling it then?[/QUOTE]
Normally you’d ensure both matrices are set correctly before drawing anything. What they’re set to afterwards shouldn’t matter.

If you need to set them after drawing, then it seems that something else is using the OpenGL state (maybe for drawing, or possibly for something like picking in response to mouse events).