# how to draw stem, twig, leaf?

Discussion in 'C Programming' started by Wonjun, Choi, Oct 30, 2011.

1. ### Wonjun, ChoiGuest

void Calculator( int x, int *stem, int *twig, int *leaf )
{
int k = 0;
while( ++k <= x )
{ *twig = calculate( k-1 );
*leaf = calculate( k+1 );
*stem = i; // check it out !
}
}

this function is that which returns the count of stem, the
count of leaf, twig. I want to use this function in below opengl code.
how can I do?

the concept is below:
Total: 1, loop: 1, twig: 1, leaf: 4

Total: 2, loop: 1, twig: 1, leaf: 4
Total: 2, loop: 2, twig: 4, leaf: 1

Total: 3, loop: 1, twig: 1, leaf: 2
Total: 3, loop: 2, twig: 2, leaf: 3
Total: 3, loop: 3, twig: 5, leaf: 1
.......

as you can see the screenshot(http://ompldr.org/vYjB3NQ), I want to
draw like this
so I want to change below code. but I don't know how to change this:
below code draws tree.
void FractalTree(int level)
{
long savedseed; /* need to save seeds while building tree too */
if (level == Level) {
glPushMatrix();
glRotatef(drand48()*180, 0, 1, 0);
glCallList(STEMANDLEAVES);
glPopMatrix();
} else {
glCallList(STEM);
glPushMatrix();
glRotatef(drand48()*180, 0, 1, 0);
glTranslatef(0, 1, 0);
glScalef(0.7, 0.7, 0.7);
savedseed = (long) drand48()*ULONG_MAX; /* recurse on a 3-
way
branching */
glPushMatrix();
glRotatef(110 + drand48()*40, 0, 1, 0);
glRotatef(30 + drand48()*20, 0, 0, 1);
FractalTree(level + 1);
glPopMatrix();
// srand48(savedseed);
// savedseed = (long) drand48()*ULONG_MAX;
// glPushMatrix();
// glRotatef(-130 + drand48()*40, 0, 1, 0);
// glRotatef(30 + drand48()*20, 0, 0, 1);
// FractalTree(level + 1);
// glPopMatrix();
//
// srand48(savedseed);
// glPushMatrix();
// glRotatef(-20 + drand48()*40, 0, 1, 0);
// glRotatef(30 + drand48()*20, 0, 0, 1);
// FractalTree(level + 1);
// glPopMatrix();
glPopMatrix();
}
}

/*
* Create display lists for a leaf, a set of leaves, and a stem
*/
void CreateTreeLists(void)
{
int i;
// ÁÙ±â
glNewList(STEM, GL_COMPILE);
glPushMatrix();
glRotatef(-90, 1, 0, 0); // ÁÙ±âÀÇ °¢µµ
gluCylinder(cylquad, 0.1, 0.08, 1, 10, 2 ); //ÁÙ±âÀÇ ¿øÅë
glPopMatrix();
glEndList();
// ÀÙÀÇ ¾ç ¿·¸éÀ» ºÙ¿© Ç¥Çö
glNewList(LEAF, GL_COMPILE); /* I think this was jeff allen's leaf
idea */
glBegin(GL_TRIANGLES);
glNormal3f(-0.1, 0, 0.25); /* not normalized */ // Á¶¸íÀ» ³Ö±â À§ÇØ
glVertex3f(0, 0, 0);
glVertex3f(0.25, 0.25, 0.1);
glVertex3f(0, 0.5, 0);
// glNormal3f(0.1, 0, 0.25);
// glVertex3f(0, 0, 0);
// glVertex3f(0, 0.5, 0);
// glVertex3f(-0.25, 0.25, 0.1);
glEnd();
glEndList();
glNewList(STEMANDLEAVES, GL_COMPILE); //3°³ÀÇ °¡Áö
glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT); //
glCallList(STEM); // 3°³ÀÇ °¡Áö »ý¼º
glCallList(LEAF_MAT); // ÀÙÀÇ »ö»ó
for(i = 0; i < 1; i++) {
glTranslatef(0, 0.333, 0); // ÀÙÀÌ ÁÙ±â¿¡ ºÙ¾îÀÖ´Â À§Ä¡ Á¶Àý
glRotatef(90, 0, 1, 0);
glPushMatrix();
glRotatef(0, 0, 1, 0);
glRotatef(50, 1, 0, 0);
glCallList(LEAF);
glPopMatrix();
// glPushMatrix();
// glRotatef(180, 0, 1, 0);
// glRotatef(60, 1, 0, 0);
// glCallList(LEAF);
// glPopMatrix();
}
glPopAttrib(); // »ö
glPopMatrix();
glEndList();
}

/*
* draw and build display list for tree
*/
void CreateTree(void)
{
srand48(TreeSeed);
glNewList(TREE, GL_COMPILE);
glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT);
glCallList(TREE_MAT);
glTranslatef(0, -1, 0);
FractalTree(0);
glPopAttrib();
glPopMatrix();
glEndList();

