Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics, Lee Byung-Gook, Dongseo Univ.

Similar presentations


Presentation on theme: "Computer Graphics, Lee Byung-Gook, Dongseo Univ."— Presentation transcript:

1 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

2 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
3D Elementary Shapes The glut library contains several functions for drawing basic 3D primitives. These are listed below. All of the following objects are centered at the origin glutWireCube(GLdouble size); glutSolidCube(GLdouble size); glutWireSphere(GLdouble radius, GLint nSlices, GLint nStacks); glutSolidSphere(GLdouble radius, GLint nSlices, GLint nStacks); glutSolidCube and glutWireCube render a solid or wireframe cube respectively. The cube is centered at the modeling coordinates origin with sides of length size. glutSolidSphere and glutWireSphere render a sphere centered at the modeling coordinates origin of the specified radius. The sphere is subdivided around the Z axis into slices and along the Z axis into stacks. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

3 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glutWireSphere This approximates a sphere with a set of polygons. The number of polygons determines the "niceness" of the approximation. nSlices is the number of subdivisions around the z-axis. nStacks is the number of bands along the z axis. Large values for nSlices and nStacks produce more accurate representations (at the cost of slower graphics). Small values for nSlices and nStacks produce poorer representations (with the benefit of faster graphics). nSlices : similar to lines of longitude, 7 nStacks :similar to lines of latitude, 10 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

4 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
3D Elementary Shapes glutWireTetrahedron(); // 4 faced pyramid glutSolidTetrahedron(); glutWireOctahedron(); // 8 faced solid glutSolidOctahedron(); glutWireDodecahedron(); // 12 faced solid glutSolidDodecahedron(); glutWireIcosahedron(); // 20 faced solid glutSolidIcosahedron(); glutSolidTetrahedron and glutWireTetrahedron render a solid or wireframe tetrahedron(4-sided regular solid) respectively centered at the modeling coordinates origin with a radius of p3 . glutSolidOctahedron and glutWireOctahedron render a solid or wireframe octahedron(8-sided regular solid) respec-tively centered at the modeling coordinates origin with a radius of 1.0. glutSolidDodecahedron and glutWireDodecahedron render a solid or wireframe dodecahedron(12-sided regular solid) respectively centered at the modeling coordinates origin with a radius of \sqrt{3} . glutSolidIcosahedronand glutWireIcosahedronrender a solid or wireframe icosahedron(20-sided regular solid) respectively. The icosahedron is centered at the modeling coordinates origin and has a radius of 1.0. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

5 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
3D Elementary Shapes glutWireTorus(GLdouble innerRadius, GLdouble outerRadius, GLint nSlices, GLint rings); glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius, GLint nSlices, GLint rings); glutWireTeapot(GLdouble size); glutSolidTeapot(GLdouble size); glutWireCone(GLdouble baseRadius, GLdouble height, GLint nSlices, GLint nStacks); glutSolidCone(GLdouble baseRadius, GLdouble height, GLint nSlices, GLint nStacks); glutSolidTorusand glutWireTorusrender a solidor wireframe torus (doughnut) respectively centered at the modeling coordinates origin whose axis is aligned with the Z axis. innerRadius Inner radius of the torus. outerRadius Outer radius of the torus. nsides Number of sides for each radial section. rings Number of radial divisions for the torus. glutSolidTeapot and glutWireTeapot render a solid or wireframe teapot respectively. Both surface normals and texture coordinates for the teapot are generated. The teapot is generated with OpenGL evaluators. glutSolidCone and glutWireCone render a solid or wireframe cone respectively oriented along the Z axis. The base of the cone is placed at Z = 0, and the top at Z = height. The cone is subdivided around the Z axis into slices, and along the Z axis into stacks. The cone is not centered at the origin. The base rests in the xy plane, centered about the origin. The height of the cone extends along the +Z axis. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

6 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab07.cpp #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint N=4; GLint mouseX = 0; GLint mouseY = 0; GLint mouseState = 0; GLint mouseButton = 0; GLint shape=1, model=2; GLfloat size=2.0; GLfloat xTheta=0., yTheta=0., zTheta=0., thetaDelta=.125; GLfloat scale=1.0, scaleDelta=1.01; void myAxis(void) { int i; glColor3f(0.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(); void myDraw(void) { glColor3f(.98, .625, .12); if(model==1) glutWireCube(1.0); else if(model==2) glutSolidCube(1.0); else if(model==3) glutWireSphere(1.0, 10, 10); else if(model==4) glutSolidSphere(1.0, 10, 10); else if(model==5) glutWireTeapot(1.0); else if(model==6) glutSolidTeapot(1.0); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

