Http://www.cnblogs.com/tornadomeet/archive/2012/08/23/2653305.html
In this section, we briefly describe how to color plane geometry and how to rotate the plotted geometry. In the previous section opengl_qt the _02 of learning notes (drawing simple planar geometry), we have described how to use OpenGL to draw some simple images, then this section will be colored on top of it and let him rotate.
Experimental Basis
First of all, coloring, actually coloring in the previous article has been used, using the function glcolor3f (). Once we have used this function to colorize and do not change the color, the graphic that is drawn later is the color. When we render a polygon, the surface is colored to its vertex, in fact because we have set the Glshapemodel (Gl_smooth) in the Initializegl () function, that is, the color shadow is smooth mode. So when we set the color of the vertex, the inside of the polygon will automatically fill the color according to certain rules, and it is a smooth transition.
Here's a look at the rotation, first of all to understand a three-dimensional space of OpenGL, as shown in:
If it is rotated by the x-axis, it is the axis that points to the viewpoint in the Nehe tutorial, where the rotation along the x-axis is likened to a station, the center of the saw blade is placed from left to right, and the sharp sawtooth turns around the x-axis, looking either upward or downward. The rotation of the y-axis is likened to a huge tornado process, where the center of the tornado points from the ground to the sky, and the rubbish and debris revolve around the y-axis from left to right or from one to the left. The rotation of the z-axis is likened to looking straight ahead at a working fan. The blades of the fan are spinning clockwise or counterclockwise around the z-axis. This metaphor is very image.
In fact, we can think of the idea is how to turn, here no longer wordy. In OpenGL, a function is used for rotation:
Glrotatef (Angle, Xvector, Yvector, Zvector)
Angle is usually a variable that represents the angle at which the object is turned. Xvector, Yvector and zvector three parameters together determine the direction of the axis of rotation. For example, the vector described (1,0,0) passes through 1 units of the X axis and is in the right direction. ( -1,0,0) The vector described is passed through the X axis at 1 units, but the direction is left.
Experiment Description
When I'm coloring a circle, I assign a color to one vertex at a time, because the circle is approximated by a triangle, so there are a lot of vertices, and the color of the vertex I give here is randomly assigned, and the random function uses the Qrand () that comes with Qt.
Qrand ()%10 represents an integer between 0~9, so (glfloat) (Qrand ()%10)/10 represents a decimal between 0.0~0.9.
In my experiment, it is also important to note that because the screen size itself is 2 units long and wide, so set the rotation of the axis when the value should be appropriate, otherwise it is difficult to observe the effect of rotation.
Experimental Results
Coloring Result:
A picture effect that is intercepted when rotated:
When rotating in order to see the effect, you can constantly change the size of the window, let Glwidget this class constantly execute the PAINTGL () function, to achieve redrawing to see the rotation effect.
The main part of the Experiment Code and explanation (appendix has the Project code download link address).
Glwidget.cpp:
#include "glwidget.h" #include "ui_glwidget.h" #include <QtGui> #include <QtCore> #include <QtOpenGL> #define GL_PI 3.1415926#define gl_radiux 0.2fglwidget::glwidget (qglwidget *parent): Qglwidget (parent), UI (new UI:: Glwidget) {//Setwindowtitle ("The Opengl for Qt Framework"); UI->SETUPUI (this); fullscreen = false; triangle_rotate = 0.0; quads_rotate = 0.0; circle_rotate = 0.0;} This is true for virtual functions, where this is overridden by the function void Glwidget::initializegl () {setgeometry (300, 150, 500, 500);//Set window initial position and size Glshademodel (gl_smooth )///Set Shadow Smoothing mode Glclearcolor (0.0, 0.0, 0.0, 0.0);//change the background color of the window, but I do not seem to have any effect here glcleardepth (1.0);//Set Depth cache glenable (G L_depth_test);//Allow depth test gldepthfunc (gl_lequal);//Set depth test type Glhint (Gl_perspective_correction_hint, gl_nicest);// Pivot correction}void Glwidget::p aintgl () {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); The Glclear () function is here to work on the color and cache depth set in the INITIALIZEGL () function//() Glloadiden Tity ();//HeavyThe current model observation matrix, when the sentence is finished, moves the focus to the center of the screen/* The following lines of code are used to draw triangles, starting with glbegin (), glend () end; glvertex3f for a 3-dimensional vertex, the coordinate value is floating-point type */ GLTRANSLATEF (-0.3, 0.3,-0.6); Glrotatef (Triangle_rotate, 0.2, 0.2, 0.0);//Set rotation angle, here in order to observe, at the same time along the x-axis and the y-axis of rotation glbegin (gl_triangles);//gl_ Triangles represents the triangle glvertex3f (0.0f, 0.2f, 0.0f);//upper Vertex coordinate glcolor3f (1.0f, 0.0f, 0.0f); glvertex3f ( -0.2f, -0.2f, 0.0f);//lower left corner coordinates glcolor3f (0.0f, 1.0f, 0.0f); glvertex3f (0.2f, -0.2f, 0.0f);//lower-right corner coordinates glcolor3f (0.0f, 0.0f, 1.0f); Glend ();//finish painting triangle_rotate + = 0.05; In order to see the dynamic effect, the angle of rotation after each refresh is added 0.05 glloadidentity ();//re-focus positioning, the same is the center of the screen Gltranslatef (0.3f,0.3f,0.0f); Move 0.3 units Glrotatef (quads_rotate, 0.0, 0.2, 0.2) in the positive direction of the x-axis; glcolor3f (0.0f,1.0f,0.0f);///The color settings are placed in this place, the following vertex settings are valid for/* below the beginning of the drawing of the quadrilateral */Glbegin (gl_quads); glvertex3f ( -0.2f, 0.2f, 0.0f); Top left Vertex glcolor3f (1.0f, 0.0f, 0.0f); glvertex3f (0.2f, 0.2f, 0.0f); Top right vertex glcolor3f (0.0f, 1.0f, 0.0f); glvertex3f (0.2f, -0.2f, 0.0f); Lower right vertex glcolor3f (0.0f, 0.0f,1.0f); glvertex3f ( -0.2f, -0.2f, 0.0f); Lower left Vertex glcolor3f (1.0f, 0.2f, 0.8f); Glend (); Quadrilateral Draw End quads_rotate + = 0.05; Glloadidentity (); Gltranslatef (0.0f, -0.3f, 0.0f); glcolor3f (0.0f, 0.0f, 1.0f); Glrotatef (circle_rotate, 0.2, 0.0, 0.2); /* Here use a continuous triangular area to approximate the area of the circle to achieve the circumference of the drawing */glint circle_points = +, i = 0; Glbegin (Gl_triangle_fan); for (int i = 0; i < circle_points; i++) {Double angle = 2*gl_pi*i/circle_points; Qrand ()%10 is an integer that produces 0~10, where the color is randomly generated glcolor3f ((glfloat) (Qrand ()%10)/10, (Glfloat) (Qrand ()%10)/10, (Glfloat) (Qrand ( )%10)/10); glvertex3f (Gl_radiux*cos (angle), gl_radiux*sin (angle), 0); } glend (); Circle_rotate + = 0.05;} The program is to set the OpenGL scene perspective, at least once in the program (when the program starts). void Glwidget::resizegl (int width, int height) {if (0 = = height) height = 1 ;//Prevent an edge of 0 glviewport (0, 0, (Glint) width, (glint) height);//Reset the current viewport, itself is not the Reset window, but here is the QT to encapsulate the Glmatrixmode (gl_projecti on);//Select projection matrix glloadidentity ();//reset selected projection matrix// Gluperspective (45.0, (glfloat) width/(glfloat) height, 0.1, 100.0);//Establish Perspective projection Matrix Glmatrixmode (Gl_modelview); Glloadidentity ();} void Glwidget::keypressevent (qkeyevent *e) {switch (E->key ()) {//F1 key for full screen and normal screen display toggle key case QT::KEY_F1: fullscreen =!fullscreen; if (fullscreen) showfullscreen (); else {setgeometry (300, 150, 500, 500); Shownormal (); } UPDATEGL (); Break ESE for Exit program key case Qt::key_escape:close (); }}glwidget::~glwidget () {Delete UI;}
Summary:OpenGL in the realization of planar graphics coloring and rotation, the implementation is relatively simple, only need to assign the corresponding function.
References:
http://nehe.gamedev.net/
http://www.owlei.com/DancingWind/
http://blog.csdn.net/qp120291570/article/details/7853513
Appendix:
Experimental Engineering code download.
OPENGL_QT Study notes _03 (shading and rotation of planar graphs) (RPM)