Learning OpenGL (3) drawing spiral graphics

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.