Display of the Sierpinski triangle (with OpenGL)

Source: Internet
Author: User
Tags random seed

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Discuss newsgroups and documents

Sierpinski triangle
I have already seen a graph. It's a pretty fragment graph. It's just today that I know it's called such a complicated name. It's obviously named after someone, because I don't study fragment, so he does not care about it.

I only recently read Interactive Computer Graphics (5nd Edward
In the book angel, the author explains OpenGL
This interesting image was introduced when the API was used. This book, as a technical book about graphics (or written as a standard teaching material in the United States), is quite interesting. There are several plots in it.
Sierpinski triangle (this image is called Sierpinski in the book)
Gasket is the same.) It is in line with the spirit of my previous studies. I use the minimum number of knowledge points I have learned to create the greatest pleasure. ^ Examples in the original text only contain source code files (without Engineering)
The source code can only be downloaded one by one, and is displayed at a time. This is not a good feeling for me. It cannot show the gradient feeling when such a fragment is generated, in particular, random vertex generation
The effect of chaos and order similar to the particle system is almost the same. On the one hand, the original version that I am interested in is matched with the xcode Project (only the vs project is needed) on the other hand, provide dynamic
The example of generating a graph can be further understood ......

In the first example, a random vertex is defined to obtain the image:
# Include <stdlib. h>
# Include <glut/glut. h>

Void Init (){
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 0.0, 0.0 );
Glmatrixmode (gl_projection );
Glloadidentity ();
Glortho (0.0, 50.0, 0.0, 50.0,-1.0, 1.0 );
}

Void display (){
Glfloat vertices [3] [3] = {0.0, 0.0, 0.0}, {25.0, 50.0, 0.0}, {50.0, 0.0 }};
Glfloat P [3] = {7.5, 5.0, 0.0 };

Glbegin (gl_points );
For (INT I = 0; I <5000; ++ I ){
Int x = rand () % 3;

P [0] = (P [0] + vertices [x] [0])/2;
P [1] = (P [1] + vertices [x] [1])/2;

Glvertex3fv (P );
}
Glend ();
Glflush ();
}

Int main (INT argc, char * argv []) {

Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (640,480 );
Gluinitwindowposition (200,200 );
Glucreatewindow ("Sierpinski gasket ");
Init ();
Gludisplayfunc (Display );
Glumainloop ();

Return 0;
}

In this example, we can see that the previous part is completely written, so it may be different from the source code of the author. The display is the same. Display Effect:

I feel pretty good. I changed it to dynamic version through dual buffering.
# Include <time. h>
# Include <stdlib. h>
# Include <glut/glut. h>
# Include <unistd. h>

Int gcount = 1;
Time_t grandseed;
Void Init (){
Glclearcolor (0.0, 0.0, 0.0, 0.0 );

Glcolor3f (1.0, 0.0, 0.0 );
Glmatrixmode (gl_projection );
Glloadidentity ();
Glortho (0.0, 50.0, 0.0, 50.0,-1.0, 1.0 );
}

Void increasedisplay (){
Gcount ++;
Usleep (10000 );
Glupostredisplay ();
}

Void display (){
// Just a RAND seed hack for Stably display
Srand (grandseed );

Glclear (gl_color_buffer_bit );
Glfloat vertices [3] [3] = {0.0, 0.0, 0.0}, {25.0, 50.0, 0.0}, {50.0, 0.0 }};
Glfloat P [3] = {7.5, 5.0, 0.0 };

Glbegin (gl_points );
For (INT I = 0; I <gcount; ++ I ){
Int x = rand () % 3;

P [0] = (P [0] + vertices [x] [0])/2;
P [1] = (P [1] + vertices [x] [1])/2;

Glvertex3fv (P );
}
Glend ();
Gluswapbuffers ();
}

Int main (INT argc, char * argv []) {
Grandseed = Time (null );
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (640,480 );
Gluinitwindowposition (200,200 );
Glucreatewindow ("Sierpinski gasket ");
Init ();
Gludisplayfunc (Display );
Glutidlefunc (increasedisplay );
Glumainloop ();

Return 0;
}

The final implementation is not as simple as it was initially thought, because the random values are different each time, which is equivalent to generating a completely new image for each display, resulting in an indefinite graph transformation, cannot reach me
The desired effect is that, by saving the original Random Seed and resetting the seed at random each time, a stable image (grandseed) is obtained. The effect is very good ^

In the second example, the image is obtained through recursive triangle segmentation:
The author's original example may be because the original example is a C language program. When I use it as a C ++ compilation, there will be a compilation error, so I made a few changes to compile and run normally. (Add the include of stdlib. h and assign n to 15)
/* Recursive subdivision of triangle to form Sierpinski gasket */
/* Number of recursive steps given on command line */

# Ifdef _ apple __
# Include <glut/glut. h>
# Else
# Include <Gl/glut. h>
# Endif