7 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab07.cpp void myDisplay(void) { static int i=0; 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); myDraw(); glFlush(); glutSwapBuffers(); } void myReshape(int width, int height) glClearColor (.75, .75, .75, 0.0); glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glOrtho(-size, size, -size, size, -size, size); glMatrixMode(GL_MODELVIEW); void glutMouse(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 if(btn==GLUT_MIDDLE_BUTTON && 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

8 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab07.cpp else if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_UP) { mouseState=-1; } else return; glutPostRedisplay(); void glutMotion(int x, int y) { if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) { yTheta -= (mouseX - x)/10.; xTheta -= (mouseY - y)/10.; else if(mouseButton == GLUT_MIDDLE_BUTTON && mouseState == GLUT_DOWN) { if(mouseY!=y) scale = scale * pow(scaleDelta, (mouseY - y)/10.); mouseX = x; mouseY = y; glutPostRedisplay(); } void myMenu(int id) { if(id == 7) exit(1); else model = id; 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

9 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab07.cpp void main(int argc, char** argv) { int shape_submenu; glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("lab07"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMouseFunc(glutMouse); glutMotionFunc(glutMotion); glutCreateMenu(myMenu); glutAddMenuEntry("WireCube", 1); glutAddMenuEntry("SolidCube", 2); glutAddMenuEntry("WireSphere", 3); glutAddMenuEntry("SolidSphere", 4); glutAddMenuEntry("WireTeapot", 5); glutAddMenuEntry("SolidTeapot", 6); glutAddMenuEntry("Quit", 7); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); } 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

10 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glutCreateMenu glutCreateMenu creates a new pop-up menu. int glutCreateMenu(void (*func)(int value)); Parameters func The callback function for the menu that is called when a menu entry from the menu is selected. The value passed to the callback is determined by the value for the selected menu entry. The menu identifier range is separate from the window identifier range. Implicitly, the current menu is set to the newly created menu. This menu identifier can be used when calling glutSetMenu. When the menu callback is called because a menu entry is selected for the menu, the current menu will be implicitly set to the menu with the selected entry before the callback is made. glutCreateMenu creates a new pop-up menu and returns a unique small integer identifier. The range of allocated identifiers starts at one. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

11 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glutAddMenuEntry glutAddMenuEntry adds a menu entry to the bottom of the current menu. void glutAddMenuEntry(char *name, int value); Parameters name ASCII character string to display in the menu entry. value Value to return to the menu’s callback function if the menu entry is selected. Description glutAddMenuEntryadds a menu entry to the bottomof the current menu. The string name will be displayed for the newly added menu entry. If the menu entry is selected by the user, the menu’s callback will be called passing value as the callback’s parameter. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

12 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glutAttachMenu glutAttachMenu attaches a mouse button for the current window to the identifier of the current menu; void glutAttachMenu(int button); Parameters button The button to attach a menu Description glutAttachMenu attaches a mouse button for the current window to the identifier of the current menu; glutDetachMenu detaches an attached mouse button from the current window. By attaching a menu identi-fier to a button, the named menu will be popped up when the user presses the specified button. button should be one of GLUT LEFT BUTTON, GLUT MIDDLE BUTTON,andGLUT RIGHT BUTTON. Note that the menu is attached to the button by identifier, not by reference. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

13 Projection, ModelView matrix
There are three different 4x4 matrices used by OpenGL to produce an image. Projection matrix - used to "project" a 3D image onto a 2D plane to create an image ModelView matrix - used to transform objects and the camera to create the desired scene and view. Commands like glTranslate, glRotate, and glScale modify the active matrix. It is the programmer's responsibility to make sure the appropriate matrix is "active." The active matrix is determined by calls to glMatrixMode(). 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

14 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glMatrixMode The glMatrixMode function specifies which matrix is the current matrix. void glMatrixMode( GLenum mode ); Parameters mode : The matrix stack that is the target for subsequent matrix operations. The mode parameter can assume one of three values: GL_MODELVIEW : Applies subsequent matrix operations to the modelview matrix stack. GL_PROJECTION : Applies subsequent matrix operations to the projection matrix stack. GL_TEXTURE : Applies subsequent matrix operations to the texture matrix stack. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

15 OpenGL transformation functions
OpenGL maintains a 4x4 transformation matrix that is multiplied by every (x,y,z) value sent to glVertex().This is sometimes referred to as the CT (current transformation, active matrix). The CT can be modified by the following functions: glLoadIdentity(); glTranslate{fd}(dx, dy, dz); glScale{fd}(sx, sy, sz); glRotate{fd}(angleInDegrees, axisXvalue, axisYvalue, axisZvalue); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

16 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics The glLoadIdentity() function is unique. It replaces the CT with an identity matrix. This has the effect of starting transformations "from unity". The arguments to these functions can be float or double data types. The last letter of the function name determines the data type of the arguments. For example: glTranslatef(3.0, 4.0, 1.2); // floating point data glTranslated(3.0, 4.0, 1.2); // double precision data Note: P = IP (a point times an identity matrix does not change). 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

17 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics To translate along one axis while leaving the other axis values unchanged, use a value of zero for the unaffected axes. For example, the following function call moves along the Y axis 6 units : glTranslatef(0.0, 6.0, 0.0); To scale along one axis while leaving the other axis values unchanged, use a value of one for the unaffected axes. For example, the following call scales only along the Z axis by a factor of 3 : glScalef(1.0, 1.0, 3.0); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

18 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics To rotate about one of the coordinate system axes, specify the appropriate vector. For example: glRotatef(20.0, 1.0, 0.0, 0.0); // rotates about the X axis glRotatef(20.0, 0.0, 1.0, 0.0); // rotates about the Y axis glRotatef(20.0, 0.0, 0.0, 1.0); // rotates about the Z axis 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

19 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics For the glTranslate, glRotate, and glScale functions, each builds a 4x4 transformation matrix using the supplied arguments and then multiplies it times the CT (current transform). For example, the glTranslatef function might look something like the following: void glTranslatef(float dx, float dy, float dz) { GLfloat m[4][4] = {{ 1.0, 0.0, 0.0, dx }, { 0.0, 1.0, 0.0, dy }, { 0.0, 0.0, 1.0, dz }, { 0.0, 0.0, 0.0, 1.0}}; CT = CT*m; // pseudocode } 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

20 Facts about transformations
Given a sequence of elementary transformations that together form a single complex transformation, if you change the order of the elementary transformations, you get a different complex transformation There is typically more than one sequence of transformations that will produce the same effect, but often one sequence is easier to create than the others. The multiplication of the matrices is always (CT*m), not (m*CT), so the order of the function calls must be in reverse order from the "conceptual" ordering of the transformations. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

21 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics If you want to: translate a point by (-2, 5, 0), then rotate it by 20 degrees about the Z axis, then scale it by a factor of 2 along the X axis, the function calls would be:   glLoadIdentity(); // always start with the identity matrix glScalef(2.0, 1.0, 1.0); // Conceptually, this happens 3rd glRotatef(20.0, 0.0, 0.0, 1.0); // Conceptually, this happens 2nd glTranslatef(-2.0, 5.0, 0.0); // Conceptually, this happens 1st drawObject(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

22 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Specifics The transformations caused by glRotate, glTranslate, and glScale are applied to the CT (current transform). They will accumulate continually unless you reset the matrix back to the Identity matrix. If you want to start a new set of transformations, then call glLoadIdentity(). Example: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // start a new sequence of transforms glTranslatef(-3.0, -4.0, 0.0); glRotatef(90.0, 1.0, 0.0, 0.0); drawObject1();   glLoadIdentity(); // start a new sequence of transforms glTranslatef(4.0, 2.0, 1.0); glScalef(2.0, 1.0, 1.0); drawObject2(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

23 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 1 Draw a wall of a building containing 5 arched windows, spaced 10 units apart. Assume the arched window is defined around the origin and is 4 units wide 4 10 8 5 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

24 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 1 1) reset the matrix 2) drawWindow 3) translate 8 on X, 5 on Y 4) reset the matrix 5) drawWindow 6) translate 18 on X, 5 on Y 7) reset the matrix 8) drawWindow 9) translate 28 on X, 5 on Y 10) reset the matrix 11) drawWindow 12) translate 38 on X, 5 on Y 13) reset the matrix 14) drawWindow 15) translate 48 on X, 5 on Y glLoadIdentity() glTranslatef(8.0, 5.0, 0.0); drawWindow(); glTranslatef(18.0, 5.0, 0.0); glTranslatef(28.0, 5.0, 0.0); glTranslatef(38.0, 5.0, 0.0); glTranslatef(48.0, 5.0, 0.0); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

