help positioning a model

Still wrestling w/ this problem. The problem is that when a user opens a file for display, the model from the file can be of any size and I’m trying to find a way to center it as soon as it opens up in my cavas for display.

I read in my models elements from a file and gather the minimum and maximum x, y, and z cooordinates.

minX: -4.77241516 maxX: 4.74154425
minY: -4.17707014 maxY: 4.07707024
minZ: -4.70177507 maxZ: 4.70177507

I then find the centroid of my model w/ these values and point my camera there.

Below is the code in my display(), the problem is that, when the model is loaded, it’s still very far off in the distance when I think using the glOrtho, gluLookAt, and glScale values below should put the model almost full screen and centered on my canvas (at least it’s centered). Sometimes depending on what model I open up, it’s so far off in the distance, it takes for ever to zoom in on it.


	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

	// Projection transformation.
	gl.glMatrixMode(GL.GL_PROJECTION);
	gl.glLoadIdentity();

// winWidth and winHeight values are populated from the reshape() method.

	gl.glOrtho(-winWidth, winWidth, -winHeight, winHeight, 100, -100);

	// reset the modelview matrix
	gl.glColor3f(1.0f, 1.0f, 1.0f);
	gl.glMatrixMode(GL.GL_MODELVIEW);
	gl.glLoadIdentity();

// Shouldn't the gluLookAt()'s eyez value below place the camera right on 
// the front edge of the model? The eyez value of centroid.getMax_Z() is the 
// maximum positive z value of the model.

	// Viewing transformation.
	glu.gluLookAt(centroid.getX(), centroid.getY(), centroid.getMax_Z(), centroid.getX(), centroid.getY(), centroid.getZ(), 0.0, 1.0, 0.0);

	// Modeling tranformation.

// getZoomFactor() is set at 1.0 which I think should not make a difference 
// in the original size of the model when loaded.

	gl.glScalef(getZoomFactor(), getZoomFactor(), 0);

	// Apply arcball transformation.
	arcBall.getTransform().get(transf);
	gl.glMultMatrixf(transf);

	// Draw the model.
	{
		drawModel(drawable);
	}

Any help much appreciated.

gluLookAt takes its arguments as in: (eyeXYZ, posXYZ, oriXYZ)

So when you want to lookAt posXYZ from the origin (0,0,0) you need to do:

gluLookAt(0,0,0, c.x,c.y,c.z, 0,1,0);

Yeah, I understand the parameters to gluLookAt(), but I don’t know why that doesn’t work for me.

I figure, if my model is built around the origin (which you can see by looking at the min and max x, y, and z coordinates I have above) then if I put gluLookAt’s camera eyeXYZ position at …

x = same as model centroid x
y = same as model centroid y
z = the max z value of the model

… then I get a nice alignment w/ my model and it works. I figure I’m just putting the model at the origin and backing up the camera to however large the model might be by using the the maxZ() of the model.

If I use eyeXYZ as 0, 0, 0, I get this wierd effect as if I’m looking at the side edge of a flat plane which my model is rendered on (sort of like a straight line on my canvas).

If I use eyeXYZ as 0, 0, model.getMaxZ(), then the effect is like, my model is on a flat plane and when I rotate it, it’s the plane that I’m flipping around and not the model.

But I can rotate my models sphere just fine if I use the x, y, and z listed above and the model appears normally. I don’t understand why though.

You’re doing glScalef(n, n, 0) which scales the current (rotated, translated, transformed, whatever) modelview matrix.

So you’re basicly resetting one axis to 0, resulting in the plane-like visual effect.

Well, that fixed the plane effect, but now when I zoom it in and out, both the front and back of the model gets cut like it’s being clipped because of the position of the front and back z-axis view planes. I ran into this before and that’s what led me to not scale the z-axis.

I set my glOrtho near and far to 100, -100, but the book says this shouldn’t make a difference as long as they are not the same value.

So, how does the near and far planes get set so as not to clip the model when it’s z value changes when zooming?


gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

// Projection transformation.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();

gl.glOrtho(-winWidth, winWidth, -winHeight, winHeight, 100, -100);

// reset the modelview matrix
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

// apply view transformation
glu.gluLookAt(0, 0, 0, centroid.getX(), centroid.getY(), centroid.getZ(), 0.0, 1.0, 0.0);

gl.glScalef(getZoomFactor(), getZoomFactor(), getZoomFactor());
arcBall.getTransform().get(transf);
gl.glMultMatrixf(transf);

// Draw the model.
{
	drawModel(drawable);
}

glOrtho defines a volume. So everything is rendered between -100 and +100 on the z-axis. Obviously the model doesn’t always fit in that volume, so you might want to expact it a bit, say -1000 and +1000, or less if you lose too much precision in your depth-buffer.