Computer Graphics 6 - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Computer Graphics 6

Description:

fieldOfView is the angle between the top and bottom faces of the frustum ... 98f, .625f, .12f}, //pumpkin orange {.98f, .04f, .70f}, //pastel pink ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 40
Provided by: kowonDo
Category:

less

Transcript and Presenter's Notes

Title: Computer Graphics 6


1
Computer Graphics 6
  • Lee Byung-Gook

2
lab11.cpp
include ltmath.hgt include ltstdio.hgt include
ltstdlib.hgt include ltGL/glut.hgt GLint N4 GLint
aniOn 0 GLint mouseX 0 GLint mouseY
0 GLint mouseState 0 GLint mouseButton 0
GLfloat size20.0, theta0.0 GLfloat xTheta0.,
yTheta0., zTheta0. GLfloat scale1.0,
scaleDelta1.01 void myAxis(void) int i glC
olor3f(.98, .04, .70) glBegin(GL_LINES) for(i
0 iltN i) glVertex2f(-size2.0isize/N,
-size)
glVertex2f(-size2.0isize/N,
size) glVertex2f(-size, -size2.0isize/N)
glVertex2f(size, -size2.0isize/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()
3
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) theta0.5 glutPostRedisplay()

void myMouse(int btn, int state, int x, int
y) if(btnGLUT_LEFT_BUTTON state
GLUT_DOWN) if(aniOn) glutIdleFunc(NULL) mou
seStatestate mouseButtonbtn mouseXx mo
useYy else if(btnGLUT_LEFT_BUTTON
state GLUT_UP) if(aniOn)
glutIdleFunc(myIdle) mouseState-1 else
if(btnGLUT_RIGHT_BUTTON state
GLUT_DOWN) if(aniOn) glutIdleFunc(NULL) mou
seStatestate mouseButtonbtn mouseXx mo
useYy
4
lab11.cpp
else if(btnGLUT_RIGHT_BUTTON state
GLUT_UP) if(aniOn) glutIdleFunc(myIdle) mou
seState-1 else if(btnGLUT_MIDDLE_BUTTON
state GLUT_DOWN) xThetayThetazTheta
0. scale1.0 else return glutPostRedisp
lay() 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 glutPostRedispla
y()
5
lab11.cpp
void main(int argc, char argv) glutInit(argc
,argv) glutInitDisplayMode (GLUT_DOUBLE
GLUT_RGB) glutInitWindowSize(500,500) glutInit
WindowPosition(0,0) glutCreateWindow("lab11 by
lbg_at_dongseo.ac.kr") glutReshapeFunc(myReshape)
glutDisplayFunc(myDisplay) glutKeyboardFunc(myK
eyboard) glutMouseFunc(myMouse) glutMotionFunc
(myMotion) glutMainLoop()
6
Exercise 1
  • Program to draw the figure using the matrix stack
    with lab11.cpp

7
Exercise 2
  • Program to draw the figure using the matrix stack
    with lab11.cpp

8
Exercise 3
  • Program to draw the figure using the matrix stack
    with lab11.cpp

9
Exercise 4
  • Program to draw the figure using the matrix stack
    with lab11.cpp

10
Exercise 5
  • Program to draw the figure using the matrix stack
    with lab11.cpp

11
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

12
Orthographic
  • glOrtho(GLdouble left, GLdouble right, GLdouble
    bottom, GLdouble top, GLdouble near, GLdouble
    far)
  • gluOrtho2D(GLdouble left, GLdouble right,
    GLdouble bottom, GLdouble top)

13
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.

14
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

15
glFrustum
  • void glFrustum(GLdouble left, GLdouble right,
    GLdouble bottom, GLdouble top, GLdouble near,
    GLdouble far)

16
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

17
gluPerspective
  • void gluPerspective(GLdouble fovy, GLdouble
    aspect, GLdouble zNear, GLdouble zFar)

18
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

19
glFrustum, gluPerspective
  • The parameters of the two projections have the
    following relationship

20
lab12.cpp
include ltmath.hgt include ltstdio.hgt include
ltwindows.hgt include ltgl/glut.hgt typedef float
vec3_t3 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.0
f, 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 .25
f, .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
21
lab12.cpp
vec3_t rot 0.,0.,0. vec3_t eye
0.,0.,-5. vec3_t center 0.,0.,0. float si
ze3. float theta.0 float thetaDelta.125 floa
t eyeDelta.125 float scale1.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 lt
20openOn4 i ) if(fillOn)
glBegin(GL_POLYGON) else glBegin(GL_LINE_LOOP)
glColor3fv(colori12) glVertex3fv(cubei)
if((i1)4 0) glEnd() void
myDisplay (void) glClear(GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELV
IEW) glLoadIdentity() if(!projection) glTra
nslatef(eye0, eye1, eye2) glRotatef(rot0
, 1.0f, 0.0f, 0.0f) glRotatef(rot1, 0.0f,
1.0f, 0.0f) glRotatef(rot2, 0.0f, 0.0f,
1.0f) glScalef(scale, scale, scale)
22
lab12.cpp
drawCube() glColor3fv(color7) glutWireTeapo
t(.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) gl
LoadIdentity() if(projection) glOrtho(-size,
size, -size, size, -size, size) else
gluPerspective(60., (float)width/height, .1,
100.) glEnable(GL_DEPTH_TEST) windowWidthwidt
h windowHeightheight 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' scalescaleDelta break case
'x' scale/scaleDelta break
23
lab12.cpp
glutPostRedisplay() void myMouse(int btn, int
state, int x, int y) if(btnGLUT_LEFT_BUTTON
state GLUT_DOWN) mouseStatestate
mouseButtonbtn mouseXx mouseYy else
if(btnGLUT_LEFT_BUTTON state
GLUT_UP) mouseState-1 else
return glutPostRedisplay() void myMotion(int
x, int y) if(mouseButton GLUT_LEFT_BUTTON
mouseState GLUT_DOWN) rot1 -
(mouseX - x)/5.
rot0 - (mouseY - y)/5. glutPostRedisplay()
mouseXx mouseYy void main
(void) glutInitDisplayMode(GLUT_RGB
GLUT_DOUBLE GLUT_DEPTH) glutInitWindowSize
(500,500) glutCreateWindow("lab12 by
lbg_at_dongseo.ac.kr") glutDisplayFunc(myDisplay)
glutReshapeFunc(myResize) glutKeyboardFunc(myKe
yboard) glutMouseFunc(myMouse) glutMotionFunc(
myMotion) glutMainLoop()
24
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
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.

26
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 lt depthBufferxy)
  • color calculate the color of face at (x,y)
  • colorBufferxy color
  • depthBufferxy depth

