/* Simple 3D recursive tree (C) Bedrich Benes 2005 /* bbenes ~ at ~ purdue.edu*/ #include #include GLUquadric *q; GLint n=1; void Init(void) { glClearColor(1,1,1,1); glShadeModel(GL_FLAT); q=gluNewQuadric(); glNewList(1,GL_COMPILE); gluCylinder(q,1.f,1.f,1.f,5.f,5.f); glEndList(); } void Display(void) { } void RenderRandom(int n) { GLfloat size; glColor3ub(0,0,0); glPushMatrix(); size=(GLfloat)rand()/RAND_MAX*0.5+1; glScalef(0.1,0.1,size); glCallList(1); glPopMatrix(); glTranslatef(0,0,size); if (n==0) return; glPushMatrix(); glRotatef(-180*(GLfloat)rand()/RAND_MAX,0,0,1); glRotatef(15+(GLfloat)rand()/RAND_MAX,1,0,0); RenderRandom(n-1); glPopMatrix(); glPushMatrix(); glRotatef(180*(GLfloat)rand()/RAND_MAX,0,0,1); glRotatef(-15-30*(GLfloat)rand()/RAND_MAX,1,0,0); RenderRandom(n-1); glPopMatrix(); } void Render(int n) { glColor3ub(0,0,0); glPushMatrix(); glScalef(0.05,0.05,1); glCallList(1); glPopMatrix(); glTranslatef(0,0,1); if (n==0) return; // glPushMatrix(); // glRotatef(90,0,0,1); // glRotatef(30,1,0,0); // Render(n-1); // glPopMatrix(); glPushMatrix(); glRotatef(180,0,0,1); glRotatef(30,1,0,0); Render(n-1); glPopMatrix(); // glPushMatrix(); // glRotatef(270,0,0,1); // glRotatef(30,1,0,0); // Render(n-1); // glPopMatrix(); glPushMatrix(); glRotatef(30,1,0,0); Render(n-1); glPopMatrix(); } void Idle(void) { static a; srand(2);//random numbers generator seed glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,-1,0); glRotatef(-90,1,0,0); glRotatef(90,0,0,1); glRotatef(a++,0,0,1); glScalef(0.3,0.3,0.3); RenderRandom(n); glutSwapBuffers(); } void Reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-2,2,-2,2,-20,20); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void Keyboard (unsigned char key, int x, int y) { switch (key) { case '+': n++; break; case '-': n--; break; case 27: exit(0); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (700, 700); glutInitWindowPosition (100, 100); glutCreateWindow("Tree"); Init(); glutDisplayFunc(Display); glutIdleFunc(Idle); glutReshapeFunc(Reshape); glutKeyboardFunc(Keyboard); glutMainLoop(); return 0; }