其他分享
首页 > 其他分享> > OpenGL二维世界坐标和屏幕坐标系之间的转换

OpenGL二维世界坐标和屏幕坐标系之间的转换

作者:互联网

ScreenToWorld2D

glm::vec3 xxxCamera2D::ScreenToWorld2D(glm::vec3 screen_pt)//screen point-->(x,y,0)
{
	glm::vec3 normalized_screen_pt;

	glm::vec3 world_pt;
	float degree = PI / 2;
	glm::vec3 pt;
	/*方法1:使用矩阵
	前提:世界坐标系固定,求得旋转至屏幕坐标中心为原点的屏幕坐标系的变换矩阵,再求逆*/
	/*Step1:比例伸缩,求伸缩矩阵*/
	glm::vec4 world_center_pt(0);
	glm::vec3 vec_scale(m_fWidth / GetViewWidth(), 1, m_fHeight / GetViewHeight());
	glm::mat4 M_scale = glm::scale(vec_scale);
	/*Step2:平移到m_Eye坐标,求平移矩阵*/
	glm::mat4 M_translate = glm::translate(m_Eye);
	/*Step3:绕Y轴旋转,求旋转矩阵*/
	glm::vec3 normal(0, 1, 0);
	glm::mat4 M_rotate;
	glm::mat4 M_convert;
	glm::mat4 M_convert_inverse;
	/*Screen中心为原点的screen_pt坐标*/
	glm::vec3 converted_screen_center(m_fWidth / 2, 0, m_fHeight / 2);
	glm::vec3 converted_screen_pt;
	converted_screen_pt = (glm::vec3)((screen_pt.x - m_fWidth / 2), 0, (screen_pt.y - m_fHeight / 2));
	glm::vec4 converted_pt_vec4(converted_screen_pt.x, converted_screen_pt.y, converted_screen_pt.z, 1);
	glm::vec4 world_pt_vec4;

	//注意:这里UP_NEGATIVE_Z和UP_POSITIVE_X都已经改成了Z轴垂直朝上的计算方法,
	//但是,UP_POSITIVE_Z和UP_NEGATIVE_X还不确定是否正确
	switch (m_iViewDirection)
	{
	case UP_POSITIVE_Z:
		world_pt = InitialScreenToWorld(screen_pt);
		break;
	case UP_POSITIVE_X:
		/*Scale:Screen坐标系原点先修改为屏幕中心,再拉伸*/
		world_pt = InitialScreenToWorld(screen_pt);
		/*Rotate*/
		pt.x = (world_pt.x - m_Eye.x)*cos(degree) - (world_pt.z - m_Eye.z)*sin(degree) + m_Eye.x;
		pt.y = 0;
		pt.z = (world_pt.z - m_Eye.z)*cos(degree) + (world_pt.x - m_Eye.x)*sin(degree) + m_Eye.z;
		world_pt = pt;
		break;
	case UP_NEGATIVE_Z:
		/*Scale:Screen坐标系原点先修改为屏幕中心,再拉伸*/
		world_pt = InitialScreenToWorld(screen_pt);
		/*Rotate*/
		degree *= 2;
		pt.x = (world_pt.x - m_Eye.x)*cos(degree) - (world_pt.z - m_Eye.z)*sin(degree) + m_Eye.x;
		pt.y = 0;
		pt.z = (world_pt.z - m_Eye.z)*cos(degree) + (world_pt.x - m_Eye.x)*sin(degree) + m_Eye.z;
		world_pt = pt;
		/*M_rotate = glm::rotate(degree * 2, normal);
		M_convert = M_translate *M_rotate* M_scale;
		M_convert_inverse = glm::inverse(M_convert);
		world_pt_vec4 = M_convert_inverse*converted_pt_vec4;
		world_pt = glm::vec3(world_pt_vec4.x, world_pt_vec4.y, world_pt_vec4.z);*/
		break;
	case UP_NEGATIVE_X:
		/*Scale:Screen坐标系原点先修改为屏幕中心,再拉伸*/
		world_pt = InitialScreenToWorld(screen_pt);
		/*Rotate*/
		degree *= 3;
		pt.x = (world_pt.x - m_Eye.x)*cos(degree) - (world_pt.z - m_Eye.z)*sin(degree) + m_Eye.x;
		pt.y = 0;
		pt.z = (world_pt.z - m_Eye.z)*cos(degree) + (world_pt.x - m_Eye.x)*sin(degree) + m_Eye.z;
		world_pt = pt;
		break;
	default:
		break;
	}
	//world_pt = glm::vec3(-world_pt.x, -world_pt.y, -world_pt.z);
	return world_pt;
}




glm::vec3 xxxCamera2D::ScreenToWorld2D(glm::vec3 eye, glm::vec3 screen_pt)//screen point-->(x,y,0)
{
	glm::vec3 normalized_screen_pt;
	normalized_screen_pt.x = (float)screen_pt.x / m_fWidth;
	normalized_screen_pt.y = 0;
	normalized_screen_pt.z = (float)screen_pt.y / m_fHeight;

	normalized_screen_pt.x -= 0.5f;
	normalized_screen_pt.z -= 0.5f;

	glm::vec3 world_pt = normalized_screen_pt * glm::vec3(GetViewWidth(), 0, -GetViewHeight());

	world_pt.x += eye.x;
	world_pt.z += eye.z;
	return world_pt;

}

WorldToScreen

glm::vec3 xxxCamera2D::InitialWorldToScreen(glm::vec3 world_pt)
{
	glm::vec3 normalized_screen_pt, screen_pt;
	world_pt.x -= m_Eye.x;
	world_pt.z -= m_Eye.z;

	normalized_screen_pt = world_pt / glm::vec3(GetViewWidth(), 0, -GetViewHeight());

	normalized_screen_pt.x += 0.5f;
	normalized_screen_pt.z += 0.5f;

	screen_pt.x = (float)normalized_screen_pt.x*m_fWidth;
	screen_pt.y = (float)normalized_screen_pt.z*m_fHeight;
	screen_pt.z = 0;

	return screen_pt;
}

标签:Eye,glm,pt,OpenGL,screen,世界坐标,vec3,world,坐标系
来源: https://blog.csdn.net/weixin_38980073/article/details/121148944