[SOLVED] How to get the size of the frustum at a distance from the camera

I have an camera at an determined position, looking at 0,0,0.
The idea is to get the fustrum size on z=0 to be able to put an quad on entire screen.

Code :

    offCamera.setFrustumPerspective(70f, (float)offCamera.getWidth() / (float)offCamera.getHeight(), 0.1f, 10f);
    offCamera.setLocation(new Vector3f(0f, 0f, 6f));
    offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);

    Vector3f relative = Vector3f.ZERO.subtract(offCamera.getLocation());
    float distance = relative.dot(offCamera.getDirection());
    float computedDistance=offCamera.getViewToProjectionZ(distance);      

Now how do I calculate it ?

I guess essentially do the reverse of what I do here:

That code is trying to figure out how far to put the camera so that something size ‘n’ is exactly the same size as the frustum.

The key part:

// In the projection matrix, [1][1] should be:
//      (2 * Zn) / camHeight
// where Zn is distance to near plane.
float m11 = camera.getViewProjectionMatrix().m11;

m11 is the key. After that it’s just algebra.

Note: projectionZ is not going to help you I think as that’s in scaled space.

Now, all of that said… if you want to put a quad on the entire screen then it sounds like you might really want a different viewport. What’s the quad for?

I use an quad to render an maze in background (same from the other post), currently its working but I fell this is a bit out of square. Since this quad will be only used to take the picture of the render and used in another quad I was looking for a more precise way to do it.
Also I did notice this quad shows diferent from pc to android, it seens the only way to fix it is to calculate right the fustrum.
I found this formula in the Unit wiki :

I guess I just need to replace camera.fieldOfView to this camera.getViewProjectionMatrix().m11 …
I will try it out.

So you only want the maze in the background when doing a screen shot? Why not just layer the maze behind the screen shot image separately then? Either paint it to the framebuffer as a separate viewport or just layer them at runtime or something.

Either way, full screen quad almost always means ‘background viewport’ to me.

No, the problem is actually in the maze image generation, it shows an image with the maze a bit to the left…
And when I put this on android it gets some empty space on left/right sides.

But if it were a viewport then you could just render it in the guiNode and make it exactly the screen size. You’d never have this problem (and indeed you will never have rounding errors or anything else should the view ever change slightly).

Displaying a full screen quad in 3D is the hardest way to achieve what it sounds like you want to end up with.

You are right, I test it, if I use the guiNode it fills the entire screen…
But… I am planning for example, to put some special effects using accelerometer, like if the user “topple” the mobile down a bit, I rotate the backgroud maze into this direction for bether visualization… I dont think its possible to rotate something on guiNode is it ?
So I think my only solution is to make the quad to fill the entire screen and add it to rootnode…

I come up with this proc :

public static void cameraLookToSquare( Camera camera, BoundingBox bb ) {
    float m11 = camera.getViewProjectionMatrix().m11;
    Vector3f min = bb.getMin(null);
    Vector3f max = bb.getMax(null);
    float xSize = Math.max(Math.abs(min.x), Math.abs(max.x));
    float ySize = max.y - min.y;
    float zSize = Math.max(Math.abs(min.z), Math.abs(max.z));
    float size = ySize * 0.5f;
    size = Math.max(size, xSize);
    size = Math.max(size, zSize);
    float z = m11 * size;
    Vector3f center = bb.getCenter();
    float sizeOffset = size - (ySize*0.5f);
    Vector3f camLoc = new Vector3f(0, center.y + sizeOffset, z);
    camera.lookAt(bb.getCenter(), Vector3f.UNIT_Y);

Its kind of working, but for square quads ( width=heigh ) it shows right but keeping the quad ratios ( square with empty space at left and right). And for square quads ( width>height ) it shows the square at the middle with empty spaces all arround. Maybe your function was done for voxel, I think it needs some ajusts right ?
Obs: I can use the same approach, actually its even better to change the camera position using an determined quad size, since I can use more intuitive sizes for it.


Sure it is. Why not? I assume you mean rotate about the z-axis? Or something else?

Its hard to explain the effect I am planning.
I am planning to atach small 3d objects to the same node of this maze 2d Board, like pickabes, monsters, etc, they will show very small in the screen, but still, when the player pushes the board for some direction it supouse to rotate the board and all other objects attached to it, its easy to do if its in rootnode, but I am not sure if it will work on guinode…

I was able to fix it doing this :

sizex=1024; sizey=1024; // x=y need to be equal for this to work;
Main.ref_cam.setFrustumPerspective(70f, 1, 1, 10000f);
Utils.cameraLookToSquare(Main.ref_cam, new BoundingBox(new Vector3f(-sizey/2,-sizex/2,0),new Vector3f(sizey/2,sizex/2,0)));
Quad quadmesh = new Quad(sizex, sizey);

I am not sure if this is the best way, but its working well.