# how to set centroid

## Recommended Posts

I now have the centroid computed for my model and I tried to center it using gluLookAt with my new centroid coordinates as follows ... glu.gluLookAt(0.0, 0.0, 0.0, centroid.getX(), centroid.getY(), centroid.getZ(), 0.0, 1.0, 0.0); But this didn't change anything. Is this the correct function to use to center the model? This is located in my display() methods where I do my view tranformations. My problem I'm trying to fix is ... to center an object in the middle of the screen (single object model in orthographic mode). I read in some points from a cad like file to render an object on screen and when I rotate it, the center of rotation is around the center of one of the far ends of the box so it rotates funny and not from the direct center of the box (this model is like a long rectangle). Any help much appreciated.

##### Share on other sites
You need to know the maximum radaius of the object's bounding sphere, or the distance from its centroid to its furthest vertex.

Then it's just something like...
glu.gluLookAt(centroid.getX(), centroid.getY(), centroid.getZ() + max_radious,               centroid.getX(), centroid.getY(), centroid.getZ(),               0.0, 1.0, 0.0);

##### Share on other sites
Quote:
 Original post by FlyingDemonYou need to know the maximum radaius of the object's bounding sphere, or the distance from its centroid to its furthest vertex.Then it's just something like...glu.gluLookAt(centroid.getX(), centroid.getY(), centroid.getZ() + max_radious, centroid.getX(), centroid.getY(), centroid.getZ(), 0.0, 1.0, 0.0);

for that max_radius you may want to make it 2 or 3 times the radius otherwise you will have your camera/eyes positioned right on the object surface.

##### Share on other sites
Quote:
 Original post by FlyingDemonYou need to know the maximum radaius of the object's bounding sphere, or the distance from its centroid to its furthest vertex.Then it's just something like...glu.gluLookAt(centroid.getX(), centroid.getY(), centroid.getZ() + max_radious, centroid.getX(), centroid.getY(), centroid.getZ(), 0.0, 1.0, 0.0);

I tried that, but it causes havoc when scaling the model (scales out of proportion) ... which I think I know how to get around (by doing same for x & y?), but even worse is that it still doesn't center my model. When I rotate it, it still rotates around one of the furthest edges.

##### Share on other sites
"but even worse is that it still doesn't center my model. When I rotate it, it still rotates around one of the furthest edges."

Thats because when you rotate it, you are doing so around its relative v(0,0,0) and not the centroid. I think you have to translate again by its negative centroid, then rotate.

And do what the AP said and multiply the max_radious by 2 or 3 if you havent allready.

##### Share on other sites
Quote:
 Thats because when you rotate it, you are doing so around its relative v(0,0,0) and not the centroid. I think you have to translate again by its negative centroid, then rotate.

Ok, I thought gluLookAt would move the camera to look at the centroid, but now I understand it's the model that has to move to the centroid so that I can rotate around it's center and not the current v(0, 0, 0) ... below is the order in which I'm doing my model tranformation, I scale, then translate, then rotate.

So my problem is I still can't get the model to rotate around the centroid. I thought doing the negative translation for the centroid coordinates after I translated my model would do it but still not working.

Anyone have any ideas what I'm doing wrong?
---

public void display(GLDrawable drawable) {

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

gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);

//glu.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
centroid.getX(), centroid.getY(), centroid.getZ() + (centroid.getMaxRadius_Z() * 3),
0.0, 1.0, 0.0);

// modeling transformation
gl.glScalef(xScale, yScale, zScale);

gl.glTranslatef(xTrans, yTrans, zTrans);

// I multiplied by a -1 to move it to the centroid.
gl.glTranslatef((-1.0f * (float)centroid.getX()),
(-1.0f * (float)centroid.getY()),
(-1.0f * (float)centroid.getZ()));

gl.glTranslatef(xTrans, yTrans, zTrans);

gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

model.draw(active_id);

gl.glDisable(GL.GL_BLEND);
gl.glFlush();
...

##### Share on other sites
Quote:
 glu.gluLookAt(0.0, 0.0, 0.0 + centroid.getMaxRadius_Z(), centroid.getX(), centroid.getY(), centroid.getZ() + (centroid.getMaxRadius_Z() * 3), 0.0, 1.0, 0.0);

Your camera is not postioned infront of the centroid plus its worldspace postion i think( xTrans, yTrans, zTrans) - so the rotations will look weird. Try this...

glu.gluLookAt(centroid.getX() + xTrans,               centroid.getY() + yTrans,               centroid.getZ() + zTrans + centroid.getMaxRadius_Z()*3,               centroid.getX() + xTrans,               centroid.getY() + yTrans,               centroid.getZ() + zTrans,               0.0, 1.0, 0.0);

Quote:
  // modeling transformation gl.glScalef(xScale, yScale, zScale); gl.glTranslatef(xTrans, yTrans, zTrans); // I multiplied by a -1 to move it to the centroid. gl.glTranslatef((-1.0f * (float)centroid.getX()), (-1.0f * (float)centroid.getY()), (-1.0f * (float)centroid.getZ())); gl.glTranslatef(xTrans, yTrans, zTrans); gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f); gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f); gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

Also why are you doing two translations by xTrans, yTrans and zTrans?

Should be something like this...
gl.glScalef(xScale, yScale, zScale);gl.glTranslatef(xTrans, yTrans, zTrans);gl.glTranslatef((-1.0f * (float)centroid.getX()), 		(-1.0f * (float)centroid.getY()), 		(-1.0f * (float)centroid.getZ()));gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);//render

This is all, ofourse, assuming that you have the centroid in object local space and not world space.

##### Share on other sites
[quote]Original post by FlyingDemon
Quote:
 Also why are you doing two translations by xTrans, yTrans and zTrans?Should be something like this...gl.glScalef(xScale, yScale, zScale);gl.glTranslatef(xTrans, yTrans, zTrans);gl.glTranslatef((-1.0f * (float)centroid.getX()), (-1.0f * (float)centroid.getY()), (-1.0f * (float)centroid.getZ()));gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);This is all, ofourse, assuming that you have the centroid in object local space and not world space.

Aside from the additional glTranslatef() which was a mistake on my part, I've got exactly that (and tried more combinations) other than that ... and it seems to make good logical since to me. But it's still not rotating correctly.

I even rendered the centroid with a large point by making PointSize(10.0f) to test if I really had the right centroid and it shows up right in the center of my model, but the translation is not making the model rotate around the centroid. I think the negative translation is not correct somehow, I mean, I'm missing something more.

##### Share on other sites
It seems I'm not capable of visualizing the matrix math myself right now, but looking at some code that I've used before, this seems to be what you're looking for:

glPushMatrix();glTranslatef (trans_coords+centroid_offsets);glRotate (in the x direction);glRotate (in the y direction);glRotate (in the z direction);glTranslatef (-centroid_offsets); // -(trans_coords+centroid_offsets)+trans_coordsDrawPrettyModel();glPopMatrix();

I'm not sure where the scaling ought to go here due to my inability to visualize right now, but I'm sure you can find it by guess and check from here. My guess would be to put it just after the final translate, but I really can't be sure.

##### Share on other sites
Translate by the positive centroid after rotation - I forgot about that.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627674
• Total Posts
2978558

• 11
• 11
• 10
• 12
• 22