Computer Graphics 3 - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Graphics 3

Description:

Computer Graphics 3 Lee Byung-Gook Event driven programming Using a GUI (graphical user interface), a user causes the software to execute commands based on events ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 30
Provided by: kowonDon
Category:

less

Transcript and Presenter's Notes

Title: Computer Graphics 3


1
Computer Graphics 3
  • Lee Byung-Gook

2
Event driven programming
  • Using a GUI (graphical user interface), a user
    causes the software to execute commands based on
    events, such as a mouse click, or a key pressed.
  • The structure of the software is totally
    different from a "batch," sequential program. The
    main program always looks like the following
  • while (not finished)
  • 1) Get the next event from the operating system
    event queue.
  • 2) Process the event.
  • Because the event loop is always the same, It can
    be written once. ? glutMainLoop()

3
Event driven programming
  • Functions are registered with particular events.
    When an event happens, if there is a registered
    event to process the event, the main loop calls
    the registered function. These registered
    functions are called "callback functions".
  • Standard events
  • window needs to be redrawn glutDisplayFunc()
  • window was reshaped glutReshapeFunc()
  • mouse button was pressed glutMouseFunc()
  • mouse was moved glutMotionFunc()
  • glutPassiveMotionFunc()
  • keyboard key was pressed glutKeyboardFunc()

4
lab02.cpp
  • // lab02.cpp, Computer Graphics,
    lbg_at_dongseo.ac.kr
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltGL/glut.hgt
  • GLint windowHeight
  • void myReshape(int width, int height)
  • glClearColor (.75, .75, .75, 0.0)
  • glViewport(0,0,width,height)
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(0, width, 0, height, -1, 1)
  • glMatrixMode(GL_MODELVIEW)
  • glClear(GL_COLOR_BUFFER_BIT)
  • glColor3f(0.98, .04, .70)
  • windowHeight height
  • glPointSize(5)

void myMouse(int button, int state, int x, int
y) if (button GLUT_LEFT_BUTTON state
GLUT_DOWN) glBegin(GL_POINTS) glVertex2i
(x, windowHeight-y) glEnd() glFlush() e
lse if (button GLUT_RIGHT_BUTTON)
exit(-1) void main(int argc, char
argv) glutInit(argc,argv) glutInitDisplayMod
e (GLUT_SINGLE GLUT_RGB) glutInitWindowSize(50
0,500) glutInitWindowPosition(0,0) glutCreateW
indow("lab02") glutReshapeFunc(myReshape) glut
DisplayFunc(myDisplay) glutMouseFunc(myMouse)
glutMainLoop()
5
glPointSize
  • glPointSize - specify the diameter of rasterized
    points
  • void glPointSize( GLfloat size )
  • PARAMETERS size Specifies the diameter of
    rasterized points. The initial value is 1.

6
glRect
  • glRectd, glRectf, glRecti, glRects, glRectdv,
    glRectfv, glRectiv, glRectsv - draw a rectangle
  • C SPECIFICATION
  • void glRectd( GLdouble x1, GLdouble y1, GLdouble
    x2, GLdouble y2 )
  • void glRectf( GLfloat x1, GLfloat y1, GLfloat
    x2, GLfloat y2 )
  • void glRecti( GLint x1, GLint y1, GLint x2,
    GLint y2 )
  • void glRects( GLshort x1, GLshort y1, GLshort
    x2, GLshort y2 )
  • PARAMETERS x1, y1 Specify one vertex of a
    rectangle. x2, y2 Specify the opposite vertex of
    the rectangle.
  • C SPECIFICATION
  • void glRectdv( const GLdouble v1, const
    GLdouble v2 )
  • void glRectfv( const GLfloat v1, const GLfloat
    v2 )
  • void glRectiv( const GLint v1, const GLint v2
    )
  • void glRectsv( const GLshort v1, const GLshort
    v2 )
  • PARAMETERS v1 Specifies a pointer to one vertex
    of a rectangle. v2 Specifies a pointer to the
    opposite vertex of the rectangle.

7
lab0201
  • struct GLintPoint
  • int x
  • int y
  • void myMouse(int button, int state, int x, int y)
  • static GLintPoint corner2
  • static int numberCorners 0
  • if (button GLUT_LEFT_BUTTON state
    GLUT_DOWN)
  • cornernumberCorners.x x
  • cornernumberCorners.y windowHeight - y
  • numberCorners
  • if (numberCorners gt2)
  • glRecti(corner0.x, corner0.y, corner1.x,
    corner1.y)
  • numberCorners 0

