Learning OpenGL (3) drawing spiral graphics
[Email protected]
Http://blog.csdn.net/kezunhai
In OpenGL, graphics are composed of some basic elements, which interpret a set of fixed points or vertex lists as some images drawn on the screen. The smallest element in OpenGL is a vertex. There are 10 elements in OpenGL, which can be used to draw closed polygon from simple points in space to any variable. These 10 elements are:
The format of the drawing is as follows:
Glbegin ("primitive type"); glvertex3f (glfloat X, glfloat y, glfloat Z); glvertex3f (glfloat X, glfloat y, glfloat Z); glvertex3f (glfloat X, glfloat, glfloat Z );...... glend ();
The following example shows how to draw a series of simple points by using the triangle principle. These points rise along the Z axis to form a spiral shape. The source code of the entire instance is as follows:
# Include "stdafx. H "# include <Gl/glut. h >#include <cmath> # include <iostream> using namespace STD; # ifndef gl_pi # define gl_pi 3.14159 # define xrot = 45.0f; glfloat yrot = 30366f; void setuprc_graphic (void) {// black backgroundglclearcolor (0.0f, 0.0f, 0.0f, 1.0f); // set drawing color to greenglcolor3f (1.0f, 1.0f, 0.0f );} // called to draw scenevoid renderscene_graphic (void) {glfloat X, Y, Z, angle; glfloat Fr = 50366f; // clear the window with current colorglclear (gl_color_buffer_bit ); // save matrix state and do the rotationglpushmatrix (); // rotateglrotatef (xrot, 1.0f, 0.0f, 0.0f); glrotatef (yrot, 0.0f, 1.0f, 0.0f ); glbegin (gl_points); Z =-fr; For (angle = 0.0f; angle <= 2.0f * gl_pi * 5.0f; angle + = 0.1f) // 5 circles {x = Fr * sin (angle); y = Fr * Cos (angle); glvertex3f (x, y, z); Z ++ = 0.5 ;} glend (); // restore tables (); // flush drawing commandsglflush ();} // called by glut library when the window has changed sizevoid changesize_graphic (glsizei W, glsizei H) {glfloat nrange = 100366f; If (H = 0) H = 1; // set viewport to window dimensionsglviewport (0, 0, W, H ); // reset coordinate systemglmatrixmode (gl_projection); glloadidentity (); If (W <= h) glortho (-nrange, nrange,-nrange * H/W, nrange * H/W, -nrange, nrange); elseglortho (-nrange * W/H, nrange * W/H,-nrange, nrange,-nrange, nrange); glmatrixmode (gl_modelview ); glloadidentity ();} int _ tmain (INT argc, char * argv []) {gluinitdisplaymode (glu_single | glu_rgb); glucreatewindow ("graphics"); gludisplayfunc (renderscene_graphic ); glureshapefunc (changesize_graphic); setuprc_graphic (); glumainloop (); Return 0 ;}
In the above Code, the glortho () function is used to create a trim space. When the window changes, the changesize () function makes some adjustments to the view area. In the rendering function renderscene_graphic (), the X and Y values of a certain angle are calculated (ranging from 0 ~ Pi loops 5 times), and glvertex3f is used for rendering. The final effect is as follows:
Kezunhai Source: http://blog.csdn.net/kezunhai or sharding, but the documents must be published.