410 likes | 684 Views
Computer Graphics 6. Lee Byung-Gook. 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;
E N D
Computer Graphics 6 Lee Byung-Gook 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); glEnd(); } 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(); 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 && state == GLUT_DOWN) { if(aniOn) glutIdleFunc(NULL); mouseState=state; mouseButton=btn; mouseX=x; mouseY=y; } 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; } glutPostRedisplay(); } 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 lbg@dongseo.ac.kr"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutKeyboardFunc(myKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutMainLoop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 1 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 2 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 3 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 4 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 5 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 6 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Exercise 7 • Program to draw the figure using the matrix stack with lab11.cpp Computer Graphics, Lee Byung-Gook, Dongseo Univ.
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 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); 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, yTopare distances along their respective axis's. • zNearandzFarare distances from the camera. They should never be equal. 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 Computer Graphics, Lee Byung-Gook, Dongseo Univ.
glFrustum • void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); 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, yTopare distances along their respective axis's. • zNearandzFarare distances from the camera. They should always be positive Computer Graphics, Lee Byung-Gook, Dongseo Univ.
gluPerspective • void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); 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. • fieldOfViewis the angle between the top and bottom faces of the frustum • aspect ratio is the width of the frustum divided by its height • zNearandzFarare distances from the camera. They should always be positive Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Y yTop fieldOfView Camera -Z axis yBottom zNear zFar glFrustum, gluPerspective • The parameters of the two projections have the following relationship 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}, {-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 }; 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); 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; break; case 'p': projection = !projection; myResize(windowWidth, windowHeight); break; case 'd': depthOn = !depthOn; if(depthOn) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break; case 'z': scale*=scaleDelta; break; case 'x': scale/=scaleDelta; break; } 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; glutPostRedisplay(); } 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 lbg@dongseo.ac.kr"); glutDisplayFunc(myDisplay); glutReshapeFunc(myResize); glutKeyboardFunc(myKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutMainLoop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ.
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 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. Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Z-buffer algorithmin 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 } } Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Z-buffer algorithmin 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); 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. Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Y -Z Camera X Camera Control • The default camera position 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. 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); } 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 : break; case GLUT_KEY_LEFT : theta-=thetaDelta; break; case GLUT_KEY_RIGHT : theta+=thetaDelta; break; default : return; } myLookAt(key); glutPostRedisplay(); } glutSpecialFunc(mySKeyboard); //in main() Computer Graphics, Lee Byung-Gook, Dongseo Univ.
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); } Up vector Center of interest theta z Center of interest Camera theta cos(theta) Eye position x sin(theta) Eye position Moving the camera 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. 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(); glPushMatrix(); glTranslatef(0., -.5, 0.); glRotatef(-anitheta*5, 0., 0., 1.); glColor3fv(color[3]); gluCylinder(c, .25, .25, .25, 12, 10); glPopMatrix(); glPushMatrix(); glTranslatef(0., .5, 0.); glRotatef(-anitheta*5, 0., 0., 1.); glColor3fv(color[4]); gluCylinder(c, .25, .25, .25, 12, 10); glPopMatrix(); 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); glRotatef(-anitheta*5, 0., 0., 1.); glColor3fv(color[6]); gluCylinder(c, .5, .1, .25, 12, 10); glTranslatef(0., 0., .25); 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; break; case 'f': fillOn = !fillOn; if(fillOn) gluQuadricDrawStyle(c, GLU_FILL); else gluQuadricDrawStyle(c, GLU_LINE); break; 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); break; case 'z': scale*=scaleDelta; break; case 'x': scale/=scaleDelta; break; } 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(); } 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 lbg@dongseo.ac.kr"); glutDisplayFunc(myDisplay); glutReshapeFunc(myResize); glutKeyboardFunc(myKeyboard); glutSpecialFunc(mySKeyboard); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutIdleFunc(myIdle); glutMainLoop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Homework 6 • Program to draw the figure using glutWireSphere and gluCylinder Computer Graphics, Lee Byung-Gook, Dongseo Univ.