Search code examples
iosobjective-copengl-esglkit

Extracting X and Y from a GLKVector3 ? iOS


Say I have a GLKVector3 and want to read only the x and y values as CGPoints - how can I do this ?


Solution

  • In the GLKVector3 doc, there is type definition:

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

    There for there are 3 options:

    GLKVector3's v attribute which is a float[3] array of {x,y,z}

    i.e.:

    GLKVector3 vector;
    
    ...
    
    float x = vector.v[0];
    float y = vector.v[1];
    float z = vector.v[2];
    
    CGPoint p = CGPointMake(x,y); 
    

    Then there are also float attributes x,y,z or less relevant r,g,b or s,t,p for different uses of the vector type:

    CGPoint p = CGPointMake(vector.x,vector.y);