void Tree::FractalTree(int branch, int depth, int x, int y, int z) { if(depth < MaxDepth) { glColor3f(0.4, 0.2, 0); glRotatef(data[branch][depth][0], data[branch][depth][1], data[branch][depth][2], 0); glTranslatef(x, y, z - height); glutSolidCylinder(0.05, height, 5, 2); glPushMatrix(); glRotatef(rand() / (float)RAND_MAX * 10, 0, rand() / (float)RAND_MAX, 0); for(int i = 0; i < MaxLeaf; i++) { glPushMatrix(); glColor3f(0, rand()/(float)RAND_MAX * 0.5 + 0.5, 0); float pos = leafData[branch][depth][i][0]; glTranslatef(leafData[branch][depth][i][1], leafData[branch][depth][i][2], -pos); glutSolidSphere(LeafWidth, 3, 2); glPopMatrix(); } glPopMatrix(); FractalTree(branch, depth + 1, x, y, z); } }
/* * recursive tree drawing thing, fleshed out from class notes pseudocode */ 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(); } }
/* * 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, 0, 0); FractalTree(2); glPopAttrib(); glPopMatrix(); glEndList(); }
void Tree::Render() { glColor3f(0.4, 0.2, 0); glRotatef(90, 1, 0, 0); glTranslatef(0, 0, -height); glutSolidCylinder(0.05, height, 5, 2); for(int i = 0; i < MaxBranch; i++) { glPushMatrix(); FractalTree(i, 0, 0, height, 0); glPopMatrix(); } glEnd(); }