• Advertisement

Archived

This topic is now archived and is closed to further replies.

Recurcion in OpenGL

This topic is 5285 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 <pre> and </pre> tags around the source tags. Like this:

<pre>
[ 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 ]
<pre>

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!

Share this post


Link to post
Share on other sites
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();
}
}
}

Share this post


Link to post
Share on other sites
Why does this look like a school project assigned to someone who doesn''t know what they''re talking about?

Share this post


Link to post
Share on other sites
how many people do YOU know that actually has a clue in a highschool computers class? . We are a small minority

Share this post


Link to post
Share on other sites

  • Advertisement