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.