[OpenGL] Case Study 03: recursive program of three-dimensional Sierpinski embedding pad

Source: Internet
Author: User
// A.3 recursive program of three-dimensional Sierpinski embedding pad/* recursive subdivision of a tetrahedron to form 3D Sierpinski gasket * // * Number of recursive steps given on command line */# include <stdlib. h> # include <Gl/glut. h>/* Initial tetrahedron */glfloat V [4] [3] = {0.0, 0.0, 1.0}, {0.0, 0.942809,-0.33333}, {-0.816497, -0.471405,-0.333333}, {0.816497,-0.471405,-0.333333 }}; glfloat colors [4] [3] = {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0 },{ 0.0, 0.0, 0.0 }}; int N; void triangle (glfloat * va, glfloat * Vb, glfloat * Vc) {glvertex3fv (VA ); glvertex3fv (VB); glvertex3fv (VC);} void Tetra (glfloat * a, glfloat * B, glfloat * C, glfloat * D) {glcolor3fv (colors [0]); triangle (A, B, C); glcolor3fv (colors [1]); triangle (A, C, D); glcolor3fv (colors [2]); triangle (A, D, b); glcolor3fv (colors [3]); triangle (B, D, C);} void divide_tetra (glfloat * a, glfloat * B, glfloat * C, glflo At * D, int m) {glfloat mid [6] [3]; Int J; If (M> 0) {/* compute six midpoints */For (j = 0; j <3; j ++) Mid [0] [J] = (a [J] + B [J])/2; for (j = 0; j <3; j ++) Mid [1] [J] = (a [J] + C [J])/2; for (j = 0; j <3; j ++) mid [2] [J] = (a [J] + d [J])/2; for (j = 0; j <3; j ++) mid [3] [J] = (B [J] + C [J])/2; for (j = 0; j <3; j ++) mid [4] [J] = (C [J] + d [J])/2; for (j = 0; j <3; j ++) mid [5] [J] = (B [J] + d [J])/2;/* create 4 tetrahedrons by subdivision */divide_tetra (A, mid [0], mid [1 ], Mid [2] m-1); divide_tetra (mid [0], B, mid [3], mid [5] m-1); divide_tetra (mid [1], mid [3], C, mid [4] m-1); divide_tetra (mid [2], mid [4], D, mid [5], m-1 );} else Tetra (A, B, C, D);/* draw tetrahedron at end of recursion */} void display (void) {glclear (gl_color_buffer_bit | gl_depth_buffer_bit ); glbegin (gl_triangles); divide_tetra (V [0], V [1], V [2], V [3], n); glend (); glflush ();} void myreshape (int w, int h) {glviewport (0, 0, W, H); glmatrixmode (gl_projection); glloadidentity (); If (W <= h) glortho (-2.0, 2.0,-2.0 * (glfloat) h/(glfloat) W, 2.0 * (glfloat) h/(glfloat) W,-10.0, 10.0); elseglortho (-2.0 * (glfloat) W/(glfloat) h, 2.0 * (glfloat) w/(glfloat) h,-2.0, 2.0,-10.0, 10.0); glmatrixmode (gl_modelview); glupostredisplay ();} void main (INT argc, char ** argv) {n = 10; // n = atoi (argv [1]);/* or set number of subdivision St EPS here * // note that an error is reported if the previous sentence is directly used in vs or VC ++, because you need to enter a parameter. // You can use the command prompt line to open the debug folder of the project in windows, and press shift to right-click // select to open the command prompt line at the current position. // Input: main.exe 10 //. main.exe is determined based on your own situation. The next 10 is the number of recursion. You can customize it. // The actual detection result is stuck when the number is greater than 15, if the number is greater than 20, the system will directly crash to the machine, such as gluinitdisplaymode (& argc, argv), gluinitdisplaymode (glu_single | glu_rgb | glu_depth), gluinitwindowsize (500,500 ); gludisplayfunc (Display); glable (gl_depth_test); glclearcolor (1.0, 1.0, 1.0, 1.0); glumainloop ();}

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.