Computer Graphics 4 Lee ByungGook 9242021 Computer Graphics
Computer Graphics 4 Lee Byung-Gook 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
Introduction to transformations • Transformations are useful for: – Composing a more complex object using many instances of a single form. – Creating a complex object from a single "motif". – Moving an object to get a different view of it. – Moving an object to animate it. "motif" 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
Mathematics notation • in 3 D, let P = (Px, Py, Pz, 1) P can be transformed into Q = (Qx, Qy, Qz, 1) by applying a transformation T Q = T(P) • There an infinite number of possible transformations – – Some transformations are rather simple Some transformations are rather complex Some transformations distort the shape of an object Some transformations maintain the relative shape of an object 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
Affine transformations • There is a special class of transformations that maintain the relative shape of objects (the affine transforms). • All affine transformations are linear. Therefore, they can be written as: Qx = m 11 Px + m 12 Py + m 13 Pz + m 14 Qy = m 21 Px + m 22 Py + m 23 Pz + m 24 Qz = m 31 Px + m 32 Py + m 33 Pz + m 34 1=1 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
Definition of affine transformations • T is an affine transformation if T(a. P + (1 -a)Q) = a. T(P) + (1 -a)T(Q) • a. P + (1 -a)Q is the parametric equation of a line segment. The definition says: "if we can take the line formed by P and Q and transform it, and then if we can get the same line by transforming only P and Q and then forming the line, T is an affine transformation. 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Standard affine transformations • • Translation Scaling Rotation Shearing 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 6
Translation • a displacement • a move in a linear direction • it is not affected by the location of the origin 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
Scaling • to enlarge (or shrink) • a point at the origin is never effected by scaling • objects always scale about the origin 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Scaling • if an object is not centered about the origin, scaling also moves it • uniform scaling (Sx = Sy = Sz) • differential scaling (Sx Sy or Sx Sz or Sy Sz) • a negative scale factor reflects (flips) about an axis -Sx: reflects about the Y axis -Sy: reflects about the X axis -Sx and -Sy reflects about the line (x=-y) 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Rotation • to move in a circular direction • to rotate about the origin • the angle is always considered a counter-clockwise rotation. • derivation: Qx = r cos( + ) = r (cos( ) - sin( )) = r cos( ) - r sin( ) = Px cos( ) - Py sin( ) Qy = r sin( + ) = r (sin( )cos( ) + cos( )sin( )) = r sin( )cos( ) + r cos( )sin( ) = Px sin( ) + Py cos( ) 9/24/2021 Q P r r Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
Rotation • Rotation about Z • Rotation about Y • Rotation about X where c = cos(angle), s = sin(angle) 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
Rotation • In 3 D, there an infinite number of axes about which to rotate. • It can be shown that any rotation about any arbitrary axis can be decomposed into a series of "elementary rotations" about the coordinate axes. • We assume the convention that all rotations follow the right-hand rule: "put your thumb along the axis of rotation and your fingers curl in the positive angle direction 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
Shearing • to shear in x: (Px + h. Py, Pz); slant vertically • to shear in y: (Px, g. Px+Py, Pz); slant horizontally 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
lab 04. cpp #include <stdio. h> #include <stdlib. h> #include <GL/glut. h> #define X_AXIS 0 #define Y_AXIS 1 #define Z_AXIS 2 GLfloat size=2. 0; GLint N=4, translate. Axis=0; GLfloat x. Translate=0. 0, y. Translate=0. 0, z. Translate=0. 0; GLfloat translate. Delta=0. 1; void my. Draw(void) { gl. Color 3 f(0. 60, . 40, . 70); gl. Begin(GL_POLYGON); gl. Vertex 2 f(0. , 0. ); gl. Vertex 2 f(1. , 1. ); gl. Vertex 2 f(0. , 1. ); gl. End(); } 9/24/2021 void my. Axis(void) { int i; gl. Color 3 f(0. 98, . 04, . 70); gl. Begin(GL_LINES); for(i=1; i<N; i++) { gl. Vertex 2 f(-size+2. 0*i*size/N, -size); 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); } gl. End(); } void my. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); my. Axis(); gl. Translatef(x. Translate, y. Translate, z. Translate); my. Draw(); gl. Flush(); glut. Swap. Buffers(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
lab 04. cpp 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. Mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(translate. Axis == X_AXIS) x. Translate-=translate. Delta; else if(translate. Axis == Y_AXIS) y. Translate-=translate. Delta; else z. Translate-=translate. Delta; } else if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { if(translate. Axis == X_AXIS) x. Translate+=translate. Delta; 9/24/2021 else if(translate. Axis == Y_AXIS) y. Translate+=translate. Delta; else z. Translate+=translate. Delta; } else if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { translate. Axis++; if(translate. Axis==3) translate. Axis=0; } else return; my. Display(); } 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 04"); glut. Reshape. Func(my. Reshape); glut. Display. Func(my. Display); glut. Mouse. Func(my. Mouse); glut. Main. Loop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
gl. Load. Identity • The gl. Load. Identity function replaces the current matrix with the identity matrix. • void gl. Load. Identity( void ); • Remarks The gl. Load. Identity function replaces the current matrix with the identity matrix. It is semantically equivalent to calling gl. Load. Matrix with the identity matrix but in some cases it is more efficient. 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
gl. Translate[d, f] • The gl. Translated and gl. Translatef functions multiply the current matrix by a translation matrix. • void gl. Translated( GLdouble x, GLdouble y, GLdouble z ); void gl. Translatef( GLfloat x, GLfloat y, GLfloat z ); • Parameters x, y, z : The x, y, and z coordinates of a translation vector. • Remarks The gl. Translate function moves the coordinate system origin to the point specified by (x, y, z). 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
Exercise • From lab 04 • Translate rectangular using keyboard event – – – x : translate -translate. Delta on X axis X : translate. Delta on X axis y : translate -translate. Delta on Y axis Y : translate. Delta on Y axis z : translate -translate. Delta on Z axis Z : translate. Delta on Z axis 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
lab 05. cpp #include <stdio. h> #include <stdlib. h> #include <GL/glut. h> GLint GLfloat N=4; size=2. 0; z. Theta=30. ; void my. Axis(void) { int i; gl. Color 3 f(0. 98, . 04, . 70); gl. Begin(GL_LINES); for(i=1; i<N; i++) { gl. Vertex 2 f(-size+2. 0*i*size/N, -size); 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); } gl. End(); } 9/24/2021 void my. Draw(void) { gl. Color 3 f(0. 98, . 04, . 70); gl. Begin(GL_POLYGON); gl. Vertex 3 f(0. , 0. ); gl. Vertex 3 f(1. , 0. ); gl. Vertex 3 f(0. , 1. , 0. ); gl. End(); gl. Color 3 f(1. , 0. ); gl. Begin(GL_LINE_LOOP); gl. Vertex 3 f(0. , 0. ); gl. Vertex 3 f(0. , 1. ); gl. End(); gl. Color 3 f(0. , 1. , 0. ); gl. Begin(GL_LINE_LOOP); gl. Vertex 3 f(1. , 0. ); gl. Vertex 3 f(1. , 1. ); gl. Vertex 3 f(1. , 0. , 1. ); gl. End(); gl. Color 3 f(0. , 1. ); Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
lab 05. cpp gl. Begin(GL_LINE_LOOP); gl. Vertex 3 f(0. , 0. ); gl. Vertex 3 f(1. , 0. , 1. ); gl. Vertex 3 f(0. , 1. ); gl. End(); gl. Color 3 f(1. , 0. ); gl. Begin(GL_LINE_LOOP); gl. Vertex 3 f(0. , 1. , 0. ); gl. Vertex 3 f(1. , 1. ); gl. Vertex 3 f(0. , 1. ); gl. End(); 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. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); my. Axis(); gl. Rotatef(z. Theta, 0. 0, 1. 0); my. Draw(); gl. Flush(); glut. Swap. Buffers(); } 9/24/2021 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 05"); glut. Reshape. Func(my. Reshape); glut. Display. Func(my. Display); glut. Main. Loop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
gl. Rotate[d, f] • The gl. Rotated and gl. Rotatef functions multiply the current matrix by a rotation matrix. • void gl. Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z ); void gl. Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z ); • Parameters angle : The angle of rotation, in degrees. x, y, z : The x, y, and z coordinates of a vector, respectively. • Remarks The gl. Rotate function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z). 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
lab 0501. cpp GLfloat x. Theta=0. , y. Theta=0. , z. Theta=0. , theta. Delta=. 01; void my. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); my. Axis(); 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); my. Draw(); gl. Flush(); glut. Swap. Buffers(); } void my. Keyboard(unsigned char the. Key, int x, int y) { switch (the. Key) { case 'x' : x. Theta -= theta. Delta; break; case 'X' : x. Theta += theta. Delta; break; case 'y' : y. Theta -= theta. Delta; break; case 'Y' : y. Theta += theta. Delta; break; case 'z' : z. Theta -= theta. Delta; break; case 'Z' : z. Theta += theta. Delta; break; case 27: exit(-1); // esc key } my. Display(); } glut. Keyboard. Func(my. Keyboard); 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. // in main(); 22
lab 0502. cpp GLfloat x. Theta=0. , y. Theta=0. , z. Theta=0. , theta. Delta=. 01; void my. Idle() { x. Theta+=theta. Delta; y. Theta+=theta. Delta; z. Theta+=theta. Delta; my. Display(); // glut. Post. Redisplay(); } glut. Idle. Func (my. Idle); 9/24/2021 // in main(); Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
glut. Idle. Func • glut. Idle. Func sets the global idle callback. • void glut. Idle. Func(void (*func)(void)); • Description glut. Idle. Func sets the global idle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received. If enabled, the idle callback is continuously called when events are not being received. 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
glut. Post. Redisplay • glut. Post. Redisplay marks the current window as needing to be redisplayed. • void glut. Post. Redisplay(void); • Description Mark the normal plane of current window as needing to be redisplayed. The next iteration through glut. Main. Loop, the window’s display callback will be called to redisplay the window’s normal plane. Multiple calls to glut. Post. Redisplay before the next display callback opportunity generates only a single redisplay callback. glut. Post. Redisplay may be called within a window’s display or overlay display callback to remark that window for redisplay. 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 25
Exercise • From lab 05 • Rotate rectangular using mouse event • Reference : lab 04 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
lab 06. cpp #include <stdio. h> #include <stdlib. h> #include <GL/glut. h> GLfloat GLint GLfloat size=2. 0; N=4; x. Scale=1. 5; void my. Axis(void) { int i; gl. Color 3 f(0. 98, . 04, . 70); gl. Begin(GL_LINES); for(i=1; i<N; i++) { gl. Vertex 2 f(-size+2. 0*i*size/N, -size); 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); } gl. End(); } 9/24/2021 void my. Draw(void) { gl. Color 3 f(0. 60, . 40, . 70); gl. Begin(GL_POLYGON); gl. Vertex 2 f(0. , 0. ); gl. Vertex 2 f(1. , 1. ); gl. Vertex 2 f(0. , 1. ); gl. End(); } void my. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); my. Axis(); gl. Scalef(x. Scale, 1. 0); my. Draw(); gl. Flush(); glut. Swap. Buffers(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 27
lab 06. cpp 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 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 06"); glut. Reshape. Func(my. Reshape); glut. Display. Func(my. Display); glut. Main. Loop(); } 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 28
gl. Scale[d, f] • The gl. Scaled and gl. Scalef functions multiply the current matrix by a general scaling matrix. • void gl. Scaled( GLdouble x, GLdouble y, GLdouble z ); void gl. Scalef( GLfloat x, GLfloat y, GLfloat z ); • Parameters x, y, z : Scale factors along the x, y, and z axes, respectively. • Remarks The gl. Scale function produces a general scaling along the x, y, and z axes. The three arguments indicate the desired scale factors along each of the three axes. 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 29
lab 0601. cpp Glfloat x. Scale=1. 0, y. Scale=1. 0, z. Scale=1. 0, scale. Factor=1. 1; void my. Draw(void) { gl. Color 3 f(0. 60, . 40, . 70); gl. Begin(GL_POLYGON); gl. Vertex 2 f(0. , 0. ); gl. Vertex 2 f(1. , 1. ); gl. Vertex 2 f(0. , 1. ); gl. End(); } void my. Display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Load. Identity(); my. Axis(); gl. Scalef(x. Scale, y. Scale, z. Scale); my. Draw(); gl. Flush(); glut. Swap. Buffers(); } 9/24/2021 void my. Keyboard(unsigned char the. Key, int x, int y) { switch (the. Key) { case 'x' : x. Scale /= scale. Factor; break; case 'X' : x. Scale *= scale. Factor; break; case 'y' : y. Scale /= scale. Factor; break; case 'Y' : y. Scale *= scale. Factor; break; case 'z' : z. Scale /= scale. Factor; break; case 'Z' : z. Scale *= scale. Factor; break; case 27: exit(-1); // esc key } my. Display(); } glut. Keyboard. Func(my. Keyboard); Computer Graphics, Lee Byung-Gook, Dongseo Univ. // in main(); 30
Homework 3. • • In two weeks From lab 05 Rotate rectangle about origin Using glut. Mouse. Func, glut. Motion. Func Rotation angle(x. Theta, y. Theta) depend on the mouse movement while left mouse buttons are pressed 9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 31
9/24/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 32
- Slides: 32