• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# How to centralize a model in a view?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Mateus Junior  Members

Posted 17 March 2013 - 06:44 PM

Good evening everyone.

I'm having a lot of difficulties in this stage:

I export an object in Blender (.obj).
Import to application and drawing it on the screen.
Until then OK.

The problem is that it is not centralized.
What I'm doing:

Import model;
I set the perspective (GLKMatrix4MakePerspective)
I set the LookAt (GLKMatrix4MakeLookAt)

Calculating the lowest point x, y, z largest point x, y, z.
The center of the object would then be (larges+lowest) / 2 for each (x, y, z)
I make a translation of the object passing these coordinates.

What happens:
Some objects are centered. Others do not.

What am I doing wrong?
Can anyone help me?

The code (Objective-C + OpenGL ES 2.0):
GLfloat eyeZ = 0;
if (MAX(height, width) > 0)
eyeZ = MAX(height, width) * 2;

GLfloat aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 perspectiveMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), aspect, -1.0, 1.0);
GLKMatrix4 lookAtMatrix = GLKMatrix4MakeLookAt(0, 0, eyeZ, 0, 0, 0, 0, 1, 0);
GLKMatrix4 origemMatrix = GLKMatrix4MakeTranslation(-xCenter, -yCenter, -zCenter);
GLKMatrix4 defaultMatrix = GLKMatrix4Identity;

defaultMatrix = GLKMatrix4Multiply(defaultMatrix, perspectiveMatrix);
defaultMatrix = GLKMatrix4Multiply(defaultMatrix, lookAtMatrix);
defaultMatrix = GLKMatrix4Multiply(defaultMatrix, origemMatrix);
self.baseEffect.transform.modelviewMatrix = defaultMatrix;


Thanks.

edit: Correction: (largest-lowest) / 2 for each (x, y, z) to (largest+lowest) / 2 for each (x, y, z)

Edited by Mateus Junior, 18 March 2013 - 10:11 AM.

### #2Ashaman73  Members

Posted 18 March 2013 - 03:03 AM

The center of the object would then be (largest-lowest) / 2 for each (x, y, z)

That's not the center, but the half-size. The center is

(largest +  lowest) / 2

Ashaman

### #3Mateus Junior  Members

Posted 18 March 2013 - 10:09 AM

The center of the object would then be (largest-lowest) / 2 for each (x, y, z)

That's not the center, but the half-size. The center is

(largest +  lowest) / 2