/*###########################################################*/ /* Programmer: L. Grewe from OpenGL source, 1999 */ /* */ /* lines.c */ /* Description: Opens a Window and draws 3 trees. */ /* Each tree is drawn with a call to glCallList() */ /* User can choose projection type desired */ /* */ /* Compile: */ /* cc -o trees trees.c -lglut -lGLU -lGL -lXmu -lX11 -lm */ /* Run: trees */ /*###########################################################*/ #include #include "gl/GL.h" #include "gl/Glu.h" #include "gl/glut.h" #include #include #include /*Function prototypes*/ GLvoid display(GLvoid); GLvoid myinit(GLvoid); GLvoid create_display_list(GLvoid); /*Global Variables*/ GLint TREE = 1; GLUquadricObj *p,*q; //pointers to quadric objects GLfloat TREE_TRUNK_RADIUS = 5.0; GLfloat TREE_TRUNK_HEIGHT = 10.0; GLfloat TREE_TOP_BOTTOM_RADIUS = 10.0; GLfloat TREE_TOP_HEIGHT = 15.0; void main(int argc, char **argv) { glutInit(&argc,argv); /*init. GLUT window sys.*/ /*there will be a single buffered window displaying RGB */ glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); /*size in pixels */ glutInitWindowPosition(0,0); /* located lower-left of screen */ /*Create the window using the setup from the previous glut calls*/ glutCreateWindow("TREE- Display List Example"); /*title=Lines Example*/ /*tell system what function will be in charge of displaying*/ glutDisplayFunc(display); /*Call another function to further initialize window contents*/ myinit(); /*Create List*/ create_display_list(); /*this call causes to go in infinite loop waiting for window i/o */ glutMainLoop(); } GLvoid create_display_list(GLvoid) { glNewList(TREE, GL_COMPILE); //Rotate so that cylinder will be along the y axis //and not the standard around the z axis glPushMatrix(); glRotatef(-90.0, 1.0,0.0,0.0); //rotate -90deg around x axis /*drawing color brown */ glColor3f(0.8,0.5,0.0); gluCylinder(p, TREE_TRUNK_RADIUS, TREE_TRUNK_RADIUS, TREE_TRUNK_HEIGHT,5,5); //move up (now the new z axis) to draw the tree top glTranslatef(0.0,0.0, TREE_TRUNK_HEIGHT); /*drawing color gree */ glColor3f(0.0,1.0,0.0); gluCylinder(q, TREE_TOP_BOTTOM_RADIUS, 0.0, TREE_TOP_HEIGHT,5,5); glPopMatrix(); glEndList(); } /*#####################################*/ /*myinit: this function does a number of * initialization routines including:clears * the background of the graphics system, * enables depth buffering and sets up * viewing conditions * the drawing color to * red. Finally, viewing parameters are set.*/ /*#####################################*/ GLvoid myinit(GLvoid) { char val; /*enable depth buffering*/ glEnable(GL_DEPTH_TEST); /*white background */ glClearColor(1.0,1.0,1.0,0.0); /*drawing color red */ glColor3f(1.0,0.0,0.0); /* Viewport settings....currently same as*/ /* default which is viewport=window*/ glViewport(0.0, 0.0, 500.0, 500.0); /*Viewing Configuration */ glMatrixMode(GL_PROJECTION); /*says will specify project. */ glLoadIdentity(); /*load identity matrix.*/ /*position of camera..defines position of viewing volume*/ /*currently same as default:*/ /* eye at origin lookind down -z axis*/ gluLookAt(0.0,0.0,0.0, 0.0,0.0,-1.0, 0.0, 1.0,0.0); /*center of camera, looking at point, up vector*/ /*Define Viewing Volume Shape*/ printf("Choose option desired\n"); printf("1: Orthographic\n"); printf("2: Perspective with glPerspective\n"); printf("3: Perspective with glFrustum\n"); val = getchar(); if(val == '1') glOrtho(-300.0,300.0, -300.0, 300.0, 100.0, 4000.0); /*specifies Othographic proj*/ else if(val == '2') gluPerspective(90.0, 1.0, 0.0, 1000.0); else glFrustum(-300.0,300.0,-300.0, 300.0, 200.0, 4000.0); /*at near=0 can't see objects...project to <1 pixel near=50 very small near=100-200 get larger near=250 starting to clip off one of the trees*/ /*allocate quadric object used in drawing cylinder*/ p = gluNewQuadric(); gluQuadricDrawStyle(p, GLU_FILL); //draw quadric in fill mode q = gluNewQuadric(); gluQuadricDrawStyle(q, GLU_FILL); //draw quadric in fill mode /*Shift to Model Specification Model */ glMatrixMode(GL_MODELVIEW); } /*#####################################*/ /*display: this function draws three trees*/ /*#####################################*/ GLvoid display(GLvoid) { /*clear color and depth buffers*/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /*draw trees*/ glLoadIdentity(); /*load identity matrix.*/ /*move back to draw first tree*/ glTranslatef(0.0,0.0,-300.0); glCallList(TREE); /*move and draw next tree*/ glTranslatef(100.0,100.0,100.0); glCallList(TREE); /*move again and draw last tree*/ glTranslatef(-200.0, -200.0, -200.0); glCallList(TREE); glFlush(); /*not needed but asks to draw ASAP.*/ }