从GLKVector3中提取X和Y? iOS版

时间:2013-10-20 18:10:39

标签: ios objective-c opengl-es glkit

假设我有一个GLKVector3,并希望只读取x和y值作为CGPoints - 我该怎么做?

2 个答案:

答案 0 :(得分:3)

GLKVector3 doc中,有类型定义:

union _GLKVector3
{
   struct { float x, y, z; };
   struct { float r, g, b; };
   struct { float s, t, p; };
      float v[3];
};
typedef union _GLKVector3 GLKVector3;

有3种选择:

GLKVector3的{​​{1}}属性v数组float[3]

即:

{x,y,z}

对于矢量类型的不同用途,还有浮点属性GLKVector3 vector; ... float x = vector.v[0]; float y = vector.v[1]; float z = vector.v[2]; CGPoint p = CGPointMake(x,y); 或不太相关的x,y,zr,g,b

s,t,p

答案 1 :(得分:1)

GLKVector3被声明为

union _GLKVector3
{
    struct { float x, y, z; };
    struct { float r, g, b; };
    struct { float s, t, p; };
    float v[3];
};
typedef union _GLKVector3 GLKVector3;

因此,最简单,最易读的转换方式是:

GLKVector3 someVector;
…
CGPoint somePoint = CGPointMake(someVector.x,someVector.y);

但请注意,CGPointCGFloat组成,在64位环境中可能是双倍。