OpenGL path (8) add illumination effects and keyboard control

Source: Internet
Author: User

Add the illumination effect to OpenGL and use a keyboard to zoom in or out.

W key zoom in s key zoom out d key switch light

The preview effect is as follows:


The source code is as follows:

# Include <Gl/glut. h> # include <Gl/Glaux. h> # include <stdio. h> # pragma comment (Lib, "opengl32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glut. lib ") # pragma comment (Lib," Glaux. lib ") uint g_bmp [1]; // texture No. glfloat lightambient [] = {1.5f, 1.5f, 1.5f, 1.0f}; // environment light parameter, semi-bright white glfloat lightdiffuse [] = {1.0f, 1.0f, 1.0f, 1.0f}; // diffuse light parameter, glfloat lightposition [] = {0.0f, 0.0f, 0.0f, 1.0f}; // void box (float X, float y, float Z) // cube {glpushmatrix (); glscalef (x, y, z); glable (gl_texture_2d); // glbegin (gl_quads); gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f ); // frontend vertex (1.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f); gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); vertex (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f); gltexcoord2f (1.0 F, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f); // post gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f ); random (0.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f); random (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, 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 ); // 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); gltexco Ord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); then (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f); then (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 the glpopmatrix ();} bool loadt Exture (char * filename, gluint & texture) {aux_rgbimagerec * pimage = auxdibimageloada (filename); // load bitmap if (pimage = NULL) return false; glgentextures (1, & texture); // generate the texture glbindtexture (gl_texture_2d, g_bmp [0]); // apply the texture glubuild2dmipmaps (outputs, 4, pimage-> sizex, pimage-> sizey, gl_rgb, gl_unsigned_byte, pimage-> data); // map data return true;} void Init () {loadtexture ("I: \ 4.bmp", g_bmp [0]); gllightfv (gl_l Ight1, gl_ambient, lightambient); // sets the environment light gllightfv (gl_light1, gl_diffuse, lightdiffuse); // sets the diffuse light gllightfv (gl_light1, gl_position, lightposition ); // set the position of the light source to gl_light1; // enable the 1 light source to gl_lighting; // open light} float S = 1, r = 0; // zoom in and out multiple and rotate the parameter void renderscene (void) {glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and the depth bufferglloadidentity (); // reset the viewgltrans Latef (0.0f, 0.0f,-5); glrotatef (R, 1.0f, 1.0f, 0.0f); glscalef (S, S, S); box (1, 1, 1 ); R + = 0.01; // rotate fig ();} void changesize (int w, int h) {If (H = 0) H = 1; float ratio = 1.0 * W/H; glmatrixmode (gl_projection); // unitized projection matrix. Glloadidentity (); glviewport (0, 0, W, H); // you can specify the window size as gluperspective (45, ratio, 1, 1000 ); // set the correct projection matrix glmatrixmode (gl_modelview); // set the Model View matrix glloadidentity (); glulookat (0.0, 0.0, 5.0, 0.0, 0.0,-1.0, 0.0f, 1.0f, 0.0f); // set the observation point} int light = 1, BP = 0; void keyfunc (unsigned char CH, int X, int y) {If (CH = 'W') // The sending size is reduced by S + = 0.1; else if (CH = 's') S-= 0.1; if (CH = 'D') // d key switch light {If (light) {Light = 0; gldisable (gl_lighting); printf ("off \ n ");} else {Light = 1; glable (gl_lighting); printf ("On \ n") ;}} int main (INT argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_depth | glu_double | glu_rgba); gluinitwindowposition (100,100); gluinitwindowsize (320,320); glucreatewindow ("Hello OpenGL"); Init (); gludisplayfunc (renderscene); glutidlefunc (renderscene); // call the function glureshapefunc (changesize) when the program is idle; // specify the gl_depth_test callback function when the window shape changes ); glukeyboardfunc (keyfunc); // The keyboard callback function: glumainloop (); Return 0 ;}


The textures used are as follows:


OpenGL path (8) add illumination effects and keyboard control

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.