The path of OpenGL (10) Brilliant mobile animations

Source: Internet
Author: User

Brilliant animation effect, Flash, W and s key rotation, R key control Flash switch

The code is as follows:

#include <gl/glut.h> #include <gl/glaux.h> #include <stdio.h> #pragma comment (lib, "Opengl32.lib") # pragma comment (lib, "Glut32.lib") #pragma comment (lib, "Glu32.lib") #pragma comment (lib, "Glut.lib") #pragma comment (lib , "Glaux.lib") void Box (float x, float y, float z)//box {Glpushmatrix (); Glscalef (x, y, z); glenable (gl_texture_2d);//Map Active G Lbegin (gl_quads); gltexcoord2f (0.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, 1.0f);//Pre-GLTEXCOORD2F (1.0f, 0.0f); glvertex3f (1.0f, -1.0f, 1.0f), gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f ( -1.0f, 1.0f, 1.0f); gltexcoord2f (1.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, -1.0f);//After gltexcoord2f (1.0f, 1.0f); glvertex3f ( -1.0f, 1.0f, -1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f), gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f, -1.0f, -1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f ( -1.0f, 1.0f, -1.0f);//gltexcoord2f (0.0f, 0.0f); glvertex3f ( -1.0f, 1.0f, 1.0f); gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f, 1.0f, 1.0f); gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f), gltexcoord2f (1.0f, 1.0f); glvertex3f ( -1.0f, -1.0f, -1.0f);//Lower gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, -1.0f, -1.0f); gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f, -1.0f, 1.0f), gltexcoord2f (1.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, 1.0f); gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f, -1.0f, -1.0f);//Left gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f), gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f, -1.0f, 1.0f), gltexcoord2f (0.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, -1.0f);//Right gltexcoord2f (1.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, 1.0f); gltexcoord2f (1.0f, 1.0f); glvertex3f ( -1.0f, 1.0f, 1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f ( -1.0f, 1.0f, -1.0f); Glend (); gldisable (gl_texture_2d);//Cancel Map Glpopmatrix ();} BOOL Loadtexture (char *filename, Gluint &texture) {Aux_rgbimagerec *pimage = Auxdibimageloada (filename),//load bitmap if ( Pimage = = NULL) return false;glgentextures (1, &texture); Generate Map GlbindtexturE (gl_texture_2d, TEXTURE); Stickers effective Glubuild2dmipmaps (gl_texture_2d, 4, Pimage->sizex, Pimage->sizey, Gl_rgb, Gl_unsigned_byte, pImage-> data); Map data return true;} float s = N, r = 0;bool flash, key_r;const int num = 30;typedef struct{int r, G, B; Glfloat Dist; Glfloat angle;} Stars; Stars Star[num];  Glfloat spin; Star rotation Gluint Loop; UINT g_bmp[1];//map number void init () {loadtexture ("i:\\5.bmp", G_bmp[0]); Glshademodel (Gl_smooth); Glclearcolor (0.0f, 0.0f , 0.0f, 0.5f); glcleardepth (1.0f); Glhint (Gl_perspective_correction_hint, gl_nicest);//Real Fine Perspective correction glblendfunc (gl_src_ ALPHA, Gl_one); glenable (gl_blend); for (loop = 0; loop < num; ++loop) {star[loop].angle = 0.0f;star[loop].dist = 1.0f * L Oop/num * 4.0F;STAR[LOOP].R = rand ()% 256;STAR[LOOP].G = rand ()% 256;star[loop].b = rand ()% 256;}} void Renderscene (void) {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Glbindtexture (gl_texture_2d, g_bmp[0]); glenable (gl_texture_2d); for (loop = 0; loop < num; + +) Loop) {glloadidentity (); Gltranslatef (0.0f, 0.0f, -15); Glrotatef (S, 1.0f, 0.0f, 0.0f), Glrotatef (Star[loop].angle, 0.0f, 1.0f, 0.0f); Gltranslatef (Star[loop].dist, 0.0f, 0.0f), Glrotatef (-star[loop].angle, 0.0f, 1.0f, 0.0f), Glrotatef (-S, 1.0f, 0.0f, 0.0f), if (Flash) {Glcolor4ub (star[ NUM-LOOP-1].R, STAR[NUM-LOOP-1].G, star[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); Orbit around Z glcolor4ub (STAR[LOOP].R, STAR[LOOP].G, star[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; Star Revolution Star[loop].angle + = 1.0f * Loop/num; Change the rotation angle star[loop].dist-= 0.01f;Change the distance of the star from the center if (star[loop].dist < 0.0f)//To center {star[loop].dist + = 4.0F;STAR[LOOP].R = rand ()% 256;STAR[LOOP].G = rand ( )% 256;star[loop].b = rand ()% 256;}} Glutswapbuffers ();} void Changesize (int w, int h) {if (H = = 0) H = 1;float ratio = 1.0* W/h;glmatrixmode (gl_projection);//unit projection matrix. Glloadidentity (); glviewport (0, 0, W, h);//Set viewport size for the entire window size gluperspective (, ratio, 1, 1000);//Set the correct projection matrix Glmatrixmode (GL  _modelview);//The following is the setting of the Model View matrix}void keyfunc (unsigned char ch, int x, int y) {if (ch = = ' W ')//hair large reduction s + = 3;else if (ch = = ' s ') s-= 3;IF (ch = = ' R ') {key_r = 1;flash =!flash;} Elsekey_r = 0;} int main (int argc, char * argv[]) {glutinit (&AMP;ARGC, argv); Glutinitdisplaymode (Glut_depth | glut_double | GLUT_RGBA); glutinitwindowposition (glutinitwindowsize); Glutcreatewindow ("Hello OpenGL"); init (); Glutdisplayfunc (Renderscene); Glutidlefunc (Renderscene); Specifies that the function Glutreshapefunc (changesize) is called when the program is idle; Specifies the callback function Glutkeyboardfunc (keyfunc) When the window shape changes,//keyboard callback function Glutmainloop (); return 0;}


Pictures used:


Effect preview:




The path of OpenGL (10) Brilliant mobile animations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.