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 effective Glbegin (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 decal Glpopmatrix (); } bool Loadtexture (char *filename, Gluint &texture) {Aux_rgbimagerec *pimage = Auxdibimageloada (filename);//Loading bitmap I F (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 * Loop/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
Set model View Matrix} void Keyfunc (unsigned char ch, int x, int y) {if (ch = = ' W ')//hair large reduced s + = 3;
else if (ch = = ' s ') s-= 3;
if (ch = = ' R ') {key_r = 1;
Flash =!flash;
} else Key_r = 0;
} int main (int argc, char * argv[]) {glutinit (&ARGC, argv); GlutinitdisplaYmode (glut_depth | glut_double |
GLUT_RGBA);
Glutinitwindowposition (100, 100);
Glutinitwindowsize (800, 600);
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: