【发布时间】:2013-10-20 18:10:38
【问题描述】:
假设我有一个 GLKVector3 并且只想读取 x 和 y 值作为 CGPoints - 我该怎么做?
【问题讨论】:
标签: ios objective-c opengl-es glkit
假设我有一个 GLKVector3 并且只想读取 x 和 y 值作为 CGPoints - 我该怎么做?
【问题讨论】:
标签: ios objective-c opengl-es glkit
在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 的v 属性是{x,y,z} 的float[3] 数组
即:
GLKVector3 vector;
...
float x = vector.v[0];
float y = vector.v[1];
float z = vector.v[2];
CGPoint p = CGPointMake(x,y);
然后还有浮点属性x,y,z 或不太相关的r,g,b 或s,t,p 用于向量类型的不同用途:
CGPoint p = CGPointMake(vector.x,vector.y);
【讨论】:
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);
但请注意,CGPoint 由 CGFloats 组成,在 64 位环境中可能是双精度数。
【讨论】: