Ortho and Perspective

Does anybody know of some simple tutorial and/or code that allows the user to switch back and forth (on the same Model) between Perspective and Orthogonal. I am fairly new to OpenGL and so don’t know all the particulars, however any help would be great.

Thanks in advance.

May I ask why would you want it?

The simplest way is just set up the matrix you need prior to rendering a particular part of the scene. There should be no performance hit if you jus treset the matrix several times every frame.

Does anybody know of some simple tutorial and/or code that allows the user to switch back and forth (on the same Model) between Perspective and Orthogonal.
I don’t know if there are any tutorials on it. Most tutorials tend to be focused pretty specifically on one or the other.

In general, this is the sort of thing that if you don’t have an intimate grasp of how the two kinds of projection matrices work, you need to actually try stuff and see how it breaks/doesn’t work, then ask a specific about why it broke like that.

May I ask why would you want it?
For a model viewer, maybe? And that’s just the first thing that jumped off the top of my head.

Originally posted by Zengar:
[b] May I ask why would you want it?

The simplest way is just set up the matrix you need prior to rendering a particular part of the scene. There should be no performance hit if you jus treset the matrix several times every frame. [/b]
Zengar: What does performance have to do with this?

Actualy i have the same problem rendering transformation gizmos in my editor. My aproach is to scale the gizmo by the distance to the camera to make it look the same no mater the camera_z_position. But it would be alot nicer if the gizmos could be rendered in ortho mode like a on screen hud.

Why not something like this?

 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-w, w, -h, h, near, far);
   DrawScene();

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-w, 2, -h, h, near, far);
   DrawGizmos();  
 

Zengar is right.
Also you need a flag, lets say “char projection_mode”, and every time you detect an event just change it.
Also you have to know that most window system tell you specify Projection matrix inside the callback windowsResized(). The coordinates for the frustrum and other parameters, like zoom, must be visible for this function and also for the eventhandler.

Bye!

Thanks for all the suggestions. I will give it a try and see what happens.

I still keep having issues with the ortho and perspective views. The major problem is that the views keep getting clipped in the front. Can someone take a look at the following code and tell me if there is something obvious I am overlooking?

if (isPerspec) {
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  float aspect = (float)width/(float)height;
  glu.gluPerspective(45, aspect, 1, 1000);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
  drawModel();
else {
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glOrtho(-width, width, -height, height, 1, 1000);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
  drawModel();
}

The 1 and 1000 for the Z-Near and Z-Far respectively I know encompass the Model. I just hope that it is something obvious.

Thanks in advance.