Moving images in qt_opengl: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 ();p rotected: void Initializegl (); void Paintgl (); void Resizegl (int w, int h); void Keypressevent (qkeyevent*); void TimerEvent (qtimerevent*);p rivate: 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>//Okay, I admit, all variables are bad 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); Assigns a value for the Num Star object 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::p aintgl () {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 the inside of the screen Glrotatef (title, 1.0, 0.0, 0.0); Rotate the title Glrotatef along the x-axis (stars[loop].angle, 0.0, 1.0, 0.0); Each star rotates its own angle Gltranslatef (stars[loop].dist, 0.0, 0.0) along the y axis; Pan Glrotatef (-stars[loop].angle, 0.0, 1.0, 0.0); Reverse Glrotatef along the y-axis (-title, 1.0, 0.0, 0.0); Reverse the IF (twinkle) {//If the Stars Blink Glcolor4ub (STARS[NUM-LOOP-1].R, STARS[NUM-LOOP-1].G) along the x-axis, 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 (); }//If the stars do not blink Glrotatef (spin, 0.0f, 0.0f, 1.0f); Rotation along the z axis 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; The distance gradually decreases, that is, getting closer to the screen 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); Color blending and depth caching cannot be enabled at the same time} UPDATEGL (); Break } case qt::key_pageup:{//Move to screen zoom-= 0.2; Updategl (); Break } case qt::key_pagedown:{//move to Off-screen zoom + = 0.2; UPdategl (); Break } case qt::key_up:{//speed up the rotation speed 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 (n., 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
#include "glwidget.h" #include <qapplication>int main (int argc, char *argv[]) { qapplication A (argc, argv); Glwidget W; W.show (); return a.exec ();}
Operation Result:
Moving images in qt_opengl:3d space