Drawing graphics with 3D enhancement Tips (iii)----triangular elements Trangle

Source: Internet
Author: User

1.

Glbegin (Gl_trangles);

........

Glend ();

2. Polygon Wrapping Direction: counterclockwise and clockwise positive and negative

Glfront (GL_CCW) and Glfront (GL_CW);

3. Triangle Belt

Glbegin (Gl_trangle_strip);

.......

Glend ();

4. Triangular fan

Glbegin (Gl_trangle_fan);

.......

Glend ();

Example:

//TRANGLE.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<GL\glut.h>#include<math.h>#definePI 3.14glfloat Range=100.0f ;BOOLdepth_test=false;BOOLCull_face=false;BOOLoutline_back=false;StaticGlfloat xrot=0.0f;StaticGlfloat yrot=0.0f;BOOLstop_depthtest=false;voidInit () {Glclearcolor (0.0f,0.0f,0.0f,1.0f);    Glfrontface (GL_CW); //Monotone ColoringGlshademodel (Gl_flat);}voidChangesize (intWinth) {    if(h==0) {h=1; } glviewport (0,0, w,h);    Glmatrixmode (gl_projection);    Glloadidentity (); if(w<=h) {Glortho (-range,range,-range*h/w,range*h/w,-Range,range); }    Else{Glortho (-range*w/h,range*w/h,-range,range,-Range,range);    } glmatrixmode (Gl_modelview); Glloadidentity ();}voidMyMenu (intvalue) {    Switch(value) { Case 1: Depth_test=!depth_test;  Break;  Case 2: Cull_face=!Cull_face;  Break;  Case 3: Outline_back=!Outline_back;  Break;  Case 4: Stop_depthtest=true; default:         Break; }}voidMyspecialkey (intKeyintXinty) {    if(key==glut_key_left) {Yrot-=5.0f; }    if(key==glut_key_right) {Yrot+=5.0f; }    if(key==glut_key_up) {Xrot-=5.0f; }    if(key==Glut_key_down) {Xrot+=5.0f; }    if(xrot>355.0f) {Xrot=0.0f; }    if(xrot<=-5.0f) {Xrot=355.0f; }    if(yrot>355.0f) {Yrot=0.0f; }    if(yrot<=-5.0f) {Yrot=355.0f; } glutpostredisplay ();}voidMydisplay () {Glint color=1; Glfloat Angle=0.0f;    Glfloat x, y; Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); if(depth_test) {glenable (gl_depth_test); }    Else{gldisable (gl_depth_test); }        if(cull_face) {glenable (gl_cull_face);    Glcullface (Gl_back); }    Else{gldisable (gl_cull_face); }    if(stop_depthtest) {gldisable (gl_depth_test); }    if(outline_back) {glpolygonmode (gl_back,gl_line); }    Else{glpolygonmode (Gl_back,gl_fill);    } Glpushmatrix (); Glrotatef (Xrot,1.0f,0.0f,0.0f); Glrotatef (Yrot,0.0f,1.0f,0.0f);    Glbegin (Gl_triangle_fan); glvertex3f (0.0f,0.0f,75.0f);  for(angle =0; angle<pi*2; angle+= (pi/8) ) {x=50.0f*cos (angle); Y=50.0f*sin (angle); if((color%2)==0) {glcolor3f (0.0f,1.0f,0.0f); }        Else{glcolor3f (1.0f,0.0f,0.0f); } Color++; glvertex3f (x, Y,0.0f);    } glend ();    Glbegin (Gl_triangle_fan); glvertex3f (0.0f,0.0f,0.0f);  for(angle =0; angle<pi*2; angle+= (pi/8) ) {x=50.0f*cos (angle); Y=50.0f*sin (angle); if((color%2)==0) {glcolor3f (0.0f,1.0f,0.0f); }        Else{glcolor3f (1.0f,0.0f,0.0f); } Color++; glvertex3f (x, Y,0.0f);    } glend ();    Glpopmatrix (); Glutswapbuffers ();}intMainintargcChar**argv) {Glutinit (&argc,argv); Glutinitdisplaymode (Glut_rgb| glut_double|glut_depth); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Trangle");    Init ();    Glutdisplayfunc (Mydisplay);        Glutreshapefunc (changesize);    Glutcreatemenu (MyMenu); Glutaddmenuentry ("Depth Test",1); Glutaddmenuentry ("Cullface",2); Glutaddmenuentry ("Outline Back",3); Glutaddmenuentry ("STOP depthtest",4);    Glutattachmenu (Glut_right_button);    Glutspecialfunc (Myspecialkey); Glutmainloop ();}

1. In-depth testing

2. Hide Face Removal

3. Wireframe mode: Glpoloygonmodel (Glenum Face,gl_line/gl_fill);

4. Shading Modes Glshademodel (Gl_flat) and Glshademodel (Gl_smooth);

Drawing graphics with 3D enhancement Tips (iii)----triangular elements trangle

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.