Recurcion in OpenGL

Started by
3 comments, last by pisaev 20 years, 7 months ago
How can i use recursion with OpenGL. Exemple: void HanoyTowers(int n, int a ,int b ,int c) { if (n=1){move(a,c)} else{HanoyTowers(n-1,a,c,b); move(a,c); HanoyTowers(n-1,a,c,b); } } . . . HanoyTowers(10,1,2,3) This must muve 10 disks from 1 to 3.
Advertisement
I don''t know how the Hanoi towers puzzle is algorithmically solved so I let''s presume your code is correct, but I do know that recusrsion isn''t in any way directly related to OpenGL (you can add OpenGL to recursive functions with no problem at all - just beware of calling certain OpenGL functions while in recursion if you do so!).

The example you brought seems okay as far as recursion is concerned. Anyway, if you haven''t noticed yet, GameDev allows you to post your code in a "source window" that you can insert to your posts by using the respective [ source ] and [ /source ] tags. To add formatting to your code (tabulation), also add the &ltpre> and </pre> tags around the source tags. Like this:

&ltpre>
[ source ]
void HanoyTowers(int n, int a ,int b ,int c)
{
if (n=1)
{
move(a,c)
}
else
{
HanoyTowers(n-1,a,c,b);
move(a,c);
HanoyTowers(n-1,a,c,b);
}
}
[ /source ]
&ltpre>

becomes this:

   void HanoyTowers(int n, int a ,int b ,int c){  if (n=1)    {    move(a,c)    }  else    {    HanoyTowers(n-1,a,c,b);    move(a,c);    HanoyTowers(n-1,a,c,b);    }}


Just don''t forget to remove the spaces from the source tags, though!
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
That''s a perfectly valid way to use recursion in openGL. An example of code from a program I''m doing involves recursion when I draw a node of an octree:
void COctree::DrawOctree(COctree *pNode, CVector3 camera){	if(!pNode) return;	else if(g_Frustum.CubeInFrustum(pNode->m_vCenter.x, pNode->m_vCenter.y, 			pNode->m_vCenter.z, pNode->m_Width / 2))	if(pNode->IsSubDivided())	{		DrawOctree(pNode->m_pOctreeNodes[TOP_LEFT_FRONT], camera);		DrawOctree(pNode->m_pOctreeNodes[TOP_LEFT_BACK], camera);		DrawOctree(pNode->m_pOctreeNodes[TOP_RIGHT_BACK], camera);		DrawOctree(pNode->m_pOctreeNodes[TOP_RIGHT_FRONT], camera);		DrawOctree(pNode->m_pOctreeNodes[BOTTOM_LEFT_FRONT], camera);		DrawOctree(pNode->m_pOctreeNodes[BOTTOM_LEFT_BACK], camera);		DrawOctree(pNode->m_pOctreeNodes[BOTTOM_RIGHT_BACK], camera);		DrawOctree(pNode->m_pOctreeNodes[BOTTOM_RIGHT_FRONT], camera);	}	else if(Distance(camera, CVector3(pNode->m_vCenter)) < 2500)	{		g_TotalNodesDrawn++;		polycount+=pNode->GetTriangleCount();		if(!pNode->m_pVertices)                        return;		CPoly *pVertices = pNode->m_pVertices;		for(int i=0;i<pNode->GetTriangleCount();i++)		{			pNode->m_pVertices[i].Draw();		}	}}
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
Why does this look like a school project assigned to someone who doesn''t know what they''re talking about?
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
how many people do YOU know that actually has a clue in a highschool computers class? . We are a small minority
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.

This topic is closed to new replies.

Advertisement