8
lab0202
  • struct GLintPoint
  • int x
  • int y
  • GLintPoint corner2 // global variable
  • void myMouse(int button, int state, int x, int y)
  • if (button GLUT_LEFT_BUTTON state
    GLUT_DOWN)
  • corner0.x x
  • corner0.y windowHeight - y
  • corner1.x corner0.x
  • corner1.y corner0.y
  • void myMotion(int x, int y)
  • corner1.x x
  • corner1.y windowHeight - y

9
Exercise 1.
  • From lab0202
  • Draw a rectangle using polyline form
  • glBegin(type)
  • glVertex(..)
  • .
  • .
  • glVertex(..)
  • glEnd()

10
lab0203
  • struct GLintPoint
  • int x
  • int y
  • const GLint MAX_RECTANGLES 10
  • GLintPoint myRectanglesMAX_RECTANGLES2
  • GLint NumberRectangles 0
  • void myMouse(int button, int state, int x, int y)
  • if (button GLUT_LEFT_BUTTON
  • state GLUT_DOWN
  • NumberRectangles lt MAX_RECTANGLES)
  • myRectanglesNumberRectangles0.x x
  • myRectanglesNumberRectangles0.y
  • windowHeight - y
  • myRectanglesNumberRectangles1.x x
  • myRectanglesNumberRectangles1.y
  • windowHeight - y

void myMotion(int x, int y) myRectanglesNumber
Rectangles-11.x x myRectanglesNumberRectan
gles-11.y windowHeight -
y myDisplay() void myDisplay(void) glClea
r(GL_COLOR_BUFFER_BIT) for (int j0
jltNumberRectangles j) glRecti(myRectanglesj
0.x, myRectanglesj0.y, myRectanglesj
1.x, myRectanglesj1.y) glFlush()
11
Double Buffering
  • When we redisplay our CRT, we want to do so at a
    rate sufficiently high that we cannot notice the
    clearing and redrawing of the screen. For most of
    us, a CRT display must be refreshed at a rate
    between 50 and 85 times per second, or we will
    notice the display flickering.
  • Double buffering can provide a solution to these
    problems.
  • glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
  • -gt glutInitDisplayMode (GLUT_DOUBLE GLUT_RGB)
  • glFlush()
  • -gt glFlush()
  • glutSwapBuffers()

12
glutInitDisplayMode
  • glutInitDisplayMode sets the initial display
    mode.
  • Usage void glutInitDisplayMode(unsigned int
    mode)
  • mode Display mode, normally the bitwise OR-ing of
    GLUT display mode bit masks. See values below
  • GLUT RGBA Bit mask to select an RGBA mode window.
    This is the default if neither GLUT RGBA nor GLUT
    INDEX are specified.
  • GLUT RGB An alias for GLUT RGBA.
  • GLUT INDEX Bit mask to select a color index mode
    window. This overrides GLUT RGBA if it is also
    specified.
  • GLUT SINGLE Bit mask to select a single buffered
    window. This is the default if neither GLUT
    DOUBLE or GLUT SINGLE are specified.
  • GLUT DOUBLE Bit mask to select a double buffered
    window. This overrides GLUT SINGLE if it is also
    specified.

13
glutInitDisplayMode
  • GLUT ACCUM Bit mask to select a window with an
    accumulation buffer.
  • GLUT ALPHA Bit mask to select a window with an
    alpha component to the color buffer(s).
  • GLUT DEPTH Bit mask to select a window with a
    depth buffer.
  • GLUT STENCIL Bit mask to select a window with a
    stencil buffer.
  • GLUT MULTISAMPLE Bit mask to select a window with
    multisampling support.
  • GLUT STEREO Bit mask to select a stereo window.
  • GLUT LUMINANCE Bit mask to select a window with a
    luminance color model.

14
glutSwapBuffers
  • glutSwapBuffers swaps the buffers of the current
    window if double buffered.
  • Usage void glutSwapBuffers(void)
  • Description
  • Performs a buffer swap on the layer in use for
    the current window. Specifically, glutSwapBuffers
    promotes the contents of the back buffer of the
    layer in use of the current window to become the
    contents of the front buffer. The contents of the
    back buffer then become undefined. The update
    typically takes place during the vertical retrace
    of the monitor, rather than immediately after
    glutSwapBuffers is called. An implicit glFlush is
    done by glutSwapBuffers before it returns.
    Subsequent OpenGL commands can be issued
    immediately after calling glutSwapBuffers, but
    are not executed until the buffer exchange is
    completed. If the layer in use is not double
    buffered, glutSwapBuffers has no effect.

