There is nothing drawn but if I uncomment glMatrixMode(GL_PROJECTION) in Display function, the triangle will be drawn. I have a lot of examples work without inserting glMatrixMode(GL_PROJECTION) inside the display function.

because in your commented code, you have glLoadIdentity() after glMatrixMode(GL_PROJECTION), which discards changes you've made with gluPerspective(...) call in Reshape() function and makes projection matrix identity. you need a perspective or orthographic projection set up in order to draw something on screen.

Hi CroCodile, in the Reshape function you are setting up the projection matrix and you first switch to the projection matrix: glMatrixMode(GL_PROJECTION); and then initialize it with an identity matrix. This is to clear (or initialize) the projection matrix to a default (identity) state. Then you setup the projection matrix with glOrtho(..) for orthographic or gluPerspective(..) for perspective.

Croc, The call to gluPerspective() creates a cool perspective matrix (Googl gluPerspective), and then multiplies that into the current matrix from the right (object) side. When you setMatrixMode(projection), you choose the projection matrix to be current. Then you set it to unity (the starting place, like zero, for a transformation) and then put the gluPerspective into it by multiplication. So that way you have that cool perspective matrix stored in your projection (instead of just unity there). If you go back in the display loop and set it back to just unity, then you undo the perspective, and offend the depth test and clipping volume, since your projection then puts your object at z = zero, which is one edge of the clipping volume as well as a depth limit. I would turn off depth test until things aren't disappearing when they shouldn't, or you may get a lot of black screen.

You shouldn't touch the projection matrix unless you want to change your wide-angle-ness of view, or reshape your window. Moving the camera and / or objects should be done with the model/view matrix. Set it to identity first, then do your scaling, rotating and moving. All OpenGL transforms only multiply a matrix from the right side, which is from the object's perspective. You have to multiply from the left to do anything from the camera's perspective, and that has to be done by hand with loadmatrix afterwards. The order I like to set my modelView matrix is: camera, lights, action, meaning start with identity, then rotate and move the camera, set the lights, then rotate and move the objects, pushing and popping the matrix between objects that need independent rotation and translation. If the camera doesn't move between frames, then you can skip setting its position and orientation by using push and pop appropriately for lights and objects. I hope that helps!

What is exactly the problem in the following code and how can I solve it?

The problem is that all vertices of the triangle have a Z coordinate of zero, which means that it's in front of the near plane and coincident with the viewpoint.

With a typical perspective projection, anything which you want to appear on screen has to have a Z coordinate between -zNear and -zFar after transformation by the model-view matrix (which in this case is the identity matrix, so the Z coordinates of the original vertices have to lie inside that range).

to add on, your triangle is outside the "viewing range" of your camera. To see the triangle, you can either move the triangle (through rewriting the coordinates or applying transformations within your modelview matrix) or move the camera (using gluLookAt(..) to configure your camera)