How to centralize a model in a view?

Started by
1 comment, last by Mateus Junior 11 years, 1 month ago
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)
Advertisement

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

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

ops! My bad.
But in the code is correct.

This topic is closed to new replies.

Advertisement