# Include <stdlib. h>

/* Initial triangle */
Glfloat V [3] [2] ={{-1.0,-0.58 },{ 1.0,-0.58 },{ 0.0, 1.15 }};

Int n = 15;

Void triangle (glfloat * a, glfloat * B, glfloat * C)

/* Specify one triangle */
{
Glvertex2fv ();
Glvertex2fv (B );
Glvertex2fv (C );
}

Void divide_triangle (glfloat * a, glfloat * B, glfloat * C, int m)
{

/* Triangle subdivision using Vertex numbers */

Glfloat V0 [2], V1 [2], V2 [2];
Int J;
If (M> 0)
{
For (j = 0; j <2; j ++) V0 [J] = (a [J] + B [J])/2;
For (j = 0; j <2; j ++) V1 [J] = (a [J] + C [J])/2;
For (j = 0; j <2; j ++) V2 [J] = (B [J] + C [J])/2;
Divide_triangle (A, V0, V1 m-1 );
Divide_triangle (C, V1, V2 m-1 );
Divide_triangle (B, V2, V0 m-1 );
}
Else triangle (A, B, C);/* draw triangle at end of recursion */
}

Void display ()
{
Glclear (gl_color_buffer_bit );
Glbegin (gl_triangles );
Divide_triangle (V [0], V [1], V [2], n );
Glend ();
Glflush ();
}

Void myinit ()
{
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluortho2d (-2.0, 2.0,-2.0, 2.0 );
Glmatrixmode (gl_modelview );
Glclearcolor (1.0, 1.0, 1.0, 1.0 );
Glcolor3f (0.0, 0.0, 0.0 );
}

Int main (INT argc, char ** argv)
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (500,500 );
Glucreatewindow ("Sierpinski gasket ");
Gludisplayfunc (Display );
Myinit ();
Glumainloop ();
}

Because
In order to use recursion, n can reach a maximum of 10 on my machine. After N is exceeded, the program will not respond for a long time (GU timely Stack Overflow ), however, the gradient process of this program looks better.
The image is gradually becoming thick. In this example, we can clearly see the process of generating the image step by step, which is very interesting. In order to display the data slowly, sleep for one second.
# Include <time. h>
# Include <stdlib. h>
# Include <glut/glut. h>
# Include <unistd. h>

/* Initial triangle */
Glfloat V [3] [2] ={{-1.0,-0.58 },{ 1.0,-0.58 },{ 0.0, 1.15 }};

Int gcount = 1;

/* Specify one triangle */
Void triangle (glfloat * a, glfloat * B, glfloat * c ){
Glvertex2fv ();
Glvertex2fv (B );
Glvertex2fv (C );
}

/* Triangle subdivision using Vertex numbers */
Void divide_triangle (glfloat * a, glfloat * B, glfloat * C, int m ){

Glfloat V0 [2], V1 [2], V2 [2];
Int J;

If (M> 0)
{
For (j = 0; j <2; j ++ ){
V0 [J] = (a [J] + B [J])/2;
}

For (j = 0; j <2; j ++ ){
V1 [J] = (a [J] + C [J])/2;
}

For (j = 0; j <2; j ++ ){
V2 [J] = (B [J] + C [J])/2;
}

Divide_triangle (A, V0, V1 m-1 );
Divide_triangle (C, V1, V2 m-1 );
Divide_triangle (B, V2, V0 m-1 );
}
Else {
Triangle (A, B, C);/* draw triangle at end of recursion */
}
}

Void Init (){
/* Attributes */
Glclearcolor (1.0, 1.0, 1.0, 1.0);/* white background */
Glcolor3f (1.0, 0.0, 0.0);/* draw in red */

Glmatrixmode (gl_projection );
Glloadidentity ();
Gluortho2d (-2.0, 2.0,-2.0, 2.0 );
Glmatrixmode (gl_modelview );
}

Void increasedisplay (){
If (gcount <= 15 ){
Gcount ++;
}

Sleep (1 );
Glupostredisplay ();
}

Void display (){

Glclear (gl_color_buffer_bit );
Glbegin (gl_triangles );
Divide_triangle (V [0], V [1], V [2], gcount );
Glend ();

Gluswapbuffers ();
}

Int main (INT argc, char * argv []) {
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (640,480 );
Gluinitwindowposition (200,200 );
Glucreatewindow ("Sierpinski gasket ");
Init ();
Gludisplayfunc (Display );
Glutidlefunc (increasedisplay );
Glumainloop ();

Return 0;
}

This is an intermediate process:

Its
There are more interesting three-dimensional Sierpinski behind the implementation.
The triangle example, but because there is nothing new, this article will not show. All the above source code is in https: // blog-Sample-
You can find the "code.jtianling.googlecode.com/hg/2010-10-12" directory.

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.