15
lab0204
  • GLint oldX
  • GLint oldY
  • void drawDot(GLint x, GLint y)
  • glBegin(GL_POINTS)
  • glVertex2i(x, y)
  • glEnd()
  • oldX x
  • oldY y
  • void drawLine(GLint x, GLint y)
  • glBegin(GL_LINES)
  • glVertex2i(oldX, oldY)
  • glVertex2i(x, y)
  • glEnd()
  • oldX x

void drawBox(GLint x, GLint y) glRecti(oldX,
oldY, x, y) oldX x oldY y void
myKeyboard(unsigned char theKey, int x, int
y) y windowHeight - y // reverse y
axis switch (theKey) case 'c'
glClear(GL_COLOR_BUFFER_BIT) break case
'.' drawDot(x,y) break case
'l' drawLine(x,y) break case
'b' drawBox(x,y) break case 27 exit(-1)
// esc key glFlush() glutKeyboardFunc(myKe
yboard) // in main()
16
Homework 2
  • In two weeks
  • Create one rectangle with mouse events
  • Move rectangle
  • Change size
  • Change color with keyboard events
  • (rred, ggreen, bblue)

17
Coordinate conversions
  • From pixels (screen coordinates) (in red) to
    world (user coordinates) (in blue)
  • Since the point is in the same location, the
    distances in pixel units must be proportional to
    the distances in world units. Therefore

18
Screen coordinate
19
World coordinate
20
Coordinate conversions
Therefore, PixelX AWorldX B, PixelY
CWorldY D
21
Coordinate conversions
  • Example Given a viewport, 200 pixels wide and
    100 pixels high, calculate the location of a
    point given its world coordinates.
  • The world coordinate system is 12 units wide (-3
    to 9) and 10 units high (-4 to 6). What is the
    pixel location of the point (3.5, 3.2)?
  • A 200 / (9 - (-3)) 16.67, B -(-3 /
    (9-(-3))200 50,
  • C -(100 / (6 - (-4))) -10, D (6 / (6 -
    (-4)))100 60
  • PixelX A3.5 B 16.673.5 50 108.33
    108,
  • PixelY C3.2 D -103.2 60 28
  • Therefore, the point (3.5, 3.2) in world
    coordinates will be drawn
  • at pixel location (108, 28).

22
glOrtho
  • The glOrtho function multiplies the current
    matrix by an orthographic matrix.
  • void glOrtho( GLdouble left, GLdouble right,
    GLdouble bottom, GLdouble top, GLdouble near,
    GLdouble far )
  • Parameters
  • left, right The coordinates for the left and
    right vertical clipping planes.
  • bottom, top The coordinates for the bottom and
    top horizontal clipping planes.
  • near, far The distances to the nearer and
    farther depth clipping planes. These distances
    are negative if the plane is to be behind the
    viewer.

23
Orthographic projection
24
glViewport
  • The glViewport function sets the viewport.
  • void glViewport( GLint x, GLint y, GLsizei width,
    GLsizei height )
  • Parameters
  • x, y The lower-left corner of the viewport
    rectangle, in pixels. The default is (0,0).
  • width, height The width and height,
    respectively, of the viewport. When an OpenGL
    context is first attached to a window, width and
    height are set to the dimensions of that window.

25
Viewport
26
lab03.cpp
  • // lab03.cpp, Computer Graphics,
    lbg_at_dongseo.ac.kr
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltGL/glut.hgt
  • GLfloat size2.0
  • GLint N4
  • void myAxis(void)
  • int i
  • glColor3f(0.98, .04, .70)
  • glBegin(GL_LINES)
  • for(i1 iltN i)
  • glVertex2f(-size2.0isize/N, -size)
  • glVertex2f(-size2.0isize/N, size)
  • glVertex2f(-size, -size2.0isize/N)
  • glVertex2f(size, -size2.0isize/N)

void myDraw(void) glColor3f(0.60, .40, .70)
glBegin(GL_POLYGON) glVertex2f(-1.,
-1.) glVertex2f(1., -1.) glVertex2f(1.,
1.) glVertex2f(-1., 1.) glEnd() void
myDisplay(void) glClear(GL_COLOR_BUFFER_BIT)
myAxis() myDraw() glFlush()
27
lab03.cpp
  • 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_SINGLE GLUT_RGB)
  • glutInitWindowSize(500,500)
  • glutInitWindowPosition(0,0)
  • glutCreateWindow("lab03")
  • glutReshapeFunc(myReshape)
  • glutDisplayFunc(myDisplay)


28
lab03
29
Exercise 2.
  • From lab03
  • modify source code to display following figures
Write a Comment
User Comments (0)
About PowerShow.com