2
votes

I just started reading initial chapters of Blue book and got to understand that the projection matrix can be used to modify the mapping of our desired coordinate system to real screen coordinates. It can be used to reset the coordinate system and change it from -1 to 1 on left, right, top and bottom by the following (as an example)

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //With 1's in the diagonal of the identity matrix, the coordinate system is rest from -1 to 1 (and the drawing should happen then inside those coordinates which be mapped later to the screen)

Another example: (Width: 1024, Height: 768, Aspect Ratio: 1.33) and to change the coordinate system, do:

glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 100.0, 1000.0);

I expected the coordinate system for OpenGL to change to -133 on left, 133 on right, -100 on bottom and 100 on top. Using these coordinates, I understand that the drawing will be done inside these coordinate and anything outside these coordinates will be clipped.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
    glOrtho(-100 * aspectRatio, 100 * aspectRatio, -100, 100, 100, 1000);
glMatrixMode(GL_MODELVIEW);
    glRectf(-50.0, 50.0, 200, 100);

However, the above command doesn't give me any output on the screen. What am I missing here?

1
You should not use the deprecated API! In the current API the setup of the coordinate system is much different ( you have to do it on your own ) and learning a deprecated API is a waste of time IMHO.Felix K.
@FelixK. : Can you point out the latest correct way here. I am not even sure about what it acutally is.user1240679
Maybe you didn't set a color. I would recommend doing some tutorial for OpenGL 3 or later to get the hang of things.Andreas Haferburg
I can't tell you what is wrong with the code above because the last time i used it is 5 or more years ago. But you should use shaders and vertex buffers for all your drawing code. For setting up the camera etc you just can use a c++ lib, there are many of them out there. Of couse there are some good tutorials too ( e.g. db-in.com/blog/2011/01/all-about-opengl-es-2-x-part-13 )Felix K.
db-in.com/blog/2011/04/cameras-on-opengl-es-2-x They are both for OpenGL ES which is a kind of limited OpenGL but works well for beginners.Felix K.

1 Answers

2
votes

I see two problems here:

  1. The rect should not by show at all, since glRectf() draws at depth z=0, but you set up your orthorgraphic projection to cover the z range [100,1000], so the object lies before the near plane and should be clipped away.
  2. You do not specifiy waht MODELVIEW matrix you use. In the comments, you mention that the object does show up, but not in the place where you expect it. This also violates my first point, but could be explained if the ModelView matrix is not identity.

So I suggest to first use a different projection matrix with like glOrtho(..., -1.0f, 1.0f); so that z=0 is actually covered, and second insert a glLoadIdentity() call after the glMatrixMode(GL_MODELVIEW) in the above code.

Another approach would be to keep the glOrtho() as it is and to specify a translation matrix wich moves the rect somewhere between z=100 and z=1000.