25 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 1 1) reset the matrix 2) drawWindow 3) translate 10 on X, 0 on Y 4) drawWindow 5) translate 10 on X, 0 on Y 6) drawWindow 7) translate 10 on X, 0 on Y 8) drawWindow 9) translate 10 on X, 0 on Y 10) drawWindow 11) translate 8 on X, 5 on Y glLoadIdentity() glTranslatef(8.0, 5.0, 0.0); drawWindow(); glTranslatef(10.0, 0.0, 0.0); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

26 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab08.cpp #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint N=12; GLfloat size=60.0; 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); glVertex2f(0, -size); glVertex2f(0, size); glVertex2f(-size, 0); glVertex2f(size, 0); glEnd(); } void drawWindow(void) { glBegin(GL_POLYGON); glVertex3f(-3., 0., 0.); glVertex3f(3., 0., 0.); glVertex3f(3., 8., 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

27 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab08.cpp glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f( , , 0.); glVertex3f(-3., 8., 0.); glEnd(); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); myAxis(); glColor3f(.25, .25, .12); drawWindow(); glColor3f(.98, .625, .12); for(int j=0; j<5; j++) { glTranslatef(8+10.0*j, 5.0, 0.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 main(int argc, char** argv) glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("lab08"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

28 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 1 Compare the difference glLoadIdentity(); glTranslatef(10.0, 0.0, 0.0); glRotatef(theta, 0.0, 0.0, 1.0); glutWireSphere(1.0, 10, 10); glLoadIdentity(); glRotatef(theta, 0.0, 0.0, 1.0); glTranslatef(10.0, 0.0, 0.0); glutWireSphere(1.0, 10, 10); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

29 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 2 Transform the triangle from its current location (left) to the new location and orientation shown on right. 10 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

30 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 2 1) drawTriangle 2) rotate 90 degrees about Z 2) translate 10 units along Y glLoadIdentity() glTranslatef(0.0, 10.0, 0.0); glRotatef(90.0, 0.0, 0.0, 1.0); drawTriangle(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

31 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 2 1) drawTriangle 2) translate 10 units along X 3) rotate 90 degrees about Z glLoadIdentity() glRotatef(90.0, 0.0, 0.0, 1.0); glTranslatef(10.0, 0.0, 0.0); drawTriangle(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

32 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Example 2 1) drawTriangle 2) rotate -90 degrees about Z 3) translate -10 along Y 4) rotate 180 degrees about Z glLoadIdentity() glRotatef(180.0, 0.0, 0.0, 1.0); glTranslatef(0.0, -10.0, 0.0); glRotatef(-90.0, 0.0, 0.0, 1.0); drawTriangle(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

33 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab09.cpp #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint N=6; GLfloat size=30.0; 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); glEnd(); } void drawTriangle(void) { glBegin(GL_POLYGON); glColor3f(.0, .0, 1.0); glVertex3f(-5., -5., 0.); glVertex3f(5., -5., 0.); glColor3f(1.0, .0, .0); glVertex3f(0., 5., 0.); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

34 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab09.cpp void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); myAxis(); glTranslatef(0.0, 10.0, 0.0); glRotatef(90.0, 0.0, 0.0, 1.0); drawTriangle(); glFlush(); glutSwapBuffers(); } void myReshape(int width, int height) glClearColor (.75, .75, .75, 0.0); glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glOrtho(-size, size, -size, size, -size, size); glMatrixMode(GL_MODELVIEW); void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("lab09"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); } 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

