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

OpenGl在3D中绘制鼠标选择框2D

程序员文章站 2024-03-16 18:07:40
...

以下代码实现:当空格键按下时,点击鼠标进行框选,屏幕中出现选择框。

//绘制3D选框
void xxxRender3D::DrawSelectionRectangle3D(){
	if (m_bSpaceKeyDown){
		if (m_bLBDown && m_bMouseMove){
			glm::vec3 firPoint = m_RectSelPt1;//鼠标的第一个点
			glm::vec3 secPoint = m_RectSelPt2;//鼠标的第二个点
			if (abs(firPoint.x - secPoint.x) > 1e-3)
			{
				glDisable(GL_LIGHTING);

				//绘制选择框
				glColor3f(1, 0, 0);

				//glm::vec3 viewdir = m_pDataManager->m_pCamera3D->GetDirection();
				//glm::vec3 offset = glm::vec3(1000 * viewdir.x, 1000 * viewdir.y, 1000 * viewdir.z);

				glm::vec3 upLeftPt, upRightPt, downLeftPt, downRightPt;

				/*upLeftPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(firPoint.x, firPoint.y,0.0)) + offset;
				upRightPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(secPoint.x, firPoint.y,0.0)) + offset;
				downRightPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(secPoint.x, secPoint.y,0.0)) + offset;
				downLeftPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(firPoint.x, secPoint.y, 0.0)) + offset;*/
				upLeftPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(firPoint.x, firPoint.y, 0.0));
				upRightPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(secPoint.x, firPoint.y, 0.0));
				downRightPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(secPoint.x, secPoint.y, 0.0));
				downLeftPt = m_pDataManager->m_pCamera3D->ScreenToWorld(glm::vec3(firPoint.x, secPoint.y, 0.0));


				GLDrawLine(upLeftPt, upRightPt);
				GLDrawLine(upRightPt, downRightPt);
				GLDrawLine(downRightPt, downLeftPt);
				GLDrawLine(downLeftPt, upLeftPt);
				glEnable(GL_LIGHTING);
			}
		}
	}
}