Jump to content

  • Log In with Google      Sign In   
  • Create Account

Mateus Junior

Member Since 17 Mar 2013
Offline Last Active Mar 29 2013 12:39 PM

Topics I've Started

How to centralize a model in a view?

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;



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