如何从Projection和ModelView矩阵中获取CATransform3D?

时间:2011-05-18 13:28:18

标签: opengl-es calayer 3d catransform3d projection-matrix

所有,

我有一个iphone项目,它使用OpenGL-ES为给定的模型视图矩阵和给定的投影矩阵绘制3D模型。我需要用CALayer替换3D模型,因此我将模型视图矩阵的值放入CATransform3D结构并将其分配给layer.transform。它运作良好,图层可见并按预期在屏幕上移动,但过了一段时间后我意识到我的图层行为不够精确,我应该考虑投影矩阵。然后出现了一个问题:当我简单地连接两个矩阵时,我的图层看起来很奇怪(它非常小,大约2个像素,而它应该是大约300个,因为它距离很远)或根本不可见。我该如何解决?

这是一段代码:

- (void)adjustImageObjectWithUserInfo:(NSDictionary *)userInfo
{
    NSNumber *objectID = [userInfo objectForKey:kObjectIDKey];
    CALayer *layer = [self.imageLayers objectForKey:objectID];
    if (!layer) { return; }

    CATransform3D transform = CATransform3DIdentity;
    NSArray *modelViewMatrix = [userInfo objectForKey:kModelViewMatrixKey];

      // Get raw model view matrix;
    CGFloat *p = (CGFloat *)&transform;
    for (int i = 0; i < 16; ++i)
    {
        *p = [[modelViewMatrix objectAtIndex:i] floatValue];
        ++p;
    }

      // Rotate around +z for Pi/2 
    transform = CATransform3DConcat(transform, CATransform3DMakeRotation(M_PI_2, 0, 0, 1));

      // Project with projection matrix
    transform = CATransform3DConcat(transform, _projectionMatrix);

    layer.transform = transform; 
}

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:10)

我最近遇到了这个问题(我也使用了ARToolKit),我很失望地看到你没有找到答案。我想你现在已经开始了,但我想出来了,我正在为任何其他失落的灵魂发布它,可能会遇到同样的问题。

对我来说最令人困惑的是,每个人都在谈论通过将m34变量设置为小的负数来进行CALayer透视变换。虽然这确实有效,但它根本没有很多信息。我最终意识到变换的工作方式与其他变换完全一样,它是同源坐标的列主变换矩阵。唯一的特殊情况是它必须组合模型视图和投影矩阵,然后在一个矩阵中缩放到openGL视口的大小。我开始尝试使用样式中的矩阵,其中m34是一个较小的负数,如更详细的解释here,但最终切换到开放的GL样式透视变换,如here所述。事实上,他们彼此equivalent只代表不同的思考变革的方式。

在我们的例子中,我们试图让CALayer变换完全复制一个开放的GL变换。所需要的只是将模型视图,投影和缩放矩阵相乘,并且翻转y轴以考虑设备屏幕原点是左上方并且打开GL是左下方的事实。只要图层锚点位于(.5,.5)并且其位置正好在屏幕的中心,结果将与开放GL的变换相同

void attach_CALayer_to_marker(CATransform3D* transform, Matrix4 modelView, Matrix4 openGL_projection, Vector2 GLViewportSize)
{
//Important: This function assumes that the CA layer has its origin in the 
//exact center of the screen.

Matrix4 flipY = {   1, 0,0,0,
                    0,-1,0,0,
                    0, 0,1,0,
                    0, 0,0,1}; 

//instead of -1 to 1 we want our result to go from -width/2 to width/2, same 
//for height
CGFloat ScreenScale = [[UIScreen mainScreen] scale];
float xscl = GLViewportSize.x/ScreenScale/2;
float yscl = GLViewportSize.y/ScreenScale/2;

//The open GL perspective matrix projects onto a 2x2x2 cube.  To get it onto the
    //device screen it needs to be scaled to the correct size but
//maintaining the aspect ratio specified by the open GL window.
    Matrix4 scalingMatrix = {xscl,0   ,0,0,
                               0,   yscl,0,0,
                               0,   0   ,1,0,
                               0,   0   ,0,1};

//Open GL measures y from the bottom and CALayers measure from the top so at the
//end the entire projection must be flipped over the xz plane.
//When that happens the contents of the CALayer will get flipped upside down.
//To correct for that they are flipped updside down at the very beginning,
//they will then be flipped right side up at the end.

Matrix flipped = MatrixMakeFromProduct(modelView, flipY);
Matrix unscaled = MatrixMakeFromProduct(openGL_projection, flipped);
Matrix scaled = MatrixMakeFromProduct(scalingMatrix, unscaled);

//flip over xz plane to move origin to bottom instead of top
Matrix Final = SiMatrix4MakeFromProduct(flipY, scaled);
*transform = convert_your_matrix_object_to_CATransform3D(Final);
}

此函数采用开放式GL投影和openGL视图大小,并使用它们为CALayer生成正确的变换。应以开放GL场景为单位指定CALayer大小。 OpenGL视口实际上包含4个变量,[xoffset,yoffset,x,y],但前两个不相关,因为CALayer的原点放在屏幕的中心以对应OpenGL 3d Origin。

