PDA

View Full Version : Understanding problem at gl.glFrustumf...



Yerst_
08-18-2012, 04:30 PM
Hi!
I just started with opengl/Android programming. (previously i worked with C# and XNA...)
I made my first Triangle, but i can't apply a perspective view.
I tried to find an example, but that's there are too much differences.
Orthographic view has worked for me with this:

gl.glOrthof(0,1,0,1,0,1)
But not in 3D.
I thought that will work, but it didn't:

gl.glFrustumf(0,1,0,1,0.01f,100.0f)
In my mind that should define a coordinate system with the origin at the bottom left corner of my device, and the upper right color as (1/1). (Like in the Orthographic view.)
And it should show all between 0.01f and 100.0f.
But it doesn't, so why? ;)
Sincerely, Yerst.

And sorry for my bad english.
If there are any language failures in my text, tell me them.

Dark Photon
08-18-2012, 07:08 PM
gl.glFrustumf(0,1,0,1,0.01f,100.0f)
In my mind that should define a coordinate system with the origin at the bottom left corner of my device, and the upper right color as (1/1). (Like in the Orthographic view.)
And it should show all between 0.01f and 100.0f.
But it doesn't, so why?

Take a good read of the glFrustum (http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml) man page. In particular, note:



( left, bottom, -nearVal ) and ( right, top, -nearVal ) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, assuming that the eye is located at (0, 0, 0). -farVal specifies the location of the far clipping plane. Both nearVal and farVal must be positive.

Note that the points mentioned are EYE-SPACE positions. Also note that while the provided nearVal and farVal arguments are positive, these correspond to the planes Z=-nearVal and Z=-farVal in EYE-SPACE.

Yerst_
08-19-2012, 02:14 PM
But why does the nearVal and farVal affect the Position of the Triangle on the Screen?

Dark Photon
08-19-2012, 05:00 PM
Because this is perspective, not orthographic (in orthographic, these wouldn't make any difference to the position on-screen).

More specifically, in perspective, nearVal (N) is the one of the two you mentioned that matters for the position of the triangle on the screen. It matters because N is the distance (along the EYE-SPACE -Z axis) that the left, right, bottom, and top (LRBT) values are actually defined.

As a thought experiment, imagine keeping LRTB (a 2D box) the same while pushing N away from the eye. Your field-of-view (FOV) angle through that little box gets smaller and smaller, right? Just hold the flat side a book up in front of your face and push it away to visualize this. Or draw this in 2D on paper with the eyepoint, -Z axis, 2 different N values, and a constant B and T to prove it.

Yerst_
08-20-2012, 11:44 AM
I think i got it, but there is still a problem.
My Eyepoint is at 0,0,0 looking to 0,0,1.
In my mind, when the nearVal is 1, the triangle (0/0/1, 0.5f/1/1, 1/0/1) should be shown very big on the screen.
But i see nothing...
Here is a picture, about it:http://www.bilder-hochladen.net/files/big/ik52-8-c9f0.png

And here is my onSurfaceChanged:


@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
gl.glViewport(0, 0, (int)width, (int)height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(0,1, 0, 1, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 1, 0, 1, 0);
}

Dark Photon
08-20-2012, 03:56 PM
I think i got it, but there is still a problem.
My Eyepoint is at 0,0,0 looking to 0,0,1.
In WORLD-SPACE (not EYE-SPACE). Thus your gluLookAt(gl, 0, 0, 0, 0, 0, 1, 0, 1, 0). The params to gluLookAt are WORLD-SPACE.

In EYE-SPACE, your eyepoint is always at 0,0,0 looking down the -Z axis (so toward 0,0,-1).


In my mind, when the nearVal is 1,

...which is the EYE-SPACE Z=-1 plane...


the triangle (0/0/1, 0.5f/1/1, 1/0/1) should be shown very big on the screen.

That depends. Your triangle vertex positions are registered with OpenGL in OBJECT-SPACE. If your MODELING transform is the identity transform, then your OBJECT-SPACE == your WORLD-SPACE. Assuming this is the case...

You know from your gluLookAt plus your glFrustum near clip value of 1 that, your near clip plane is Z=1 in WORLD-SPACE. If you put a triangle right on the near clip plane, it may very well be culled. I'd change the OBJECT/WORLD-SPACE Z of your triangle verts to 1.5 so it is safely inside the view frustum.

Yerst_
08-21-2012, 04:05 PM
I changed my cameraView from (0,0,1) to (0,0,-1) and the triangle Coordinations to (0,0,-1).
And it worked!
Maybe i looked at the back of it? How can i apply CullFace? (gl.glEnable(GL10.GL_CULL_FACE) doesn't work, the triangle disappears, but that should go to another thread.)
The next Problem is, that when i change the Z-Coorinates to (0,0,-2) for example, the Triangle isn't anymore in the mid of my window. It gets smaller, but the left corner of the triangle is still in the left corner of my window.
I tried to rotate to rotate (gl.glRotatef(0.05f,0,1,0);) the camera, but it grows and shrinks all the time and moves upwards and downward while rotating...
Thanks for your Answers!

Dark Photon
08-21-2012, 05:37 PM
I changed my cameraView from (0,0,1) to (0,0,-1) and the triangle Coordinations to (0,0,-1).
And it worked!
Maybe i looked at the back of it?

Yes, probably so. Your original verts were oriented clockwise (CW) relative to your eyepoint, and by default, the "front face" is counter-clockwise (CCW). This is set by glFrontFace() and by default it's GL_CCW. So yes, with your viewpoint and triangle Z changes, you're now looking at the side that's CCW oriented from the eyepoint, which is the front.


How can i apply CullFace?

Beyond setting which is the "front" side... You can disable back-face culling with glDisable( GL_CULL_FACE ). Or you can enable it and tune which faces are culled (front, back, or front and back) with glCullFace(), which by default is set to GL_BACK.


The next Problem is, that when i change the Z-Coorinates to (0,0,-2) for example, the Triangle isn't anymore in the mid of my window. It gets smaller, but the left corner of the triangle is still in the left corner of my window.
That's not a bug, that's a feature. That's how perspective projection works. The further back in your view frustum, the smaller it gets. If you don't like it, you need to use another projection (such as orthographic, where this doesn't happen), or you need to dynamically adjust your frustum as your objects move. Or you need to do whacky things with perspective like put the eyepoint really far from the object and make the frustum long and thin (essentially making it virtually an orthographic frustum).

See your prof for specific requirements here.

Yerst_
08-22-2012, 12:13 PM
When i try gl.glenable(gl10.gl_cull_face);, the triangle disappears. I tried only adding gl.glCullFace(GL10.GL_Front_and_Back); but it has the same problem as if i add nothing...
I also tried to change the frontface order, but that changed nothing too.

Too the Z-coordinates: i'm, glad that the triangle is smaller, but it shouldn't, go to the left corner. It should stay in the mid of the window and get smaller.