
Perspective and Orthographic Problem
Posted Thursday, 25 September, 2008 - 11:15 by phoenix inHi everybody,
I have a problem in switching between orthographic and perspective projection. My code runs well with GL.Ortho but if I replace it with Glu.Perspective, it doesn't work. My Render() function is here:
public void Render()
{
thisTransformation.getRenamed(matrix);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
GL.Ortho(-width / 2, width / 2, -height / 2, height / 2, -1000, 1000); // works fine
// Glu.Perspective(45, width / height, 0.1, 100); // problem
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
GL.MultMatrixf(matrix);
GL.Enable(EnableCap.DepthTest);
#region draw something here
// draw something ....
#endregion
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();
GL.Flush();
this.Invalidate();
}
Any suggestion ?


Comments
Re: Perspective and Orthographic Problem
I believe the problem is that the Window_Coor_To_World_Coor and Render functions do not setup the ModelView matrix exactly the same.
Render() calls GL.MultMatrixf(matrix) to apply your arcball transformation to the rendering.
Window_Coor_To_World_Coor() needs to also call GL.MultMatrixf(matrix).
A "safer" way to do this could be to save off the ModelView and Projection matrices after they have been setup in the Render(). Then in the Window_Coor_To_World_Coor() you could apply those matrices to guarantee that they are the same.
Re: Perspective and Orthographic Problem
Doh! I just noticed after I made that last post that your UnProject is not using the z value from the depth buffer.
Also change
Glu.UnProject(new Vector3(x, viewport[3] - y, 0), ModelViewMatrix, ProjectionMatrix, Viewport, out result);
to
Glu.UnProject(new Vector3(x, viewport[3] - y, z), ModelViewMatrix, ProjectionMatrix, Viewport, out result);
Re: Perspective and Orthographic Problem
I used your Render and ScreenToWorld functions. When I run the program, it gives me an error, kind of unhandled exception and can't launch the scene.
Re: Perspective and Orthographic Problem
I'll get a working demo up in the next couple of days and upload the source.
Re: Perspective and Orthographic Problem
It will be great.
Re: Perspective and Orthographic Problem
Draws a rotating triangle and as you move the mouse it will convert the mouse screen coordinates to world coordinates.
I then stores those coordinates and draws them as points so you can see where it is calculating the value.
Works for both types of projection - Ortho & Perspective
Re: Perspective and Orthographic Problem
I works with GL.Ortho(-viewport[2] / 2, viewport[2] / 2, -viewport[3] / 2, viewport[3] / 2, -1000, 1000);
With Glu.Perspective(90, (float)viewport[2] / (float)viewport[3], 1, 1000); It doesn't work, i just can see a part of what i draw (I also use GL.MultMatrix(matrix);) and the scene is weird. I really dont know why.
Re: Perspective and Orthographic Problem
With GL.Ortho(-viewport[2] / 2, viewport[2] / 2, -viewport[3] / 2, viewport[3] / 2, -1000, 1000); I can zoom in and out the whole scene with the little mouse button but I can not do it with Glu.Perspective(90, (float)viewport[2] / (float)viewport[3], 1, 1000);
Re: Perspective and Orthographic Problem
If you attach your source project I'll take a look.
Re: Perspective and Orthographic Problem
I have the same problem.
I still don't understand it's bug of OpenTK or OpenGL, or something in my head?
I looks like a perspective view, but very polygons look like something wrong. Only when I do very small scale, it looks good.