27
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)

28
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.

29
Camera Control
  • The default camera position

30
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.

31
lab1201.cpp
void myDisplay (void) glClear(GL_COLOR_BUFF
ER_BIT GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() if(!projection) gluLookAt(e
ye0, eye1, eye2, center0, center1,
center2, 0, 1, 0) glRotatef(rot0, 1.0f,
0.0f, 0.0f) glRotatef(rot1, 0.0f, 1.0f,
0.0f) glRotatef(rot2, 0.0f, 0.0f,
1.0f) glScalef(scale, scale, scale) drawCube()
glColor3fv(color7) glutWireTeapot(.5) glF
lush() glutSwapBuffers()
void myLookAt(int key) if(key GLUT_KEY_UP)
eye2 eye2-cos(theta)eyeDelta eye0
eye0sin(theta)eyeDelta else if(key
GLUT_KEY_DOWN) eye2 eye2cos(theta)eyeD
elta eye0 eye0-sin(theta)eyeDelta c
enter2 eye2-cos(theta) center0
eye0sin(theta)
32
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) eye2 0. else
gluPerspective(60., (float)width/height, .1,
100.) eye2 5. myLookAt(0) glEnable(
GL_DEPTH_TEST) windowWidthwidth 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
thetathetaDelta break default
return myLookAt(key) glutPostRedisplay(
) glutSpecialFunc(mySKeyboard) //in main()
33
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 thetathetaDelta break
  • default return
  • myLookAt(key)
  • glutPostRedisplay()
  • void myLookAt(int key)
  • if(key GLUT_KEY_UP)
  • eye2 eye2-cos(theta)eyeDelta
  • eye0 eye0sin(theta)eyeDelta

34
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.

