Computer Graphics 6 Lee ByungGook 1262020 Computer Graphics
Computer Graphics 6 Lee Byung-Gook 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
lab 11. cpp #include <math. h> #include <stdio. h> #include <stdlib. h> #include <GL/glut. h> gl. Vertex 2 f(-size+2. 0*i*size/N, size); gl. Vertex 2 f(-size, -size+2. 0*i*size/N); gl. Vertex 2 f(size, -size+2. 0*i*size/N); GLint N=4; GLint ani. On = 0; GLint mouse. X = 0; GLint mouse. Y = 0; GLint mouse. State = 0; GLint mouse. Button = 0; GLfloat size=20. 0, theta=0. 0; GLfloat x. Theta=0. , y. Theta=0. , z. Theta=0. ; GLfloat scale=1. 0, scale. Delta=1. 01; void my. Axis(void) { int i; gl. Color 3 f(. 98, . 04, . 70); gl. Begin(GL_LINES); for(i=0; i<=N; i++) { gl. Vertex 2 f(-size+2. 0*i*size/N, -size); 12/6/2020 } gl. End(); gl. Color 3 f(. 25, . 25); gl. Begin(GL_LINES); gl. Vertex 2 f(0, -size); gl. Vertex 2 f(0, size); gl. Vertex 2 f(-size, 0); gl. Vertex 2 f(size, 0); gl. End(); } void my. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); gl. Rotatef(x. Theta, 1. 0, 0. 0); gl. Rotatef(y. Theta, 0. 0, 1. 0, 0. 0); gl. Rotatef(z. Theta, 0. 0, 1. 0); gl. Scalef(scale, scale); my. Axis(); Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
lab 11. cpp gl. Color 3 f(. 98, . 625, . 12); glut. Wire. Cube(1. 0); gl. Flush(); glut. Swap. Buffers(); } void my. Reshape(int width, int height) { gl. Clear. Color (. 75, 0. 0); gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(-size, -size, size); gl. Matrix. Mode(GL_MODELVIEW); } void my. Idle(void) { theta+=0. 5; glut. Post. Redisplay(); } 12/6/2020 void my. Mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(ani. On) glut. Idle. Func(NULL); mouse. State=state; mouse. Button=btn; mouse. X=x; mouse. Y=y; } else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) { if(ani. On) glut. Idle. Func(my. Idle); mouse. State=-1; } else if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { if(ani. On) glut. Idle. Func(NULL); mouse. State=state; mouse. Button=btn; mouse. X=x; mouse. Y=y; } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
lab 11. cpp else if(btn==GLUT_RIGHT_BUTTON && state == GLUT_UP) { if(ani. On) glut. Idle. Func(my. Idle); mouse. State=-1; } else if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { x. Theta=y. Theta=z. Theta=0. ; scale=1. 0; } else return; glut. Post. Redisplay(); } void my. Motion(int x, int y) { if(mouse. Button == GLUT_LEFT_BUTTON && mouse. State == GLUT_DOWN) { y. Theta -= (mouse. X - x)/10. ; x. Theta -= (mouse. Y - y)/10. ; } 12/6/2020 else if(mouse. Button == GLUT_RIGHT_BUTTON && mouse. State == GLUT_DOWN) { if(mouse. Y!=y) scale = scale * pow(scale. Delta, (mouse. Y - y)/10. ); } else return; mouse. X = x; mouse. Y = y; glut. Post. Redisplay(); } void my. Keyboard (unsigned char key, int x, int y) { switch (key) { case ' ': ani. On = !ani. On; if(ani. On) glut. Idle. Func(my. Idle); else glut. Idle. Func(NULL); break; } glut. Post. Redisplay(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
lab 11. cpp void main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DOUBLE | GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(0, 0); glut. Create. Window("lab 11 by lbg@dongseo. ac. kr"); glut. Reshape. Func(my. Reshape); glut. Display. Func(my. Display); glut. Keyboard. Func(my. Keyboard); glut. Mouse. Func(my. Mouse); glut. Motion. Func(my. Motion); glut. Main. Loop(); } 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Exercise 1 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 6
Exercise 2 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
Exercise 3 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Exercise 4 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Exercise 5 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
Exercise 6 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
Exercise 7 • Program to draw the figure using the matrix stack with lab 11. cpp 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
Projection Transformations • Converts objects defined in 3 D space into objects defined in 2 D space. • There are two basic types of 3 D to 2 D projects: - orthographic : a parallel project of 3 D values onto a 2 D plane; useful for engineering drawings - perspective : the way your eye sees the normal world around you; used for animation and visual simulation 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
Orthographic • gl. Ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); • glu. Ortho 2 D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
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. • x. Left, x. Right, y. Bottom, y. Top are distances along their respective axis's. • z. Near and z. Far are distances from the camera. They should never be equal. 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
Perspective • all points in 3 D are projected towards to the position of the camera. The location where this projection intersects the "picture plane" is its "perspective transform" in 2 D space • gl. Frustum, glu. Perspective 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
gl. Frustum • void gl. Frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
gl. Frustum • 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. • x. Left, x. Right, y. Bottom, y. Top are distances along their respective axis's. • z. Near and z. Far are distances from the camera. They should always be positive 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
glu. Perspective • void glu. Perspective(GLdouble fovy, GLdouble aspect, GLdouble z. Near, GLdouble z. Far); 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
glu. Perspective • 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. • field. Of. View is the angle between the top and bottom faces of the frustum • aspect ratio is the width of the frustum divided by its height • z. Near and z. Far are distances from the camera. They should always be positive 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
gl. Frustum, glu. Perspective • The parameters of the two projections have the following relationship Y y. Top Camera field. Of. View -Z axis y. Bottom z. Near 12/6/2020 z. Far Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
lab 12. cpp #include <math. h> #include <stdio. h> #include <windows. h> #include <gl/glut. h> typedef float vec 3_t[3]; vec 3_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}, 12/6/2020 {1, -1, 1}, {1, -1}, {1, 1, -1}, {-1, 1}, {1, 1, 1}, {-1, 1, 1} }; vec 3_t color[] = { {1. 0 f, 0. 0 f}, {0. 0 f, 1. 0 f}, {1. 0 f, 0. 0 f, 1. 0 f}, {0. 0 f, 1. 0 f}, {1. 0 f, 1. 0 f}, {. 25 f, . 25 f}, {. 60 f, . 40 f, . 70 f}, {. 98 f, . 625 f, . 12 f}, {. 98 f, . 04 f, . 70 f}, {. 75 f, . 75 f}, {. 60 f, . 40 f, . 12 f} }; //red //green //blue //yellow //magenta //cyan //white //dark gray //barney purple //pumpkin orange //pastel pink //light gray //brown Computer Graphics, Lee Byung-Gook, Dongseo Univ. 22
lab 12. cpp vec 3_t rot = {0. , 0. }; vec 3_t eye = {0. , -5. }; vec 3_t center = {0. , 0. }; float float int int int void draw. Cube (void){ int i; for(i = 0; i < 20+open. On*4; i ++) { if(fill. On) gl. Begin(GL_POLYGON); else gl. Begin(GL_LINE_LOOP); gl. Color 3 fv(color[i%12]); gl. Vertex 3 fv(cube[i]); if((i+1)%4 == 0) gl. End(); } } size=3. ; theta=. 0; theta. Delta=. 125; eye. Delta=. 125; scale=1. 0; scale. Delta=1. 125; mouse. X = 0; mouse. Y = 0; mouse. State = 0; mouse. Button = 0; projection = 0; ani. On = 0; depth. On = 1; open. On = 0; fill. On = 0; window. Width, window. Height; 12/6/2020 void my. Display (void){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); if(!projection) gl. Translatef(eye[0], eye[1], eye[2]); gl. Rotatef(rot[0], 1. 0 f, 0. 0 f); gl. Rotatef(rot[1], 0. 0 f, 1. 0 f, 0. 0 f); gl. Rotatef(rot[2], 0. 0 f, 1. 0 f); gl. Scalef(scale, scale); Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
lab 12. cpp draw. Cube(); gl. Color 3 fv(color[7]); glut. Wire. Teapot(. 5); gl. Flush(); glut. Swap. Buffers(); } void my. Resize (int width, int height) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if(projection) gl. Ortho(-size, -size, size); else glu. Perspective(60. , (float)width/height, . 1, 100. ); gl. Enable(GL_DEPTH_TEST); window. Width=width; window. Height=height; } void my. Keyboard (unsigned char key, int x, int y) { 12/6/2020 switch (key) { case 'o': open. On = !open. On; break; case 'f': fill. On = !fill. On; break; case 'p': projection = !projection; my. Resize(window. Width, window. Height); break; case 'd': depth. On = !depth. On; if(depth. On) gl. Enable(GL_DEPTH_TEST); else gl. Disable(GL_DEPTH_TEST); break; case 'z': scale*=scale. Delta; break; case 'x': scale/=scale. Delta; break; } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
lab 12. cpp glut. Post. Redisplay(); } void my. Mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { mouse. State=state; mouse. Button=btn; mouse. X=x; mouse. Y=y; } else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) mouse. State=-1; else return; glut. Post. Redisplay(); } void my. Motion(int x, int y) { if(mouse. Button == GLUT_LEFT_BUTTON && mouse. State == GLUT_DOWN) { rot[1] -= (mouse. X - x)/5. ; 12/6/2020 rot[0] -= (mouse. Y - y)/5. ; glut. Post. Redisplay(); mouse. X=x; mouse. Y=y; } } void main (void) { glut. Init. Display. Mode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window("lab 12 by lbg@dongseo. ac. kr"); glut. Display. Func(my. Display); glut. Reshape. Func(my. Resize); glut. Keyboard. Func(my. Keyboard); glut. Mouse. Func(my. Mouse); glut. Motion. Func(my. Motion); glut. Main. Loop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 25
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 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
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. 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 27
Z-buffer algorithm in pseudocode (x, y) clear the color. Buffer to the background color clear the depth. Buffer 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 < depth. Buffer[x][y]) { color = calculate the color of face at color. Buffer[x][y] = color depth. Buffer[x][y] = depth } } 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 28
Z-buffer algorithm in Open. GL • glut. Init. Display. Mode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); GLUT_DEPTH : Bit mask to select a window with a depth buffer. • gl. Enable, gl. Disable(GL_DEPTH_TEST); If enabled, do depth comparisons and update the depth buffer. • gl. Clear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 29
Camera placement • There are two fundamental ways that you can think about camera placement. 1. Think of the camera as never moving from the origin and always looking down the negative z axis. 2. Think of the camera as being moved in front of the scene. 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 30
Camera Control • The default camera position Y -Z Camera X 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 31
glu. Look. At • To simplify the placement of the camera, a utility function was created as follows: • glu. Look. At(eye. X, eye. Y, eye. Z, center. X, center. Y, center. Z, up. Vector. X, up. Vector. Y, up. Vectorz); • 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. 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 32
lab 1201. cpp void my. Display (void) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); if(!projection) glu. Look. At(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); gl. Rotatef(rot[0], 1. 0 f, 0. 0 f); gl. Rotatef(rot[1], 0. 0 f, 1. 0 f, 0. 0 f); gl. Rotatef(rot[2], 0. 0 f, 1. 0 f); gl. Scalef(scale, scale); draw. Cube(); gl. Color 3 fv(color[7]); glut. Wire. Teapot(. 5); gl. Flush(); glut. Swap. Buffers(); } 12/6/2020 void my. Look. At(int key) { if(key == GLUT_KEY_UP) { eye[2] = eye[2]-cos(theta)*eye. Delta; eye[0] = eye[0]+sin(theta)*eye. Delta; } else if(key == GLUT_KEY_DOWN) { eye[2] = eye[2]+cos(theta)*eye. Delta; eye[0] = eye[0]-sin(theta)*eye. Delta; } center[2] = eye[2]-cos(theta); center[0] = eye[0]+sin(theta); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 33
lab 1201. cpp void my. Resize (int width, int height){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if(projection) { gl. Ortho(-size, -size, size); eye[2] = 0. ; } else { glu. Perspective(60. , (float)width/height, . 1, 100. ); eye[2] = 5. ; my. Look. At(0); } gl. Enable(GL_DEPTH_TEST); window. Width=width; window. Height=height; } void my. SKeyboard (int key, int x, int y) { switch (key) { case GLUT_KEY_UP : break; case GLUT_KEY_DOWN : break; case GLUT_KEY_LEFT : theta-=theta. Delta; break; case GLUT_KEY_RIGHT : theta+=theta. Delta; break; default : return; } my. Look. At(key); glut. Post. Redisplay(); } glut. Special. Func(my. SKeyboard); //in main() 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 34
Moving the camera void my. SKeyboard (int key, int x, int y) { switch (key) { case GLUT_KEY_UP : break; case GLUT_KEY_DOWN : break; case GLUT_KEY_LEFT : theta-=theta. Delta; break; case GLUT_KEY_RIGHT : theta+=theta. Delta; break; default : return; } my. Look. At(key); glut. Post. Redisplay(); } Center of interest theta Camera void my. Look. At(int key) { if(key == GLUT_KEY_UP) { cos(theta)*eye. Delta; Up vector z eye[2] = eye[2]- eye[0] = eye[0]+sin(theta)*eye. Delta; } else if(key == GLUT_KEY_DOWN) { eye[2] = eye[2]+cos(theta)*eye. Delta; eye[0] = eye[0]-sin(theta)*eye. Delta; } center[2] = eye[2]-cos(theta); center[0] = eye[0]+sin(theta); Eye position Center of interest theta cos(theta) x sin(theta) Eye position } 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 35
glut. Special. Func • glut. Special. Func sets the special keyboard callback for the current window. • void glut. Special. Func(void (*func)(int key, int x, int y)); GLUT_KEY_F 1 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. 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 36
lab 1202. cpp float anitheta=. 0; float anitheta. Delta=. 125; GLUquadric. Obj *c; void my. Display (void) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); if(!projection) glu. Look. At(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); gl. Rotatef(rot[0], 1. 0 f, 0. 0 f); gl. Rotatef(rot[1], 0. 0 f, 1. 0 f, 0. 0 f); gl. Rotatef(rot[2], 0. 0 f, 1. 0 f); gl. Scalef(scale, scale); gl. Rotatef(-90. , 1. , 0. ); gl. Push. Matrix(); gl. Scalef(3. , . 1); gl. Color 3 fv(color[0]); if(fill. On) glut. Solid. Cube(1. 0); else glut. Wire. Cube(1. 0); gl. Pop. Matrix(); 12/6/2020 gl. Translatef(0. , . 1); gl. Rotatef(anitheta, 0. , 1. ); gl. Color 3 fv(color[1]); glu. Cylinder(c, 1. , . 5, 12, 10); gl. Translatef(0. , . 5); gl. Push. Matrix(); gl. Translatef(-. 5, 0. ); gl. Rotatef(-anitheta*5, 0. , 1. ); gl. Color 3 fv(color[2]); glu. Cylinder(c, . 25, 12, 10); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translatef(0. , -. 5, 0. ); gl. Rotatef(-anitheta*5, 0. , 1. ); gl. Color 3 fv(color[3]); glu. Cylinder(c, . 25, 12, 10); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translatef(0. , . 5, 0. ); gl. Rotatef(-anitheta*5, 0. , 1. ); gl. Color 3 fv(color[4]); glu. Cylinder(c, . 25, 12, 10); gl. Pop. Matrix(); Computer Graphics, Lee Byung-Gook, Dongseo Univ. 37
lab 1202. cpp gl. Push. Matrix(); gl. Translatef(. 5, 0. ); gl. Rotatef(-anitheta*5, 0. , 1. ); gl. Color 3 fv(color[5]); glu. Cylinder(c, . 25, 12, 10); gl. Pop. Matrix(); gl. Translatef(0. , . 25); gl. Rotatef(-anitheta*5, 0. , 1. ); gl. Color 3 fv(color[6]); glu. Cylinder(c, . 5, . 1, . 25, 12, 10); gl. Translatef(0. , . 25); gl. Color 3 fv(color[7]); glu. Cylinder(c, . 1, . 5, 12, 10); gl. Translatef(0. , . 725); gl. Color 3 fv(color[8]); if(fill. On) glut. Solid. Sphere(. 25, 10); else glut. Wire. Sphere(. 25, 10); gl. Flush(); glut. Swap. Buffers(); } 12/6/2020 void my. Idle() { anitheta += anitheta. Delta; glut. Post. Redisplay(); } void my. Keyboard (unsigned char key, int x, int y) { switch (key) { case ' ': ani. On = !ani. On; if(ani. On) glut. Idle. Func(my. Idle); else glut. Idle. Func(NULL); break; case 'o': open. On = !open. On; break; case 'f': fill. On = !fill. On; if(fill. On) glu. Quadric. Draw. Style(c, GLU_FILL); else glu. Quadric. Draw. Style(c, GLU_LINE); break; Computer Graphics, Lee Byung-Gook, Dongseo Univ. 38
lab 1202. cpp case 'p': projection = !projection; my. Resize(window. Width, window. Height); break; case 'd': depth. On = !depth. On; if(depth. On) gl. Enable(GL_DEPTH_TEST); else gl. Disable(GL_DEPTH_TEST); break; case 'z': scale*=scale. Delta; break; case 'x': scale/=scale. Delta; break; } glut. Post. Redisplay(); } 12/6/2020 void my. Mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(ani. On) glut. Idle. Func(NULL); mouse. State=state; mouse. Button=btn; mouse. X=x; mouse. Y=y; } else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) { mouse. State=-1; if(ani. On) glut. Idle. Func(my. Idle); } else return; glut. Post. Redisplay(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 39
lab 1202. cpp void my. Init() { c=glu. New. Quadric(); glu. Quadric. Draw. Style(c, GLU_LINE); } void main (void) { my. Init(); glut. Init. Display. Mode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window("lab 12 by lbg@dongseo. ac. kr"); glut. Display. Func(my. Display); glut. Reshape. Func(my. Resize); glut. Keyboard. Func(my. Keyboard); glut. Special. Func(my. SKeyboard); glut. Mouse. Func(my. Mouse); glut. Motion. Func(my. Motion); glut. Idle. Func(my. Idle); glut. Main. Loop(); } 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 40
Homework 6 • Program to draw the figure using glut. Wire. Sphere and glu. Cylinder 12/6/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 41
- Slides: 41