NeHe 系列教程之九: 在3D空间中移动位图

英文教程地址:lesson09

本课基于第一课的代码, 利用颜色混合的方法,将一个黑白纹理与随机颜色进行混合,产生绚丽的效果。

首先是定义相关变量和数据结构,如下所示:

namespace {bool    twinkle;                        // Twinkling Starsbool    tp;                         // 'T' Key Pressed?const   int num = 50;                         // Number Of Stars To Drawtypedef struct                          // Create A Structure For Star{int r, g, b;                        // Stars ColorGLfloat dist;                       // Stars Distance From CenterGLfloat angle;                      // Stars Current Angle}stars;                              // Structures Name Is Starsstars star[num];                    // Make 'star' Array Of 'num' Using Info From The StructureGLfloat zoom=-15.0f;                        // Viewing Distance Away From StarsGLfloat tilt=90.0f;                     // Tilt The ViewGLfloat spin;                           // Spin Twinkling StarsGLuint  loop;                           // General Loop VariableGLuint  texture[1];                     // Storage For One Texture
}

加载纹理的代码跟之前课程代码类似。

然后是初始化代码:

void MyGLWidget::initializeGL()
{loadTextures();glEnable(GL_TEXTURE_2D);glShadeModel(GL_SMOOTH);   // Enables Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // Black BackgroundglClearDepth(1.0f);             // Depth Buffer Setup//   glEnable(GL_DEPTH_TEST);        // Enables Depth Testing//   glDepthFunc(GL_LEQUAL);        // The Type Of Depth Test To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective CalculationsglBlendFunc(GL_SRC_ALPHA,GL_ONE);           // Set The Blending Function For TranslucencyglEnable(GL_BLEND);                 // Enable Blendingfor (loop=0; loop<num; loop++)               // Create A Loop That Goes Through All The Stars{star[loop].angle=0.0f;              // Start All The Stars At Angle Zerostar[loop].dist=(float(loop)/num)*5.0f;     // Calculate Distance From The Centerstar[loop].r=rand()%256;            // Give star[loop] A Random Red Intensitystar[loop].g=rand()%256;            // Give star[loop] A Random Green Intensitystar[loop].b=rand()%256;            // Give star[loop] A Random Blue Intensity}
}

注意,开启混合的时候要关闭深度测试。

接着是绘制代码,如下所示:

void MyGLWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear The Screen And The Depth BufferglLoadIdentity();       // Reset The Current Modelview MatrixglBindTexture(GL_TEXTURE_2D, texture[0]);       // Select Our Texturefor (loop=0; loop<num; loop++)               // Loop Through All The Stars{glLoadIdentity();               // Reset The View Before We Draw Each StarglTranslatef(0.0f,0.0f,zoom);           // Zoom Into The Screen (Using The Value In 'zoom')glRotatef(tilt,1.0f,0.0f,0.0f);         // Tilt The View (Using The Value In 'tilt')glRotatef(star[loop].angle,0.0f,1.0f,0.0f); // Rotate To The Current Stars AngleglTranslatef(star[loop].dist,0.0f,0.0f);    // Move Forward On The X PlaneglRotatef(-star[loop].angle,0.0f,1.0f,0.0f);    // Cancel The Current Stars AngleglRotatef(-tilt,1.0f,0.0f,0.0f);        // Cancel The Screen Tiltif (twinkle)                    // Twinkling Stars Enabled{// Assign A Color Using BytesglColor4ub(star[(num-loop)-1].r,star[(num-loop)-1].g,star[(num-loop)-1].b,255);glBegin(GL_QUADS);          // Begin Drawing The Textured QuadglTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);glEnd();                // Done Drawing The Textured Quad}glRotatef(spin,0.0f,0.0f,1.0f);         // Rotate The Star On The Z Axis// Assign A Color Using BytesglColor4ub(star[loop].r,star[loop].g,star[loop].b,255);glBegin(GL_QUADS);              // Begin Drawing The Textured QuadglTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);glEnd();                    // Done Drawing The Textured Quadspin+=0.01f;                    // Used To Spin The Starsstar[loop].angle += float(loop)/num;      // Changes The Angle Of A Starstar[loop].dist -= 0.01f;             // Changes The Distance Of A Starif (star[loop].dist<0.0f)            // Is The Star In The Middle Yet{star[loop].dist += 5.0f;          // Move The Star 5 Units From The Centerstar[loop].r = rand()%256;        // Give It A New Red Valuestar[loop].g = rand()%256;        // Give It A New Green Valuestar[loop].b = rand()%256;        // Give It A New Blue Value}}
}