35
lab1202.cpp
float anitheta.0 float anithetaDelta.125 GLUqu
adricObj c void myDisplay (void)
glClear(GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELV
IEW) glLoadIdentity() if(!projection) gl
uLookAt(eye0, eye1, eye2, center0,
center1, center2, 0, 1,
0) glRotatef(rot0, 1.0f, 0.0f,
0.0f) glRotatef(rot1, 0.0f, 1.0f,
0.0f) glRotatef(rot2, 0.0f, 0.0f,
1.0f) glScalef(scale, scale, scale) glRotatef(
-90., 1., 0., 0.) glPushMatrix() glScalef(3.,
3., .1) glColor3fv(color0) if(fillOn)
glutSolidCube(1.0) else glutWireCube(1.0) gl
PopMatrix()
glTranslatef(0., 0., .1) glRotatef(anitheta,
0., 0., 1.) glColor3fv(color1) gluCylinder(c
, 1., 1., .5, 12, 10) glTranslatef(0., 0.,
.5) glPushMatrix() glTranslatef(-.5, 0.,
0.) glRotatef(-anitheta5, 0., 0.,
1.) glColor3fv(color2) gluCylinder(c,
.25, .25, .25, 12, 10) glPopMatrix() glPushMat
rix() glTranslatef(0., -.5, 0.) glRotatef(-a
nitheta5, 0., 0., 1.) glColor3fv(color3)
gluCylinder(c, .25, .25, .25, 12,
10) glPopMatrix() glPushMatrix() glTranslat
ef(0., .5, 0.) glRotatef(-anitheta5, 0., 0.,
1.) glColor3fv(color4) gluCylinder(c,
.25, .25, .25, 12, 10) glPopMatrix()
36
lab1202.cpp
glPushMatrix() glTranslatef(.5, 0.,
0.) glRotatef(-anitheta5, 0., 0.,
1.) glColor3fv(color5) gluCylinder(c,
.25, .25, .25, 12, 10) glPopMatrix() glTransla
tef(0., 0., .25) glRotatef(-anitheta5, 0., 0.,
1.) glColor3fv(color6) gluCylinder(c, .5,
.1, .25, 12, 10) glTranslatef(0., 0.,
.25) glColor3fv(color7) gluCylinder(c, .1,
.1, .5, 12, 10) glTranslatef(0., 0.,
.725) glColor3fv(color8) if(fillOn)
glutSolidSphere(.25, 10, 10) else
glutWireSphere(.25, 10, 10) glFlush() glutSwap
Buffers()
void myIdle() anitheta anithetaDelta glutP
ostRedisplay() 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
37
lab1202.cpp
case 'p' projection !projection myResi
ze(windowWidth, windowHeight) break case
'd' depthOn !depthOn if(depthOn)
glEnable(GL_DEPTH_TEST) else
glDisable(GL_DEPTH_TEST) break case
'z' scalescaleDelta break case
'x' scale/scaleDelta break glutPostRe
display()
void myMouse(int btn, int state, int x, int
y) if(btnGLUT_LEFT_BUTTON state
GLUT_DOWN) if(aniOn) glutIdelFunc(NULL)
mouseStatestate mouseButtonbtn mouseXx
mouseYy else if(btnGLUT_LEFT_BUTTON
state GLUT_UP) mouseState-1 if(aniOn
) glutIdelFunc(myIdle) else
return glutPostRedisplay()
38
lab1202.cpp
void myInit() cgluNewQuadric() gluQuadricDra
wStyle(c, GLU_LINE) void main
(void) myInit() glutInitDisplayMode(GLUT_
RGB GLUT_DOUBLE GLUT_DEPTH)
glutInitWindowSize(500,500)
glutCreateWindow("lab12 by lbg_at_dongseo.ac.kr")
glutDisplayFunc(myDisplay) glutReshapeFunc(myRe
size) glutKeyboardFunc(myKeyboard)
glutSpecialFunc(mySKeyboard) glutMouseFunc(myM
ouse) glutMotionFunc(myMotion) glutIdleFunc(my
Idle) glutMainLoop()
39
Homework 6
  • Program to draw the figure using
  • glutWireSphere and gluCylinder
Write a Comment
User Comments (0)
About PowerShow.com