How to get CATransform3D from Projection and ModelView matrices?

后端 未结 5 974
萌比男神i
萌比男神i 2020-12-06 07:55

all,

I\'ve got an iphone project that draws a 3D model using OpenGL-ES for a given model view matrix and given projection matrix. I needed to replace 3D model with C

5条回答
  •  情话喂你
    2020-12-06 08:31

    I just got rid of projection matrix and it is the best variant I've got:

    - (void)adjustTransformationOfLayerWithMarkerId:(NSNumber *)markerId forModelViewMatrix:(NSArray *)modelViewMatrix
    {   
        CALayer *layer = [self.imageLayers objectForKey:markerId];
    
                ...
    
        CATransform3D transform = CATransform3DIdentity;        
        CGFloat *p = (CGFloat *)&transform;
        for (int i = 0; i < 16; ++i) {
            *p = [[modelViewMatrix objectAtIndex:i] floatValue];
            ++p;
        }
    
        transform.m44 = (transform.m43 > 0) ? transform.m43/kZDistanceWithoutDistortion : 1;
    
        CGFloat angle = -M_PI_2;
        if (self.delegate.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)      { angle = M_PI; }
        if (self.delegate.interfaceOrientation == UIInterfaceOrientationLandscapeRight)     { angle = 0; }
        if (self.delegate.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { angle = M_PI_2; }
        transform = CATransform3DConcat(transform, CATransform3DMakeRotation(angle, 0, 0, -1));
    
        transform = CATransform3DConcat(CATransform3DMakeScale(-1, 1, 1), transform);
    
            // Normalize transformation
        CGFloat scaleFactor = 1.0f / transform.m44;
        transform.m41 = transform.m41 * scaleFactor;
        transform.m42 = transform.m42 * scaleFactor;
        transform.m43 = transform.m43 * scaleFactor;
        transform = CATransform3DScale(transform, scaleFactor, scaleFactor, scaleFactor);
        transform.m44 = 1;
    
        BOOL disableAction = YES;
    
                ...
    
        [CATransaction begin];
        [CATransaction setDisableActions:disableAction]; // Disable animation for layer to move faster
        layer.transform = transform;                
        [CATransaction commit];
    }
    

    It wasn't absolutely precise, but it was accurate enough for my purposes. Deflection becomes noticeable when x or y displacement were about screen size.

提交回复
热议问题