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

本文介绍如何使用OpenGL绘制多种二次曲面,包括球体、圆柱、圆锥等,并通过菜单选项实现不同曲面间的切换。
 本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码;
      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 drw 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,在CCY457OpenGLView类中加入下列变量,用来表示二次曲面类型:

    //Quadric
    GLuint m_Quadric;
2,加入四个控制二次曲面类型的菜单项及其事件处理函数

复制代码
void CCY457OpenGLView::OnQuadricSphere() 
{//球体
    m_Quadric = 0;
    InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnUpdateQuadricSphere(CCmdUI* pCmdUI) 
{
    if(m_Quadric == 0)
        pCmdUI->SetRadio(TRUE);
    else
        pCmdUI->SetRadio(FALSE);    
}
void CCY457OpenGLView::OnQuadricCone() 
{//圆锥
    m_Quadric = 2;    
    InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnUpdateQuadricCone(CCmdUI* pCmdUI) 
{
    if(m_Quadric == 2)
        pCmdUI->SetRadio(TRUE);
    else
        pCmdUI->SetRadio(FALSE);    
}
void CCY457OpenGLView::OnQuadricCylinder() 
{//柱体
    m_Quadric = 1;
    InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnUpdateQuadricCylinder(CCmdUI* pCmdUI) 
{
    if(m_Quadric == 1)
        pCmdUI->SetRadio(TRUE);
    else
        pCmdUI->SetRadio(FALSE);    
}
void CCY457OpenGLView::OnQuadricDisk() 
{//圆盘
    m_Quadric = 3;
    InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnUpdateQuadricDisk(CCmdUI* pCmdUI) 
{
    if(m_Quadric == 3)
        pCmdUI->SetRadio(TRUE);
    else
        pCmdUI->SetRadio(FALSE);
}
void CCY457OpenGLView::OnQuadricPartialdisk() 
{////半圆盘
    m_Quadric = 4;
    InvalidateRect(NULL,FALSE);
    
}
void CCY457OpenGLView::OnUpdateQuadricPartialdisk(CCmdUI* pCmdUI) 
{
    if(m_Quadric == 4)
        pCmdUI->SetRadio(TRUE);
    else
        pCmdUI->SetRadio(FALSE);    
}
复制代码
3,绘制函数修改如下:

复制代码
void CCY457OpenGLView::RenderScene ()
{//绘制函数
        glTranslatef(0.0f,0.0f,-5.0f);
        glRotatef(m_xRot,1.0f,0.0f,0.0f);
        glRotatef(m_yRot,0.0f,1.0f,0.0f);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,m_Texture[0]);
        GLUquadricObj* obj;
        obj = gluNewQuadric();
        gluQuadricNormals(obj, GLU_SMOOTH);
        gluQuadricTexture(obj, GL_TRUE);
        switch(m_Quadric)
        {
            case 0:    gluSphere(obj, 1, 20, 20);
                    break;
            case 1:    gluCylinder(obj, 1, 1, 3, 20, 20);
                    break;
            case 2: gluCylinder(obj, 0.1, 1, 3, 20, 20);
                    break;
            case 3: gluDisk(obj, 0.5, 1, 20, 20);
                    break;
            case 4: gluPartialDisk(obj, 0.5, 1, 20, 20, 10, 180);
                    break;
        }
        glDisable(GL_TEXTURE_2D);
}
复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/06/1328269.html,如需转载请自行联系原作者l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值