Reprinted from http://www.gameres.com/
I have limited level. If there is any problem, please ask in the form of the article. Can you discuss it ...
[OpenGL how to use] OpenGL programming similar C programming, the actual interface is C, so familiar C is necessary to use the usual programming library includes: OpenGL Practical Library: Function with GLU Opengl Auxiliary Library: Function with AUX Windows Dedicated Function Library: Function starts with WIN32API: No dedicated prefix
There are 115 core functions in OpenGL, which can use the OpenGL practical library on any OpenGL Practical library, providing advanced calls OpenGL auxiliary libraries to provide a function of beginners to introduce beginners, not guaranteed to use in any platform. But just can be used under Win32, so this lecture will be quantified.
There are two convenient ways to program in Win32: 1 Using auxiliary library 2 Use C based on message-driven programming programming Obviously 1 is simple, starting from here.
[Preparation before] 1 You need to include the following * .lib is included in your project: OpenGL32.lib Glu32.lib Glaux.lib This lecture All example "will" debug from VC5, so from Project-> setting- > link-> general-> object / libary modules Add to three * .lib (these libs, VC4 or higher version have come to join, join, no need to search for files around)
2 Impaired under your running program or / win95 / system / under you need some * .dll dynamic connection library OpenGL32.dll glu32.dll rxddi.dll mga.drv
If anyone needs the above file, tell me, don't tell me, Visual C 5.0
[Programming] Here I will give a small example to make everyone familiar with the programming structure of the auxiliary library:
// Glos.h //// this is an OS specific header file // discriminate the basic file of the operating system
#include
// Disable Data Conversion Warnings
#pragma Warning (Disable: 4244) // MIPS # Pragma Warning (Disable: 4136) // x86 # Pragma Warning (Disable: 4051) // Alpha /// OpenGL.cpp // Main Program #include "Glos.h "#include
Void main (void) {/ * initialization: * / auxinitdisplaymode (aux_single | aux_rgba); // Window Display Single Cache and RGB (Color) Mode AuxinitPosition (0,0,500,500); // Size X = 500 y = 500 (0, 0) Yes, the screen left is auxinitWindow ("Sample1"); // window initialization, the parameter is the title GLCLEARCOLOR (0.0, 0.0, 0.0, 0.0); // Clear the window to black glclear (GL_COLOR_BUFFER_BIT); // Clear the color to clear the color Color / / ie background colors set for the glclearcolor command
/ * Drawing * / Glcolor3f (1.0, 0.0, 0.0); // Select color (R, G, B), parameter 0 1.OpenGL function, variable naming nature Variable: Prefix Type C Variable B 8-Bit Int Signed Chars 16-Bit Int Shorti 32-Bit Int Longf 32-Bit Float Floatd 64-Bit Float DoubleUB 8-Bit Unsigned Int Unsigned Charus 16-bit unsigned int unsigned shortui 32- Bit unsigned int unsigned long, for example, 1.0F is actually 1.0, which is generally written to 1.0F looks better. Function: Function Parameter Type As a function suffix, for example, GlvertEX2i (2, 4) indicates that the OpenGL basic function (GL-) is a function of the paint point (-Vertex-) is two integer parameters (-2i) future to a function I will know what it is doing. The 2.callback function is some functions used to call the system. For example, if you are displayed, accept the keyboard input ... You are better than the building blocks, and the main program is initialized (the order is generally irrelevant), system They will automatically perform them, such as: Void Callback Display (Void), you write a good music to paint what Dongdong, then use auxmainloop (display) in the main program; you can make this stuff have always shown on the onpaint in VC. () 3. Howengl Basic Library Drawing Method Glbegin; glcolor3f (...); glvertex (...) Glcolor3f (...); GLVERTEX (...); ... glend (); What flat is first specified by "Type": GL_Points single vertex set gl_lines multi-set line, 2 points one line GL_GL_POLYGON single simple fill converged polygon GL_TRAINGLES multi-group triangle, 3 points a triangle .. Can be found in VC Help Index) //// Sample.cpp #include "glos.h" #include Void Myinit (Void); Void Callback Display (Void Callback Reshape (Glsizei W, Glsizei H); // This is not used to reshape (), just a process for windows to change the size of the size // is not related to the drawing, behind The focus here is display (), please pay attention to the method of drawing void myinit (void) {/ * initialization: * / auxinitdisplaymode (aux_single | aux_rgba); // Window Display Single Cache and RGB (Color) Mode AuxInitPosition (0, 0, 500, 500); // Size X = 500 y = 500 (0, 0) is the left point auxinitwindow ("Sample1") on the screen; // Window initialization, the parameter is the title GlclearColor (0.0, 0.0, 0.0, 0.0); // Clear the window to black glclear (GL_COLOR_BUFFER_BIT); // Clear the color cache set the color // ie background color} Void Callback Reshape (GLSizei W, Glsizei H) {GLVIEWPORT (0, W, W, h); GLMatrixmode (GL_Projection); GLLoadident (); if (w <= h) Glortho (-20.0, 20.0, -20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0 * (GLFLOAT) W , -50.0, 50.0; Elseglortho (-20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0 * (GLFLOAT) H / (GLFLOAT) W, -20.0, 20.0, -50.0, 50.0); GLmatrixMode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {Glbegin (GL_TRIANGLE_STRIP); // Draws a plurality of triangular Glcolor3f (1.0, 0.0, 0.0); GlvertEx3f (15.0, 0.0, 0.0); Glcolor 3F (0.0, 1.0, 0.0); GlvertEx3f ( -15.0, 0.0, 0.0); Glcolor3f (0.0, 0.0, 1.0); Glvertex3f (0.0, 15.0, 15.0); // First triangle Glcolor3f (0.0, 1.0, 1.0); Glvertex3f (10.0, 15.0, -15.0); // Second Triangle Glcolor3f (1.0, 1.0, 0.0); GlvertEX3F (15.0, 0.0, 0.0); // Third triangle Glend (); glflush (); Void main (void) {Myinit (); AuxreshapeFunc (reshape); AuxMainLoop (Display);} // end of sample so-called continuous padding is 1, 2, 3 points constitute a triangular 12, 3, 4 points constitute a triangular 2 ... Here is a total of 3 to connect to the colored triangles, form a happy triangular vertebral spine enclosed by three color planes ... Enjoy IT The last book says that there is a reshape to further explain that this function function is to perform some redraw actions to change the window size of the window (onResize in the VC). Among them, some transformations I hope that everyone has a preliminary knowledge of computer graphics, which will help this part of understanding. If there is nothing, it doesn't matter, I try to explain some of the popular 3D drawings, write the 3D coordinates to the form of the 4 * 4 matrix (detail .................................... ....................... You can refer to the relevant literature, which will also be mentioned later.) Any projection, rotation ... operation can be seen as a matrix multiply operation. Matrix Operation ---- This concept must be formed! ! For example, in the 2D general rotation, it can be written: | cos θ sin θ 0 | [x 'y' 1] = [xy 1] | -SIN θ cos θ 0 || 0 0 1 | 3D medium genus is exactly the same, one 3D graphic The display includes the following steps: 1. Viewpoint transform (the camera is placed in a suitable place to align 3D scene) 2. Model transformation (placed 3D object in a suitable place) 3. Projection transformation (adjust the camera lens, make 3D scene projection On 2D film) 4. The viewport transform (determined film sizes) where 1, 2 is not essentially different. There are several key functions inside: One geometric transform 1.void Gltranslated (GLDouble X, GLDOUBLE Y, GLDOUBLE Z); Void GltranslateF (GLFLOAT X, GLFLOAT Y, GLFLOAT Z); Transfer in the X Y Z axis x y z 2.void Glrotated (GLDOUBLE ANGLE, GLDOUBLE Z); Void Glrotatef (GLFLOAT Angle, GLFLOAT X, GLFLOAT Y, GLFLOAT Z); Targets rotate x y z with x y z axis 3.void Glscaled (Gldouble X, Gldouble Y, GLDOUBLE Z); Void Glscalef (GLFLOAT X, GLFLOAT Y, GLFLOAT Z); target is zoomed in x y z direction, the zoom factor is x y z Two projection transformation 1. orthogonal projection (i.e. not "near the far smaller") void glOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) created (matrix) parallel to a scene, i.e. the projected line Is parallel line, put the first rectangle ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ The view is projected onto the second rectangular view. And use this matrix to multiply the current matrix to complete the transformation. The first rectangle 3D spatial coordinate (LEFT, BOTTOM, -NEAR), three-dimensional spatial coordinates in the lower right corner (Right, Top, -near); Vision Second Rectangle 3D Space Coordinate (Left, Bottom, -far), three-dimensional spatial coordinates (Right, Top, -far) in the lower right corner; 2. Transport projection ("near the big far") Void GLFRUSTUM (GLDOUBLE LEFT, GLDOUBLE TOP, GLDOUBLE BOTTOM, GLDOUBLE TOP, GLDOUBLE BOTTOM, GLDOUBLE TOP, GLDOUBLE ZNEAR, GLDOUBLAR); Create a type of view of the prism, its near-cut surface LEFT RIGHT BOTTOM TOP ZNEAR OK, the remote surface is determined by the position of the viewpoint projection near the adjacent surface to the z-axis ZFAR position Three viewport transform Void GLVIEWPORT (Glint X, Glint Y, Glsize Width, Glsize Height); This function defines a viewport, X and Y is the viewport in the upper left coordinate default in the screen window coordinate system is (0,0). Width HEIGHT is wide and high. Note that the adjustment of the medium-skewers long width ratio can cause image deformation. Therefore, reshape () To detect window size, correct the viewport size, and ensure that the image is not deformed. Attachment: Void Glmatrixmode (Glenum Mode); converts the current matrix into the specified matrix type The Chinese description of these three functions is described in the example: //sample.cpp#include "gles.h" #include // Enter the world coordinate system, prepare the necessary steps, initialize the transformation matrix step: 1 Determine Type 2 Clear unit array GLmatrixMode (GL_Projection); glloadIdentity (); / / Define a suitable view body IF (W <= H) glortho (-20.0, 20.0, -20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0, H / (GLFLOAT) W, -50.0 , 50.0); Elseglortho (-20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0 * (GLFLOAT) H / (GLFLOAT) W, -20.0, 20.0, -50.0, 50.0); // Return the result of the conversion to the viewpoint coordinate system GLmatrixmode (GL_MODELVIEW); GLLoadIdentity ();} /// Draw three rope cones, functionally on the same way void Draw (void) {glbegin (GL_TRIANGLE_STRIP); Glcolor3f (1.0, 0.0, 0.0) GlvertEX3F (15.0, 0.0, 0.0); Glcolor3f (0.0, 1.0, 0.0); GlvertEX3F (-15.0, 0.0, 0.0); Glcolor3F (0.0, 0.0, 1.0); GlvertEX3F (0.0, 15.0, 15.0); // Glcolor3f (0.0, 1.0, 1.0); GlvertEX3F (10.0, 15.0, -15.0); // Glcolor3f (1.0, 1.0, 0.0); Glvertex3f (15.0, 0.0, 0.0); // glend ();} Void Callback Display (void ) {// Painted the first three-prism GlclearColor (0.0, 0.0, 0.0, 1.0); GLLoadIdentity (); gltranslatef (0.0, 0.0, -25.0); DRAW (); // Started the second triangular prism / / transformation start, the change matrix is the unit array GLLoadIdentity (); // First, the XYZ translation-5 -10 -4, the screen is to move to the left, and the z direction is Parallel projection, there is no "near the big far", so you can't see the effect GLTranslatef (-5.0, -10.0, -4.0); // Return along the Z axis 90 degrees GLROTATEF (90, 0.0, 0.0, 1.0); DRAW () ; // Drawing works, enforcement to end GLFLUSH (); Void main (void) {Myinit (); AuxreshapeFunc (reshape); AuxMainLoop (Display);} // Sample Ends Here / If you run it, you know the role of the above functions, not to move the object, turn, deformed, fluctural ... I want to have the top 3, OpenGL's basic principle has been so provided. The mathematical model used by a standard computer graphology to the displayed interface, as long as graphics knowledge, master the OpenGL API function, the drawing is very easy, the basic part has been completed, then slowly talk about some of the plot functions such as: texture , Light source, animation ... sincerely hope that everyone will mention comments and high theories :) The previous article has completed the basic structure of OpenGL's programming. It will gradually deepen on this basis, and new content is increated. This article tells the keyboard operation and animation basis (which is still too bad). It's just a simple animation that can be controlled by the user, allowing the object to move, rotate left and right. Is our own first quake! Of course, this version is buying, huh, huh. Another purpose of this is to deepen the awareness of the Callback function and the intuitive explanation of the transformation. Any transformation you can see from the screen from the screen: I only mark the Chinese explanation with the previously changed part //sample.cpp#include "gles.h" #include Void Myinit; Void Callback Display (Void); Void Callback Reshape (Glsizei W, GLSizei H); / / Do not pay attention to music? Here, the new TEO 4 Callback function // similar to display () reshape (), also called them // to implement the response void callback left (void) input to the keyboard; // Press LEFTVOID CALLBACK RIGHT (Void); / / Press Rightvoid Callback Up (Void); // Press UPVOID CALLBACK DOWN (VOID); // Press DOWN // Two global variables, z_motion Use to control the object to be remote // rotate to control the object rotation static int z_motion = 0, Rotate = 0; Void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); AuxinitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GLCLEARCOLOR (0.0, 0.0, 0.0, 0.0); Glclear (GL_COLOR_BUFFER_BIT);} Void Callback Reshape (Glsizei W, Glsizei H) {IF (W <= 500 && H <= 500) GLVIEWPORT (0, 0, W, H); IF (W> 500 && H <= 500) GLVIEWPORT (0,0,500, h); IF (W <= 500 && H> 500) GLVIEWPORT (0, 0, W, 500); IF (W> 500 && H> 500) GLVIEWPORT (0, 0, 500, 500); Glmatrixmode (GL_Projection); GLLoadIdentity (); /// * if (w <= h) // GLORTHO (-20.0, 20.0, -20.0 * (GLFLOAT) H / (GLFLOAT) W, // 20.0 * (GLFLOAT) h /( (GLFLOAT )W ,-50.0, 50.0 );//ELSE// glortho (-20.0 * (glflloat) h / (glfloat) W, // 20.0 * (GLFLOAT) H / (GLFLOAT) W, -20.0, 20.0 , -50.0, 50.0); // * // *********************************************** ********************* / / Here we change a projection method: transmissively projected, there is a three-dimensional saying // replace the last parallel projection. The first four parameters are the // Left Right Top Bottom of the first intercept surface, and then two parameters control the // Z coordinate of the near-cut surface, the Zsheld the zygle of the remote surface, the function is called below: // Void GLFRUSTUM GLDOUBLE LEFT, GLDOUBLE Right, // GLDOUBLE BOTTOM, GLDOUBLE TOP, / / GLDOUBLE NEAR, GLDOUBLE FAR); / ************************* ******************************************* / GLFRUSTUM (-20.0, 20.0, -20.0, 20.0, 10.0, 50.0 ); glmatrixmode (GL_MODELVIEW); GLLoadIdentity (); Void Draw (Void) {Glbegin (GL_TRIANGLE_STRIP); GLCOLOR3F (1.0, 0.0, 0.0); GlvertEX3F (15.0, 0.0, 0.0); Glcolor3f (0.0, 1.0, 0.0); GlvertEX3F (-15.0, 0.0, 0.0); Glcolor3f ( 0.0, 0.0, 1.0); GlvertEX3F (0.0, 15.0, 15.0); // Glcolor3f (0.0, 1.0, 10.0); GlvertEX3F (10.0, 15.0, -15.0); // Glcolor3f (1.0, 1.0, 0.0); GlvertEx3f ( 15.0, 0.0, 0.0); // glend (); Void Callback Display (Void) {GlclearColor (0.0, 0.0, 0.0, 1.0); GLCLEAR (GL_COLOR_BUFFER_BIT); // glpushmatrix (); glloadident (); / / According to the z_motion rotate two parameters, the specific value of the transformation is determined: // z_motion changes, the object is selected from the original Z coordinate-25, translates the z_motion unit GLTRANSLATEF (0.0, 0.0, -25.0 z_motion); // Rotate changes, The object extension Y-axis (up and down direction) rotates 5 * Rotate Glrotatef (Rotate * 5.0, 0.0, 1.0, 0.0); Draw (); // glpopmatrix (); GLFLUSH ();} void callback left (void) {// Whenever you press LEFT, Rotate value 1 // The following functions are similar to Rotate ;} void callback right (void) {rotate -; void callback up (void ) {z_motion ;} void callback down (void) {z_motion -} void main (void) {Myinit (); // Use the auxiliary library function to set the Left () right () Up () // // () // (Aux_Right, LEFT); AuxKeyFunc (AuxKeyFunc); AuxKeyFunc (AUX_UX_UX, UP) AuxKeyFunc (Aux_Down, Down); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample If you run this program, you will find a more obvious flicker. This is a single cache mode that will be used to use dual cache mode, which can be avoided. Flashes This time may be the simplest content of OpenGL: color. One RGB mode generally provides color to form RGB three-color color. This is the RGB mode, we have always used this method (for example: Glcolor3f (1.0, 0.0, 0.0); GlvertEX3F (0.0, 0.0, 0/0); draw a red point.) Void Glcolor3 {BSIFD UB US UI} TYPE R, TYPE G, TYPE B); Void Glcolor4 {BSIFD UB US UI} (Type R, Type G, Type B, Type A); Void Glcolor3 {BSIFD UB US UI} V (Type * V); Void Glcolor4 { BSIFD UB US UI} V (Type * V) is an optional value accuracy (see the previous introduction); parameter A is an Alpha value characterized by transparency. The latter two functions of the V suffix indicate that their parameters are vectors (for detailed use of the examples). Take Glcolor3f as an example, its parameter value range -1.0--1.0, other numeric types of functions will automatically make uniformity of the parameters to this range, such as the suffix type MIN MIN mapping Max Max mapping B 1byte integer -128 -1.0 127 1.0 Two-color index mode uses Void Glindex {S I f D} (Type C); Void GlIndex {S I f D} (Type * C); color is selected from the color index table. Set the current color index value (palette), which is larger than the total number. All of the previous examples are RGB mode, so here is given an example of a color index: //sample.cpp//#clude "gles.h" #include void myinit (void) {auxInitDisplayMode (AUX_SINGLE | AUX_INDEX); auxInitPosition (0,0,500,500); auxInitWindow ( "sample1"); glClearColor (0.0,0.0,0.0,0.0); glClear (GL_COLOR_BUFFER_BIT); glShadeModel (GL_FLAT); // Gl_flat filling mode} Void Callback Reshape (Glsizei W, Glsizei H) {IF (W <= 500 && H <= 500) GLVIEWPORT (0, 0, W, H); IF (W> 500 && H <= 500) GLVIEWPORT (0,0,500, h); IF (W <= 500 && H> 500) GLVIEWPORT (0, 0, W, 500); IF (W> 500 && H> 500) GLVIEWPORT (0, 0, 500, 500); Glmatrixmode (GL_Projection); GLLoadIdentity (); if (w <= h) Glortho (-20.0, 20.0, -20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0 * (GLFLOAT) W, -50.0 , 50.0); Elseglortho (-20.0 * (GLFLOAT) H / (GLFLOAT) W, 20.0 * (GLFLOAT) H / (GLFLOAT) W, -20.0, 20.0, 20.0); GLmatrixMode (GL_ModelView); GLLoadIdentity () } Void Draw (void) {GLINT N; // First give a given triangle fan coordinate information GLFLOAT PP [8] [2] = {{7.0, -7.0}, {0.0, -10.0}, {- 7.0, -7.0}, {- 10.0, 0.0}, { -7.0, 7.0}, {0.0, 10.0}, {10.0, 0.0}}; // Pick up the first two points, then read the previous vector (array) information to draw // complete graphic Glbegin (GL_TRIANGLE_FAN); Glvertex2f (0.0, 0.0); GlvertEX2F (10.0, 0.0); for (n = 0 ; n <8; n ) { / / Each time you find a new color from the color lookup table, then draw a triangular fan // with this color, the use of the V suffix in the GlvertEx2Fv (), did not encounter before, the V suffix represented // parameter is a vector (array) Glindexi (N 1); GlvertEX2FV (PP [N]);} glend (); Void initpalette (void) {// This is the key to this example, initialize the color lookup table, which defines the color of the color here, is gradual from blue to blue. GLINT I; Static GLFLOAT RGB [] [3] = {{0.0, 0.0, 0.2}, {0.0, 0.0, 0.6}, {0.0, 0.0, 1.0}, {0.0, 0.2, 1.0}, {0.0, 0.4, 1.0}, {0.0, 0.8, 1.0}, {0.0, 1.0, 1.0}} // Call the simple secondary library function to set the system palette, set the contents of the eight colors to all // color index for for (i = 0; i <8; i ) AuxsetoneColor (i 1, RGB [i] [0], RGB [I] [1], RGB [I] [2]); Void Callback Display (Void) {// First call custom initPalette () initializes Toner INITPALETTE (); GLCLEARCOLOR (0.0, 0.0, 0.0, 1.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLLOADITITY (); DRAW (); GLFLUSH (); Void main (void) {Myinit (); AuxreshapeFunc (reshape); AuxMainLoop (Display);} // end of sample Next time you will introduce OpenGL's illumination and material effect, one step into the 3D ladder, everyone comes .. .... This part is the most important part, just the foundation. Here, the concept of light processing, shading, light source setting, material definition, and related computer graphics. Generally, the step of generating 3D image: 1 Modeling 2 geometric model transform into 2D perspective 3 Determine all visible scenes, perform blanking 4 Calculation scene color We have repeatedly introduced music 1 2 two steps Is OpenGL's work, we don't have to care, so 4 is the focus here. (1) Light is divided into: reflection, transmitted light 1 Simple illumination model Simple illumination model only considers the visual effect of the surface reflected light of the object. It is assumed that the surface is smooth and opaque and is constructed from ideal materials, and the environment is assumed to be white lighting. General reflection light is divided into: 3 components of environmental reflection, diffuse reflection and mirror reflection. Ambient Light: The incident light is uniformly incident from the surrounding environment to the surface and is equivalent to the surface. Diffuse light (DIFFUSE LIGH): A particular light source is reflected in the reflected light of the object to be uniformly reflected in each direction. Specular Light: Highlights produced on a metal ball in a certain direction of reflected light, such as highlights produced on the metal ball. See university physics in detail. Ha ha To introduce important functions: (1) Void gllight {if} [v] (Glenum Light, Glenum PNAME, TYPE PARAM) Sets the source characteristics. Light is name, for example: gl_light0, gl_light1 ... gl_light7. PNAME Default Value Description GL_AMBIENT 0,0,0,1 RGBA Mode Ambient Light GL_DIFFUSE 1,1,1,1 RGBA mode diffuse light GL_SPECULAR 1, 1, 1 RGBA mode mirror GL_POSTION 1, 0, 1,0 Light Source Location Coordinate GL_SPOT_DIRECTION 0, 0, -1 Point Light Source Convection Direction Vector (X, Y, Z, W) GL_SPOT_EXPONENT 0 Pit Number Convection Index GL_SPOT_CUTOFF 180 Point Light Source Concentration Diffusion Half-angle GL_CONSTANT_ATTENUATION 1 constant attenuation factor Gl_Liner_attion 0 linear attenuation factor GL_QUADRATIC_ATTENUATION 0 square attenuation factor Description: The default value of GL_DIFFUSE GL_SPECULAR is only used for GL_Light0, other light source GL_DIFFUSE GL_SPECULAR defaults to: (0.0, 0.0, 0.0, 1.0) ! ! ! I may have forgotten it in front! ! ! TYPE is the type of parameter in {}, for example: i is int, f is float. v is optional, indicating that an array is parameter to define a set of light sources. (2) Enable Light / Close Light Source Void Glenable (Glenum CAP) Void GLDISABLE (Glenum CAP), for example, makes the light source: glenable (GL_Light0); Examples of simple lighting are given below: ///sample.cpp #include "glos.h" #include Void Myinit; Void Callback Display (Void); Void Callback Reshape (Glsizei W, GLSizei H); Void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); AuxinitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); Glclear (GL_COLOR_BUFFER_BIT); // Glshademodel is used to determine the color fill mode, the default GL_SMOOTH is better, but the calculation // is large, if you add the following sentence, the filling is filled according to the geometric model, the calculation amount // big Decrease, but the effect is not good. // Glshademodel (GL_FLAT); // Defines the location of a light source GLFLOAT LIGHT_POSITION [] = {1.0, 1.0, 1.0, 0.0}; GLLIGHTFV (GL_Light0, GL_Position, Light_Position); / / Define the diffuse color of the light source (blue) and ambient light (red), if you try this // program, you can see the effect of the light source. If there is no condition, you can imagine: under a faint red / / background light, the light-illuminated portion is pure blue, and the backlight portion is red. / / You can also specify other properties in more detail in more detail, here you have the default. GLfloat light_diffuse [] = {0.0,0.0,1.0,1.0}; glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); GLfloat light_ambient [] = {1.0,0.0,0.0,1.0}; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); // note on GL_LIGHTING of:. // If enabled, use the current lighting parameters to compute the // vertex color or index If disabled, associate the current color // or index with each vertex compute If Enabled, using the current lighting parameters. Each // point color. You can try to drop this sentence, then the default white diffuse light source will replace your // Gray light source you will see a white without three-dimensional ball. Glenable (GL_Lighting); Glenable (GL_LIGHT0); // About GL_SSS // Passs if the incoming z value is less the stored z value.//this is the default value.// The depth comparison is activated with glenable (GL_DEPTH_TEST), and then defined if the Z coordinate is less than buffer / / Value (current point z is smaller, closer to the observation point), then display, actually blanking. GLDEPTHFUNC (GL_SS); Glenable (GL_Depth_test); Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); if (w <= h) Glortho (-2.0, 2.0, -2.0 * (GLFLOAT ) h / (glfloat) h / (glfloat) W, -10.0, 10.0); Elseglortho (-2.0 * (glfloat) h / (glfloat) W, 2.0 * (GLFLOAT) h / (GLFloat ) W, -2.0, 2.0, -10.0, 10.0); Glmatrixmode (GL_ModelView); gLloadIdentity (); Void Draw (void) {// Call the auxiliary library function draws a solid sphere. Radius 1.0AuxSolidSphere (1.0); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLLOADITENTITY (); DRAW (); GLFLUSH ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample / The next time, this back the material: OpenGL approximates the color of the material by the material to R, G, and B. Also divided into environment, diffuse reflection, mirror reflection ingredients. They determine that the material is reflected in the ambient light, diffuse reflected light and the specular reflected light. The combination of the characteristics of the material and the light source characteristics is the final display effect of the observation. For example, the red plastic ball is mostly red, and the characteristic color of the light source occurs at the highlight of the light source. Very easy, isn't it? Material definition: void glmaterial {if} [v] (Glenum Face, Glenum PName, Type Param); where: Face: can be GL_FRONT, GL_BACK, GL_FRONT_AND_BACK, indicating which of the current material is applied to which of the object. PNAME illustrates a specific material property (very similar to the definition of the last source): PNAME Default Description GL_AMBIENT 0.2, 0.2, 0.2, 1.0 Material Reflected Light GL_DIFFUSE 0.8, 0.8, 0.8, 1.0 Material of the Material Gl_Ambient_and_diffuse Material Light and Reflected Light Color GL_Specular 0.0, 0.0.0, 1.0 Material Mirror Reflective Light GL_SHINESS 0.0 Mirror Index (Light Exit) GL_EMISSION 0.0, 0.0, 1.0 Material Radiation Color GL_COLOR_INDEXES 0,1,1 Material Light, diffuse reflected light and mirror reflected light color, see the simple example of the material: //sample.cpp Include "Glos.h" #include void myinit (void) {auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0,0,500,500); auxInitWindow ( "sample1"); glClearColor (0.0,0.0,0.0,0.0); glClear (GL_COLOR_BUFFER_BIT); // glShadeModel (GL_FLAT); // First define a material, define how the method is very similar to the above-mentioned light source definition. GLFLOAT MAT_AMBIENT [] = {0.8, 0.8, 0.8, 1.0}; // Define purple diffuse characteristics GLFLOAT MAT_DIFFUSE [] = {0.8, 0.0, 0.8, 1.0}; // Define bright purple specular species GLFLOAT MAT_SPECULAR [ ] = {1.0, 0.0, 1.0, 1.0}; // Define the brightness GLFLOAT MAT_SHININESS [] = {50.0} of the specular reflection [] = {50.0}; // Apply the above material definition to 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_SHININESS, MAT_SHININESS); // We simplify the light source to display the effect of the material. / / We only specify the light source location, other defaults: white light source. / / You can also join the definition of the light source, see the effect of the Synthesis of the light source and material is because they can be synthesized, in order to produce more effects than the real life, this is also // 3D technology attractive The charm is located. GLfloat light_position [] = {1.0,1.0,1.0,0.0}; glLightfv (GL_LIGHT0, GL_POSITION, light_position); // GLfloat light_diffuse [] = {0.0,0.0,1.0,1.0}; // glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse ); // Set the light source to Glenable; GLENABLE (GL_LIGHT0); // Coloring blanking // ******* actually speaks Baile, this is the famous Z-Buffer *********** // glDepthfunc (GL_SS); GLENABLE (GL_Depth_test); } Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); if (w <= h) Glortho (-2.0, 2.0, -2.0 * (GLFLOAT ) h / (glfloat) h / (glfloat) W, -10.0, 10.0); Elseglortho (-2.0 * (glfloat) h / (glfloat) W, 2.0 * (GLFLOAT) h / (GLFloat ) W, -2.0, 2.0, -10.0, 10.0); Glmatrixmode (GL_ModelView); gLloadIdentity (); Void Draw (void) {auxsolidsphere (1.0); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLLOADITENTITY (); DRAW (); GLFLUSH ();} void main (void) {Myinit (); Auxreshapefunc (Reshape); AuxMainLoop (Display);} // end of sample Through the above example, we will see the realization of the material definition and the effect of the light source, as our daily feel, define the material more in line with people. The light source uses white light. Here we see a purple ball, and its high-gloss has a bright purple. As a comparison, we will give an example of 12 color balls. You can see the effects of various light sources, material applications, compare them together. Before you give an example, introduce two functions: void glpushmatrix (); void glpopmatrix (); I have already talked in front of me, all geometric projection transformations are the results of matrix multiplication. If you want to keep an initial coordinate point, you should use these two important functions: matrix into the stack and matrix outlet. You can understand this, in order to paint a ball in the upper left, you will save the current matrix, GLPushmatrix () (in the stack), then move the coordinates to the upper left corner, then call AuxSolidSphere (1.0), draw a radius 1.0 ball, this When you call GLPopmatrix () (matrix outlet), return to the original coordinate point, then the ball is in the upper left corner. This program is very long, but in fact, the long part of the company is the work of repeating 12 balls, so it is better understood. ///sample.cpp#include "gles.h" #include // First define the light source GLFLOAT LIGHT_AMBIENT [] = {0.0, 0.0, 0.0, 1.0}; GLFLOAT LIGHT_DIFFUSE [] = {1.0, 1.0, 1.0, 1.0}; GLFLOAT LIGHT_SPECULAR [] = {1.0, 1.0, 1.0, 1.0}; GLFLOAT LIGHT_POSITION [] = {0.0, 3.0, 2.0, 0.0}; GLFLOAT Imodel_Ambient [] = {0.4, 0.4, 0.4, 1.0}; // Applied light source GLLIGHTFV (GL_Light0, GL_AMBIENT, LIGHT_AMBIENT); GLLIGHTFV (GL_LIGHT0, GL_POSITION, LIGHT_POSITION); GLLIGHTFV (GL_LIGHT0, GL_DIFFUSE, LIGHT_DIFFUSE); Glenable (GL_Lighting); Glenable (GL_LIGHT0); // Initialize z buffergldepthfunc (gl_less); glenable (gl_depth_test);} Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); if (w <= h) glortho (-6.0, 6.0, -6.0 * (GLFloat ) h / (glfloat) W, -10.0, 10.0); Elseglortho (-6.0 * (GLFLOAT) H / (GLFLOAT) W, 6.0 * (GLFLOAT) h / (GLFloat ) W, -6.0, 6.0, -10.0, 10.0); Glmatrixmode (GL_ModelView); gLloadIdentity ();} void draw (void) {auxsolidsphere (1.0); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Establish material database GLFLOAT NO_MAT [] = {0.0, 0.0, 0.0, 1.0}; GLFLOAT MAT_AMBIENT [] = {0.7, 0.7, 0.7, 1.0}; GLFLOAT MAT_AMBIENT_COLOR [] = {0.8, 0.8, 0.2, 1.0}; GLfloat mat_diffuse [] = {0.1,0.5,0.8,1.0}; GLfloat mat_specular [] = {1.0,1.0,1.0,1.0}; GLfloat no_shininess [] = {0.0}; GLfloat low_shininess [] = {5.0}; GLfloat high_shininess [] = {100.0}; GLFLOAT MAT_EMISSION [] = {0.3, 0.2, 0.2, 0.0}; //// 1-1 only diffuse reflected light, no ambient light and mirror light GLPushmatrix (); gltranslatef (-3.75, 3.0,0.0); glMaterialfv (GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv (GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv (GL_FRONT, GL_EMISSION, no_mat); Draw (); glpopmatrix (); // 1-2 diffusely reflected light, and there is low high light, no ambient light GLPushmatrix (); GLTranslateF (-1.25, 3.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, NO_MAT); GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE); Glmaterialfv (GL_FRONT, GL_SPECULAR, MAT_SPECULAR); GLMATERIALFV (GL_FRONT, GL_SHINININININININININININININININININININININININ Department; GLMATERIALFV (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); GLPopMatrix (); // 1-3 diffusely reflected light and mirror light, very bright high light, no ambient light GLPushmatrix (); gltranslatef (1.25, 3.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, NO_MAT); GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE ); GLMATERIALFV (GL_FRONT, GL_SPECULAR, MAT_SPECULAR); GLMATERIALFV (GL_FRONT, GL_SHININESS, HIGH_SHININESS); GLMATERIALFV (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); glpopmatrix (); // 1-4 diffusely reflected light and radiant light, no ambient light and mirror reflected light GLPushmatrix (); GLTranslateF (3.75, 3.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, NO_MAT); GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE) ; glMaterialfv (GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv (GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv (GL_FRONT, GL_EMISSION, mat_emission); draw (); glPopMatrix (); //// 2-1 diffusely reflected light and ambient light , no specular glPushMatrix (); glTranslatef (-3.75,0.0,0.0); glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv (GL_FRONT, GL_SHININESS, NO_SHININESS; GLMATERIALFV (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); GLPopMatrix (); // 2-2 Reflected light, ambient light and mirror light, and low high-gloss GLPushmatrix (); gltranslatef (-1.25 , 0.0,0.0); 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_SHININESS, low_shininess); glMaterialfv (GL_FRONT, GL_EMISSION, no_mat) DRAW (); glpopmatrix (); // 2-3 Reflected light ambient light and mirror light, and there is a very bright high-light glpushmatrix (); GLTranslateF (1.25, 0.0, 0.0); 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_SHININESS, HIGH_SHININESS); GLMATERIALFV (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); glpopmatrix (); // 2-4 diffusely reflected light, ambient light and radiation light, no mirror-free GLPushmatrix (); GLTranslateF (3.75, 0.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, MAT_AMBIENT); GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE); GLMATERIALFV (GL_FRONT, GL_SPECULAR, NO_MAT); GLMATERIALFV (GL_FRONT, GL_SHINININESS, NO_SHININESS); GLMATERIALFV (GL_FRONT, GL_EMISSION, MAT_EMISSION); DRAW (); glpopmatrix (); ///// 3-1 diffuse light and color ambient light, without the specular light glPushMatrix (); glTranslatef (-3.75, -3.0,0.0); glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv (GL_FRONT, GL_SHINININESS, NO_SHININESS; GLMATERIALFV (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); GLPopMatrix (); // 3-2 Reflected light and mirror light, there is a low high light GLPushmatrix ( ); glTranslatef (-1.25, -3.0,0.0); glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv (GL_FRONT, GL_SHININESS, low_shininess); glMaterialfv (GL_FRONT, GL_EMISSION, NO_MAT); DRAW (); glpopmatrix (); // 3-3 Reflected light and a natural environmental light and mirror light, and there is a very bright high-light GLPushmatrix (); GLTranslateF (1.25, -3.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, MAT_AMBIENT_COLOR); GLMATERIALFV GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv (GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv (GL_FRONT, GL_EMISSION, no_mat); draw (); glPopMatrix (); // 3-4 Reflected light and color environment light and radiant light, no mirror-free GLPushmatrix (); glTranslatef (3.75, -3.0, 0.0); GLMATERIALFV (GL_FRONT, GL_AMBIENT, MAT_AMBIENT_COLOR); GLMATERIALFV (GL_FRONT, GL_DIFFUSE , MAT_DIFFUSE); GLMATERIALFV (GL_FRONT, GL_SPECULAR, NO_MAT); GLMATERIALFV (GL_FRONT, GL_SHINININESS, NO_SHININESS); GLMATERIALFV (GL_FRONT, GL_EMISSION, MAT_EMISSION); DRAW (); glpopmatrix (); Glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} This time, the material is fully obtained, huh, the last time 12 different three-dimensional material balls use GLMATERIALFV () to change the material with its inherent system consumption. The same features can be implemented using: Void GlcolormaTerial (Glenum Face, Glenum Mode). Face value: GL_FRONT GL_BACK GL_FRONT_AD_BACKMODE: GL_AMBIENT GL_DIFFUSE GL_AMBIENT_AND_DIFFUSE GL_SPECULAR GL_EMISSION When you want to use this function, you want to enable Glenable (GL_COLORIALIAL) to function when you work in the drawing, use GLCOLOR * () to change the material color, or use GLMATERIAL () to change the material component, you should use GLDISABLE (GL_COLOR_MATERIAL) Close this material pattern. For example: GlcolormaTerial (GL_FRONT, GL_DIFFUSE); GLENABLE (GL_COLORTER_MATERIAL); Glcolor3f (0.3, 0.5, 0.7); // Painting some objects:. Glcolor3f (0.0, 1.0, 0.0); // Draw more other objects:. GLDISABLE (GL_COLOR_MATERIAL); Description: When you need to change most of the individual materials in the scene, you should call GlcolormaTerial () when you modify the not only one material parameters, it is best to call GLMATERIAL * (), which is currently different from the unique use function. Bar. Please see the example: ///sample.cpp#include "gles.h" #include void myinit (void) {auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0,0,500,500); auxInitWindow ( "sample1"); glClearColor (0.0,0.1,0.1,0.0); glClear (GL_COLOR_BUFFER_BIT); // glShadeModel (GL_FLAT); / / Define a white simple light source GLFLOAT LIGHT_POSITION [] = {0.0, 3.0, 2.0, 0.0}; GLLIGHTFV (GL_Light0, GL_Position, Light_Position); Glenable (GL_Lighting); GLENABLE (GL_LIGHT0); // Enable blanking GLDEPTHFUNC (GL_SSS); glenable (GL_Depth_test); // Enable color material mode GlcolormaTerial (GL_FRONT, GL_DIFFUSE); GLENABLE (GL_COLOR_MATERIAL); Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); if (w <= h) glortho (-6.0, 6.0, -6.0 * (GLFloat ) h / (glfloat) W, -10.0, 10.0); Elseglortho (-6.0 * (GLFLOAT) H / (GLFLOAT) W, 6.0 * (GLFLOAT) h / (GLFloat ) W, -6.0, 6.0, -10.0, 10.0); GLmatrixMode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Pain a yellow material GLLoadident (); gltranslatef (-0.7, 0.0); Glcolor3f (1.0, 1.0, 0.0); AuxSolidSphere (1.0); // Picture of a red ball in the right side of the yellow ball, 3.7; Glrotatef (-60.0, 1.0, 0.0, 0.0); Gltranslatef (2.7, 0.0, 0.0); Glcolor3F (1.0, 0.0, 0.0); auxsolidsphere (1.0); // Then the left side of the yellow ball (between two goals), a cyan triangle cone GLLOADITY (); GLTranslatef (-1.0, -1.0, -5.0); Glrotatef (30.0, 1.0, 0.0, 0.0); Glcolor3f (0.0, 1.0, 1.0); AuxSolidCone (2.0, 2.0); Glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample In addition to the use of GlcolormaRial (), we can also experience OpenGL to work for us in blanking. Does we draw an example of a triangular cone with a color plane? Where is it flattening, which plane occlusion occurs. Here we see although the triangle cone is finally drawn, OpenGL's blanking function has ensured that it should be hidden. You can try to cancel the two sentences in Myinit (), then the triangle cone runs to the front, huh, huh. OpenGL bitmap and image Unlike general bitmap definitions, the bitmap in OpenGL refers to only one information with each pixel; and the image can include multiple information (R, g, b, alpha value). An additional bitmap can be used for mask, covering other images, while the data of the image is simple to cover the previously present data or fuse. (1) Bitmap and character (font) are often used to shield the corresponding area of the window, such as the current color is red, the place in the matrix is 1 in the matrix, and the place remains unchanged. The bitmap is often used in characters. Grating position: void Glrasterpos {234} {sifd} [v] (Type X, Type Y, Type Z, Type W); Set the origin of the current drawing or image. The general color setting should be placed in front of the glrasterpos * (), and then tightly followed the color of the currently set up. Bitmap display: Void Glbitmap (Glsizei Width, Glsizei Height, GLFLOAT XBO, GLFLAOT YBO, GLFLOAT XBI, GLFLOAT YBI, CONST GLUBYTE * BITMAP); XBO, YBO Defines the origin of the bitmap, detail See example: //// Sample .cpp # include "gles.h" #include // Describe the storage method in the computer memory in the computer, do not have to study GLPixElStorei (GL_UNPACK_ALIGNMENT, 1); } Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLOADIDENTITY (); Glortho (0, W, 0, H, -1.0, 1.0); GLmatrixMode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Define red fill: Glcolor3f (1.0, 0.0, 1.0); GlRasterPos2i (100,200); // Draw 8 (purple) Glbitmap (8, 12, 0.0, 0.0, 20.0, 20.0, rasters) in 2 different locations; GLBITMAP (8, 12, 0.0, 0.0, 20.0, 20.0, Rasters); // Draw another 8 (yellow) Glcolor3f (1.0, 1.0, 0.0); GlRasterPos2i (150,200); Glbitmap (8, 12, 0.0, 0.0, 0.0, 0.0, Rasters); Glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample ////// (2) image Character display is just a small content, I think everyone may care more about the image of the image 1. Pixel read and write OpenGL to provide basic pixel read and write functions: Void Glreadpixels (Glint X, Glint Y, Glsizei Width, Glsizei Height, Glenum Format , Glenum Type, GLVOID * PIXELS; Function Parameters (X, Y) Define the left lower corner coordinates of the image area, and Width Height describes the image wide. Pixel is a pointer, pointing to an image data array. Format points out data element format (index or r, g, b, a value): format: Description: GL_INDEX single color index GL_RGB sequentially red, green, blue splitter GL_red single red component GL_GREEN single green component GL_BLUE single blue component GL_alpha single alpha Value GL_LUMINANCE_ALPHAGL_STENCIL_INDEX single template index GL_DEPTH_COMPONENT single depth component And Type points out element data type: Type: GL_UNSIGNED_BYTE No Symbol 8-bit integer GL_BYTE 8-bit integer GL_BITMAP unsigned 8-bit integer array in single number GL_unsigned_short unsigned 16-bit integer GL_SHORT 16-bit integer GL_UNSIGNED_INT unsigned 32-bit integer GL_INT 32-bit integer GL_FLOAT Single precision floating point Similar write pixel functions: Void Gldrawpixels (Glsizei Width, Glsizei Height, Glenum Format, Glenum Type, Const Glvoid * Pixels); 2. Pixel copy Void GlcopyPixels (Glint X, Glint Y, Glsizei Width, Glsizei Height, Glenum Type); This function is very similar to first using glreadpixels () and then calls GLDRAWPIELS (), but it does not consume system memory, just copy, Type may be: GL_COLOR GL_STENCIL GL_DEPTH before copying, type Yaoan manner converted format: 1) type of format corresponds GL_DEPTH GL_STENCIL then should be GL_DEPTH_COMPONENT or GL_STENCIL_INDEX2) type format as it should be GL_RGB GL_COLOR or GL_COLOR_INDEX. 3. The image zooms Void Glpixelzoom (GLFLOAT zoomx, GLFLOAT zoomy; zoomx zoomy is the zoom factor in the X Y direction. The default is 1.0. #include "gles.h" #include Void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); AuxinitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); Glclear (GL_COLOR_BUFFER_BIT); // Glpixelstorei (GL_UNPACK_ALIGNMENT, 1); } Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); if (w <= h) Gluortho2D (0.0, 15.0, 0.0, 15.0 * (GLFLOAT) H / (GLFLOAT) W); Elsegluostho2D ( 0.0, 15.0 * (GLFLOAT) W / (GLFLOAT) H, 0.0, 15.0); Glmatrixmode (GL_ModelView); gLloadIdentity (); Void Draw () {// Draw a color triangle (2D) Glbegin (GL_TRIANGLES); GLCOLOR3F (1.0, 0.0, 0.0); GlvertEX2F (2.0, 3.0); Glcolor3f (0.0, 1.0, 0.0); GlvertEx2f (12.0, 3.0) Glcolor3f (0.0, 0.0, 1.0); GlvertEX2F (7.0, 12.0); glend (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the original image on the screen GLPushmatrix (); gltranslatef (4.0, 8.0, 0.0); Glscalef (0.5, 0.5, 0.5); DRAW (); glpopmatrix (); INT i; for (i = 0; i <5; i ) {// Loop 5 draws 5 copies // First defined display zoom factor (increment) GLPixelzoom (1.0 0.1 * i, 1.0 0.1 * i) ; // Redefile copy original (getting more and more right) GLRasterPos2i (1 i * 2, i); // Image Copy GlcopyPixels (160, 310, 180, 160, GL_COLOR);} Glflush ();} void main (void) {Myinit (); Auxreshapefunc (Reshape); AuxMainLoop (Display);} // end of sample This example draws a color triangle on the center of the screen and then draws its copy from left to right, sequentially draws its copy, the copy position continues to the upper right direction and (by increasing the zoom factor) new copy change is getting bigger. Of course, the copy will be drawn to cover the front graphics. OpenGL's texture is widely used in 3D graphics, texture mapping is widely used. Texture mapping is also quite complex process: a definition texture two control filter three description mapping mode four drawing scene to give the top of the texture coordinates and geometric coordinates attention! ! Texture mapping can only be used in RGBA mode and does not apply to color index mode 1. Texture definition void Glteximage2D (Glenum Target, Glint Level, Glint Components, Glint Border, Glsizei Height, Glint Border, Glenum Format, Glenum Type, Const Glvoid * Pixels); Define a two-dimensional texture map. Target is a constant GL_TEXTURE_2DLEVEL representing the number of texture images of multi-level resolution. If there is only one resolution, Level is 0. Components is an integer from 1 to 4, 1: Select R; 2: Select R A; 3: Select R G B; 4: Select R G B A; Width Height is the size of the texture. Format and Type describe mapping formats and data types. They are the same as the meaning of the previous GLDRAWPIELS (). You can treat the texture as a map. Another one-dimensional texture definitions: void glTexImage1D (GLenum target, GLint level, GLint components, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid * pixels); except that the target is constant GL_TEXTURE_1D, data exceptions provided It should be a one-dimensional array. The general texture data dimension should be a power of 2, and sometimes the boundary data is added according to the type. 2. Texture control (filtering and repetition and removal) All texture control passes: void gltexparameter {if} [v] (Glenum target, glenum pname, Type param); Target can be GL_TEXTURE_1D GL_TEXTURE_2D to be a one-dimensional or Two-dimensional texture. pname and possible values of param below: pname: param: GL_TEXTURE_WRAP_S GL_CLAMPGL_REPEATGL_TEXTURE_WRAP_T GL_CLAMPGL_REPEATGL_TEXTURE_MAG_FILTER GL_NEARESTGL_LINEARGL_TEXTURE_MIN_FILTER GL_NEARESTGL_NEAREST_MIPMAP_NEARESTGL_NEAREST_MIPMAP_LINEARGL_LINEAR_MIPMAP_NEARESTGL_LINEAR_MIPMAP_LINEAR2.1 texture filtering the original image is a square image, it is mapped to the grotesque object, typically one pixel on the image can not A pixel corresponding to the screen. Thus topical zoom, will define the appropriate filter system (an example in 2D): void glTexParameter (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); void glTexParameter (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); the former is amplified and filtered (GL_TEXTURE_MAG_FILTER), which Is a reduced filtering (GL_TEXTURE_MIN_FILTER); in addition, GL_Nearest is a texture element that is closest to the center of the fixin, which is likely to make the pattern, but the calculation speed is fast; GL_Linear utilizes the line interpolation, the effect is good, but the amount of calculation is high. 2.2 Repeat and shrinking texture maps can be repeated mapping or rescue mapping, and the texture can be repeated in their own coordinate S t when repeated mapping. For repeated mapping: void glTexParameterfv (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); void glTexParameterfv (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); GL_REPEAT parameters to GL_CLAMP, the shrinkage limit, all the texel values greater than 1 is set to 1. All texture elements of less than 0 are set to 0.3. Mapping mode processing texture itself pattern color and object itself color relationship: void gltexenv {if} [v] (Glenum target, glenum pname, type param); target must be GL_TEXTURE_ENV ; PNAME is GL_TEXTURE_ENV_MODE, and the param can be GL_DECAL GL_MODULATE or GL_BLEND, indicating that the texture value is different from the original color. PNAME is GL_TEXTURE_ENV_COLOR, the parameter param is an array containing 4 floats (R, G, B, A). These values are only used when using GL_BLEND texture functions. 4. Definition of texture coordinate coordinates: Texture image is square, texture coordinates can be defined as S, T, R, Q coordinates, simulation X, Y, Z, W coordinate of the homogeneous coordinate system. Void gltexcoord {1234} {sifd} [v] (Type CoRDS); Set the current texture coordinates, which will then call the vertices generated by GlvertEX * () to give the current texture coordinates. 5. The coordinate automatically generates sometimes imposing the texture coordinates for each object, and Void GltexGen {if} (Glenum Coord, Glenum PNAME, TYPE PARAM); Coord is: GL_S GL_T GL_R or GL_Q, indicating which coordinate automatically generates PNAME is GL_TEXTURE_GEN_MODE PARAM to constant: GL_Object_Linear GL_EYE_LINEAR or GL_SPHERE_MAP, which function does it decide which function is used to generate texture coordinates PNAME is GL_OBJECT_PLANE or GL_EYE_PLANE, and a pointer to the array array when param. Please see a simple example: //sample.cpp#include "gles.h" #include // Creating a texture image #define texturewidth 64 # Define TextureHeight 64glubyte Texture [TextureWidth] [TextureHeight] [3]; Void MakeTexture (void) {INT I, J, R, G, B; For (i = 0 i void myinit (void) {auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0,0,500,500); auxInitWindow ( "sample1"); glClearColor (0.0,0.0,0.0,0.0); glClear (GL_COLOR_BUFFER_BIT); // create a texture image Original data is saved in Texture [] [] [] Maketexture (); GLPIELSTOREI (GL_UNPACK_ALIGNMENT, 1); // definition of two-dimensional texture glTexImage2D (GL_TEXTURE_2D, 0,3, TEXTUREWIDTH, TEXTUREHEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, & Texture [0] [0] [0]); // Control Filter glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); / / Describe the mapping mode GLTEXENVF (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); // This should be very familiar, enable texture mode glenable (GL_Texture_2D); // Glshademodel (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); // Defines Stereo View Gluperspective (60.0, 1.0 * (GLFLOAT) W / (GLFLOAT) H, 1.0, 30.0); GLmatrixMode GL_MODELVIEW); GLLOADIDENTITY (); GLTRANSLATEF (0.0, 0.0, -3.6);} Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Glbegin (GL_QUADS); // Draw quadrilateral // Draw squares first, used to display the actual unmatched texture pattern GLTEXCOORD2F (0.0, 0.0); Gltex3f (-2.0, -1.0, 0.0); gltexcoord2f (0.0, 1.0); GlvertEx3f (-2.0, 1.0, 0.0); gltexcoord2f (1.0, 1.0); Glvertex3f (0.0, 1.0, 0.0); gltexcoord2f (1.0, 0.0); GlvertEX3F (0.0, -1.0, 0.0); // Draw an irregular quadrangular shape, which is used to display how the texture is deformed with the object shape. Gltexcoord2f (0.0, 0.0); Glvertex3f (0.0, -1.0, 0.0); gltexcoord2f (0.0, 1.0); Glvertex3f (0.0, 1.0, 0.0); gltexcoord2f (1.0, 1.0); Glvertex3f (1.41421, 1.0, -1.41421); GLTEXCOORD2F (1.0, 0.0); GlvertEX3F (1.41421, -1.0, -1.41421); glend (); Glflush ();} void main (void) {Myinit (); AuxReshapefunc (reshape); AuxMainLoop (Display);} // end of sample /// From an example, the application is very convenient in addition to the definition and control of texture and is not easy to understand. It is only necessary to select a suitable point on the vertices of the actual object from the coordinate system of the texture. For complex texture definitions and control, you can also change some parameters yourself and see how the effect. For example, the GL_LINEAR is changed to GL_NEAREST, and the texture is clearly rough, but the calculation results are more fast. You can also change the parameters of GLTEXCOORD2F () to see how to select a part of the texture (which is the selected full texture) to map. For example, 1.0 is changed to 0.5, select the top left 1/4 portion of the actual texture to map. Next, an example and description of a more complex texture application will be given. 18:03 98-1-21 This time will end the texture. The last next time, in the last flat texture map, we first define an array (one-dimensional or two-dimensional ...) to define the data of the texture, so the texture itself is a n-dimensional space, with its own coordinates and vertices . In the last example, we learned how to combine the coordinates of the texture data and the screen object coordinates, just put a piece of cloth into a suitable shape on the surface of the object. The only function of the last unused function is the automatic generation of texture coordinates (the last function), its meaning is to generate an environmental texture, and the objects in all environments give this texture, very similar to a special light source. ///sample.cpp#include "gles.h" #include / / Define a one-dimensional textured data, from the generation, keep the red, the blue component 255 (max), and // is the gradient purple texture, and the saturation is constantly changing. #define TextureWidth 64Glubyte Texture [3 * TextureWidth]; Void Maketexture (Void) {INT i; for (i = 0; i Void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); AuxinitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); Glclear (GL_COLOR_BUFFER_BIT); // create a texture makeTexture (); glPixelStorei (GL_UNPACK_ALIGNMENT, 1); // control texture glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameterf (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage1D (GL_TEXTURE_1D, 0,3, TEXTUREWIDTH, 0, GL_RGB, GL_UNSIGNED_BYTE, texture); // previous example with the only different places: to enable automatic texture coordinate generation, texture generation environment // direction of the texture SglTexGeni ( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv (GL_S, GL_OBJECT_PLANE, sgenparams); // enable texture glEnable (GL_TEXTURE_1D); glEnable (GL_TEXTURE_GEN_S); // enable blanking glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS); // some drawing control, details refer to the online help VC5 glEnable (GL_CULL_FACE); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable (GL_AUTO_NORMAL); glEnable (GL_NORMALIZE); glFrontFace (GL_CW); glCullFace (GL_BACK); glMaterialf (GL_FRONT , GL_SHININESS, 64.0); // Glshademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (w <= h) glortho (-4.0, 4.0, -4.0 * (GLFLOAT) H / (GLFLOAT) W, 4.0 * (GLFLOAT) W, -4.0, 4.0); Elseglortho (-4.0 * (GLFLOAT) H / (GLFLOAT) H / (GLFLOAT) W, -4.0, 4.0, -4.0, 4.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLPUSHMATRIX (); GLROTATEF (30.0, 1.0, 0.0, 0.0); // Powerful auxiliary library function: huh, draw a big teapot. AuxSolidteapot (1.5); glpopmatrix (); glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample to this texture is complete. From the results of the operation, one object has all surface texture mappings. This time, OpenGL complex modeling is explained, and it will be divided into several parts, which first introduces Tura Extensions: How to use a dedicated function to exact planar graphics. The next time I explain how to use the normal vector to generate a surface. 1. Point and line void glpointsize (glfloat size); set point width, size must be> 0, default 1 Void GLLINEWIDTH (GLFOAT Width); Set line width, width> 0, default is 1Void GLLINESTIPPLE (Glint Factor, Glushort Pattern); set line mode, Factor is used to stretch the scale factor for stretching mode, pattern is line mode For example, 11001100 is dashed (1 drawing, 0 is not drawn) You must enable Glenable (GL_LINE_STIPLE) to use the above function, call GLDISABLE (GL_LINE_STIPPLE) when it is no longer used, which is similar to the previous glenable (); gldisable (); usage is similar. Please see the following example: ////sample.cpp#include "gles.h" #include Void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); AuxinitPosition (0,0,600,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); Glshademodel (GL_FLAT);} / * Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (w <= h) glortho (-4.0, 4.0, -4.0 * (GLFLOAT) H / (GLFLOAT) W, 4.0 * (GLFLOAT) W, -4.0, 4.0); Elseglortho (-4.0 * (GLFLOAT) H / (GLFLOAT) W, 4.0 * (GLFLOAT) W, -4.0, 4.0, -4.0, 4.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity ();} * / // Customize the function of the straight line, the parameter is the starting point and the termination point coordinate void line2i (Glint X1, Glint Y1, Glint X2, GLINT Y2) {Glbegin (GL_LINES); GLVERTEX2F (X1, Y1); GlvertEX2F (X2, Y2); glend (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / / First draw a series of points, the size of the point is constantly increasing INT i; GLCOLOR3F (0.8, 0.6, 0.4); for (i = 1; i <= 10; i ) {glpointsize (i * 2); glbegin (GL_Points) GlvertEX2F (30.0 (GLFLOAT) i * 50.0), 330.0); glend ();} // plotted two dashed lines, the second is loose than the first strip, and you can see GLLINEWIDTH by the pattern parameter (1.0 ); glenable (GL_LINE_STIPPLE); GLLINESTIPPPLE (1,0x0f0f); // Interval 1 Glcolor3F (1.0, 0.0, 0.0); line2i (20, 250, 250, 250); GLLINESTIPPLE (1,0x00f); // Interval 2 Glcolor3F (0.0, 0.0 , 1.0); Line2i (300, 250, 550, 250); // Change the effect of the drawing width of the line - Widen // Reaplect the above two dashed GLLINEWIDTH (5.0); GLENABLE (GL_LINE_STIPLLE); GLLINESTIPPLE (1,0x0f0f); Glcolor3f ( 1.0, 0.0, 0.0); Line2i (50, 150, 250, 150); GLLINESTIPPLLE (1,0 x00ff); Glcolor3f (0.0, 0.0, 1.0); line2i (300, 150, 550, 150); Glflush ();} void main (void) {Myinit (); // auxreshapefunc (reshape); auxmainloop (display);} // end of sample // 2. Polygon VOID GLPOLYGONMODE (Glenum Face, Glenum Mode); Control Polygon Specified Face Draw mode, Face is: GL_FRONT GL_BACK or GL_FRONT_AND BackMode as: GL_POINT GL_LINE or GL_FILL represents a polygonal contour point, an outline, and fill mode drawing method . The default is a fill method. Void GLPOLYGONSTIPLLE (const glubyte * mask); where MASK must be pointing to the bitmap pointer to 32 * 32, 1 is drawing, 0 does not draw Use the above functions to call: glenable (GL_POLYGON--stipple); GLDISABLE (GL_POLYGON_STIPPLE); see below: ///sample.cpp#include "gles.h" #include // Define the fill mode 32 * 32 points of glubyte pattern [] = {0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x0f, 0xF0, 0x00, 0x00, 0x1f, 0xF8, 0x00, 0x00, 0x3f, 0xFc, 0x00, 0x00, 0x7f, 0xFe, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x01, 0xFF, 0xFF, 0x80, 0x03, 0xFF, 0xFF, 0XC0, 0x07, 0xFF, 0xFF, 0xFF, 0xF0, 0x1F, 0xFF, 0xFF, 0xF8, 0X3F, 0xFF, 0xFF, 0xFC, 0x7F, 0xFF, 0xFF, 0xFe, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFE, 0x3F, 0xFF, 0xFF, 0xFC, 0x1F, 0xFF, 0xFF, 0xF8, 0x0F, 0xFF, 0xFF, 0xF0, 0x07, 0xFF, 0xFF, 0XE0, 0X03, 0xFF, 0xFF, 0xC0, 0x01, 0xFF, 0xFF, 0x80, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x7F, 0xFe, 0x00, 0x00, 0x3f, 0xFc, 0x00, 0x00, 0x1f, 0xF8, 0x00, 0x00, 0x0f, 0xF0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0x00, 0x03, 0XC0, 0x00, 0x00, 0x01, 0x80, 0x00}; void myinit (void) {auxinitdisplaymode (aux_single | aux_rgba); auxinitposition (0,400,400); auxinitwindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); Glshademodel (GL_FLAT);} / * Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (w <= h) glortho (-4.0, 4.0, -4.0 * (GLFLOAT) H / (GLFLOAT) W, 4.0 * (GLFLOAT) W, -4.0, 4.0); Elseglortho (-4.0 * (GLFLOAT) H / (GLFLOAT) W, 4.0 * (GLFLOAT) W, -4.0, 4.0, -4.0, 4.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity ();} * / Void Callback Display (Void) {Glclear_buffer_bit | GL_DEPTH_BUFFER_BIT); // Select blue as filling color Glcolor3f (0.0, 0.0, 1.0); // Enable polygonal draw mode glenable (GL_POLYGON_STIPPLE); // Using defined fill mode Draw polygonal GLPOLYGONSTIPPLE (PATTERN); // Draw rectangular GLRECTF (48.0, 80.0, 210.0, 305.0); Glflush ();} void main (void) {Myinit (); // auxreshapefunc (reshape); AuxMainLoop (Display);} // End of sample examples The result is to give a rectangle that defines the pattern of the surface. Here, explain OpenGL curve generation 1. Curve definition VOID GLMAP1 {fd} (Glenum Target, Type U1, Type U2, Glint Stride, Glint Order, Const Type * Points); Target points out the meaning of the control point and how much value is needed in the Points parameter. details as follows: Significance target GL_MAP1_VERTEX_3 X Y Z coordinates of the vertices GL_MAP1_VERTEX_4 X Y Z W vertex coordinates GL_MAP1_INDEX color index GL_MAP1_COLOR_4 R G B AGL_MAP1_NORMAL normals GL_MAP1_TEXTURE_COORD_1 S texture coordinate GL_MAP1_TEXTURE_COORD_2 S T texture coordinates, texture coordinates GL_MAP1_TEXTURE_COORD_3 S T R U1, U2 is the range of curve variable u (specifically refer to graphics book) is usually 0 to 1Stride is a span, indicating that the control point offset in Points (or is a dimension of the control point) Order is the order, and control When the number of points is the control point coordinate curve definition to be enabled, it can be drawn, and also use Glenable (), GLDISABLE (). 2. Calculation Draw Void Glevalcoord1 {FD} [V] (Type U); use the control point to generate the coordinates of the curve U coordinate, call only one coordinate can only be generated at a time. The general curve drawing method is to let U change from 0 to 1 (steps of steps) and connect these coordinates with a straight line. The function of the drawing curve can be completed with the above two functions. There are also two functions to implement similar functions: void glmapgrid1 {fd} (GLINT N, TYPE U1, TYPE U2); Define a space grid, from U1 to U2 to n, is equal interval (curve parameters) . Void GlevAlmesh1 (Glenum Mode, Glint P1, GLINT P2); calculated and drawing the coordinate point. Mode can be GL_POINT, GL_LINE, curve paint point or connection line segment it is equivalent to: glbegin (gl_point); for (i = p1; i <= p2; i ) glevalcoord1 (u1 i * (u2-u1) / n); glend (); Example of a Bezier curve is given: //sample.cpp#include "gles.h" #include // Define the coordinate of four control points GLFLOAT POINTS [4] [3] = {{- 4.0, -4.0, 0.0}, {- 2.0, 4.0, 0.0}, {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}}; Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); / / Define the curve and enable the pattern GLMAP1F (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, & Points [0]); GLENABLE (GL_MAP1_VERTEX_3); Glshademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); void CALLBACK display (void) {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f (0.0,0.0,1.0); glBegin (GL_LINE_STRIP); // first step in a straight line segment 30 is connected to draw curves, note that the use of straight line segments connected GL_LINE_STRIP INT i; for (i = 0; i <= 30; glfloat) I / 30.0); glend (); // Draw 4 control points glpointsize (4.0); Glcolor3F (1.0, 0.0, 0.0); Glbegin (GL_Points); for (i = 0; i <4; i ) GlvertEx3FV (& Points [i] [0]); glend (); GLFlush (); } void main (void) {myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample This example draws a Bezier curve with 4 control points. The curve is determined by the control points and positions through the control points and positions, and in turn fall within the quadrangular shape. The next time you will introduce the generation of the surface and its surface texture, color application The structure of the surface can be in the form of grid lines and fillings, in fact, similar to the curve is similar to a two-dimensional. 1. Surface definition void glmap2 {fd} (Glenum Target, Type U1, Type U2, GLINT USTRIDE, GLINT UORDER, TYPE V1, TYPE V2, GLINT VSTRIDE, GLINT VORDER, TYPE POINTS); Target defines the curve introduced in the last introduction The definition of Target. U V is a two-dimensional curved coordinate Uorder, Vorder; USTRIDE, and VSTRIDE definitions are similar to the curve definition. Points is a control point coordinate 2. Calculation of any point of the surface void glevalcoord2 {fd} [v] (Type U, TYPE V); the position of any world coordinate in the surface is calculated with curve coordinate U v 3. Control of the surface drawn Void GlmapGrid2 {fd} (Glenum Nu, Type U1, Type U2, Glenum NV, Type V1, Type V2); Defines the uniform mesh of the surface parameter space, from U1 to U2 to the equal interval Nu step, Divided from V1 to V2 into equal interval NV steps. The following is given an example of grid line depiction surface: //sample.cpp#include "gles.h" #include // Control point coordinate GLFLOAT POINTS [4] [4] [3] = {{{- 1.5, -1.5, 2.0}, {- 0.5, -1.5, 2.0}, {0.5, -1.5, -1.0}, { 1.5, -1.5, 2.0}}, {{- 1.5, -0.5, 1.0}, {- 0.5, 1.5, 2.0}, {0.5, 0.5, 1.0}, {1.5, -0.5, -1.0}}, {{ -1.5, 0.5, 2.0}, {- 0.5, 0.5, 3.0}, {1.5, -1.5, 1.5}}, {{- 1.5, 1.5, -2.0}, {- 0.5, 1.5, -2.0}, {0.5, 0.5, 1.0}, {1.5, 1.5, -1.0}}} // For a set of color GLFLOAT Color [4] [3] = {{1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 10.0}, {1.0, 1.0, 1.0}}; Void myinit (void) { auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0,0,500,500); auxInitWindow ( "sample1"); glClearColor (0.0,0.0,0.0,0.0); glClear (GL_COLOR_BUFFER_BIT); // use glEnable () to enable the surface mode glMap2f (GL_MAP2_VERTEX_3 , 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0] [0]); GLENABLE (GL_MAP2_VERTEX_3); GlmapGrid2f (20, 0.0, 1.0, 20, 0.0, 1.0); Glenable (GL_DEPTH_TEST); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); void CALLBACK display (void) {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f (0.0,0.0,1.0); glPushMatrix (); glRotatef (35.0,1.0,1.0,1.0); // glBegin connection structure depicted curved line segment ( GL_LINE_STRIP); INT i, j; // Draw 8 grid lines in each UV direction to construct surface structure for (j = 0; j <= 8; j ) {// use 30 straight lines in U Segment depicts a curve Glbegin (GL_LINE_STRIP); for (i = 0; i <= 30; i ) Glevalcoord2f ((GLFLOAT) I / 30.0, (GLFLOAT) J / 8.0); glend (); // Draw a curve Glbegin (GL_LINE_STRIP) in V-direction; for (i = 0; i <= 30; i ) Glevalcoord 2f ((GLFLOAT) J / 8.0, (GLFLOAT) I / 30.0); Glend } // Use different colors to display GLPointSize (4.0); glbegin (gl_points); for (i = 0; i <4; i ) for (j = 0; J <4; J ) {Glcolor3FV (Color [ I]); GLVERTEX3FV (& Points [i] [j] [0]);} glend (); GLPOPMATRIX (); glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample ////// The frame structure of the surface is often not satisfied with the surface of the surface. The method of drawing the surface is described below: Void Glease, GLENUM MODE, GLINT P1, GLINT P2, GLINT Q1, GLINT Q2; applies the grid set with GlmapGrid2 {fd} () to the surface that has enabled. Mode can be GL_POINT GL_LINE GL_FILL. As the name suggests, GL_FILL is generating fill surfaces An example of a BEZier surface with illumination is given (this surface is different from the form of the mathematical form, in the previous form). ////sample.cpp#include "glos.h" #include // Initialization of the light, the material of the material void initlights (void) {GLFLOAT Ambient [] = {0.4, 0.6, 0.2, 1.0}; GLFLOAT position [] = {0.0, 1.0, 3.0, 1.0}; GLFLOAT MAT_DIFFUSE [] = { 0.2,0.4,0.8,1.0}; GLfloat mat_specular [] = {1.0,1.0,1.0,1.0}; GLfloat mat_shininess [] = {80.0}; glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glLightfv (GL_LIGHT0, GL_AMBIENT, ambient ); glLightfv (GL_LIGHT0, GL_POSITION, position); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);} Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLMAP2F (GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0] [0]); GLENABLE (GL_MAP2_VERTEX_3); GLENABLE (GL_AUTO_NORMAL); // GLENABLE (GL_NORMALIZE); GLMapGrid2f (20, 0.0, 1.0, 20, 0.0, 1.0); Glenable (GL_DEPTH_TEST); // Initializing the light, material INITLIGHTS (); // Glshademodel (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity ();} void callback display (void) { Glclear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Glcolor3f (0.0, 0.0, 1.0); glpushmatrix (); Glrotatef (35.0, 1.0, 1.0, 1.0); // Comment out the drawing part of the original grid, instead of the drawing method of filling the surface GlevAlmesh2 (GL_FILL, 0, 20, 0, 20); INT I, J; / * for (j = 0; J <= 8; J ) {Glbegin (GL_LINE_STRIP); for (i = 0; i <= 30; i ) Glevalcoord2f ((GLFLOAT) I / 30.0, (GLFLOAT) J / 8.0); glend (); Glbegin (GL_LINE_STRIP); for (i = 0; i <= 30; i ) Glevalcoord2f ((GLFLOAT) J / 8.0, (GLFLOAT) I / 30.0); glend ();} * / GLPointSize (4.0); glbegin (gl_points); for (i = 0; i <4; i ) for (j = 0; j <4; j ) {Glcolor3FV (Color [i]); GlvertEX3FV (& Points [i] [ J] [0]);} glend (); glpopmatrix (); glflush (); } void main (void) {myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of samples A true entity surface exhibits music. The control point is different, which can control the shape of the surface to see the powerful function of OpenGL, only the real 3D effect is generated using very little original code. An example of a NURBS surface (non-uniform B-spline surface) is given before the complex modeling is ended, and the special function provided by the music is used here. All new stoves are in the program, and the comment given the meaning of the process, and the operation of the specific function can be found in detail. ////sample.cpp#include "gles.h" #include Void Myinit; Void Callback Display (Void); Void Callback Reshape (Glsizei W, GLSizei H); / / Define a set of control points for storage GLFLOAT POINTS [4] [4] [3]; // Defines a pointer to NURBS surface objects Glunurbsobj *1nuRB; // The process used to generate a control point, replacing the original direct assignment mode to generate control points void init_surface (void) {INT U, V; for (u = 0; u <4; u ) {for (v = 0; V <4; V ) {Points [u] [V] [0] = 2.0 * ((GLFLOAT) U-1.5); Points [U] [V] [1] = 2.0 * ((GLFLOAT) V- 1.5); if (u == 1 || u == 2) && (v == 1 || V == 2)) Points [u] [v] [2] = 3.0; Elsepoints [u] [v ] [2] = - 3.0;}}} / * GLFLOAT POINTS [4] [4] [3] = {{{- 1.5, -1.5, 2.0}, {- 0.5, -1.5, 4.0}, {0.5, -1.5, 3.0}, {1.5, -1.5, 2.0}, {{- 1.5, -0.5, 3.0}, {- 0.5, 1.5, 4.0}, {0.5, 0.5, 2.0}, {1.5, -0.5, -1.0}}, {{- 1.5, 0.5, 4.0}, {0.5, 0.5, 5.0}, {1.5, -1.5, 1.5}}, {{- 1.5, 1.5, - 2.0}, {- 0.5, 1.5, 1.0}, {1.5, 1.5, -1.0}}}; * / glfloat color [4] [3] = {{1.0, 0.0, 0.0} {0.0, 1.0, 0.0}, {1.0, 1.0, 1.0}}; voidinits (void) {GLFLOAT Ambient [] = {0.4, 0.6, 0.2, 1.0}; GLFLOAT Position [ ] = {0.0, 1.0, 3.0, 1.0}; GLFLOAT MAT_DIFFUSE [] = {0.2, 0.4, 0.8, 1.0}; GLFLOAT MAT_SPECULAR [] = {1.0, 1.0, 1.0, 1.0}; GLFLOAT MAT_SHINININESS [] = {80.0} ; glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glLightfv (GL_LIGHT0, GL_AMBIENT, ambient); glLightfv (GL_LIGHT0, GL_POSITION, position); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (G L_FRONT, GL_SPECULAR, mat_specular); glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess); // initialize the first control point init_surface (); // Create a NURBS surface object theNurb = gluNewNurbsRenderer (); // modify the properties of this surface gluNurbsProperty object ( THENURB, GLU_SAMPLING_TOLERANCE, 25.0); GLUNURBSPROPERTY (1Urb, glu_display_mode, glu_fill);} Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLMAP2F (GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0]); GLENABLE (GL_MAP2_VERTEX_3); GLENABLE (GL_AUTO_NORMAL); GLENABLE (GL_NORMALIZE); GLMapGrid2f (20 , 0.0, 1.0, 20, 0.0, 1.0); Glenable (GL_Depth_test); INITLIGHTS (); // Glshademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); Void Callback Display (void) {// B Swap Surface (NURBS) control vector, you can refer to Graphics related books GLFLOAT KNOTS [8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0} ; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f (0.0,0.0,1.0); glPushMatrix (); glRotatef (35.0,1.0,1.0,1.0); glRotatef (-60.0,1.0,0.0,0.0); glScalef (0.5,0.5,0.5 ); // Note The original surface drawing function is recorded, and the new NURBS surface draw function // glevalmesh2 (GL_FILL, 0, 20, 20); // Defines the mathematical model of the surface to determine its shape Glunurbssurface (thenurb, 8 , Knots, 8, Knots, 4 * 3, 3, & Points [0] [0] [0], 4, 4, GL_MAP2_VERTEX_3); // End the surface of the surface, this structure is very similar to the standard Glbegin () .. .glend () gluendsurface (kilb); INT I, J; GLPOINTSIZE (4.0); glbegin (gl_points); for (i = 0; i <4; i ) for (j = 0; j <4; j ) {Glcolor3FV (Color [i]); GlvertEx3FV ( & Points [i] [j] [0]);} glend (); glpopmatrix (); GLFLUSH (); } void main (void) {myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample //, complex construction is in paragraph. Next introduction special light Why can 3D makers often produce another shocking effect? Because of the use of 3D map, you can generate a true feeling that is rare in reality. Especially some special light and shadow effects. In fact, the front source has been very comprehensive, but lacks some specialized examples. Here we have to deepen aware of understanding, we will see a place in an example to make different lights from different objects, are there rare in reality? 1. Double-sided light: void gllightmodeli (light_model_two_side, gl_true); light calculation is usually performed for polygons. Generally, the conditions of the light are always on the positive polygon, regardless of the back, but if the number of light sources will affect the visibility, it is necessary to calculate both the polygon, which is above. The function of the function; cancel this feature is only necessary to change the third parameter to GL_FALSE. 2. The function of the concentrating source defining the concentrating source is still using GLLightFv (), and the parameters that need to be set include: the light source position, the source divergence half angle, and the light source spot direction. Specific implementation can be seen in the example: ////sample.cpp#include "gles.h" #include Void Myinit; Void Callback Display (Void); Void Callback Reshape (Glsizei W, GLSizei H); Void Initlights (Void) {// Defines the value of the material material characteristics GLFLOAT MAT_AMBIENT [] = {0.2, 0.2, 0.2, 1.0}; GLFLOAT MAT_DIFFUSE [] = {0.8, 0.8, 0.8, 1.0}; GLFLOAT MAT_SPECULAR [] = { 1.0, 1.0, 1.0, 1.0}; glfloat mat_shininess [] = {80.0}; / / Define the attribute of the 0th source (this is parallel ambient light, no concentration effect) GLFLOAT LIGHT_DIFFUSE [] = {0.0, 0.0, 1.0, 1.0}; GLFLOAT Light0_Position [] = {1.0, 1.0, 1.0, 0.0 }; // Define the properties of the first source (spotlight 1) GLFLOAT LIGHT1_AMBIENT [] = {0.2, 0.2, 0.2, 1.0}; GLFLOAT Light1_Diffuse [] = {1.0, 0.0, 0.0, 1.0}; GLFLOAT Light1_Specular [] = {1.0, 0.6, 0.6, 1.0}; GLFLOAT Light1_Position [] = {- 3.0, -3.0, 3.0, 1.0}; GLFLOAT Spot_Direction [] = {1.0, 1.0, -1.0}; / / Define the attribute of the second source (spotlight 2) GLFLOAT LIGHT2_AMBIENT [] = {0.2, 0.6, 0.2, 1.0}; GLFLOAT LIGHT2_DIFFUSE [] = {0.0, 1.0, 0.0, 1.0}; GLFLOAT LIGHT_SPECULAR [] = {0.0 , 1.0, 0.0, 1.0}; GLFLOAT LIGHT2_POSITION [] = {- 3.0, -3.0, 3.0, 1.0}; GLFLOAT Spot2_Direction [] = {1.0, 1.0, -1.0}; //! ! ! We see the first and second concentrated sources in addition to a biaden red in color, a green / / other does not have any difference, so if you work back, open a light source for an object, / / An object opens another light source, which generates a point light source to emit different lights to different objects. // Define the previous attribute to apply GLLIGHTFV (GL_LIGHT0, GL_DIFFUSE, LIGHT0_DIFFUSE); GLLightFv (GL_Light0, GL_Position, Light0_position); glLightfv (GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv (GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv (GL_LIGHT1, GL_SPECULAR, light1_specular); glLightfv (GL_LIGHT1, GL_POSITION, light1_position); // definition of divergence angle spotlight glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0) ; // define spotlight emission direction vector glLightfv (GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction); glLightfv (GL_LIGHT2, GL_AMBIENT, light2_ambient); glLightfv (GL_LIGHT2, GL_DIFFUSE, light2_diffuse); glLightfv (GL_LIGHT2, GL_SPECULAR, light2_specular); glLightfv (GL_LIGHT2, GL_POSITION Light2_position); GLLightf (GL_Light2, GL_SPOT_CUTOFF, 30.0); GLLIGHTFV (GL_Light2, GL_SPOT_DIRECTION, Spot2_Direction); 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_SHININESS, mat_shininess); Glenable (GL_Lighting); Glenable (GL_LIGHT0); GLENABLE (GL_Light1); Glenable (GL_Light2); } Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLDEPTHFUNC (GL_SS); GLENABLE (GL_Depth_test); Initlights (); // Glshademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {GLCLORAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // First, the position (in the same position) of the concentrating source first, two (the same position); GLTRANSLATED (-3.0, -3.0, 3.0); GLDISABLE (GL_Lighting); Glcolor3f (1.0, 0.0, 0.0); Auxwirecube (0.1); Glenable (GL_Lighting); GLPopMatrix (); / / Turn off the second source, only the first source (red), draw a glpushmatrix (); gldisable (GL_Light2); GLTranslated (0.0, 2.0, 0.0); AuxSolidSphere (2.0); glpopmatrix (); / / Turn off the first source, only the second light source (green) is enabled, draw the bony two glpushmatrix (); gldisable (GL_Light1); GLTRANSLATED (0.0, -2.0, 0.0); auxsolidsphere (2.0); GLPOPMAMATRIX (); Glflush ();} void main (void) {Myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample /// A scenario in a reality appears. Not try not to try? Before ending the light source, an example of a light source movement is given, in which the response of the mouse message is used, it is very simple to invoke a Callback function in the way OpenGL's auxiliary library, and the method of the previously lecture response keyboard is completely same. ///sample.cpp#include "gles.h" #include / / Control the variable of the light source moves static int adjust = 0; // Mouse response Callback function void callback mailight (aux_eventrec * Event) {Step = (Step 15)% 360;} Void Initlights (Void) {GLFLOAT MAT_AMBIENT [] = {0.2, 0.2, 0.2, 1.0}; GLFLOAT MAT_DIFFUSE [] = {0.8, 0.8, 0.8, 1.0}; GLFLOAT MAT_SPECULAR [] = {1.0, 1.0, 1.0, 1.0} GLFLOAT MAT_SHININESS [] = {80.0}; GLFLOAT LIGHT0_DIFFUSE [] = {0.0, 0.0, 1.0, 1.0}; GLFLOAT LIGHT0_AMBIENT [] = {0.2, 0.5, 0.5, 1.0}; GLLIGHTFV (GL_LIGHT0, GL_DIFFUSE, LIGHT0_DIFFUSE); GLLIGHTFV (GL_LIGHT0, GL_AMBIENT, LIGHT0_AMBIENT); 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_SHININESS, mat_shininess); Glenable (GL_Lighting); Glenable (GL_LIGHT0); } Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLDEPTHFUNC (GL_SS); Glenable (GL_Depth_test); INITLIGHTS (); // Glshademodel (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLmatrixmode (GL_ModelView); GLLoadIdentity (); Void Callback Display (Void) {GLFLOAT POSITION [] = {0.0, 0.0, 1.5, 1.0}; GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Glpushmatrix (); gltranslatef (0.0, 0.0, -5.0); GLPUSHMAMATRIX (); / / The position of the light source is only determined by the rotary variable STEP. Each time the left mouse button is pressed, the value of STEP changes // causes the change of the light source position to change glrew, -1.0, 1.0, 1.0); GLLightfv ( GL_LIGHT0, GL_POSITION, POSITION; GLTRANSLATED (0.0, 0.0, 1.5); GLDISABLE (GL_Lighting); Glcolor3f (1.0, 1.0, 0.0); auxwiresphere (0.25); Glenable (GL_Lighting); Glpopmatrix (); AuxSolidtorus (0.5, 2.5); glpopmatrix (); GLFlush (); } void main (void) {myinit (); Auxmousefunc (Aux_Leftbutton, Aux_MouseDown, Movelight); AuxReshapeFunc (Reshape); AuxMainLoop (Display);} // end of sample / in the example, yellow ball represents the position of the current source, its rotation causes the annular surface surface The light shadow of the light portion is changed, and the light source will rotate in each of the left mouse. Special effects of OpenGL 1 Fusion The front never has never been exposed to transparent or translucent objects, because we have never enabled the fusion treatment so-called fusion is assumed in RGBA mode, the source is (RS, GS, BS, AS), the target color is (RD, GD, BD, AD), the source factor (SR, SG, SB, SA), the final effect of the fusion of the destination factor (DR, DG, DB, DA): (RS * SR RD * DR, GS * SG GD * DG, BS * SB BD * DB, AS * SA AD * DA) then return to one. The formula is quite complicated, but it looks carefully, which is qualitatively consistent with usual integration. The key is how to set the fusion factor (SR, SG, SB, SA) (DR, DG, DB, DA) to achieve different fusion effects: Void GlblendFunc (Glenum Sfactor, Glenum DFactor); two of these parameters can be taken The following value: The value-related factor calculates the fusion factor GL_ZERO source, the purpose (0, 0, 0, 0) GL_ONE source, the purpose (1, 1, 1, 1) GL_DST_COLOR source (RD, GD, BD, AD) GL_SRC_COLOR (RS, GS, BS, AS) GL_ONE_MINUS_DST_COLOR Source (1, 1, 1, 1) - (RD, GD, BD, AD) GL_ONE_MINUS_SRC_COLOR Objective (1, 1, 1, 1) - (RS, GS, BS, AS) GL_SRC_ALPHA Source , Objective (AS, AS, AS, AS) GL_ONE_MINUS_SRC_ALPHA source, destination (1, 1, 1, 1) - (AS, AS, AS, AS) GL_DST_ALPHA source, destination (AD, AD, AD, AD) GL_ONE_MINUS_DST_ALPHA source, Objective (1, 1, 1, 1) - (AD, AD, AD, AD) GL_SRC_ALPHA_SATURATE Source (F, F, F, 1) -min (AS, 1-AD) also utilizes GLENABLE (GL_Blend) GLDISABLE (GL_BLEND) ) To enable, turn off the fusion processing. //sample.cpp#include "gles.h" #include Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); / / Set the fusion effect and enable Glenable (GL_BLEND); GLBLENDFUNC (GL_SRC_SRC_ALPHA); // GLDEPTHFUNC (GL_SS); // GLENABLE (GL_DEPTH_TEST); GLShademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0.0, W, H); GLMATRIXMODE (GL_Projection); GLLoadIdentity (); if (w <= h) Gluortho2D (0.0, 1.0, 0.0, 1.0 * (GLFLOAT H / (GLFLOAT) W); ElsegluortHO2D (0.0, 1.0 * (GLFLOAT) W / (GLFLOAT) H, 0.0, 1.0); GLmatrixMode (GL_ModelView); Void Callback Display (void) { GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Squares of three different colors and transparency, the effect of color fusion Glcolor4f (1.0, 0.0, 0.0, 0.7); glRectf (0.25, 0.4, 0.75, 0.9); Glcolor4f (0.0, 1.0, 0.0, 1.0); glRECTF (0.1, 0.1, 0.6, 0.6); Glcolor4f (0.0, 0.0, 1.0, 0.3); GlRectf (0.4, 0.1, 0.9, 0.6); GLFlush (); } void main (void) {myinit (); AuxreshapeFunc (reshape); AuxMainLoop (Display);} // end of sample / You can try to adjust parameter size, experience the meaning of a parameter. 2 Reverse-like Anti-AliaSing Since the computer generates a graphic in the discrete point, the generation of graphics will inevitably have a gap with the real scene, and the gap is manifested: the sawtooth of the straight or smooth curved surface, the pattern is lost in the original color shape, and the small object is disappeared in the picture. . It is called a sample. Reversely can reduce this situation. It is roughly, that is, the part of the original boundary is brought into low saturation, which does not affect the overall contour, and achieve better smooth effect. "Tips" are provided before the reverse procedure: Void Glhint (Glenum Target, Glenum Hint); where Hint can be: GL_FASTEST gives the most efficient selection of GL_NICEST GL_DONT_CARE no choice Target meaning GL_POINT_SMOOTH_HINT Specify points, GL_LINE_SMOOTH_HINT line, GL_POLYGON_SMOOTH_HINT polygon sampling quality GL_FOG_HINT points out that atomization calculation is performed according to each pixel (GL_NICEST) or according to each vertex (GL_FASTEST) GL_PERSPECTIVE_CORRECTION_HINT Specifies the quality of the color texture interpolated where GL_PERSPECTIVE_CORRECTION_HINT is used to correct the observation error caused by simple linear interpolation. Of course, the main work is also glenable () to do. Prevent a point, the right way is the case. It should be noted that this work is preferably carried out in RGBA mode, first using gleNable () (parameter GL_POINT_SMOOTH GL_LINE_SMOOTH or GL_POLYGON_SMOOTH) Enable anti-walking. In RGBA mode is enabled, the fusion processing must be enabled. Moreover, the most common fusion factors are: GL_SRC_ALPHA (Source) and GL_ONE_MINUS_SRC_ALPHA or GL_ONE (purpose). ////sample.cpp#include "glos.h" #include Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); // Example All new content is 4 sentences, you can try to cancel the anti-destination statement (1, 4) // can clearly compare the improvement of the image quality to the image quality. Glenable (GL_LINE_SMOOTH; GLENABLE (GL_BLEND); GLBLENDFUNC (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLHINT (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); GLDEPTHFUNC (GL_SS); GLENABLE (GL_DEPTH_TEST); GLSHADEMODEL (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity ();} void callback display (void) { GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLLINEWIDTH (5.0); Glcolor4f (1.0, 1.0, 0.0, 0.7); glpushmatrix (); Glrotatef (45.0, 1.0, 1.0, 0.0); AuxwireOctahedron (2.0); glpopmatrix (); GLFlush (); } void main (void) {myinit (); Auxreshapefunc (reshape); AuxMainLoop (Display);} // end of sample ////// Another example of a polygonal anti-sample /////sample.cpp#include "gles.h" #include Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLFLOAT MAT_AMBIENT [] = {0.5, 0.5, 0.0, 1.0}; GLFLOAT MAT_DIFFUSE [] = {1.0, 0.8, 0.1, 1.0}; GLFLOAT POSITION [] = {1.0, 0.0, 1.0, 0.0}; GLMATERIALFV (GL_FRONT, GL_AMBIENT, MAT_AMBIENT); GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE); GLLightFv (GL_Light0, GL_Position, Position); Glenable (GL_Lighting); Glenable (GL_Light0); GLCULLFACE (GL_BACK); GLENABLE (GL_CULL_FACE); // Enable polygonal anti-pending glenable (GL_POLYGON_SMOOTH); GLENABLE (GL_BLEND); GLBLENDFUNC (GL_SRC_ALPHA, GL_ONE); GLDEPTHFUNC (GL_SS); GLENABLE (GL_DEPTH_TEST); GLSHADEMODEL (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) {GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIdentity (); IF (W <= h) glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W, 5.0 * (GLFLOAT) W, -5.0, 5.0); Elseglortho (-5.0 * (GLFLOAT) H / (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity ();} void callback display (void) { GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLLINEWIDTH (5.0); Glcolor4f (1.0, 1.0, 0.0, 0.7); glpushmatrix (); glrotatef (45.0, 1.0, 1.0, 0.0); AuxSolidicosahedron (2.0); glpopmatrix (); GLFLUSH ();} void main (void) {Myinit (); AuxReshapefunc (reshape); AuxMainLoop (Display);} // end of sample compares the result after the reverse procedure, it will be apparent that the multi-deformable linear boundary sawtooth is smoothed. 3 Atomization is finally introduced atomization, atomization can not only make the scenery more real, and greatly reduce the amount of calculation, and the player who has opened F22 will not forget the speed of the gathering of the game. The general atomization model is considered to fuse the actual color and atomization color of the object. The dense mathematical model of the specific atomization is determined to include linear changes, exponential changes, and index variations. Defining atomization is also very simple, just follow the steps: enable atomizing glenable; two control atomization GLFOG * () void glfog {if} [v] (Glenum, Type param); When Glenum is GL_FOG Mode, param can be GL_EXP (index) GL_EXP2 (index square) GL_LINEAR (linear) When Glenum is GL_FOG_DENSITY GL_FOG_START GL_FOG_END, PARAM specifies the parameters of different calculation formulas under different atomization mathematical models, and details can be referred to in the condolence manual. When GLNUM is GL_FOG_COLOR, PARAM is a pointer to color vectors. 3 If necessary, use GLHINT (GL_FOG_HINT, XX) to specify the atomization effect: ////sample.cpp#include "gles.h" #include Void myinit (void) { AuxinitDisplayMode (aux_single | aux_rgba); AuxInitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GlclearColor (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLFLOAT MAT_AMBIENT [] = {0.0, 0.1, 0.8, 1.0}; GLFLOAT MAT_DIFFUSE [] = {0.0, 0.3, 0.6, 1.0}; GLFLOAT MAT_SPECULAR [] = {1.0, 0.0, 1.0, 1.0}; GLFLOAT MAT_SHININESS [] = {15.0}; glfloat position [] = {5.0, 5.0, 5.0, 0.0}; GLFLOAT FOGCOLOR [4] = {0.6, 0.6, 0.0, 1.0}; 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_SHINININESS, MAT_SHININESS); GLLIGHTFV (GL_Light0, GL_Position, Position); Glenable (GL_Light); GLENABLE (GL_Light0); GLFRONTFACE (GL_CW); // Glenable (GL_POLYGON_SMOOTH); // Glenable (GL_BLEND); // GLBLENDFUNC (GL_SRC_ALPHA, GL_ONE); GLDEPTHFUNC (GL_SS); Glenable (GL_Depth_test); // Enable atomization processing Glenable (GL_FOG); {/ Night change effect GLFOGI (GL_FOG_MODE, GL_LINEAR); // Specify atomization color (yellow) GLFOGFV (GL_FOG_COLOR, FOGCOLOR); // Specify a linear change Calculation formula GLFOGF (GL_FOG_START, 3.0); GLFOGF (GL_FOG_END, 15.0); // Specify the quality of the atomization effect GLHINT (GL_FOG_HINT, GL_DONT_CARE);} // Glshademodel (GL_FLAT); Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixmode (GL_Projection); GLLoadIdentity (); IF (w <= h * 3) Glortho (-6.0, 6.0, -2.0 * (GLFLOAT) H * 3 / (GLFLOAT) W, 2.0 * (GLFLOAT) H * 3 / (GLFLOAT) W, 0.0, 10.0); Elseglortho (-6.0 * (GLFLOAT) H / (GLFLOAT) W, 6.0 * (GLFLOAT) W, -2.0, 2.0, 0.0, 10.0); GLmatrixMode (GL_ModelView); GLLoadIdentity (); Void Callback Display (void) { GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the same size, color ring, showing atomization effect glpushmatrix (); gltranslatef (-3.0, -1.5, -3.0); AuxSolidtorus (0.6, 1.5); glpopmatrix (); GLPOPMATRIX (); Glpushmatrix (); gltranslatef (-0.5, -0.5, -6.0); AuxSolidtorus (0.6, 1.5); glpopmatrix (); Glpushmatrix (); gltranslatef (2.0, 0.5, -8.0); AUXSOLIDTORUS (0.6, 1.5); glpopmatrix (); GLFlush (); } void main (void) {myinit (); AuxreshapeFunc (reshape); AuxMainLoop (Display);} // end of sampleopenengl display list (1) Introduction The so-called display list is a function statement that is called after a set of preselected reservations. Call this display list to perform the function in order. Previously all function calls can be called ImmediateMode, now we will introduce a list of display listed. The display list can optimize the program running performance. It is designed to be commanded cache, not dynamic database caches. Once the list is established, before deleting, it cannot be modified, otherwise the system efficiency will be reduced. The main purpose of the display list is to improve operational efficiency. For example, when any one of the geometric transformations is in the display list, the system only retains the final transformation matrix, which is called, and the workload range generates a mathematical model by the function parameters, and the re-matrix conversion efficiency is obviously improved. Generally speaking, the display list is eaten to: a matrix operation stores the final transform matrix to improve the efficiency of the two-numeral bitmap and the image compiled list and the advanced program data, will be the data three light, material and light model of the hardware implementation. Provincial complex model calculates four textured five polygons pattern filling modes Note The following functions are generally not used to display a list as a function for delivering parameters or returns a value, and the list may be called outside the parameter scope, so even these functions are there, the actual system is often completed immediately, sometimes even even Will go wrong. Glislists () glgenlistore () Glpixelstore () Glpixelstore () Glpixelstore () Glpixelstore () Glgenlists () GlRenderMode () Glget * () GlselectBuffer () Glget * () GlselectBuffer () GLGET * () GLSELECTBUFER () (2) Realizing the list of creation (the format is very similar to Glbegin () ... glend ()) Void Glnewlist (Gluint List, Glenum Mode); Description The start of the list, the subsequent function is stored in the list until the flag (see). Mode: GL_Compile only compiles GL_COMILE_AND_EXECUTE to perform it again, then execute it again Void glendlist (void) list ends When you want to call, just use Void GlcallList (Gluint List); as simple as the icon is as simple /, for example, you define somewhere: {Gluint ListName = 1; Glnewlist (ListName, GL_Compile); GLCOLOR3F (1.0, 0.0, 0.0); Glbegin (GL_TRIANGLES); ... glend (); glendlist (); } The replication function is called: {GlcallList (listName);}. (3) Management Display The example above in the example we specify the list of funses 1 display, if this index has been occupied? So you need a set of managed lists. Void gluint GlgenList (GLSIZE RANGE); System Generates Range Accompiled Available Listing Space, Returns to its index, if you do not properly allocate space, return 0 Void Glboolean Glislist (Gluint List); Decision List has been occupied Void Gldeletelists (Gluint List, Glsizei Ran); Delete a set of continuous display lists, starting from list, continuously deleting Range. Note: When a list is created with an existing index, the previous old list is deleted. A secure creation can be as follows: ListIndex = GlGenLists (1); if (ListIndex! = 0) {GLNewList (ListIndex, GL_Compile); ... glendlist ();} (4) Multi-level display list display list can be called A display list, for example: Create a display list 1 2 3 Now create a list 4GlNewList (4, GL_Compile); glbegin (GL_OLYGON); GlcallList (1); GlcallList (2); glend (); glendlist () ; Since this part does not have any visually new enjoyment, no new example is given. You can try yourself to modify the procedure, at least I didn't see too much difference, Xixi. OpenGL frame cache and animation as the last level, we will set up an instant lighting animation, so that there is no VOODOO player to see the effect of OpenGL (at least I think so), complete all this will be nearly 20 kinds of irrigation ultimate goals . We have long (as if it is the third or fourth time) how to use geometric transform to achieve animation. At that time, the effect now looked at all, because frequent flashes were not what we need. Because the time (before this is also) is a single cache mode. The calculated edge modification is inevitably caused by the partial side of the display, and it is flashing. Generally, formal animation production is achieved by dual cache (hardware, software), you can refer to "Household Computer and Game Machine" an article. When the current station displays the cache, the background cache has been calculated, and all content is completely completed by cache copying, preventing flashing. Implementation of an OpenGL frame cache 1 Color Buffer, which is a color index or RGBA data. If the OpenGL system is supported, there are two caches. 2 Depth Buffer is z-buffer for saving the value of the pixel Z direction, replacing the depth is small, used to achieve blanking 3 Template cache is used to keep some of the positions on the screen unchanged, while others are redraw. For example, everyone is familiar with the cockpit angle of the game of the game, only scene changes outside the windshield, and the inside instrumentation, etc. 4 Accumulation Buffer only saves RGBA data for synthesis images, such as a bitmap in a cache to transfer here a new map. Two-frame cache clearance to high resolution mode Clear cache is a huge task of workload, OpenGL generally seeks hardware to complete, otherwise the software is resolved in turn. Every time we must use GlclearColor () everyone is already unfamiliar. First set clearance values void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); void glClearIndex (GLfloat index); void glClearDepth (GLclampd depth); void glClearStencil (GLint s); void glClerAccum (GLfloat red, GLfloat green, GLFLOAT Blue, GLFLOAT Alpha); Then remove the void glclear; mask: GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT Three-dupless animation You can see all the transformations as the calculation of the background cache, then copy all the results to the front desk. So we only need two new content: first in initialization, call AUXINITDISPLAYMODE (AUX_DOUBLE | AUX_RGBA); use AUX_DOUBLE to set a dual cache mode with AUX_SINGLE Then call AUXswapBuffers () in the drawing (GLFLUSH (); after the cache copy. Easy Like Sunday Morning !! Of course, under different systems, this function may be different (after all, is a secondary library function), such as the use of glxswapBuffers (), which can be used under X-Windows. Let's talk about the function of the following example: There is a blue ring as a body, with a yellow highlighted ball represents the position of the light source. The ball is constantly moving from the left side of the screen to the right, and it can be seen that the change in light shadow on the ring. Operation: Mouse left button / right: Start / stop the light source on the top / lower / left / right: Control the proceeds / back / rotation of the ring ///sample.cpp#include "gles.h" #include void myinit (void); void CALLBACK display (void); void CALLBACK reshape (GLsizei w, GLsizei h); void CALLBACK stepDisplay (void); void CALLBACK startIdleFunc (AUX_EVENTREC * event); void CALLBACK stopIdleFunc (AUX_EVENTREC * event); // Step is a parameter indicating the rotation of the cyclic; z is the parameter static glfloat step = 0.0, z = 0.0; // position is the parameter of the location of the light source [] = {- 20.0, 0.0, -5.0, 1.0}; Void myinit (void) {// Initialization Note is a dual cache mode AuxinitDisplayMode (Aux_double | aux_rgba); AuxinitPosition (0,0,500,500); AuxinitWindow ("Sample1"); GLCLEARCOLOR (0.0, 0.0, 0.0, 0.0); GLCLEAR (GL_COLOR_BUFFER_BIT); GLFRONTFACE (GL_CW); Glenable (GL_Lighting); GLFRONTFACE (GL_CW); // Glenable (GL_POLYGON_SMOOTH); // Glenable (GL_BLEND); // GLBLENDFUNC (GL_SRC_ALPHA, GL_ONE); GLDEPTHFUNC (GL_SS); GLENABLE (GL_Depth_test); // Glshademodel (GL_FLAT);} Void Callback Reshape (Glsizei W, Glsizei H) { GLVIEWPORT (0, 0, W, H); GLmatrixMode (GL_Projection); GLLoadIndIndity (); / * if (W <= h * 3) Glortho (-2.0, 2.0, -2.0 * (GLFLOAT) H / (GLFLOAT) W , 2.0 * (GLFLOAT) H / (GLFLOAT) W, -10.0, 10.0); ElSeglortho (-2.0 * (GLFLOAT) H / (GLFLOAT) W, 2.0 * (GLFLOAT) H / (GLFLOAT) W, -2.0, 2.0 , -10.0, 10.0); * / // Enable three-dimensional views, with nearly far-long-distance effects GLFRUSTUM (-6.0, 6.0, -6.0, 6.0, 3.0, 20.0); GLMATRIXMODE (GL_ModelView); GLLoadIdentity (); Void Callback Display (void) { GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // First, set the light source GLPushmatrix (); GLFLOAT LIGHT_AMBIENT [] = {0.3, 0.5, 0.3}; GLFLOAT LIGHT_DIFFUSE [] = {1.0, 1.0, 1.0}; GLFLOAT LIGHT_SPECULAR [] = {0.8, 0.8, 0.0} ; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv (GL_LIGHT0, GL_POSITION, position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); // in Light source draws yellow highlights, marking light source location / / Mark and light source location by position [] determines GLTranslatef (position [0], position [1], position [2] -1.0); GLFLOAT MAT_AMBIENT [] = {1.0, 0.0, 0.0, 1.0}; GLFLOAT MAT_DIFFUSE [] = {1.0, 1.0, 0.0, 1.0}; GLFLOAT MAT_SPECULAR [] = {1.0, 1.0, 0.0, 1.0}; GLFLOAT MAT_SHININESS [] = {50.0}; 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_SHININESS, mat_shininess); auxSolidSphere (0.5); glPopMatrix (); // Draw a blue ring in the current position, and its position is co-determined by Step Z; GLTranslateF (0.0, 0.0, -8.0 Z); GLROTATEF (135.0 Step, 0.0, 1.0, 0.0); GLFLOAT MAT2_AMBIENT [] = {0.0, 0.0, 1.0, 1.0}; GLFLOAT mat2_diffuse [] = {0.2, 0.0, 0.99, 1.0}; GLFLOAT mat2_specular [] = {1.0, 1.0, 0.0, 1.0}; glfloat mat2_shininess [] = {50.0 }; glMaterialfv (GL_FRONT, GL_AMBIENT, mat2_ambient); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat2_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat2_specular); glMaterialfv (GL_FRONT, GL_SHININESS, mat2_shininess); auxSolidTorus (2.0,3.5); glPopMatrix (); GLFLUSH (); // Draw, caching exchange auxswapbuffrs (); } Void callback up (void) {// keyboard "on" processing z = z 0.05;} void callback down (void) {// keyboard "under" processing z = z-0.05;} Void Callback Left (void ) {// keyboard "left" processing Step = step 2.0;} Void callback right (void) {// keyboard "right" processing Step = stepback stepdisplay (void) {// System is free Calling process position [0] = position [0] 0.5; if (position [0]> 20.0) POSITION [0] = - 20.0; Display ();} Void Callback StartFunc (AUX_EVENTREC * Event) {// Mouse left mouse button processing auxidlefunc (stepdisplay);} Void Callback Stopidlefunc (AUX_EVENTREC * Event) {// Mouse button processing AUXIdlefunc (0);} Void main (void) {myinit (); auxReshapeFunc (reshape); auxIdleFunc (stepDisplay); auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, startFunc); auxMouseFunc (AUX_RIGHTBUTTON, AUX_MOUSEDOWN, stopIdleFunc); auxKeyFunc (AUX_UP, Up); auxKeyFunc (AUX_DOWN, Down); auxKeyFunc (AUX_LEFT, Left); auxKeyFunc (AUX_RIGHT, RIGHT); AuxMainLoop (Display);} // end of sample where a large number of Callback functions are used to handle different messages, and the phenomenon of flashing is clearly improved (you can put this program two about Double cache is changed to a single cache: set AUX_SINGLE and get off the auxswapBuffers (), see how powerful, as for the drawing of the drawing, can only blame your machine is not happy. Write here, finally playing the music OpenGL! ! ! Imagine the idea of "long-term knowledge" proposed. I have any new content in the future, I will try to supplement it. Everyone can freely supplement.