OpenGL notes (2)

Source: Internet
Author: User

/*-* Opengl Demo Test * * fredric:2016-7-10*/#include<GLUT/GLUT.h>#include<stdlib.h>voiddisplay_demo01 ();voidDisplay_demo1_idle ();intMainintargcConst Char*argv[]) {Glutinit (&argc, argv); Glutcreatewindow ("Fredric Practice Demo");        Glutdisplayfunc (DISPLAY_DEMO01); /** void Apientry glutidlefunc (void (*func) (void)) * Global callback function when no window event occurs*/Glutidlefunc (&display_demo1_idle);    Glutmainloop (); }//****************************************************************************Static intG_angle =0;/** Basic animation and Lighting effects * Four light source models in 3D graphics: * 1, point light Source: light source from a certain point in all directions, such as the sun * 2, Infinity Light Source: For the irradiated object, light parallel intake, such as infinity of sunlight; * 3, directional light: In a certain direction at a specific angle Internal intake, such as flashlights, lights, etc. * 4, ambient light: from all angles have projected into the scene of Light * PS: In OpenGL also consider the light source of their own light * * Light will be attenuated with the increase in distance, the degree of attenuation becomes the attenuation factor K, the directional light source in the direction of deviation will also decay * at the same time   Materials need to consider reflection in the light, divided into: * 1, slow reflection: light emitted into the object, its reflection is in any direction; * 2, specular reflection: the light is emitted to the object, its reflection is based on the normal direction of the irradiated object; * * In general, a 3D image vertex in OpenGL is drawn based on the following formula: * Vertex color = material Spontaneous light color item +∑ (ambient light color + attenuation factor X (Diffuse item + specular item))*/voiddisplay_demo01 () {glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);            Glshademodel (Gl_smooth);            Glmatrixmode (gl_projection);            Glloadidentity (); Gluperspective ( the,1,1, +);            Glmatrixmode (Gl_modelview);            Glloadidentity (); Glulookat (0, - -, $,0, -,Ten,0,0,1); glcolor3f (0.0f,0.4f,0.2f); Glrotatef (-(G_angle *10.0f),10.0f,5.0f,0.0f); //Set the light{glfloat light_position[]= {200.0f,300.0f,400.0f,1.0f}; Glfloat light_ambient[]= {1.0f,1.0f,1.0f,1.0f}; Glfloat light_diffuse[]= {1.0f,1.0f,1.0f,1.0f}; Glfloat light_specular[]= {1.0f,1.0f,1.0f,1.0f}; /** GLLIGHTFV (glenum light, glenum pname, const glfloat *params); * Control the position, intensity, color of the light source * Light:gl_light0 indicates that the first light source gl_light1 represents the second light source * Panme: Type of light control * gl_positio         N: Light Source position * Gl_ambient: ambient light intensity in light source * Gl_diffuse: Intensity of scattered light in light source * gl_specular: Intensity of specular reflection light in light source */GLLIGHTFV (gl_light0, gl_position, light_position);        GLLIGHTFV (Gl_light0, gl_ambient, light_ambient);        GLLIGHTFV (Gl_light0, Gl_diffuse, Light_diffuse);                GLLIGHTFV (Gl_light0, Gl_specular, light_specular);        Glenable (GL_LIGHT0);        Glenable (gl_lighting);    Glenable (gl_depth_test); }        //Set Material{glfloat mat_ambient[]= {0.0f,0.5f,0.3f,1.0f}; Glfloat mat_diffuse[]= {0.0f,0.3f,0.2f,1.0f}; Glfloat mat_specular[]= {0.0f,0.8f,0.2f,1.0f}; Glfloat mat_emission[]= {0.5f,0.0f,0.0f,1.0f}; Glfloat mat_shininess=68.0f; /** GLMATERIALFV (glenum face, glenum pname, const glfloat *params) * Set material * Face: positive and negative      * Gl_front: Front * gl_back: Reverse * gl_front_and_back: positive and negative * pname: Material Properties * Gl_ambient: Ambient Color of material * Gl_diffuse: scattering color of material * Gl_specular: Specular color of material * gl_emission: Hair of material Light color * gl_shininess: Specular reflectance index, 0~128 value is smaller and coarser*/GLMATERIALFV (Gl_front, gl_ambient, mat_ambient);        GLMATERIALFV (Gl_front, Gl_diffuse, Mat_diffuse);        GLMATERIALFV (Gl_front, Gl_specular, mat_specular);        GLMATERIALFV (Gl_front, gl_emission, mat_emission);    Glmaterialf (Gl_front, gl_shininess, mat_shininess); }                //Glutsolidsphere (100,30,30);Glutsolidcube ( the);        Glflush ();    Glutswapbuffers (); }//callback functionvoidDisplay_demo1_idle () {G_angle+=1; Display_demo01 ();}

OpenGL notes (2)

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.