Introduction to Computer GraphicsFarhana Bandukwala, PhDLecture 12: Rendering in Open GLOutline• Scene Setup• Modeling transformations• View setup• Projections• Scale to Screen (to pixels)• Cross product• Drawing surfacesScene Setup• Model transformation– Arrange objects within viewing volume• Viewing transformation– Camera position– Viewing direction– View-up vector• Projection – Choose shape of viewing volume• Viewport transformation– Project to display coordinatesDrawing A CubezxydrawCube(GLfloat color[3]) {glBegin(GL_QUAD_STRIP)glColor3f(color[0],color[1],color[2]);glVertex3f(-1,-1,-1);glVertex3f(-1,1,-1);glVertex3f(1,-1,-1);glVertex3f(1,1,-1);glVertex3f(1,-1,1);glVertex3f(1,1,1);glVertex3f(-1,-1,1);glVertex3f(-1,1,1);glVertex3f(-1,-1,-1);glVertex3f(-1,1,-1);glEnd()glBegin(GL_QUADS)glColor3f(color[0],color[1],color[2]);glVertex3f(-1,-1,-1);glVertex3f(1,-1,-1);glVertex3f(1,-1,1);glVertex3f(1,-1,1);glVertex3f(-1,1,-1);glVertex3f(1,1,-1);glVertex3f(1,1,1);glVertex3f(1,1,1);glEnd()}(-1,-1,-1)(1,1,1)Object coordinates implied by cube definitionDrawing 2 Cubesvoid renderScene(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );// Draw the geometric primitives{glMatrixMode(GL_MODELVIEW);glLoadIdentity();// DRAW CUBE 1glPushMatrix();glTranslatef(1,4,2);glRotated(45.0,0,1,0);drawCube( color1);glPopMatrix();//DRAW CUBE 2glPushMatrix();glTranslatef(5,2,1);glScalef(1.0,2.0,1.0);drawCube( color2);glPopMatrix();}glutSwapBuffers();}zxyObject coordinates embedded within a world coordinate systemzxyzxyModeling Transformation• Rotation, translation, or scale glMatrixMode(GL_MODELVIEW)glRotate*(angle,x,y,z), glTranslate*(x,y,z), glScale*(x,y,z)glPushMatrix() & glPopMatrix() to isolate transformations applied to different objectsyzxDefine Camera positionvoid renderScene(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );// Draw the geometric primitives{glMatrixMode(GL_MODELVIEW);glLoadIdentity();//Set up view w/ camera position & orientationgluLookAt(3,3,-3.0,3,3,0.0,0.0,1.0,0.0);// DRAW CUBE 1glPushMatrix();glTranslatef(1,4,2);glRotated(45.0,0,1,0);drawCube( color1);glPopMatrix();//DRAW CUBE 2glPushMatrix();glTranslatef(5,2,1);glScalef(1.0,2.0,1.0);drawCube( color2);glPopMatrix();}glutSwapBuffers();}zxyDefine a camera position & orientationwithin a worldcoordinate systemzxyzxyzxyViewing transformation• Place camera w/ respect to world coordinates• Specify point in object as “eye” point– This determines viewing direction • Specify view-up vectorglMatrixMode(GL_MODELVIEW)gluLookAt(camerax,cameray,cameraz,objectx,objecty,objectz,VUPx,VUPy,VUPz)yzxDefine Projectionvoid renderScene(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );// Draw the geometric primitives{glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(90.0,1.0,0.50,6);glMatrixMode(GL_MODELVIEW);glLoadIdentity();//Set up view w/ camera position & orientationgluLookAt(3,3,-3.0,3,3,0.0,0.0,1.0,0.0);// DRAW CUBE 1glPushMatrix();glTranslatef(1,4,2);glRotated(45.0,0,1,0);drawCube( color1);glPopMatrix();//DRAW CUBE 2glPushMatrix();glTranslatef(5,2,1);glScalef(1.0,2.0,1.0);drawCube( color2);glPopMatrix();}glutSwapBuffers();}zxyzxyzxyzxynearfarField of view angle along VUPAspect ratio perpendicular to VUPProjection• Specify viewing volume shape & extents (clipping planes)• Choose between perspective and orthographic • (glPerspective(field_of_view,aspect_ratio, near,far) or glOrtho(left,right,bottom,top,near,far))Screen windowViewplane to Windowzxyzxyzxyzxynearfarviewplanevoid reshapeCB(int width, int height){double aspect_ratio=(double)width/(double)height;glViewport(0,0,width,height);}void renderScene(){{glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(90.0,aspect_ratio,0.50,6);Viewport Transformation• Defines the region on screen within which graphics will be rendered (glViewport())• Measured in window coordinates• Corresponds to pixels on screenviewplaneScreen
View Full Document