# Thread: Need help with Loop?

1. ## Need help with Loop?

Hey guys,

I made a program in open GL which draws sierpinksy triangle recursivley. Since it's based on different levels and i'm passing "level" as param. So I tried to use a loop and increase the counter by one to see every level and paused for 1 millisecond. However, everytime it draws 0 level triangle which basically is the first one. It doesn't continue drawing until my loop condition is true. Here is the code:

Code:
```void Draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer
glLoadIdentity ();												// Reset The Modelview Matrix
glTranslatef (0.0f, 0.0f, -4.0f);							// Translate 6 Units Into The Screen

int level = 0;
Point a, b, c;
a.x = -0.5; a.y = -0.5;
b.x = 0.5; b.y = -0.5;
c.x = 0.0; c.y = 0.5;

while (level != 5) {

drawSierpinski(a, b, c, level);
level += 1;
Sleep(1);
}

glFlush ();													// Flush The GL Rendering Pipeline
}

// Draws a triangle
GLint drawTri(Point a, Point b, Point c)
{
glBegin(GL_POLYGON);

glVertex2f(a.x,a.y);
glVertex2f(b.x,b.y);
glVertex2f(c.x,c.y);

glEnd();

} // end draw triangle

/*******************************************************
* Function from class to draw the Sierpinski fractal
*   Recursion is FUN!
*******************************************************/

GLint drawSierpinski(Point a, Point b, Point c, GLint level)
{
Point m0, m1, m2;
Sleep(1);
if (level > 0) {

m0.x = (a.x + b.x) /2.0;
m0.y = (a.y + b.y) / 2.0;
m1.x = (a.x + c.x) / 2.0;
m1.y = (a.y + c.y) / 2.0;
m2.x = (b.x + c.x) / 2.0;
m2.y = (c.y + b.y) / 2.0;

drawSierpinski(a,m0,m1, level - 1);
drawSierpinski(b, m2, m0, level - 1);
drawSierpinski(c, m1, m2, level - 1);

}
else
drawTri(a, b, c);

} // end draw Sierpinski```
Please someone help me with it or give me any idea to fix it. Don't worry about Point and void Draw(void) they have been declared in ".h" and i've inculded the headers on the top.

I did this program in Java like few months ago as my school assignment; it worked then and i've same code and everything. But now it doesn't work. I would really apperciate the help. Thank you!!

2. Your recursive function will run to completion before your Sleep, and one milisecond is a very short time, basically 'immeasurable' on most common CPUs as the timeslice of a program is 10-15 miliseconds.

3. Thanks for the reply but i don't think that could be teh problem becuase if you look at this code:

Code:
```void Draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear Screen And Depth Buffer
glLoadIdentity ();			// Reset The Modelview Matrix
glTranslatef (0.0f, 0.0f, -4.0f);	  // Translate 6 Units Into The Screen

int level = 0;
Point a, b, c;
a.x = -0.5; a.y = -0.5;
b.x = 0.5; b.y = -0.5;
c.x = 0.0; c.y = 0.5;

while (level != 5) {

drawSierpinski(a, b, c, level);
level += 1;
Sleep(1);
}

glFlush ();													// Flush The GL Rendering Pipeline
}```
inside the loop it should keep drawing to level 5 but it only draw to the level 0. I don't understand why it doesn't keep contnuing to 5. I can rearrange my loop code to following.
Code:
```while (level != 5) {
Sleep(10);
drawSierpinski(a, b, c, level);
level += 1;

}```
``` else