按键T 的处理, 当按下T键时,会有闪烁效果,且亮度更大:

void MyGLWidget::keyReleaseEvent(QKeyEvent *e)
{switch (e->key()) {case Qt::Key_T:tp = false;break;default:QGLWidget::keyReleaseEvent(e);}
}void MyGLWidget::keyPressEvent(QKeyEvent *e)
{switch (e->key()) {case Qt::Key_F:fullscreen = !fullscreen;if (fullscreen) {showFullScreen();} else {resize(640, 480);showNormal();}break;case Qt::Key_T:if (!tp) {tp = TRUE;twinkle = !twinkle;}break;case Qt::Key_Up:tilt -= 0.5f;break;case Qt::Key_Down:tilt += 0.5f;break;case Qt::Key_PageUp:zoom -= 0.2f;break;case Qt::Key_PageDown:zoom += 0.2f;break;case Qt::Key_Escape:QMessageBox::StandardButton reply;reply = QMessageBox::question(NULL, "NeHe","Do you want to exit?",QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes);if (reply == QMessageBox::Yes) {qApp->quit();}break;default:QGLWidget::keyPressEvent(e);break;}
}

运行效果如下所示: (T 键按下)

转载于:https://my.oschina.net/fuyajun1983cn/blog/263935

NeHe教程Qt实现——lesson09相关推荐

  1. NeHe教程Qt实现——lesson01

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

  2. NeHe教程Qt实现——lesson10

    NeHe 系列教程之十:在3D空间中漫游 英文教程地址:lesson10 本课演示了从外部文件中加载数据构建3D模型的实例,代码基于第一课. 首先是3D模型的数据结构定义: namespace {bo ...

  3. NeHe教程Qt实现——lesson07

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

  4. NeHe教程Qt实现——lesson08

    NeHe 系列教程之八: 混合 英文教程地址:lesson08 本课将在第七课的基础上添加颜色混合的代码: namespace { ... bool blend; // Blending OFF/ON ...

  5. NeHe教程Qt实现——lesson16

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

  6. NeHe教程Qt实现——lesson06

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

  7. NeHe教程Qt实现——lesson15

    NeHe 系列教程之十四:纹理 轮廓字体 英文教程地址:lesson15 本课展示如何创建和显示纹理轮廓字体, 代码基于第一课. 首先是字体库的创建: namespace {#define USE_D ...

  8. NeHe教程Qt实现——lesson13

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

  9. NeHe教程Qt实现——lesson14

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

最新文章

  1. 类选择器和所作用的标签一起写为什么不起作用? - CSDN博客
  2. 笔记-项目质量管理-质量保证和质量控制
  3. eclipse中护眼色设置
  4. 别扯了,这才是应对高并发的正确处理思路。
  5. Linux 文件系统IO性能优化
  6. ssms18还原数据_SSMS 18中的静态数据屏蔽
  7. 数学:概率论与数理统计
  8. 数据结构之图的基本介绍
  9. Electron入门——代码压缩与打包exe文件
  10. 在网页右下角添加一个卡通动漫人物
  11. 支付宝《神奇海洋》素材
  12. Android之原始的QQ好友分组实现
  13. 关于相机坐标到投影空间坐标转换的理解
  14. mysql 设置连接超时_如何配置MySQL数据库超时设置
  15. binlog是什么?能做什么?Window下怎么开启binlog?怎么查看binlog日志?利用binlog日志恢复数据
  16. Win10家庭版gpedit.msc命令打不开组策略的解决方案
  17. 【shell】shell脚本实战-sed流编辑器
  18. 中年人可以学计算机吗,中老年人学计算机与上网的操作系统是windows xp还是window7...
  19. 第14集 关于库卡机器人程序与子程序举例介绍
  20. 神经网络和深度学习(5)-- 逻辑回归

热门文章

  1. 小马拉大车,无线网络优化
  2. 项目经理的超越(一)你超越了吗?
  3. mesos+marathon平台搭建
  4. AfxBeginThread的介绍/基本用法和Window多线程使用详解
  5. com.android.providers.telephony.MmsSmsDatabaseHelper
  6. php字符串处理函数大全
  7. 解决ERROR 2003 (HY000): Can't connect to MySQL server on host (111)
  8. linux 进程 地址空间 内存分布 简介
  9. golang runes 字符串 互转
  10. golang 字符串查找总结