35 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 2 From lab09 Find another sequence of transformations that will produce the same effect 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

36 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Matrix Stack Problem : Matrix multiplication is computationally expensive. The multiplication of two 4x4 matrices requires 64 multiples and 48 additions Solution : Avoid duplicate matrix multiplication by saving transformations that will be needed again. A stack is a good way to store a series of transformations for later use 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

37 OpenGL implementation
OpenGL maintains a stack that can hold a minimum of 32 matrices. The matrix at the top of the stack is always the CT (Current Transform) The following two commands manipulate the matrix stack: glPushMatrix(); glPopMatrix(); void glPushMatrix(void); Pushes all matrices in the current stack down one level. The current stack is determined by glMatrixMode(). The topmost matrix is copied, so its contents are duplicated in both the top and second-from-the-top matrix. If too many matrices are pushed, an error is generated. void glPopMatrix(void); Pops the top matrix off the stack. What was the second-from-the-top matrix becomes the top matrix. The current stack is determined by glMatrixMode(). The contents of the topmost matrix are destroyed. If the stack contains a single matrix, calling glPopMatrix() generates an error. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

38 OpenGL implementation
Assuming that the stack is implemented as an array, these two functions look like the following pseudocode: // The stack begins with an Identity matrix on top int stackTop = 1; Matrix4x4 Identity = {{1, 0, 0, 0},{0, 1, 0, 0},{0, 0, 1, 0},{0, 0, 0, 1}}; Matrix4x4 MatrixStack[32] = {Identity, {0}};  void glPushMatrix(void) { MatrixStack[stackTop] = MatrixStack[stackTop-1]; stackTop++; }   void glPopMatrix(void) stackTop--;   // The CT (current transform) is always MatrixStack[stacktop-1] 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

