Skip to main content
GameDev.net gamedev.net
🔒 Locked

is this recursive function too memory taxing?

Started by BungoMan85 May 22, 2003 at 12:19 AM 3 replies 650+ views
Original Post
BungoMan85
BungoMan85
for some reason my pathetic attempt at making a recursive drawing function in openGL wont work. well it does sometimes. but mostly it causes the program to hang up =/
  
//Detail is a global unsigned long with a value of 256, and Pi is a global float with a value of pi =P, Point is a simple class with an X,Y, and Z float in it.


void DrawStuff(Point offset, float size, unsigned long currentdetail, Point red, Point green, Point blue)//, unsigned long colorscheme)

{
	if (currentdetail<Detail)
	{
		Point nextoffset1;
		Point nextoffset2;
		Point nextoffset3;

		glBegin(GL_TRIANGLES);

		float degrees=2*Pi/3;
		int pass=0;

		for (float f=0.0f;f<2*Pi;f+=degrees)
		{
			float X1=sinf(f)*size;
			float Y1=cosf(f)*size;
			float X2=sinf(f+degrees)*size;
			float Y2=cosf(f+degrees)*size;

			glColor3f(red.X,green.X,blue.X);
			glVertex3f(offset.X,offset.Y,offset.Z);
			glColor3f(red.Y,green.Y,blue.Y);
			glVertex3f(X1+offset.X,Y1+offset.Y,offset.Z);
			glColor3f(red.Z,green.Z,blue.Z);
			glVertex3f(X2+offset.X,Y2+offset.Y,offset.Z);

			switch (pass)
			{
				case 0:	nextoffset1.Set(X2,Y2,offset.Z-0.02f);
						break;
				case 1:	nextoffset2.Set(X2,Y2,offset.Z-0.02f);
						break;
				case 2:	nextoffset3.Set(X2,Y2,offset.Z-0.02f);
						break;
			}

			pass++;
		}

		glEnd();

		DrawStuff(nextoffset1,size-0.0005f,currentdetail+1,red,green,blue);
		DrawStuff(nextoffset2,size-0.0005f,currentdetail+1,green,blue,red);
		DrawStuff(nextoffset3,size-0.0005f,currentdetail+1,blue,red,green);
	}
}
  
in theory what this would do is recursively draw triangles (yeah i know i can one triangle to draw it but it looks cooler if i use the three and it makes it so i can draw from the center =P) with a new one drawn slightly further in the background at each vertex of each triangle out to where there are 256 levels of triangles from the top one. now if i comment out one or two of the recursive calls it sorta works. it doesnt do 256 layers of triangles, just two. oh, when the function is called initialy, currentdetail is set to 0. i can see why this would crash the program. but it does behave a little strangely =/ Bungo!
Bungo!
pag
pag
eh i think im to tired to find any problem, and I never use recursive functions.

Why not use goto instead? its faster than making a function call..

simply:>

void draw(...)
{
labelStart:
if((currentDetail < Detail) == false) goto labelExit;
//code
;
goto labelStart;
labelExit:
;
}

I am a signature virus. Please add me to your signature so that I may multiply.
Digitalfragment
Digitalfragment
Ok, one thing with resursive functions. Are you aware that when you have the

DrawStuff(nextoffset1,...); // here on as DS1()
DrawStuff(nextoffset2,...); // here on as DS2()
DrawStuff(nextoffset3,...); // here on as DS3()

You are going to probably screw up.

If you follow the execution path, you draw your first ''Stuff'' (I''ve got no idea what you are trying to draw here) then you call DS1()

okay, before i get lost here is a temporary counter for detail:

DETAIL = 1

And we are in the second call of DrawStuff. which now calls DS1()

DETAIL = 2

etc.

till DETAIL = 256 in which case your guard prevents another call.

we are in the 256th iteration of DrawStuff().
the stack pops back to the 255th iteraton of DrawStuff(), and now peforms DS2() with DETAIL = 256. Then DS3() with DETAIL = 256.

Then the stack pops back to the 254th iteration, which calls DS2() with DETAIL = 255. DS2(,,255,,,) will then call DS1(,,256,,,), then DS2(,,.....)

Okay im getting lost. But you are going to end up with probably end up either in an infinite loop or the stack will overflow. Either of which can cause the problem you described. I think.

Run the application through the debugger ( most IDEs have one ), and follow the execution path and watch the program stack.

Anyway i''m in my Advanced Database class at TAFE right now and gotta go.

Hope this helps.


Dont listen to me, im just high off coffee.
ICQ: 173141399
Email: _Adrenaline_@phayze.com
BungoMan85
BungoMan85
well in theory it should only do it what? ummm 3^256? wait... oh sweet jesus thats a big number..... 1.3900845237714473276493978678966 * 10^122.... holy fucking shit... lol perhaps i underestimated how many calls were gonna be going on lol.... if i change it to like 4 or 8 it sholdnt be too bad 3^4 =81 an 3^8=6561.... i think im doing the math right... i mean each level should have three times as many triangles as the one before it, right?, yikes... wait...then that means its 3^256+2^255+3^254...3^2+3^1+3^0. good GOD... what have i done? lol
Bungo!
MOVSW
MOVSW
quote:
Original post by pag
eh i think im to tired to find any problem, and I never use recursive functions.

Why not use goto instead? its faster than making a function call..

simply:>

void draw(...)
{
labelStart:
if((currentDetail < Detail) == false) goto labelExit;
//code
;
goto labelStart;
labelExit:
;
}



No, no, no. If you''re going to choose this path then at least use some legitimate control loop statement.


  
while (currentDetail<Detail)
{
// do your stuff here

}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.