2012-02-15 62 views
1

要检测的3D世界,通过了iOS的2D屏幕坐标的坐标,有没有除了gluUnProject端口的任何其他可能的方式?GluUnProject为iOS

我一直在摆弄周围上到底这天,我不能看到一些它的窍门。

-(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; 
} 
} 

我的代码很好。是的,我可以很容易地使用一个结构,但当时我太精神上很难做。这是我以后可以回去修理的东西。

Anywho的一点是,使用的NSLog我输出到调试器时后,我用gluUnProject的nearplane的和farplane的不转发结果甚至接近准确。事实上,他们都传达了完全相同的结果,更不用说,第一次点击总是再现x,y,所有等于“nan”。

难道我跳过一些非常重要的位置?

回答

0

有一个在ES2.0没有gluUnProject功能,这是什么端口,您正在使用?也没有GL_MODELVIEW_MATRIXGL_PROJECTION_MATRIX,这很可能是您的问题。

+0

我知道没有,这是我在网上找到的一些代码。这让我不确定要做什么。 我应该用什么来代替'GL_PROJECTION_MATRIX'和'GL_MODELVIEW_MATRIX'? – Fiattarone 2012-04-11 19:28:04