Convert GLKMatrix4 and CATransform3D

Is there a way to cast from CATransform3D to GLKMatrix4 or do I always need to manually convert them from value to value? I think casting will be faster.

+6
source share
2 answers

Unfortunately, no at the moment. Most likely, the hidden API call that Apple uses to convert through CALayers and OpenGL, but at the moment this is the best choice. I would just make a utility class that contains this in it.

- (GLKMatrix4)matrixFrom3DTransformation:(CATransform3D)transform { GLKMatrix4 matrix = GLKMatrix4Make(transform.m11, transform.m12, transform.m13, transform.m14, transform.m21, transform.m22, transform.m23, transform.m24, transform.m31, transform.m32, transform.m33, transform.m34, transform.m41, transform.m42, transform.m43, transform.m44); return matrix; } 

Using something like

 CALayer *layer = [CALayer layer]; CATransform3D transform = layer.transform; GLKMatrix4 matrix4 = *(GLKMatrix4 *)&transform; 

The punning type is called. If you do not understand how this works, you should not use it. You cannot type CATransform3D on GLKMatrix4 because their data structures are different in memory.

reference link: punning type

GLKMatrix4

 union _GLKMatrix4 { struct { float m00, m01, m02, m03; float m10, m11, m12, m13; float m20, m21, m22, m23; float m30, m31, m32, m33; }; float m[16]; } typedef union _GLKMatrix4 GLKMatrix4; 

CATransform3D

 struct CATransform3D { CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m24; CGFloat m31, m32, m33, m34; CGFloat m41, m42, m43, m44; }; typedef struct CATransform3D CATransform3D; 
+9
source

Something like this should work:

 CATransform3D t; GLKMatrix4 t2 = *((GLKMatrix4 *)&t); t = *((CATransform3D *)&t2); 

Assuming CATransform3d and GLKMatrix4 have the same column / row settings. (I think so)

-1
source

Source: https://habr.com/ru/post/924051/


All Articles