效果目標:在 3D 場景中讓貼圖星點不斷飛向視點,做出簡單的星空動畫。完整程式碼如下,方便逐段對照。
在 .pro 檔中加入:
LIBS += glut.lib glut32.lib
並把這兩個庫放到 Qt 的 bin 目錄下。
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QtOpenGL>
#include <QWidget>
namespace Ui {
class GLWidget;
}
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QGLWidget *parent = 0);
~GLWidget();
protected:
void initializeGL(); // 初始化 OpenGL 窗口部件
void paintGL(); // 繪製整個窗口,有更新時被呼叫
void resizeGL(int width, int height); // 處理視窗大小變化
void keyPressEvent(QKeyEvent *e);
void loadGLTextures();
void timerEvent(QTimerEvent *);
protected:
bool fullscreen; // 是否全螢幕
bool twinkle; // 星星是否閃爍
bool tp;
bool blend;
GLfloat rotate_angle;
double title;
double zoom;
double tilt;
GLfloat spin;
GLuint loop;
GLuint texture[1];
private:
Ui::GLWidget *ui;
};
#endif // GLWIDGET_H
#include "glwidget.h"
#include "ui_glwidget.h"
#include <QtGui>
#include <QtCore>
#include <QtOpenGL>
#include <GL/GLU.h>
const int num = 50;
typedef struct
{
int r, g, b;
GLfloat dist;
GLfloat angle;
} stars;
stars star[num];
GLWidget::GLWidget(QGLWidget *parent) :
QGLWidget(parent),
ui(new Ui::GLWidget)
{
ui->setupUi(this);
fullscreen = false;
rotate_angle = 0.0;
zoom = -15.0;
title = 90.0;
spin = (GLfloat)0.1;
loop = 0;
twinkle = false;
blend = false;
startTimer(5); // 開啟 5ms 定時器
}
void GLWidget::initializeGL()
{
setGeometry(300, 150, 500, 500);
loadGLTextures();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.5);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
// 為 num 個星星結構賦初值
for (loop = 0; loop < num; loop++)
{
star[loop].angle = 0.0;
star[loop].dist = (float(loop) / num) * 5.0; // 離中心越來越遠
star[loop].r = rand() % 256;
star[loop].g = rand() % 256;
star[loop].b = rand() % 256;
}
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture[0]);
for (loop = 0; loop < num; loop++)
{
glLoadIdentity();
glTranslatef(0.0, 0.0, zoom); // 推向螢幕內部
glRotatef(title, 1.0, 0.0, 0.0); // 繞 x 軸傾斜
glRotatef(star[loop].angle, 0.0, 1.0, 0.0);
glTranslatef(star[loop].dist, 0.0, 0.0);
glRotatef(-star[loop].angle, 0.0, 1.0, 0.0); // 轉回來,讓星點面向視點
glRotatef(-title, 1.0, 0.0, 0.0);
if (twinkle) // 閃爍模式:採用對側星星的顏色
{
glColor4ub(star[num - loop - 1].r, star[num - loop - 1].g,
star[num - loop - 1].b, 255);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
}
glRotatef(spin, 0.0, 0.0, 1.0); // 星點自轉
glColor4ub(star[loop].r, star[loop].g, star[loop].b, 255);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
spin += 0.01f;
star[loop].angle += float(loop) / num; // 角度慢慢增大
star[loop].dist -= 0.01f; // 距離慢慢變小,被吸向中心
if (star[loop].dist < 0) // 飛出視線就重置
{
star[loop].dist += 5.0f;
star[loop].r = rand() % 256;
star[loop].g = rand() % 256;
star[loop].b = rand() % 256;
}
}
}
void GLWidget::resizeGL(int width, int height)
{
if (0 == height) height = 1;
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)width / (GLfloat)height, 0.1, 100.0);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
按鍵配置:T 切換閃爍、B 切換色彩融合、PageUp/PageDown 調整 zoom、上下鍵調整 title 傾斜角、F1 全螢幕切換、Esc 離開。要注意的是,色彩融合與深度測試不能同時開啟,切換 B 時必須成對地 enable/disable。
void GLWidget::loadGLTextures()
{
QImage tex, buf;
if (!buf.load(":/images/2.bmp"))
{
qWarning("Cannot open the image...");
QImage dummy(128, 128, QImage::Format_RGB32); // 找不到圖片時的替代影像
dummy.fill(Qt::green);
buf = dummy;
}
tex = convertToGLFormat(buf); // Qt 影像格式轉為 OpenGL 格式
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void GLWidget::timerEvent(QTimerEvent *) { updateGL(); }
定時器是這個動畫的引擎:每 5 毫秒觸發一次 updateGL(),paintGL() 裡的 dist 遞減就讓星點持續朝視點飛來,形成無限延伸的隧道感。