We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Fit 3d object into view with camera dolly
Page Index Toggle Pages: 1
Fit 3d object into view with camera dolly (Read 948 times)
Fit 3d object into view with camera dolly
Oct 17th, 2007, 4:26pm
 
Hi,

it's probably not too complicated but I can't find the solution. I'm looking for a quick way to fit a 3d object into the view.
I found a not so nice solution with the help of screenX etc, but I have to make a loop until I have the camera at the right z position.  I couldn't find the formula to directly calculate  the correct z distance. I would appreciate any help or ideas Wink

phil
Re: Fit 3d object into view with camera dolly
Reply #1 - Oct 22nd, 2007, 3:06pm
 
hmm, I'll grab the sources and have a look inside, maybe that will help me a little bit.

Code:

public float screenX(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float ox =
projection.m00*ax + projection.m01*ay +
projection.m02*az + projection.m03*aw;
float ow =
projection.m30*ax + projection.m31*ay +
projection.m32*az + projection.m33*aw;

if (ow != 0) ox /= ow;
return width * (1 + ox) / 2.0f;
}


ok.. me think I don't really understand the code Wink
Re: Fit 3d object into view with camera dolly
Reply #2 - Oct 22nd, 2007, 10:33pm
 
I have code that does this, but I'll have to dig it up.  Basically though, it's a two part process:

First, you need to figure out a bounding box around your object(s), this can be the hard part if the object is arbitrarily rotated.  But let's just assume that you have a "bbox" object with min/max/center/dimensions already calc'd and ready to go, so I can fake up some code for ya...

Second, fitting the bbox to the display is the easy part.  You know the field of view (g.cameraFOV = 60 degrees), and one side of the triangle (bbox width or height), and you're solving for the other side (z distance).  So you plug in twice for the x and y extents of the box and take the further distance.  (because aspect of bounding box may not match screen)

Off the top of my head, psuedocode, something like:

float zdistW = (bbox.width/2f) / tan(g.cameraFOV/2f);
float zdistH = (bbox.height/2f) / tan(g.cameraFOV/2f);
float zdist = max(zdistW, zdistH);

// then put your camera here:
float eyeX = bbox.centerX;
float eyeY = bbox.centerY;
float eyeZ = bbox.maxZ + zdist;

You might want to add a tiny bit of additional fudge factor and zoom out a bit further, otherwise any objects right at the near face of the bbox might be zoomed TOO tight to the screen edges.
Re: Fit 3d object into view with camera dolly
Reply #3 - Oct 23rd, 2007, 12:44am
 
oh no damnit, grr, that's what I had all the time but it didn't work, so I tried tried and tried but in vain and then due to your post I rethought what I did... guess what..?? I forgot that processing calculates in radians and not in degrees but I had given him degrees ;((
so a simple .../Math.tan(radians(50)) did the trick. OMG , precious little time, gone...must sleep more, must must...

Thank you very much for keeping me from insanity...
Page Index Toggle Pages: 1