Download presentation
Presentation is loading. Please wait.
Published byMartina Grant Modified over 6 years ago
1
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
2
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab11.cpp #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint N=4; GLint aniOn = 0; GLint mouseX = 0; GLint mouseY = 0; GLint mouseState = 0; GLint mouseButton = 0; GLfloat size=20.0, theta=0.0; GLfloat xTheta=0., yTheta=0., zTheta=0.; GLfloat scale=1.0, scaleDelta=1.01; void myAxis(void) { int i; glColor3f(.98, .04, .70); glBegin(GL_LINES); for(i=0; i<=N; i++) { glVertex2f(-size+2.0*i*size/N, -size); glVertex2f(-size+2.0*i*size/N, size); glVertex2f(-size, -size+2.0*i*size/N); glVertex2f(size, -size+2.0*i*size/N); } glEnd(); glColor3f(.25, .25, .25); glBegin(GL_LINES); glVertex2f(0, -size); glVertex2f(0, size); glVertex2f(-size, 0); glVertex2f(size, 0); void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glRotatef(xTheta, 1.0, 0.0, 0.0); glRotatef(yTheta, 0.0, 1.0, 0.0); glRotatef(zTheta, 0.0, 0.0, 1.0); glScalef(scale, scale, scale); myAxis(); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
3
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab11.cpp glColor3f(.98, .625, .12); glutWireCube(1.0); glFlush(); glutSwapBuffers(); } void myReshape(int width, int height) { glClearColor (.75, .75, .75, 0.0); glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-size, size, -size, size, -size, size); glMatrixMode(GL_MODELVIEW); void myIdle(void) theta+=0.5; glutPostRedisplay(); void myMouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(aniOn) glutIdleFunc(NULL); mouseState=state; mouseButton=btn; mouseX=x; mouseY=y; } else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) { if(aniOn) glutIdleFunc(myIdle); mouseState=-1; else if(btn==GLUT_RIGHT_BUTTON && 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
4
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab11.cpp else if(btn==GLUT_RIGHT_BUTTON && state == GLUT_UP) { if(aniOn) glutIdleFunc(myIdle); mouseState=-1; } else if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { xTheta=yTheta=zTheta=0.; scale=1.0; else return; glutPostRedisplay(); void myMotion(int x, int y) { if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) { yTheta -= (mouseX - x)/10.; xTheta -= (mouseY - y)/10.; else if(mouseButton == GLUT_RIGHT_BUTTON && mouseState == GLUT_DOWN) { if(mouseY!=y) scale = scale * pow(scaleDelta, (mouseY - y)/10.); } else return; mouseX = x; mouseY = y; glutPostRedisplay(); void myKeyboard (unsigned char key, int x, int y) { switch (key) { case ' ': aniOn = !aniOn; if(aniOn) glutIdleFunc(myIdle); else glutIdleFunc(NULL); break; 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
5
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab11.cpp void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("lab11 by glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutKeyboardFunc(myKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutMainLoop(); } 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
6
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 1 Program to draw the figure using the matrix stack with lab11.cpp 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
7
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 2 Program to draw the figure using the matrix stack with lab11.cpp 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
8
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 3 Program to draw the figure using the matrix stack with lab11.cpp 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
9
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 4 Program to draw the figure using the matrix stack with lab11.cpp 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
10
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 5 Program to draw the figure using the matrix stack with lab11.cpp 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
11
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 6 Program to draw the figure using the matrix stack with lab11.cpp Rotate the left 2 pink sphere around the left blue cube with count-clock wise. Rotate the right 2 pink sphere around the right blue cube with clock wise. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
12
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 7 Program to draw the figure using the matrix stack with lab11.cpp Rotate the left blue cube around the red cube at the origin with count-clock wise. Rotate the right blue cube around the origin with clock wise. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
13
Projection Transformations
Converts objects defined in 3D space into objects defined in 2D space. There are two basic types of 3D to 2D projects: - orthographic : a parallel project of 3D values onto a 2D plane; useful for engineering drawings - perspective : the way your eye sees the normal world around you; used for animation and visual simulation Projection Transformations converts objects defined in 3D space into objects defined in 2D space. There are two basis types of 3D to 2D projects by using a perspective or an orthographic projection. There are other types of 3D to 2D projections that are commonly used in mapping. For example, consider a mapping of the 3D earth (continents, oceans, cities, etc.) onto a 2D drawing. There is no mapping that preserves both distances and area. Many projections have been developed, but they all distort the world in different ways. To define the desired projection matrix, which is also used to transform the vertices in your scene. Before you issue any of the transformation commands, remember to call glMatrixMode(GL_PROJECTION); glLoadIdentity();so that the commands affect the projection matrix rather than the modelview matrix. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
14
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Orthographic glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); With an orthographic projection, the viewing volume is a rectangular parallelepiped, or more informally, a box. Unlike perspective projection, the size of the viewing volume doesn't change from one end to the other, so distance from the camera doesn't affect how large an object appears. This type of projection is used for applications such as creating architectural blueprints and computer-aided design, where it's crucial to maintain the actual sizes of objects and angles between them as they're projected. The command glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); creates a matrix for an orthographic parallel viewing volume and multiplies the current matrix by it. The near clipping plane is a rectangle with the lower left corner at (left, bottom, -near) and the upper right corner at (right, top, -near). The far clipping plane is a rectangle with corners at (left, bottom, -far) and (right, top, -far). Both near and far can be positive or negative. For the special case of projecting a two-dimensional image onto a two-dimensional screen, use the Utility Library routine gluOrtho2D(). This routine is identical to the three-dimensional version, glOrtho(), except that all the z coordinates for objects in the scene are assumed to lie between -1.0 and 1.0. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
15
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Orthographic Assume that the camera is at the origin looking down the negative z axis. The x axis is horizontal and the y axis is vertical. xLeft, xRight, yBottom, yTop are distances along their respective axis's. zNear and zFar are distances from the camera. They should never be equal. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
16
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Perspective all points in 3D are projected towards to the position of the camera. The location where this projection intersects the "picture plane" is its "perspective transform" in 2D space glFrustum, gluPerspective The most unmistakable characteristic of perspective projection is foreshortening: the farther an object is from the camera, the smaller it appears in the final image. This occurs because the viewing volume for a perspective projection is a frustum of a pyramid (a truncated pyramid whose top has been cut off by a plane parallel to its base). Objects that fall within the viewing volume are projected toward the apex of the pyramid, where the camera or viewpoint is. Objects that are closer to the viewpoint appear larger because they occupy a proportionally larger amount of the viewing volume than those that are farther away, in the larger part of the frustum. This method of projection is commonly used for animation, visual simulation, and any other applications that strive for some degree of realism because it's similar to how our eye (or a camera) works. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
17
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glFrustum void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); The command to define a frustum, glFrustum(), calculates a matrix that accomplishes perspective projection and multiplies the current projection matrix (typically the identity matrix) by it. Recall that the viewing volume is used to clip objects that lie outside of it; the four sides of the frustum, its top, and its base correspond to the six clipping planes of the viewing volume, as shown in Figure. Objects or parts of objects outside these planes are clipped from the final image. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
18
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glFrustum Assume that the camera is at the origin looking down the negative z axis. The x axis is horizontal and the y axis is vertical. xLeft, xRight, yBottom, yTop are distances along their respective axis's. zNear and zFar are distances from the camera. They should always be positive Creates a matrix for a perspective-view frustum and multiplies the current matrix by it. The frustum's viewing volume is defined by the parameters: (left, bottom, -near) and (right, top, -near) specify the (x, y, z) coordinates of the lower left and upper right corners of the near clipping plane; near and far give the distances from the viewpoint to the near and far clipping planes. They should always be positive. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
19
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
gluPerspective void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); Although it's easy to understand conceptually, glFrustum() isn't intuitive to use. Instead, you might try the Utility Library routine gluPerspective(). This routine creates a viewing volume of the same shape as glFrustum() does, but you specify it in a different way. Rather than specifying corners of the near clipping plane, you specify the angle of the field of view in the x-z plane and the aspect ratio of the width to height (x/y). (For a square portion of the screen, the aspect ratio is 1.0.) These two parameters are enough to determine an untruncated pyramid along the line of sight, as shown in Figure. You also specify the distance between the viewpoint and the near and far clipping planes, thereby truncating the pyramid. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
20
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
gluPerspective Assume that the camera is at the origin looking down the negative z axis. The x axis is horizontal and the y axis is vertical. fieldOfView is the angle between the top and bottom faces of the frustum aspect ratio is the width of the frustum divided by its height zNear and zFar are distances from the camera. They should always be positive The fovy argument is the angle of the field of view in the x-z plane; its value must be in the range [0.0,180.0]. The aspect ratio is the width of the frustum divided by its height. The zNear and zFar values are the distances between the viewpoint and the clipping planes, along the negative z-axis. They should always be positive. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
21
glFrustum, gluPerspective
The parameters of the two projections have the following relationship -Z axis Y Camera zNear zFar yBottom yTop fieldOfView glFrustum and gluPerspective equivalence 1.For the glFrustum command, the distance from the z-axis to yTop does not have to be equal to the distance from the z-axis to yBottom. For the gluPerspective command, the distances are assumed to be equal. 2.tan(fieldOfView/2) = yTop/zNear, which implies that ytop = zNear * tan(fieldOfView/2) 3.Because of required symmetry, yBottom = -yTop. 4.Since the aspectRatio = width / height and because the gluPerspective command is symmetrical about the origin, aspectRatio = 2*xRight / 2*yTop Therefore, xRight = aspectRatio * yTop, xLeft = -xRight 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
22
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab12.cpp #include <math.h> #include <stdio.h> #include <windows.h> #include <gl/glut.h> typedef float vec3_t[3]; vec3_t cube[] = { {-1,-1,-1}, {-1,1,-1}, {1,1,-1}, {1,-1,-1}, {1,1,1}, {-1,1,1}, {1,-1,1}, {-1,-1,1}, {1,-1,1}, {1,-1,-1}, {1,1,-1}, {-1,-1,1}, {1,1,1}, {-1,1,1} }; vec3_t color[] = { {1.0f, 0.0f, 0.0f}, //red {0.0f, 1.0f, 0.0f}, //green {0.0f, 0.0f, 1.0f}, //blue {1.0f, 1.0f, 0.0f}, //yellow {1.0f, 0.0f, 1.0f}, //magenta {0.0f, 1.0f, 1.0f}, //cyan {1.0f, 1.0f, 1.0f}, //white {.25f, .25f, .25f}, //dark gray {.60f, .40f, .70f}, //barney purple {.98f, .625f, .12f}, //pumpkin orange {.98f, .04f, .70f}, //pastel pink {.75f, .75f, .75f}, //light gray {.60f, .40f, .12f} //brown 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
23
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab12.cpp vec3_t rot = {0.,0.,0.}; vec3_t eye = {0.,0.,-5.}; vec3_t center = {0.,0.,0.}; float size=3.; float theta=.0; float thetaDelta=.125; float eyeDelta=.125; float scale=1.0; float scaleDelta=1.125; int mouseX = 0; int mouseY = 0; int mouseState = 0; int mouseButton = 0; int projection = 0; int aniOn = 0; int depthOn = 1; int openOn = 0; int fillOn = 0; int windowWidth, windowHeight; void drawCube (void){ int i; for(i = 0; i < 20+openOn*4; i ++) { if(fillOn) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glColor3fv(color[i%12]); glVertex3fv(cube[i]); if((i+1)%4 == 0) glEnd(); } void myDisplay (void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(!projection) glTranslatef(eye[0], eye[1], eye[2]); glRotatef(rot[0], 1.0f, 0.0f, 0.0f); glRotatef(rot[1], 0.0f, 1.0f, 0.0f); glRotatef(rot[2], 0.0f, 0.0f, 1.0f); glScalef(scale, scale, scale); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
24
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab12.cpp drawCube(); glColor3fv(color[7]); glutWireTeapot(.5); glFlush(); glutSwapBuffers(); } void myResize (int width, int height) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(projection) glOrtho(-size, size, -size, size, -size, size); else gluPerspective(60., (float)width/height, .1, 100.); glEnable(GL_DEPTH_TEST); windowWidth=width; windowHeight=height; void myKeyboard (unsigned char key, int x, int y) { switch (key) { case 'o': openOn = !openOn; break; case 'f': fillOn = !fillOn; case 'p': projection = !projection; myResize(windowWidth, windowHeight); case 'd': depthOn = !depthOn; if(depthOn) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); case 'z': scale*=scaleDelta; case 'x': scale/=scaleDelta; } 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
25
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab12.cpp glutPostRedisplay(); } void myMouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { mouseState=state; mouseButton=btn; mouseX=x; mouseY=y; else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) mouseState=-1; else return; void myMotion(int x, int y) if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) { rot[1] -= (mouseX - x)/5.; rot[0] -= (mouseY - y)/5.; glutPostRedisplay(); mouseX=x; mouseY=y; } void main (void) { glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow("lab12 by glutDisplayFunc(myDisplay); glutReshapeFunc(myResize); glutKeyboardFunc(myKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutMainLoop(); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
26
Algorithms for hidden surface removal
z-buffer (or depth buffer) algorithm painter's algorithm (depth-sorting algorithm) binary space-partitioning (BSP) tree algorithm scan-line algorithms Warnock's Algorithm (area-subdivision algorithm) ray tracing algorithms 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
27
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Z-buffer algorithm For every pixel location, store its color and its depth (distance from camera) Process the polygons in the scene in any order (no sorting required) Rasterize a polygon into a set of pixels and determine a color and depth as each pixel location Replace the current color of a pixel only if the depth associated with the current polygon is less than the current depth stored at the pixel. C. Advantages 1. It is very simple to implement 2. It is very efficient (resulting in fast rendering) 3. It can easily be implemented in hardware (which makes it even faster) 4. It always creates a correct rendering, even for overlapping and intersecting polygons 5. Transparency can be handled using the alpha component of each pixel. D. Disadvantages 1. It requires a large amount of memory to store the depth at each pixel 2. Shadows cannot be easily calculated. 3. Reflections cannot be easily calculated. (as in a mirror) 4. Light refracting (bending) through objects cannot be easily calculated (as in light passing through water). 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
28
Z-buffer algorithm in pseudocode
clear the colorBuffer to the background color clear the depthBuffer to the largest depth value possible for every face in the scene for each pixel (x,y) covering the face { depth = the depth of the face at (x,y) if (depth < depthBuffer[x][y]) color = calculate the color of face at (x,y) colorBuffer[x][y] = color depthBuffer[x][y] = depth } Initialize every pixel's color to the background color and the depth at every pixel to a very large value for every polygon in the scene scan convert the polygon to pixels, calculating a color and depth at each pixel location if the depth of this polygon at this pixel is less than the current pixel depth update the pixel's color with the color of this new polygon The z or depth coordinate is encoded and then stored during the viewport transformation. Unlike x and y window coordinates, z window coordinates are treated by OpenGL as though they always range from 0.0 to 1.0. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
29
Z-buffer algorithm in OpenGL
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); GLUT_DEPTH : Bit mask to select a window with a depth buffer. glEnable, glDisable(GL_DEPTH_TEST); If enabled, do depth comparisons and update the depth buffer. glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); This is the algorithm used by OpenGL to calculate hidden surface removal. glutInitDisplayMode() tells glutInitWindow() whether to create an RGBA or color-index window. You can also specify a single- or double-buffered window. Finally, you can use this routine to indicate that you want the window to have an associated depth buffer. You turn the algorithm "on" by: glEnable(GL_DEPTH_TEST); The depth buffer is cleared by: glClear(GL_DEPTH_BUFFER_BIT); The color buffer is cleared by: glClear(GL_COLOR_BUFFER_BIT); Clearing the buffers is an expensive operation. (If your screen is 800 by 600, clearing the buffer requires 480,000 assignments.) Some hardware enhanced graphics boards can clear multiple buffers at the same time. Therefore, it is best to call glClear once with both buffers specified, as in glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
30
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Camera placement There are two fundamental ways that you can think about camera placement. Think of the camera as never moving from the origin and always looking down the negative z axis. Think of the camera as being moved in front of the scene. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
31
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Camera Control The default camera position X Y -Z Camera The camera is at the origin looking down the negative z axis. The x axis is horizontal and the y axis is vertical Actually, the camera never moves. The objects in the scene must be transformed to be in front of the camera. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
32
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
gluLookAt To simplify the placement of the camera, a utility function was created as follows: gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upVectorX, upVectorY, upVectorz); The first 3 parameters specify the (x,y,z) location of the camera. The second 3 parameters specify the (x,y,z) location of the center of interest; that is, the point being looked at; that is, the point that will appear in the exact center of the output window. The last 3 parameters specify a vector in the "up" direction. Use the gluLookAt() function to change the position and orientation of the camera The 9 parameters are all specified in global coordinates 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
33
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1201.cpp void myDisplay (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(!projection) gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); glRotatef(rot[0], 1.0f, 0.0f, 0.0f); glRotatef(rot[1], 0.0f, 1.0f, 0.0f); glRotatef(rot[2], 0.0f, 0.0f, 1.0f); glScalef(scale, scale, scale); drawCube(); glColor3fv(color[7]); glutWireTeapot(.5); glFlush(); glutSwapBuffers(); } void myLookAt(int key) { if(key == GLUT_KEY_UP) { eye[2] = eye[2]-cos(theta)*eyeDelta; eye[0] = eye[0]+sin(theta)*eyeDelta; } else if(key == GLUT_KEY_DOWN) { eye[2] = eye[2]+cos(theta)*eyeDelta; eye[0] = eye[0]-sin(theta)*eyeDelta; center[2] = eye[2]-cos(theta); center[0] = eye[0]+sin(theta); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
34
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1201.cpp void myResize (int width, int height){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(projection) { glOrtho(-size, size, -size, size, -size, size); eye[2] = 0.; } else { gluPerspective(60., (float)width/height, .1, 100.); eye[2] = 5.; myLookAt(0); glEnable(GL_DEPTH_TEST); windowWidth=width; windowHeight=height; void mySKeyboard (int key, int x, int y) { switch (key) { case GLUT_KEY_UP : break; case GLUT_KEY_DOWN : case GLUT_KEY_LEFT : theta-=thetaDelta; case GLUT_KEY_RIGHT : theta+=thetaDelta; default : return; } myLookAt(key); glutPostRedisplay(); glutSpecialFunc(mySKeyboard); //in main() 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
35
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Moving the camera void mySKeyboard (int key, int x, int y) { switch (key) { case GLUT_KEY_UP : break; case GLUT_KEY_DOWN : break; case GLUT_KEY_LEFT : theta-=thetaDelta; break; case GLUT_KEY_RIGHT : theta+=thetaDelta; break; default : return; } myLookAt(key); glutPostRedisplay(); void myLookAt(int key) if(key == GLUT_KEY_UP) { eye[2] = eye[2]-cos(theta)*eyeDelta; eye[0] = eye[0]+sin(theta)*eyeDelta; else if(key == GLUT_KEY_DOWN) { eye[2] = eye[2]+cos(theta)*eyeDelta; eye[0] = eye[0]-sin(theta)*eyeDelta; center[2] = eye[2]-cos(theta); center[0] = eye[0]+sin(theta); Center of interest Camera Up vector Eye position theta x z Eye position Center of interest theta cos(theta) sin(theta) 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
36
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glutSpecialFunc glutSpecialFunc sets the special keyboard callback for the current window. void glutSpecialFunc(void (*func)(int key, int x, int y)); GLUT_KEY_F1 F1 function key. GLUT_KEY_LEFT Left directional key. GLUT_KEY_UP Up directional key. GLUT_KEY_RIGHT Right directional key. GLUT_KEY_DOWN Down directional key. GLUT_KEY_PAGE UP Page up directional key. GLUT_KEY_PAGE DOWN Page down directional key. GLUT_KEY_HOME Home directional key. GLUT_KEY_END End directional key. GLUT_KEY_INSERT Inset directional key. 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
37
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1202.cpp float anitheta=.0; float anithetaDelta=.125; GLUquadricObj *c; void myDisplay (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(!projection) gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); glRotatef(rot[0], 1.0f, 0.0f, 0.0f); glRotatef(rot[1], 0.0f, 1.0f, 0.0f); glRotatef(rot[2], 0.0f, 0.0f, 1.0f); glScalef(scale, scale, scale); glRotatef(-90., 1., 0., 0.); glPushMatrix(); glScalef(3., 3., .1); glColor3fv(color[0]); if(fillOn) glutSolidCube(1.0); else glutWireCube(1.0); glPopMatrix(); glTranslatef(0., 0., .1); glRotatef(anitheta, 0., 0., 1.); glColor3fv(color[1]); gluCylinder(c, 1., 1., .5, 12, 10); glTranslatef(0., 0., .5); glPushMatrix(); glTranslatef(-.5, 0., 0.); glRotatef(-anitheta*5, 0., 0., 1.); glColor3fv(color[2]); gluCylinder(c, .25, .25, .25, 12, 10); glPopMatrix(); glTranslatef(0., -.5, 0.); glColor3fv(color[3]); glTranslatef(0., .5, 0.); glColor3fv(color[4]); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
38
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1202.cpp glPushMatrix(); glTranslatef(.5, 0., 0.); glRotatef(-anitheta*5, 0., 0., 1.); glColor3fv(color[5]); gluCylinder(c, .25, .25, .25, 12, 10); glPopMatrix(); glTranslatef(0., 0., .25); glColor3fv(color[6]); gluCylinder(c, .5, .1, .25, 12, 10); glColor3fv(color[7]); gluCylinder(c, .1, .1, .5, 12, 10); glTranslatef(0., 0., .725); glColor3fv(color[8]); if(fillOn) glutSolidSphere(.25, 10, 10); else glutWireSphere(.25, 10, 10); glFlush(); glutSwapBuffers(); } void myIdle() { anitheta += anithetaDelta; glutPostRedisplay(); } void myKeyboard (unsigned char key, int x, int y) switch (key) { case ' ': aniOn = !aniOn; if(aniOn) glutIdleFunc(myIdle); else glutIdleFunc(NULL); break; case 'o': openOn = !openOn; case 'f': fillOn = !fillOn; if(fillOn) gluQuadricDrawStyle(c, GLU_FILL); else gluQuadricDrawStyle(c, GLU_LINE); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
39
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1202.cpp case 'p': projection = !projection; myResize(windowWidth, windowHeight); break; case 'd': depthOn = !depthOn; if(depthOn) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); case 'z': scale*=scaleDelta; case 'x': scale/=scaleDelta; } glutPostRedisplay(); void myMouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(aniOn) glutIdleFunc(NULL); mouseState=state; mouseButton=btn; mouseX=x; mouseY=y; } else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) { mouseState=-1; if(aniOn) glutIdleFunc(myIdle); else return; glutPostRedisplay(); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
40
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab1202.cpp void myInit() { c=gluNewQuadric(); gluQuadricDrawStyle(c, GLU_LINE); } void main (void) myInit(); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow("lab12 by glutDisplayFunc(myDisplay); glutReshapeFunc(myResize); glutKeyboardFunc(myKeyboard); glutSpecialFunc(mySKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutIdleFunc(myIdle); glutMainLoop(); 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
41
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Homework 6 Program to draw the figure using glutWireSphere and gluCylinder 25 November 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.