Thread: Need help with Loop?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    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. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    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.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    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; 
    		  
    }
    Any other suggestions? Please someone??

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    How do you know that it stops after level 0?
    Code:
    void function(void)
     {
      function();
     }

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM