見出し画像

Cinderでクリックした2D座標を3Dワールド座標に変換する。

/**
 screen position change to World 3D position
 
 @param screenPos normalized screen position 0~1
 @param cam change world to screen camera
 @return world 3d position vec3
 */
vec3 testApp::worldFromScreen( const vec2 screenPos, const CameraPersp *cam ) {
    // generate a ray from the camera into our world
    float u = screenPos.x;
    float v = screenPos.y;
    // because OpenGL and Cinder use a coordinate system
    // where (0, 0) is in the LOWERleft corner, we have to flip the v-coordinate
    Ray ray = cam->generateRay(u , 1.0f - v, cam->getAspectRatio() );
    
    float result = 0.0f;
    //vec3 planePos = this->mCamera.getEyePoint();
    vec3 planePos = vec3(0.f,0.f,0.f);
    vec3 normal = cam->getViewDirection();
    if ( ray.calcPlaneIntersection( planePos, normal, &result ) ) {
        return ray.calcPosition( result );
    }
    return vec3(0.f,0.f,0.f);
}

プロットされる座標は原点を中心としたカメラと同じ向きの平面上になる。

vec3 planePosとvec3 normalを変更することでプロット座標は操作可能。

vec3 planePos : プロット平面の中心点。

vec3 normal : プロット平面の姿勢。

参考元 : https://forum.libcinder.org/topic/glu-s-gluunproject-substitute