Wonjun, Choi, Oct 30, 2011

2. ### Jean-ChristopheGuest

On 30 oct, 15:56, "Wonjun, Choi"

> ...
> this function is that which returns the count of stem,
> the count of leaf, twig. I want to use this function
> in below opengl code. how can I do?
> ...

Why not like this ?

----

int twig, leaf, stem; // global

void FractalTree(int level)
{
int k;

k = ... ; // up to you

if( level == Level )
{
...
twig = calculate(k-1);
leaf = calculate(k+1);
stem = k;
glCallList(STEMANDLEAVES);
...
}
else
{
...
FractalTree( level + 1 );
...
}
}

Jean-Christophe, Oct 30, 2011

3. ### Wonjun, ChoiGuest

I need to increase the size of stem, the count of twig and the count
of leaf.

To increase the size of stem.
---------------------------------------------------------------------------
glNewList(STEM, GL_COMPILE);
glPushMatrix();
glRotatef(-90, 1, 0, 0);
gluCylinder(cylquad, 0.1, 0.07, increase count, 10, 2 );
glPopMatrix();
glEndList();
---------------------------------------------------------------------------
increase count is as same as Total

To increase the count of twig.
I don't know how to make this loop even though I know I should modify
this part.
should I call glCallList(STEM) before glCallList(STEMANDLEAVES)?
and how can I attach the twig to the particular joint of stem?
---------------------------------------------------------------------------
if (level == Total) {
glPushMatrix();
glRotatef(drand48()*180, 0, 1, 0);
glCallList(STEMANDLEAVES);
glPopMatrix();
} else {
glCallList(STEM);
glPushMatrix();
glRotatef(drand48()*180, 0, 1, 0);
glTranslatef(0, 1, 0);
glScalef(0.7, 0.7, 0.7);

savedseed = (long) drand48()*ULONG_MAX; /* recurse on a 3-way
branching */
glPushMatrix();
glRotatef(110 + drand48()*40, 0, 1, 0);
glRotatef(30 + drand48()*20, 0, 0, 1);
FractalTree(level + 1);
glPopMatrix();
---------------------------------------------------------------------------

To increase the count of leaf.
should I look at this part? I am not sure.
---------------------------------------------------------------------------
glNewList(STEMANDLEAVES, GL_COMPILE); //3°³ÀÇ °¡Áö
glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT); //
glCallList(STEM); // 3°³ÀÇ °¡Áö »ý¼º
glCallList(LEAF_MAT); // ÀÙÀÇ »ö»ó
for(i = 0; i < 1; i++) {
glTranslatef(0, 0.333, 0); // ÀÙÀÌ ÁÙ±â¿¡ ºÙ¾îÀÖ´Â À§Ä¡ Á¶Àý
glRotatef(90, 0, 1, 0);
glPushMatrix();
glRotatef(0, 0, 1, 0);
glRotatef(50, 1, 0, 0);
glCallList(LEAF);
glPopMatrix();
glPushMatrix();
glRotatef(180, 0, 1, 0);
glRotatef(60, 1, 0, 0);
glCallList(LEAF);
glPopMatrix();
}
glPopAttrib(); // »ö
glPopMatrix();
glEndList();
---------------------------------------------------------------------------

Wonjun, Choi, Oct 31, 2011
4. ### Jean-ChristopheGuest

On 31 oct, 09:49, "Wonjun, Choi"

> I need to increase the size of stem, the count
> of twig and the count of leaf.
> To increase the size of stem.
> glNewList(STEM, GL_COMPILE);
> glPushMatrix();
> glRotatef(-90, 1, 0, 0);
> gluCylinder(cylquad, 0.1, 0.07, increase_count, 10, 2 );
> glPopMatrix();
> glEndList();

Okay, so now I understand your question,
wich is indeed an OpenGL related question !
( it would have been far better if you
formulated it correctly the first time )
So you have to post it back to <comp.graphics.api.opengl>

The point is that you created a glNewList(STEM,GL_COMPILE)
so you can't change the values of 'increase_count' stem
once the list has been created. Each time you will call
the drawing thru this pre-compiled list, its parameters
will still have the values given at the time you created it.

A solution will be to NOT use a precompiled list so you
can change whatever you want at the time of the drawing.
This mean that, instead of calling glCallList(STEMANDLEAVES)
you call a function which dynamically does the actual drawing :

void DrawStem( int increase_count )
{
glPushMatrix();
glRotatef(-90, 1, 0, 0);
gluCylinder(cylquad, 0.1, 0.07, increase_count, 10, 2 );
// etc ...
glPopMatrix();
}

I suggest that you formulate your question cristal-clear,
and don't post tons of code, otherwise
nobody will take the time to unscramble it.

HTH

Jean-Christophe, Oct 31, 2011