setting centroid to origin

Hey all,

I read in a model and render it. If the model is a square with min/max value’s of …

minX: 0 maxX: 1
minY: 0 maxY: 1
minZ: 0 maxZ: 0

… then the centroid of the model is .5, .5, 0 … so how should I properly handle adjusting the model to the new centroid so that when it’s rotated on the canvas, it’s rotated around the centroid of the model and not the 0, 0 origin where it looks as if it’s off balance or wobbles when it rotates?

I’ve got models with 10 of thousands of points and I’m wondering if I have to shift the values of the points of the model by the values of the centroid … just seems like lots of room for error so wondering if there is a better and easier way?

Untested:


glPushMatrix();
glTranslatef(...initial position...);
glTranslatef(-xOffset, -yOffset, -zOffset);
glRotatef(....orientation...);
glTranslatef(+xOffset, +yOffset, +zOffset);
// render
glPopMatrix();

Well, I was able to do something close to what you listed, but I’m not sure if it’s correct cause I don’t quite understand it (and it’s not what’s listed above, no pushing, popping).

I think I’m trying to do a fixed coordinate system. So I’m thinking of multiplication occuring in the opposite order that the code is listed (chpt. 3 of the red book). All I did was add the glTranslates you had and changed each sign.

But I don’t understand why I translate to the + centroid, apply my arcball rotation, then translate back to the - centroid, which I think is back at the origin since I think this is a fixed coord system. Or would be w/o the second translate. I’m just confused. And what exactly would the push and pop do if I only draw one model? I don’t have methods that draw different parts of the model in different locations like the car, wheel and lugnuts example in the redbook. I just have vertex arrays populated from coords from an input file and draw the entire thing at once.

What I have below works, but … I don’t think it’s what I wanted, or if it’s what was intended. I found out that by doing this, I no longer need a viewing tranformation (or point my camera at the centroid of the model since we’re moving the model to the origin).

Any advice or comments on below would be great.

// my display()


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, 1000, -1000);

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

// Modeling tranformation.
gl.glTranslatef((float) centroid.getX(), (float) centroid.getY(), (float) centroid.getZ());
gl.glScalef(getZoomFactor(), getZoomFactor(), getZoomFactor());

arcBall.getTransform().get(transf);
gl.glMultMatrixf(transf);

gl.glTranslatef((float) -centroid.getX(), (float) -centroid.getY(), (float) -centroid.getZ());

// Draw the model.
drawModel(drawable);