39 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Matrix Stack Statements stack[0] stack[1] 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); glutWireSphere(3.0, 10, 10); glRotatef(theta, 0., 0., 1.); glTranslatef(20., 0., 0.); glutWireSphere(1.0, 10, 10); glPushMatrix(); glTranslatef(5., 0., 0.); glutWireSphere(.5, 10, 10); glPopMatrix(); glTranslatef(-5., 0., 0.); M0 = I M0 = M0*Rx, xTheta M0 = M0*Ry, yTheta M0 = M0*Rz, zTheta M0 = M0*Sscale, scale, scale M0 M0 = M0*Rz, theta M0 = M0*Tx, 20. M0 = M0*Tx,,5. - M1 = M0 M1 = M1*Rz, theta M1 = M1*Tx,,5. M1 Building a Solar System The program described in this section draws a simple solar system with a planet and a sun, both using the same sphere-drawing routine. To write this program, you need to use glRotate*() for the revolution of the planet around the sun and for the rotation of the planet around its own axis. You also need glTranslate*() to move the planet out to its orbit, away from the origin of the solar system. Remember that you can specify the desired size of the two spheres by supplying the appropriate arguments for the auxSphere() routine. To draw the solar system, you first want to set up a projection and a viewing transformation. For this example, gluPerspective() and gluLookAt() are used. Drawing the sun is straightforward, since it should be located at the origin of the grand, fixed coordinate system, which is where the sphere routine places it. Thus, drawing the sun doesn't require translation; you can use glRotate*() to make the sun rotate about an arbitrary axis. To draw a planet rotating around the sun, as shown in Figure 3-21 , requires several modeling transformations. The planet needs to rotate about its own axis once a day. And once a year, the planet completes one revolution around the sun. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

40 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab10.cpp #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> bool idleFlag = 1; GLint N=6; GLint mouseX = 0; GLint mouseY = 0; GLint mouseState = 0; GLint mouseButton = 0; GLfloat size=30.0, theta=0.0; GLfloat xTheta=0., yTheta=0., zTheta=0., thetaDelta=.125; GLfloat scale=1.0, scaleDelta=1.01; void myAxis(void) { int i; glColor3f(.98, .04, .70); glutWireCube(size*2.0); 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); glVertex2f(0, -size); glVertex2f(0, size); glVertex2f(-size, 0); glVertex2f(size, 0); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

41 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab10.cpp 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(); glColor3f(.98, .625, .12); glutWireSphere(3.0, 10, 10); glRotatef(theta, 0., 0., 1.); glTranslatef(20., 0., 0.); glutWireSphere(1.0, 10, 10); glPushMatrix(); glTranslatef(5., 0., 0.); glutWireSphere(.5, 10, 10); glPopMatrix(); glTranslatef(-5., 0., 0.); glutWireSphere(.5, 10, 10); 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(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

42 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab10.cpp 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 if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { else if(btn==GLUT_RIGHT_BUTTON && state == GLUT_UP) { mouseState=-1; } else if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { xTheta=yTheta=zTheta=0.; scale=1.0; else return; if(idleFlag && state==GLUT_DOWN) glutIdleFunc(NULL); else if(idleFlag && state==GLUT_UP) glutIdleFunc(myIdle); glutPostRedisplay(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

43 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
lab10.cpp void myMotion(int x, int y) { if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) { yTheta -= (mouseX - x)/1.; xTheta -= (mouseY - y)/1.; } else if(mouseButton == GLUT_RIGHT_BUTTON && mouseState == GLUT_DOWN) { if(mouseY!=y) scale = scale * pow(scaleDelta, (mouseY - y)/1.); else return; mouseX = x; mouseY = y; glutPostRedisplay(); void myKeyboard(unsigned char theKey, int x, int y) switch (theKey) { case ' ' : idleFlag=!idleFlag; if(idleFlag) glutIdleFunc(myIdle); else glutIdleFunc(NULL); break; case 27: exit(-1); // esc key } glutPostRedisplay(); void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("lab09"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutKeyboardFunc(myKeyboard); glutMainLoop(); 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.

44 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Homework 4. In two weeks Draw a house with more than 2 windows and 2 doors. Rotate the house depend on the mouse movement while left mouse button are pressed. 19 September 2018 Computer Graphics, Lee Byung-Gook, Dongseo Univ.


Download ppt "Computer Graphics, Lee Byung-Gook, Dongseo Univ."

Similar presentations


Ads by Google