关闭

NeHe教程Qt实现——lesson11

649人阅读 评论(0) 收藏 举报

NeHe 系列教程之十一:飘扬的旗帜

英文教程地址:lesson11

本课实现了类似旗帜飘扬的效果, 其要领就是实时循环改变纹理坐标。本课以第六课的代码为基础。


首先是一些全局变量的定义:

namespace {

    GLfloat     xrot = 0;             // X Rotation
    GLfloat     yrot = 0;             // Y Rotation
    GLfloat     zrot = 0;             // Z Rotation

    float points[45][45][3];     // The Array For The Points On The Grid Of Our "Wave"
    int wiggle_count = 0;           // Counter Used To Control How Fast Flag Waves
    GLfloat hold;                   // Temporarily Holds A Floating Point Value

    GLuint      texture[1];       // Storage For One Texture ( NEW )
}

加载纹理的过程与第六课类似,略。


然后是初始化过程, 这里重点关注纹理坐标points数据的构造:

void MyGLWidget::initializeGL()
{
    loadTextures();
    glEnable(GL_TEXTURE_2D);    // Enable Texture Mapping ( NEW )

    glShadeModel(GL_SMOOTH);   // Enables Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // Black Background
    glClearDepth(1.0f);             // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);        // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);        // The Type Of Depth Test To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    glPolygonMode( GL_BACK, GL_FILL );      // Back Face Is Filled In
    glPolygonMode( GL_FRONT, GL_LINE );     // Front Face Is Drawn With Lines
    // Loop Through The X Plane
    for(int x=0; x<45; x++) {
        // Loop Through The Y Plane
        for(int y=0; y<45; y++) {
            // Apply The Wave To Our Mesh
            points[x][y][0] = float((x/5.0f)-4.5f);
            points[x][y][1] = float((y/5.0f)-4.5f);
            points[x][y][2] = float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));
        }
    }
}

最后是绘制代码:

void MyGLWidget::paintGL()
{
    int x , y;                       // Loop Variables
    float float_x, float_y, float_xb, float_yb;     // Used To Break The Flag Into Tiny Quads
    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,-15.0f);       // Move Into The Screen 12 Units

    glRotatef(xrot,1.0f,0.0f,0.0f);     // Rotate On The X Axis
    glRotatef(yrot,0.0f,1.0f,0.0f);   // Rotate On The Y Axis
    glRotatef(zrot,0.0f,0.0f,1.0f);       // Rotate On The Z Axis

    glBindTexture(GL_TEXTURE_2D, texture[0]);       // Select Our Texture
    glBegin(GL_QUADS);
        for(x = 0; x < 44; x++ ) {
            for (y = 0; y < 44; y++) {
                float_x = float(x)/44.0f;       // Create A Floating Point X Value
                float_y = float(y)/44.0f;       // Create A Floating Point Y Value
                float_xb = float(x+1)/44.0f;        // Create A Floating Point Y Value+0.0227f
                float_yb = float(y+1)/44.0f;        // Create A Floating Point Y Value+0.0227f

                glTexCoord2f( float_x, float_y);    // First Texture Coordinate (Bottom Left)
                glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );

                glTexCoord2f( float_x, float_yb );  // Second Texture Coordinate (Top Left)
                glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );

                glTexCoord2f( float_xb, float_yb ); // Third Texture Coordinate (Top Right)
                glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );

                glTexCoord2f( float_xb, float_y );  // Fourth Texture Coordinate (Bottom Right)
                glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
            }
        }
    glEnd();
    if( wiggle_count == 2 ) {                // Used To Slow Down The Wave (Every 2nd Frame Only)
        for( y = 0; y < 45; y++ ) {           // Loop Through The Y Plane
                hold=points[0][y][2];           // Store Current Value One Left Side Of Wave
                for( x = 0; x < 44; x++) {    // Loop Through The X Plane
                    // Current Wave Value Equals Value To The Right
                    points[x][y][2] = points[x+1][y][2];
                }
                points[44][y][2]=hold;          // Last Value Becomes The Far Left Stored Value
            }
            wiggle_count = 0;               // Set Counter Back To Zero
    }
    wiggle_count++;                     // Increase The Counter

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

运行效果如下所示:





0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:449276次
    • 积分:6480
    • 等级:
    • 排名:第3674名
    • 原创:201篇
    • 转载:18篇
    • 译文:5篇
    • 评论:97条
    最新评论