iOS:如何从opengl模型视图和投影矩阵中获取屏幕(X,Y点)?

时间:2017-04-10 08:56:33

标签: ios opengl-es opengl-es-2.0

我有模型视图矩阵,项目视图矩阵和位置槽以及方形顶点。我想在iPhone屏幕上获得正方形的屏幕坐标(X,Y点)。

static GLfloat rectVerts[] = {-0.1f,  -0.1f, 0.0f,-0.1f,
    0.1f, 0.0f,0.1f, 0.1f,
    0.0f,0.1f, -0.1f, 0.0f
    ,0.0f,0.0f,0.0f,1.0f};
GLKVector4 position_Vector = GLKVector4MakeWithArray(rectVerts);

float p_clip[4];
for (int i=0;i<4;i++){
    p_clip[i]=0;
}
for (int i=0;i<4;i++){
    for (int j=0;j<12;j++){
        p_clip[i] += ( modelViewprojection.m[i + j] * position_Vector.v[j]);
    }
}
float p_ndc[4];
p_ndc[0] = p_clip[0] / p_clip[3];
p_ndc[1] = p_clip[1] / p_clip[3];
p_ndc[2] = p_clip[2] / p_clip[3];
p_ndc[3] = p_clip[3] / p_clip[3];

GLint view[4]={0};
glGetIntegerv(GL_VIEWPORT,view);
CGRect rect =  CGRectMake(view[0], view[1], view[2], view[3]);
float x = (rect.origin.x + rect.size.width) *  (p_ndc[0] + 1) /2;
NSLog(@"Test x = %f",x);

请找到参考enter image description here

的附件

1 个答案:

答案 0 :(得分:2)

只需按照转换步骤操作:

p_clip = P · MV · p
p_ndc = p_clip/p_clip.w
p_viewport.xy = viewport.xy + viewport.wh · (p_ndc.xy + 1) / 2