phoenix's picture

Perspective and Orthographic Problem

Hi 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

GL.MultMatrixf(matrix);

Why do you have this line?

BaSa2902's picture

what are you wrong?

phoenix's picture

I used the arcball for the three transformations (rotation, scale and translation). They are controled by mouse events. The "matrix" is a 16-double array and keeps the values of matrices which are successively created by mouse events. I also caught the world coordinates corresponding to the mouse click. So, if i don't have GL.MultMatrixf(matrix); line, the world coordinates will be wrong.

objarni's picture

I guess it does not work if you remove that line?

I cannot see any specific error in your code ... try to remove stuff piece by piece see if anything happens

BaSa2902's picture

I think that you can use perpective but ratio of the factory (near,far...ect) is logical.

phoenix's picture

Of course, it works fine if I remove two pieces
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();

and

GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();

But that is not what i want. I want to get the world coordinates corresponding to the mouse click in the perspective projection (I print three values x, y, z out to a text box to check). With the Render (as I posted) and Window_Coor_To_World_Coor function

private Point3d Window_Coor_To_World_Coor(int x, int y) // x,y : x,y-coordinates of the mouse
{
Vector3 result = new Vector3();
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();

int[] Viewport = new int[4];
double[] ModelViewMatrix = new double[16];
double[] ProjectionMatrix = new double[16];

GL.GetInteger(GetPName.Viewport, Viewport);
GL.GetDouble(GetPName.ModelviewMatrix, ModelViewMatrix);
GL.GetDouble(GetPName.ProjectionMatrix, ProjectionMatrix);

object t = x;
GL.ReadPixels(x, viewport[3] - y, 1, 1, PixelFormat.DepthComponent, PixelType.Float,t);
float z = (float)t;

Glu.UnProject(new Vector3(mouseX, viewport[3] - mouseY, 0), ModelViewMatrix, ProjectionMatrix, Viewport, out result);

GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();

return result;
}

I got the right world coordinates with the line
GL.Ortho(-width / 2, width / 2, -height / 2, height / 2, -1000, 1000); // works fine
With
// Glu.Perspective(45, width / height, 0.1, 100); // problem
I can't see anything when i launch the scene.

Do you get my point ? I don't know why the Glu.Perspective doesn't work in this situation.

objarni's picture

And it's not a s simple as -1000,1000 being 2000 units in range, while 1,100 is 99 units in range..? The object beeing too far off into the scene, I mean?

phoenix's picture

If the object is too far off into the scene, I can translate and zoom in the hold screen with mouse events. So the distance now is not a problem. I just want to have an exact perspective view for my scene. Can you give me some code for this ?

objarni's picture

First try changing 1,100 too something like 1,10000 (Glu.Perspective).

Do you see anything then?

phoenix's picture

I've just changed 1,100 to 1,10000 in Glu.Perspective. I see only one line. This line is a part of the plane( a plane like in 3ds max) that I draw on the scene.