OpenGL path (4) Self-made graphic functions (cube, cylindrical, and conical)

Source: Internet
Author: User

Labels: OpenGL graphics

# Include <Gl/glut. h> # include <Gl/Glu. h> # include <Gl/GL. h> # pragma comment (Lib, "opengl32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glut. lib ") # include <math. h> void cube () {glbegin (1.0); // fill in the convex polygon glvertex3f (0.0f, 0.0f, 0.0f); glvertex3f (0.0f, 1.0f, 0.0f); glvertex3f (1.0f, 0.0f, 0.0f); glvertex3f (1.0f, 1.0f, 0.0f); glvertex3f (1.0f, 0.0f,-1.0f); GLV Ertex3f (1.0f, 1.0f,-1.0f); glvertex3f (0.0f, 0.0f,-1.0f); then (0.0f, 1.0f,-1.0f); glvertex3f (0.0f, 0.0f, 0.0f ); values (0.0f, 1.0f, 0.0f); glend (); glbegin (values); glvertex3f (0.0f, 0.0f, 0.0f); values (1.0f, 0.0f, 0.0f); glvertex3f (0.0f, 0.0f,-1.0f); glvertex3f (1.0f, 0.0f,-1.0f); then (0.0f, 1.0f, 0.0f); then (1.0f, 1.0f, 0.0f); glvertex3f (0.0f, 1.0f, -1.0f); glvertex3f (1.0f, 1. 0f,-1.0f); glend ();} void circle () {glbegin (gl_triangle_fan); // slice continuously filled triangle string glvertex3f (0.0f, 0.0f, 0.0f ); int I = 0; for (I = 0; I <= 390; I ++ = 15) {float P = I * 3.14/180; glvertex3f (sin (P ), cos (P), 0.0f);} glend ();} void cylinder () {glbegin (gl_quad_strip); // continuously populate the Quadrilateral string int I = 0; for (I = 0; I <= 390; I + = 15) {float P = I * 3.14/180; glvertex3f (sin (P), cos (P ), 1.0f); glvertex3f (sin (P), cos (P), 0.0f);} glend (); Circle (); gltranslatef (0, 0, 1); Circle ();} void cone () {glbegin (gl_quad_strip); // consecutively filled quadrilateral string int I = 0; for (I = 0; I <= 390; I + = 15) {float P = I * 3.14/180; glvertex3f (0, 0, 1.0f ); glvertex3f (sin (P), cos (P), 0.0f);} glend (); Circle ();} void renderscene (void) {static float I = 0; I + = 0.1; if (I> 360) I = 0; glclear (gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity (); glpushmatrix (); glcolor3f (0, 1, 0); GL Translatef (-2, 2.0,-12); glrotatef (I, 1, 1, 1); cylinder (); glpopmatrix (); glpushmatrix (); glcolor3f (1, 0, 0); gltranslatef (2, 2,-12); glrotatef (I, 1, 1, 1); Circle (); glpopmatrix (); glpushmatrix (); glcolor3f (0, 1, 1); gltranslatef (-2,-2,-12); glrotatef (I, 1, 1, 1); cube (); glpopmatrix (); glpushmatrix (); glcolor3f (1, 1, 0); gltranslatef (2,-2,-12); glrotatef (I, 1, 1, 1); cone (); glpopmatrix (); gluswapbuffers ();} V Oid changesize (int w, int h) {// prevents the divisor (that is, the height is 0) // (you can set the window width to 0 ). if (H = 0) H = 1; float ratio = 1.0 * W/H; // unitized projection matrix. Glmatrixmode (gl_projection); glloadidentity (); // you can specify the window size as glviewport (0, 0, W, h). // set the correct projection matrix gluperspective (45, ratio, 1, 1000); // The following is the set model view matrix glmatrixmode (gl_modelview); glloadidentity (); glulookat (0.0, 0.0, 5.0, 0.0, 0.0,-1.0, 0.0f, 1.0f, 0.0f); // set the observation point} int main (INT argc, char * argv []) {gluinit (& argc, argv ); fig ); // call the function glureshapefunc (changesize) when the specified program is idle; // specify the callback function glable (gl_depth_test) when the window shape changes; glumainloop (); Return 0 ;}

Effect Preview


OpenGL path (4) Self-made graphic functions (cube, cylindrical, and conical)

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.