NeHe教程Qt实现——lesson12

原创 2012年03月24日 17:05:54

NeHe 系列教程之十二: 显示列表

英文教程地址:lesson12

本课主要演示了显示列表的使用。代码基于第六课

显示列表的主要接口:

glGenLists

glNewList

glCallList


具体来看代码中的使用:

namespace {

    ...

    static GLfloat boxcol[5][3]=     // Array For Box Colors
    {
        // Bright:  Red, Orange, Yellow, Green, Blue
        {1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,1.0f}
    };

    static GLfloat topcol[5][3]=     // Array For Top Colors
    {
        // Dark:  Red, Orange, Yellow, Green, Blue
        {.5f,0.0f,0.0f},{0.5f,0.25f,0.0f},{0.5f,0.5f,0.0f},{0.0f,0.5f,0.0f},{0.0f,0.5f,0.5f}
    };

    GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };             // Ambient Light Values ( NEW )
    GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };              // Diffuse Light Values ( NEW )
    GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };            // Light Position ( NEW )

    GLvoid BuildLists()             // Build Box Display List
    {
        box=glGenLists(2);          // Building Two Lists
        glNewList(box,GL_COMPILE);       // New Compiled box Display List
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
            // Back Face
            glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
            // Top Face
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
            // Bottom Face
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Top Right Of The Texture and Quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Top Left Of The Texture and Quad
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
            glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
            // Right face
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
            // Left Face
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
            glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
        glEnd();
        glEndList();

        top = box + 1;
        glNewList(top,GL_COMPILE);
        // Top Face
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
        glEndList();

    }
}

最后 我们再看在绘制代码中的使用:

void MyGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear The Screen And The Depth Buffer
    glLoadIdentity();       // Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,-5.0f);       // Move Into The Screen 5 Units

    glBindTexture(GL_TEXTURE_2D, texture[0]);       // Select Our Texture

    for (yloop=1;yloop<6;yloop++) {
        for (xloop=0;xloop<yloop;xloop++) {
            glLoadIdentity();
            glTranslatef(1.4f+(float(xloop)*2.8f)-(float(yloop)*1.4f),((6.0f-float(yloop))*2.4f)-7.0f,-20.0f);
            glRotatef(45.0f-(2.0f*yloop)+xrot,1.0f,0.0f,0.0f);      // Tilt The Cubes Up And Down
            glRotatef(45.0f+yrot,0.0f,1.0f,0.0f);               // Spin Cubes Left And Right
            glColor3fv(boxcol[yloop-1]);                    // Select A Box Color
            glCallList(box);
            glColor3fv(topcol[yloop-1]);                    // Select The Top Color
            glCallList(top);                        // Draw The Top
        }
    }

    xrot += 0.3f;                             // X Axis Rotation
    yrot += 0.2f;                             // Y Axis Rotation
}

运行效果如下图所示:



相关文章推荐

NeHe教程Qt实现——lesson01

NeHe 系列教程之一: 创建一个OpenGL 窗口 英文教程地址: lesson01 在Qt的实现中, 我们主要依赖QGLWidget类, 我们主要重载三个重要方法 :     voi...

NeHe教程Qt实现——lesson14

NeHe 系列教程之十四: 轮廓字体 英文教程地址:lesson14 本课展示如何创建和显示轮廓字体,即带有尝试的字体,可沿Z轴旋转和移动, 代码基于第一课。 同前一课类似,首先也是要创建字...

NeHe教程Qt实现——lesson11

NeHe 系列教程之十一:飘扬的旗帜 英文教程地址:lesson11 本课实现了类似旗帜飘扬的效果, 其要领就是实时循环改变纹理坐标。本课以第六课的代码为基础。 首先是一些全局变量的...

NeHe教程Qt实现——lesson13

NeHe 系列教程之十三: 位图字体 英文教程地址:lesson13 本课将展示位图字体的创建和显示, 代码基于第一课。 首先是字休库的创建,如下所示: namespace {...

NeHe教程Qt实现——lesson06

NeHe 系列教程之六: 纹理映射 英文教程地址:lesson06 本课以第一课的代码为基础,演示了加载纹理的过程。 首先给出的是绘制几何对象和加载纹理坐标的代码 na...

NeHe教程Qt实现——lesson16

NeHe 系列教程之十四:雾 英文教程地址:lesson16 本课展示产生雾。 相关变量和函数定义: namespace { bool gp; GLuint f...

NeHe教程Qt实现——lesson05

NeHe 系列教程之五: 绘制3D几何对象 英文教程地址:lesson05 本课在第四课的基础上, 绘制真实的3D 对象, 具体如下: void MyGLWidget::pain...

NeHe教程Qt实现——lesson17

NeHe 系列教程之十四:2D纹理字体 英文教程地址:lesson17 本课展示2D纹理生成字体。 首先是字体库的建立及相关字符输出函数: namespace { ...

NeHe教程Qt实现——lesson07

NeHe 系列教程之七: 光照及纹理过滤 英文教程地址:lesson07 本课将以第一课的代码为基础, 实现光照效果。 首先是对象定义与纹理加载的代码: namespace {...

Qt下使用OpenGL(1)-根据NeHe的教程改写的

下面这个网址上有前辈们已经实现的一部分,但发现有些地方对Qt5以上版本的不适用,故做了些修改……有兴趣的可以先看看前辈实现的这个: http://www.qiliang.net/old/nehe_qt...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:NeHe教程Qt实现——lesson12
举报原因:
原因补充:

(最多只允许输入30个字)