只需将Matrix替换为您有权访问的通用4x4列主矩阵类。任何工作只会确保您按正确的顺序乘以矩阵。所有这些基本上都在复制OpenGL管道(减去剪辑)。

答案 1 :(得分:2)

我摆脱了投影矩阵,这是我得到的最好的变种:

- (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];
}

这不是绝对精确,但它对我的目的来说足够准确。当x或y位移与屏幕尺寸有关时,偏转变得明显。

答案 2 :(得分:1)

两个可能的问题:

1)连接顺序。经典矩阵数学是从右到左。所以试试

CATransform3DConcat(_projectionMatrix, transform)

2)投影系数的值是错误的。您使用的是什么值?

希望这有帮助。

答案 3 :(得分:0)

您是否考虑过将图层渲染到已应用了正交投影矩阵的GL上下文中?

请参阅Mac上的介绍性评论;这个类在iPhone上是私有的,但原则是一样的。

此外,与CATransform3D相比,OpenGL矩阵在内存中转置。考虑到这一点;虽然大多数结果看起来都是一样的,但有些结果却不一样。

答案 4 :(得分:0)

作为问题的直接答案:投影矩阵旨在输出-1 ... 1范围内的某些内容,而CoreAnimation通常适用于像素。从这个麻烦来了。因此,为了解决这个问题,你应该在将模型视图与投影矩阵相乘之前,减少你的模型以适应-1 ... 1范围(取决于你的世界是什么,你可以将它分成bounds.size)和投影矩阵乘法之后你可以返回像素。

以下是Swift中的代码片段(我个人讨厌Swift,但我的客户喜欢它)我相信它可以被理解。它是为CoreAnimation和ARKit编写的,并测试它是否有效。我希望ARKit矩阵可以被认为是opengl的

func initCATransform3D(_ t_ : float4x4) -> CATransform3D
{
    let t = t_.transpose //surprise m43 means 4th column 3rd row, thanks to Apple for amazing documenation for CATransform3D and total disregarding standard math with Mij notation
    //surprise: at Apple didn't care about CA & ARKit compatibility so no any easier way to do this
    return CATransform3D(
        m11: CGFloat(t[0][0]), m12: CGFloat(t[1][0]), m13: CGFloat(t[2][0]), m14: CGFloat(t[3][0]),
        m21: CGFloat(t[0][1]), m22: CGFloat(t[1][1]), m23: CGFloat(t[2][1]), m24: CGFloat(t[3][1]),
        m31: CGFloat(t[0][2]), m32: CGFloat(t[1][2]), m33: CGFloat(t[2][2]), m34: CGFloat(t[3][2]),
        m41: CGFloat(t[0][3]), m42: CGFloat(t[1][3]), m43: CGFloat(t[2][3]), m44: CGFloat(t[3][3])
    )
}

override func updateAnchors(frame: ARFrame) {
    for animView in stickerViews {
        guard let anchor = animView.anchor else { continue }

        //100 here to make object smaller... on input it's in pixels, but we want it to be more real.
        //we work in meters at this place
        //surprise: nevertheless the matrices are column-major they are inited in "transposed" form because arrays/columns are written in horizontal direction
        let mx = float4x4(
            [1/Float(100), 0, 0, 0],
            [0, -1/Float(100), 0, 0],    //flip Y axis; it's directed up in 3d world while for CA on iOS it's directed down
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        )

        let simd_atr = anchor.transform * mx //* matrix_scale(1/Float(bounds.size.height), matrix_identity_float4x4)
        var atr = initCATransform3D(simd_atr)   //atr = anchor transform

        let camera = frame.camera
        let view = initCATransform3D(camera.viewMatrix(for: .landscapeRight))
        let proj = initCATransform3D(camera.projectionMatrix(for: .landscapeRight, viewportSize: camera.imageResolution, zNear: 0.01, zFar: 1000))

        //surprise: CATransform3DConcat(a,b) is equal to mathematical b*a, documentation in apple is wrong
        //for fun it's distinct from glsl or opengl multiplication order
        atr = CATransform3DConcat(CATransform3DConcat(atr, view), proj)
        let norm = CATransform3DMakeScale(0.5, -0.5, 1)     //on iOS Y axis is directed down, but we flipped it formerly, so return back!
        let shift = CATransform3DMakeTranslation(1, -1, 0)  //we should shift it to another end of projection matrix output before flipping
        let screen_scale = CATransform3DMakeScale(bounds.size.width, bounds.size.height, 1)
        atr = CATransform3DConcat(CATransform3DConcat(atr, shift), norm)
        atr = CATransform3DConcat(atr, CATransform3DMakeAffineTransform(frame.displayTransform(for: self.orientation(), viewportSize: bounds.size)));
        atr = CATransform3DConcat(atr, screen_scale)    //scale back to pixels

        //printCATransform(atr)
        //we assume center is in 0,0 i.e. formerly there was animView.layer.center = CGPoint(x:0, y:0)
        animView.layer.transform = atr
    }
}

P.S。我认为对于那些创造了与Left&amp; amp;右手坐标系,Y轴和Z轴方向,柱主矩阵,浮点数和CGFloat的不兼容性和缺乏CA和ARKit集成在地狱中的地方将特别热......