Qt_OpenGL:3D空间中移动图像,供大家参考,具体内容如下
//.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QtOpenGL>
class QGLWidget;
class QTimer;
typedef struct Stars{
public :
int r, g, b;
GLfloat dist, angle;
}Stars;
class GLWidget : public QGLWidget
{
Q_OBJECT
public :
GLWidget(QWidget *parent = 0);
~GLWidget();
protected :
void initializeGL();
void paintGL();
void resizeGL( int w, int h);
void keyPressEvent(QKeyEvent*);
void timerEvent(QTimerEvent*);
private :
bool fullscreen;
GLfloat rotate_angle;
GLfloat zoom;
GLfloat title;
GLfloat spin;
GLuint loop;
bool twinkle;
GLfloat blend;
private :
void loadTextures();
GLuint texture[1];
};
#endif // GLWIDGET_H
|
//.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include "glwidget.h"
#include <glut.h>
#include <QtGui>
#include <QtCore>
//好吧我承认全部变量不好
GLfloat light_ambient[4] = {0.5,0.5,0.5,1.0};
GLfloat light_diffiuse[4] = {1.0,1.0,1.0,1.0};
GLfloat light_position[4] = {0.0,0.0,2.0,0.0};
static const int num = 50;
static Stars stars[num];
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
fullscreen = false ;
rotate_angle = 0.0;
zoom = -15.0;
title = 90.0;
spin = 0.1;
loop = 0;
twinkle = false ;
blend = false ;
startTimer(5);
}
void GLWidget::initializeGL(){
setGeometry(300,150,500,500);
loadTextures();
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){
stars[loop].angle = 0.0;
stars[loop].dist = ( float (loop)/num) * 5.0;
stars[loop].r = rand () % 256;
stars[loop].g = rand () % 256;
stars[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轴旋转title
glRotatef(stars[loop].angle, 0.0, 1.0, 0.0); //每个星星沿y轴旋转自己的角度
glTranslatef(stars[loop].dist, 0.0, 0.0); //平移
glRotatef(-stars[loop].angle, 0.0, 1.0, 0.0); //沿y轴反转
glRotatef(-title, 1.0, 0.0, 0.0); //沿x轴反转
if (twinkle){ //如果星星闪烁
glColor4ub(stars[num-loop-1].r, stars[num-loop-1].g,
stars[num-loop-1].b, 255);
glBegin(GL_QUADS);
glTexCoord2f(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();
}
//如果星星不闪烁
glRotatef(spin, 0.0f, 0.0f, 1.0f); //沿z轴自转spin
glColor4ub(stars[loop].r, stars[loop].g, stars[loop].b, 255);
glBegin(GL_QUADS);
glTexCoord2f(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();
spin += 0.01f;
stars[loop].angle += float (loop)/num;
stars[loop].dist -= 0.01f; //距离逐渐减小,即越来越靠近屏幕
if (stars[loop].dist < 0){
stars[loop].dist += 5.0f;
stars[loop].r = rand () % 256;
stars[loop].g = rand () % 256;
stars[loop].b = rand () % 256;
}
}
}
void GLWidget::resizeGL( int w, int h){
if ( 0 == h){
h = 1;
}
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)w/(GLdouble)h, 0.1, 100.0);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::keyPressEvent(QKeyEvent *event){
switch (event->key()){
case Qt::Key_T:{
twinkle = !twinkle;
updateGL();
break ;
}
case Qt::Key_B:{
blend = !blend;
if (blend){
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
} else {
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST); //色彩混合和深度缓存不能同时开启
}
updateGL();
break ;
}
case Qt::Key_PageUp:{ //移向屏幕
zoom -= 0.2;
updateGL();
break ;
}
case Qt::Key_PageDown:{ //移向屏幕外
zoom += 0.2;
updateGL();
break ;
}
case Qt::Key_Up:{ //加快旋转速度
title += 0.5;
updateGL();
break ;
}
case Qt::Key_Down:{
title -= 0.5;
updateGL();
break ;
}
case Qt::Key_F1:{
fullscreen = !fullscreen;
if (fullscreen){
showFullScreen();
} else {
setGeometry(300,150,500,500);
showNormal();
}
updateGL();
break ;
}
case Qt::Key_Escape:{
close();
}
}
}
void GLWidget::loadTextures(){
QImage tex, buf;
if (!buf.load( ":Star.bmp" )){
qWarning( "Cannot open the image..." );
QImage dummy(128, 128, QImage::Format_RGB32);
dummy.fill(Qt::green);
buf = dummy;
}
tex = convertToGLFormat(buf);
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_NEAREST);
}
void GLWidget::timerEvent(QTimerEvent *){
updateGL();
}
GLWidget::~GLWidget()
{
}
|
//main.cpp
1
2
3
4
5
6
7
8
9
10
11
|
#include "glwidget.h"
#include <QApplication>
int main( int argc, char *argv[])
{
QApplication a(argc, argv);
GLWidget w;
w.show();
return a.exec();
}
|
运行结果截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010002704/article/details/41889285