位图波浪:Jeff Molofee(NeHe) 的 OPENGL 教程-第十一课

 Jeff Molofee(NeHe) 的 OPENGL 教程 第十一课 Translated by CKER

#include <math.h>                                               // 引入数学函数库中的Sin()

float points[ 45 ][ 45 ][3];                                      // Points 网格顶点数组

int wiggle_count = 0;                                            // 指定旗形波浪的运动速度

GLfloat hold;                                                        // 临时变量

glPolygonMode( GL_BACK, GL_FILL );               // 后表面完全填充

glPolygonMode( GL_FRONT, GL_LINE );            // 前表面使用线条绘制

// 沿X平面循环

for(int x=0; x<45; x++)

{

// 沿Y平面循环

for(int y=0; y<45; y++)

{

// 向表面添加波浪效果

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)

}

}

[x][y][2]最后的值就是一个sine函数计算的结果。Sin()函数需要一个弧度参变量。将float_x乘以40.0f，得到角度值。然后除以360.0f再乘以PI，乘以2，就转换为弧度了。

int DrawGLScene(GLvoid) // Draw Our GL Scene

{

int x, y; // 循环变量

float float_x, float_y, float_xb, float_yb;                // 用来将旗形的波浪分割成很小的四边形

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓冲

glTranslatef(0.0f,0.0f,-12.0f);                               // 移入屏幕12个单位

glRotatef(xrot,1.0f,0.0f,0.0f);                                    // X 轴旋转

glRotatef(yrot,0.0f,1.0f,0.0f);                               // Y 轴旋转

glRotatef(zrot,0.0f,0.0f,1.0f);                                // Z 轴旋转

glBindTexture(GL_TEXTURE_2D, texture[0]);      // 选择纹理

for( x = 0; x < 44; x++ )                                         // 沿 X 平面 0-44 循环(45)

{

for( y = 0; y < 44; y++ )                                         // 沿 Y 平面 0-44 循环(45)

{

float_x = float(x)/44.0f;                                         // 生成X浮点值

float_y = float(y)/44.0f;                                         //生成Y浮点值

float_xb = float(x+1)/44.0f;                                    //X浮点值+0.0227f

float_yb = float(y+1)/44.0f;                                   //Y浮点值+0.0227f

glTexCoord2f( float_x, float_y);                            // 第一个纹理坐标 (左下角)

glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );

glTexCoord2f( float_x, float_yb );                         // 第二个纹理坐标 (左上角)

glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] )

glTexCoord2f( float_xb, float_yb );                       //第三个纹理坐标 (右上角)

glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1

glTexCoord2f( float_xb, float_y ); // 第四个纹理坐标 (右下角)

glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] )

}

}

glEnd(); // 四边形绘制结束

if( wiggle_count == 2 ) // 用来降低波浪速度(每隔2帧一次)

{

for( y = 0; y < 45; y++ ) // 沿Y平面循环

{

hold=points[0][y][2]; // 存储当前左侧波浪值

for( x = 0; x < 44; x++) // 沿X平面循环

{

// 当前波浪值等于其右侧的波浪值

points[x][y][2] = points[x+1][y][2];

}

points[44][y][2]=hold; // 刚才的值成为最左侧的波浪值

}

wiggle_count = 0; // 计数器清零

}

wiggle_count++; // 计数器加一

xrot+=0.3f; // X 轴旋转

yrot+=0.2f; // Y 轴旋转

zrot+=0.4f; // Z 轴旋转

return TRUE; // 返回

}

Bosco (bosco4@home.com)

Visual C++ / OpenIL Code For This Lesson. ( Conversion by Denton Woods )
Visual Basic Code For This Lesson. ( Conversion by Ross Dawson )
Cygwin (FREE Language) Code For This Lesson. ( Conversion by Stephan Ferraro )
Irix Code For This Lesson. ( Conversion by Rob Fletcher )
MacOS X/GLUT Code For This Lesson. ( Conversion by Raal Goff )
MASM Code For This Lesson. ( Conversion by Nico (Scalp) )
Linux Code For This Lesson. ( Conversion by Richard Campbell )
Linux/GLX Code For This Lesson. ( Conversion by Mihael Vrbanec )
Visual Fortran Code For This Lesson. ( Conversion by Jean-Philippe Perois )
Delphi Code For This Lesson. ( Conversion by Marc Aarts )
Mac OS Code For This Lesson. ( Conversion by Anthony Parker )