Need help with Loop?

• 07-12-2005
salman86
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!!
• 07-12-2005
mitakeet
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.
• 07-12-2005
salman86
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;                   }```
• 07-12-2005
Jaken Veina
How do you know that it stops after level 0?
• 07-12-2005
salman86
Quote:

Originally Posted by Jaken Veina
How do you know that it stops after level 0?

Because it only draws one triangle which is the first one. It's reading following condition, since level is 0 when loop starts:
Code:

``` else   drawTri (a, b, c);```
I debugged the loop which does continue till level hits 5 but it only draws level 0 or maybe it is drawing all other levels behind level 0 which i can't see. I'm saying this because the position and points a, b, c doesn't change. Can this be possible? Any other ideas?