OPenGL在MFC框架中实现平移缩放

一、鼠标中键实现平移

1、在类属性中添加消息

中键down up

鼠标滚轮移动



2、在构造函数中设置初值

CtestviewView::CtestviewView() 
{
	// TODO: 在此处添加构造代码
	Xshift=0.0;
	Yshift=0.0;
	m_Scale=1.0f;
	SHOWPOINTS=false;
	SHOWMESH=false;
	LButtonSta=false;
	RButtonSta=false;
	ShiftSta=false;
	SHOWTEXTURE=false;
	LIGHT=false;
}

3、添加函数代码

BOOL CtestviewView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if(zDelta>0)
	{
		m_Scale = m_Scale * 1.5 ;	
	}

	if(zDelta<0)
	{
		if (m_Scale > 0)
		{
			m_Scale = m_Scale / 1.5 ;
		}

	}
	return CView::OnMouseWheel(nFlags, zDelta, pt);
}

void CtestviewView::OnMButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	ShiftSta=TRUE;
	CView::OnMButtonDown(nFlags, point);
}

void CtestviewView::OnMButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	ShiftSta=FALSE;
	CView::OnMButtonUp(nFlags, point);
}


4、在鼠标移动的函数中添加如果按下鼠标中键的代码

void CtestviewView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (LButtonSta==TRUE)
	{
	Quat4fT ThisQuat;
	MousePt.s.X=point.x;
	MousePt.s.Y=point.y;
	ArcBall.drag(&MousePt,&ThisQuat);// 更新轨迹球的变量
	Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat);// Convert Quaternion Into Matrix3fT
	Matrix3fMulMatrix3f(&ThisRot, &LastRot);// Accumulate Last Rotation Into This One
	Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);
	}

	else if (ShiftSta==TRUE)
	{
	Xshift-=(mouseX-point.x)/200.0f;
	Yshift+=(mouseY-point.y)/200.0f;
	}
	mouseX=point.x;
	mouseY=point.y;

	CView::OnMouseMove(nFlags, point);
}


5、在CtestviewView::RenderScene() 绘图函数中添加平移、缩放矩阵。


BOOL CtestviewView::RenderScene() 
{
	glClearColor(0.0f,0.0f,0.0f,0.0f);//设置清屏颜色
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); //清除颜色缓冲区和深度缓冲区
	
	glPolygonMode(GL_BACK,GL_FILL);
	glShadeModel(GL_SMOOTH);
	glMatrixMode(GL_MODELVIEW);
	//设置为双缓冲
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glLoadIdentity();
	//添加绘图的代码

	glTranslated(Xshift,Yshift,0);//平移
	glScalef(m_Scale,m_Scale,m_Scale);//缩放
	glMultMatrixf(Transform.M);//旋转
        glFinish();	
	SwapBuffers( wglGetCurrentDC()); //双缓冲
	return TRUE;
}


你可能感兴趣的:(OpenGL,MFC)