将2d屏幕坐标转换为3d世界坐标

时间:2015-05-17 03:06:19

标签: android c++ ios opengl 3d

我想将2d屏幕坐标转换为3d世界坐标。

我把一个黑客放在一起,但想要一个可靠的解决方案。如果可能,请使用下面示例中的等式/算法(如果已经有链接,您是否可以使用该链接显示下面问题的解决方案)

我的环境是使用OpenGL 2.0的Android和iOS的Java / C ++

解决问题

屏幕 (0,0)是左上角,右下角是screenWidth和screenHeight。

屏幕宽度= 667; screenHeight = 375;

//相机的位置,相机正在看的位置和相机方向

CamPos(0.0f,0.0f,11.0f); CamLookAt(0.0f,0.0f,10.5f); CamDir(0.0F,1.0F,0.0F);

// The Frustum

左= 1.3f;右= 1.3F;底= 1.0F;顶= 1.0F;邻近= 3.0F;远= 1000.0f;

的OBJ

//对象在3d空间x,y,z和缩放中的位置

objPos(1.0f,-.5f,5.0f); objScale(.1F,.1F,0.0F);

问题是如何将(600,200)屏幕坐标(缩放为(。1f,.1f.0.0f))转换为对象的(1.0f, - 。5f,5.0f)世界坐标检查它们是否发生碰撞(我正在进行简单的盒子碰撞,只需要将2d屏幕坐标转换为世界)。我会把我下面的黑客,但我相信有更好的答案。预先感谢您的任何帮助。请使用上面的数字来说明算法的工作原理。

哈克

//this is 0,0 in screen coordinates//-Cam_Offset =hardcoded to camera's pos
float convertedZeroY= (((mCam->right)*(Cam_Offset))); //2
float convertedZeroX= -(((Cam_Offset)+convertedZeroY));

convertedZeroY=convertedZeroY/2;
convertedZeroX=convertedZeroX/2;
//this is the length from top to bottom in game coordinates
float normX=convertedZeroX*2;
float normY=convertedZeroY*2;

//this is the screen coordinates converted to 3d world coordinates
mInput->mUserInputConverted->mPos.x=(-(((mInput->x)/mCam->mScreenW)*normX)+convertedZeroX);
mInput->mUserInputConverted->mPos.y=(-(((mInput->y)/mCam->mScreenH)*(normY))+convertedZeroY);

0 个答案:

没有答案
相关问题