《基于MFC的OpenGL编程》Part 13 Quadrics

《基于MFC的OpenGL编程》Part 13 Quadrics

         原文链接
 源码链接
本文在第十篇文章的基础上,为其加入显示各种二次曲面的代码;
Quadrics

     Every quadric has a few settings associated with it. We have to create a quadric first and then customize its settings to render the shape we want. The gluNewQuadric function creates a state variable that describes the current drawing style, orientation, lighting mode, texturing mode and the callback functions. Once we use this function to create a new quadric, we can customize the drawing of shapes by changing its state using functions such as

gluQuadricDrawStyle- which selects the type of OpenGL drawing primitives that are used to draw the shape.

gluQuadricOrientation- which controls the direction of the lighting normals.

gluQuadricNormals- which controls the generation of lighting normals.

gluQuadricTexture - which generates texture coordinates automatically for the quadric.

1、在CMy2OpenGLView类中加入下列变量,用来表示二次曲面(Quadrics)类型:

    //Quadric
    GLuint m_Quadric;

2、加入四个控制二次曲面类型的菜单项及其事件处理函数:

void CMy2OpenGLView::OnQuadricSphere() 
{
	// TODO: Add your command handler code here
	//画一个球体
	m_Quadric=0;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricSphere(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==0)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricCone() 
{
	// TODO: Add your command handler code here
	//画一个圆锥
	m_Quadric=2;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricCone(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==2)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricCylinder() 
{
	// TODO: Add your command handler code here
	//画一个柱体
	m_Quadric=1;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricCylinder(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==1)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);

3、绘制函数修改如下:

void CMy2OpenGLView::OnQuadricSphere() 
{
	// TODO: Add your command handler code here
	//画一个球体
	m_Quadric=0;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricSphere(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==0)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricCone() 
{
	// TODO: Add your command handler code here
	//画一个圆锥
	m_Quadric=2;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricCone(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==2)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricCylinder() 
{
	// TODO: Add your command handler code here
	//画一个柱体
	m_Quadric=1;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricCylinder(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==1)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricDisk() 
{
	// TODO: Add your command handler code here
	//画一个圆盘
	m_Quadric=3;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricDisk(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==3)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CMy2OpenGLView::OnQuadricPartialdisk() 
{
	// TODO: Add your command handler code here
	m_Quadric=4;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateQuadricPartialdisk(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_Quadric==4)
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}


4、把RenderSceneQuadrics()函数放入OnDraw()函数中。

运行结果:《基于MFC的OpenGL编程》Part 13 Quadrics_第1张图片

你可能感兴趣的:(《基于MFC的OpenGL编程》Part 13 Quadrics)