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. Gallery 之滑动速度的问题
  2. JavaScript跨域方法
  3. 数据挖掘十大经典算法之——KNN 算法
  4. 按照这步骤来刷题,两个月你亦能成为王者
  5. Codechef Coders’Legacy 2018 CLSUMG Sum of Primes
  6. matlab如何将两张图画在一起,如何在MATLAB里面将两个图画在一起
  7. mysql increment by_Mysql设置auto_increment_increment和auto_increment_offset
  8. iphone、Android接收System.Net.Mail发的邮件标题乱码
  9. cstringw转lpctstr_新手必看:CString 和 LPCTSTR等之间的转换大全 | 求索阁
  10. 【Docker】安装并测试安装成功
  11. php mud游戏源码,mud 文字游戏 - 源码下载|游戏|源代码 - 源码中国
  12. 【收藏】DIABLO 2 CD KEY
  13. vba字典学习案例二
  14. 【搜索引擎】Apache Solr 神经搜索
  15. idea 光标 快捷键_idea光标快捷键
  16. Python实现FP树
  17. 还不清楚如何编辑图片上的文字的话,就看看这篇文章吧
  18. 2020年8月腾讯云服务器收费标准(CPU/内存/带宽/磁盘价格表)
  19. BaseMultiItemQuickAdapter 条目position获取
  20. 保留申请的 Google Voice 号码

热门文章

  1. [开发笔记]-未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出...
  2. Go语言连接 zookeeper
  3. 【JAVA秒会技术之秒杀面试官】JavaEE常见面试题(六)
  4. 利用 Swoole 给应用写个防火墙
  5. 数据结构实验指导书(朱素英)
  6. com.android.providers.telephony.MmsSmsDatabaseHelper
  7. 文件上传--Smartupload上传组件【上】
  8. Oracle10g OEM乱码解决
  9. Oracle等待事件说明
  10. linux cpu占用率 监控工具 简介