繁体   English   中英

使用2D屏幕坐标gluUnproject获取3D模型坐标

[英]Get 3D model coordinate with 2D screen coordinates gluUnproject

我尝试获取OpenGL模型的3D坐标。 我在论坛上找到了此代码,但我不了解如何检测到冲突。

-(void)receivePoint:(CGPoint)loke
{

GLfloat projectionF[16];
GLfloat modelViewF[16];
GLint viewportI[4];

glGetFloatv(GL_MODELVIEW_MATRIX, modelViewF);
glGetFloatv(GL_PROJECTION_MATRIX, projectionF);
glGetIntegerv(GL_VIEWPORT, viewportI);

loke.y = (float) viewportI[3] - loke.y;

float nearPlanex, nearPlaney, nearPlanez, farPlanex, farPlaney, farPlanez;

gluUnProject(loke.x, loke.y, 0, modelViewF, projectionF, viewportI, &nearPlanex, &nearPlaney, &nearPlanez);
gluUnProject(loke.x, loke.y, 1, modelViewF, projectionF, viewportI, &farPlanex, &farPlaney, &farPlanez);

float rayx = farPlanex - nearPlanex;
float rayy = farPlaney - nearPlaney;
float rayz = farPlanez - nearPlanez;

float rayLength = sqrtf((rayx*rayx)+(rayy*rayy)+(rayz*rayz));

//normalizing rayVector

rayx /= rayLength;
rayy /= rayLength;
rayz /= rayLength;

float collisionPointx, collisionPointy, collisionPointz;

for (int i = 0; i < 50; i++) 
{
    collisionPointx = rayx * rayLength/i*50;
    collisionPointy = rayy * rayLength/i*50;
    collisionPointz = rayz * rayLength/i*50;
}
}

我认为没有中断条件。 我什么时候可以找到碰撞点? 另一个问题是:如何在这些碰撞点处处理纹理? 我认为我需要相应的顶点!

最好的祝福

该代码将光线从您最近的剪切位置带到您的l子位置的远处,然后将其分割为50,并沿着该光线在3D中对点的所有可能位置进行插值。 在循环的出口,在您发布的原始代码中,碰撞点x,y和z是最远点的值。 该代码中没有“碰撞”测试。 您实际上需要针对要与之碰撞的3D对象测试3D坐标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM