Qt_OpenGL: Moving Images in 3D space and moving images in qt_opengl
Qt_OpenGL: Moving Images in 3D space
//. H
#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_OBJECTpublic: 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
# Include "glwidget. h "# include <glut. h ># include <QtGui> # include <QtCore> // Well, I admit that all variables are not good. 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 (); GL_SMOOTH; glClearColor (0.0, 0.0, 0.0); glClearDepth (0.5); glable (latency); glDepthFunc (GL_LEQUAL); glHint (latency, GL_NICEST); glBlendFunc (GL_SRC_ALPHA, GL_ONE); glable (GL_BLEND); // assign the initial value for num star objects (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); // move to glRotatef (title, 1.0, 0.0, 0.0); // rotate title glRotatef (stars [loop] along the X axis. angle, 0.0, 1.0, 0.0); // each star rotates along the Y axis