欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ogre-捏脸-三维坐标投影到屏幕坐标

程序员文章站 2024-03-25 20:04:28
...

详细原理可查看博客:

https://blog.csdn.net/miyu1994/article/details/106554004

 


bool World2Screen(Vector3 objPos, Vector2& screenPos)
{
    //视图矩阵
	Matrix4 viewMat = m_camera->getViewMatrix();
    //透视投影矩阵
	Matrix4 projMat = m_camera->getProjectionMatrix();
 
 
	Vector4 inP = Vector4(objPos.x, objPos.y, objPos.z ,1.0);
    //先做视图变换
	Vector4 outP = viewMat * inP;
    //再做投影变换
	outP = projMat * outP;
 
	if(outP.w <= 0.0f)
		return false;
 
    //齐次坐标
	outP.x /= outP.w;
	outP.y /= outP.w;
	outP.z /= outP.w;
 
    //[-1,1]->[0,1]
	outP.x = outP.x*0.5 + 0.5;
	outP.y = outP.y*0.5 + 0.5;
	outP.z = outP.z*0.5 + 0.5;
 
	outP.x = outP.x;
	outP.y = (1-outP.y);
 
	// outP.x = outP.x * mWindow->getWidth();
	// outP.y = (1-outP.y) * mWindow->getHeight();

    //返回归一后的坐标
	screenPos.x = outP.x;
	screenPos.y = outP.y;

	return true;
}

 

相关标签: OGRE