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

时间:2021-11-08 21:19:23

I have modelview matrix, project view matrix and position slot along with square vertices. I want to get the screen coordinates (X,Y points) of square in the iPhone screen.

我有模型视图矩阵,项目视图矩阵和位置槽以及方形顶点。我想在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);

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

请找附件作为参考

1 个解决方案

#1


2  

Just follow the transformation steps:

只需按照转换步骤操作:

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

#1


2  

Just follow the transformation steps:

只需按照转换步骤操作:

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