Refer to "OpenGL Primer", "OpenGL coordinate Transformation", "OpenGL draw pipeline Operation Details" and other materials.
Leave a memo under review:)
/*-* Opengl Demo Test * * fredric:2016-7-8*/#include<GLUT/GLUT.h>voiddisplay_demo01 ();voiddisplay_demo02 ();voiddisplay_demo03 ();voiddisplay_demo04 ();/** Main Function*/intMainintargcChar**argv) { /*Glutinit * GLUT initialization*/Glutinit (&argc, argv); /** int Glutcreatewindow (char* name); * Generate a top-level window * char* Name: Window name * return int: Window marker symbol, can glutgetwindow get reference*/Glutcreatewindow ("Fredric Practice Demo"); /** void Glutdisplayfunc (void (*func) (void)); * Display callback function for the current window setting*/Glutdisplayfunc (display_demo04); /** void Glutmainloop () * Glut program enters event loop*/Glutmainloop (); }//****************************************************************************/** Basic Display function Demo*/voiddisplay_demo01 () {/** void Glclear (Glbitfield mask); * Clear buffer value according to mask * Gl_color_buffer_bit: Current writable Color buffer * Gl_depth_buffer_bit: Depth buffer * Gl_accum_buffer_bit: Cumulative buffering * Gl_stencil_buffer_bit: Template buffering*/glclear (gl_color_buffer_bit); /** void GLRECTF (glfloat x1, glfloat y1, glfloat x2, Glfloat y2) * equivalent to: * Glbegin (Gl_polygon); * GLVERTEX2 (x1, y1); * GLVERTEX2 (x2, y1); * GLVERTEX2 (x2, y2); * GLVERTEX2 (x1, y2); * Glend (); * In OpenGL's World coordinate system, (0,0,0) is the center of the screen origin, the entire window range is 1 to 1 * In the following example draws a rectangle to cover all Windows*/GLRECTF (-1.0f, -1.0f,1.0f,1.0f); /** OpenGL Command executes immediately*/Glflush ();}//****************************************************************************/** Draw Basic Graphics Demo*/voiddisplay_demo02 () {//Draw pointsglclear (gl_color_buffer_bit); /** Define one or more sets of elements*/Glbegin (gl_points); /** void Glpointsize (glfloat size); * Defines the size of the grid point, with an initial size of 1*/Glpointsize (5.0f); GLVERTEX2F (-0.5f,0.0f); GLVERTEX2F (0.0f,0.0f); GLVERTEX2F (0.5f,0.0f); Glend (); Glflush (); //Draw a lineglclear (gl_color_buffer_bit); Glbegin (Gl_lines); //in the following example, two lines are drawnGLVERTEX2F (-0.5f,0.0f); GLVERTEX2F (0.5f,0.0f); GLVERTEX2F (-0.5f, -0.3f); GLVERTEX2F (0.5f,0.3f); Glend (); Glflush (); //Drawing Polygonsglclear (gl_color_buffer_bit); Glbegin (Gl_polygon); GLVERTEX2F (-0.5f,0.0f); GLVERTEX2F (0.5f,0.0f); GLVERTEX2F (-0.5f, -0.3f); GLVERTEX2F (0.5f,0.3f); Glend (); Glflush (); //Draw a circle intn = +; floatPI =3.1415926; floatD =0.5f; Glclear (Gl_color_buffer_bit); Glbegin (Gl_polygon); for(inti =0; I < n; i++) {glvertex2f (d*cos (2*pi/n*i), D*sin (2*pi/n*i)); } glend (); Glflush (); }//*********************************************************************************/** Color Basic processing Demo*/voiddisplay_demo03 () {floatPI =3.1415926; Glclear (Gl_color_buffer_bit); Glbegin (Gl_triangle_fan); /** void glcolor3f (glfloat red, Glfloat Green, glfloat blue); * r\g\b three primary color values*/glcolor3f (1.0f,1.0f,1.0f); GLVERTEX2F (0.0f,0.0f); for(intI=0; i<=8; ++i) {glcolor3f (i*0x04,0,0); glvertex2f (cos (i*pi/4), Sin (i*pi/4)); } glend (); Glflush ();}//***********************************************************************************/** Draw three-dimensional basic shape demo * In OpenGL's drawing pipeline, it is divided into two parts: * 1, model observation change: the actual position and angle of the specific object * 2, Projection transformation: The image of 3-dimensional information is mapped to a 2-dimensional screen * This process involves 6 coordinates: * 1, belonging to the model Type Observation Transformation: * 1.1, MC (model coordinate)------the coordinate of the object's own coordinates, such as we say a square point 1, 1, is relative to one of their own origin 0, 0; * 1.2, WC (Word coordinate), world coordinates, unify n multi-object model coordinates into a common coordinate system; * 1.3, VC (view coordinate), observing coordinates, understood as the angle of observation, that is, the position and angle of camera placement * 2, belong to the projection transformation: * 2.1, PC (Project coordinate), the above model transformed 3-dimensional information mapped to 2-dimensional plane, divided into orthogonal, oblique projection, transmission projection, including transmission projection with near small broad concept; * 2.2, NC ( normalization coordinate), normalized the results of the projection transformation into a space of 1 * 3, Viewport transformation: * 3.1 DC (device coordinate), the actual coordinates displayed on the device as a screen*/voiddisplay_demo04 () {/** OpenGL feature is enabled, for example: * gl_depth_test: Enable depth test to achieve occlusion based on coordinates near distance * Gl_alpha_test: Whether it is obscured according to its degree of transparency * gl_auto_normal : Capable of Light reflection * gl_texture_2d: Enable two-dimensional texture*/glenable (gl_depth_test); Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); /** void Glmatrixmode (glenum mode); * Gl_projection: operation of the projected views * Gl_modelview: operation of the Model view * gl_texture: Manipulating Textures*/Glmatrixmode (gl_projection); //sets the current matrix to the unit matrixglloadidentity (); /** void Gluperspective (gldouble Fovy, gldouble aspect, gldouble znear, gldouble zfar) * Fovy: Perspective * ASP ECT: Aspect ratio * Znear: Near Point distance * Zfar: distance from far point*/gluperspective ( the,1,1, +); Glmatrixmode (Gl_modelview); Glloadidentity (); /** Glulookat (gldouble Eyex, gldouble eyey, Gldouble Eyez, gldouble CenterX, gldouble centery, GLdouble CenterZ, Gldouble UpX, gldouble upy, gldouble UpZ) * eyex/y/z: Position of camera in world coordinates * centerx/y/z: Position of object in world coordinates * upx/y/z: Camera orientation (relative World coordinate system)*/Glulookat (0, - -, $,0, -,Ten,0,0,1); glcolor3f (1.0f,0.5f,0.0f); /** GLROTATEF (glfloat angle, glfloat x, glfloat y, glfloat z) * angle: direction of rotation * x/y/z: direction vector of rotation*/Glrotatef (-80.0f,10.0f,5.0f,0.0f); /** void Apientry glutsolidsphere (gldouble radius, glint slices, glint stacks) * radius: Radius * Slices: Number of meridians * Stacks: Number of parallels*/Glutwirecube ( -); Glflush (); }
OpenGL notes (i)