Rotation around Center

Hi guys,

I want to rotate a mesh around its center (kinda like in a arcball). The mesh is read in from a .obj file.
The way I have it now, the model is rotating around a point which is not its center (kinda like if you run around a house in circles).

I read that I would have to translate the object to 0,0,0 and then rotate, but I have no ideas which values I should take.

Oh, I am using JOGL btw.

Here’s the code for rendering:

	
public void display(GLAutoDrawable drawable) {
		final GL gl = drawable.getGL();		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		
		if(zoom)
			setProjectionMatrix(gl);
		
		drawBone(gl);

		
		gl.glFlush();
	
private void drawBone(GL gl){
		gl.glPushMatrix();
		
		gl.glTranslated(0, 0,0);		
				
		gl.glRotated(rotX, 1, 0, 0);
		gl.glRotated(rotY, 0, 1, 0);
		
		if(faces != null){
			for(Face f: faces){
				f.draw(gl);
			}
		}

		gl.glPopMatrix();
	}

reshape:

	public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
		GL gl = drawable.getGL();
		if(h<=0){
			this.height=1;
		}
		else{
			this.height = h;
		}
		this.width = w;
		
		aspect = (float)width/(float)height;
		
		// Set the viewport to be the entire window
		gl.glViewport(0, 0, width, height);
		
		// Reset the coordinate system before modifying
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		// Set the correct perspective.
		glu.gluPerspective(DEFAULT_ZOOM*zoomFactor, aspect, 1.0, 1000.0f);
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		glu.gluLookAt(0.0, 0.0, 5.0, 
		0.0, 0.0, 0.0, 
		0.0, 1.0, 0.0);
	
public void setProjectionMatrix(GL gl){
		GLU glu = new GLU();
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		glu.gluPerspective(DEFAULT_ZOOM*zoomFactor, aspect, 1f, 100.0f);
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

Any help is much appreciated. Thank you!

If you plan on rotating the object from it’s center a lot, you probably want to go ahead and center it’s vertices around a 0,0,0 origin on import.

tha following have no effect

gl.glTranslated(0, 0,0);

what you probalby want to do is

gl.glTranslated(-centerX, -centerY,-centerZ);

where centerX,centerY,centerZ is the center of your object vertices in local object space => sum all your vertices and divide each result by number of vertices (inded do it only once and apply same transform to it if you apply/modify vertices of your object (zoom for example…) like if it was another vertex)

for(n=0;n<nbVertices;n++) 
{ 
centerX+=v[n].x;
centerY+=v[n].y;
etc....
} 
centerX/=nbVertices;
centerY/=nbVertices;
etc...

yes, the rotation will be only done around the objects center :slight_smile: how would i center the vertices around the origin at import? with the method DzzD described below or is there a simpler way ?

thanks for your suggestion, i will try this and report back my results :slight_smile:

okay, i did what you said and it seems it’s better now :slight_smile:

instead of the object flying in circles around “my head”, i can see it now in front of me and it’s rotating. but still not around its center, it looks like it’s attached to a point at its lower side and it’s going around circles there :frowning:

may be verify the value of centerx,centery,centerz to see if they match the intended object center location. averaging vertices to find center may not work as expected in severals cases.

hi DzzD

it might be a stupid question, but how can i find out the intended object center location coordinates ?

find the bouding box : find min X,max X min Y max Y, min Z max Z of all vertices this will give you the bouding box then you got a pretty good center point with
centerX=(maxX+minX)/2;
centerY=(maxY+minY)/2;
centerZ=(maxZ+minZ)/2;

Thank you very much for your answer DzzD.

The bounding box coordinates are around the same coordinates as the ones I calculated before with summing up each axis and dividing through number of points, so that seems to be okay.

getCenterVertex at : 119.68530292026117/62.29729093334656/239.88274836337095
Bounding box coordinates at : 116.318302/67.105699/248.7705065

I still don’t get why it is not rotating around the centroid :frowning:

Did I set up my view wrong ?

Sure there isn’t some junk data in the original mesh which is skewing your center?

Kev

I’m not really sure, no. How would I go and check this ?