Understanding problem at gl.glFrustumf...

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? :wink:
Sincerely, Yerst.

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

[QUOTE=Yerst_;1241564]

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?[/QUOTE]

Take a good read of the glFrustum man page. In particular, note:

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.

But why does the nearVal and farVal affect the Position of the Triangle on the Screen?

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.

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:

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);
	}

[QUOTE=Yerst_;1241642]I think i got it, but there is still a problem.
My Eyepoint is at 0,0,0 looking to 0,0,1.[/QUOTE]
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.

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):wink: the camera, but it grows and shrinks all the time and moves upwards and downward while rotating…
Thanks for your Answers!

[QUOTE=Yerst_;1241669]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?[/QUOTE]

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.

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.