# NeHe教程Qt实现——lesson11

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

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 )
}

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

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
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
}

• 本文已收录于以下专栏：

## NeHe教程Qt实现——lesson01

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

## NeHe教程Qt实现——lesson06

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

## NeHe教程Qt实现——lesson17

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

## NeHe教程Qt实现——lesson05

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

## Qt下使用OpenGL（17）-根据NeHe的教程改写的

举报原因： 您举报文章：NeHe教程Qt实现——lesson11 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)