我尝试获取OpenGL模型的3D坐标。我在论坛中发现了这个代码,但我不明白碰撞是如何检测到的。使用2D屏幕坐标获取3D模型坐标gluUnproject
-(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;
}
}
在我看来有一个休息条件失踪。我什么时候可以找到collisionPoint? 另一个问题是: 如何操作这些碰撞点的纹理?我认为我需要相应的顶点!?
问候
GluUnproject用于采用屏幕坐标并使用当前的ModelView矩阵将它们解除投影到场景中。因此,给出* current *变换矩阵,它给出了屏幕上某个x和y位置的世界坐标。我不太明白你如何使用它来进行物理计算? – Bartvbl
碰撞算法被称为射线追踪。我只想将对象的相应3D像素添加到我的2D触摸位置。 – Philsen