More Primitives - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

More Primitives

Description:

We will try to do a fair amount with a very simple basic object a box. ... Basic Affine Transformations. Translation (moving something) Rotation (turning something) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 29
Provided by: geraldh6
Category:

less

Transcript and Presenter's Notes

Title: More Primitives


1
More Primitives Attributes
  • September 1, 2004

2
This Example
  • We will try to do a fair amount with a very
    simple basic object a box.
  • The width, depth, height, and position of the box
    can be specified.
  • We will also look at how to "move around" the box
    and how to get the hidden surfaces to look
    "right".

3
Preamble
  • /
  • This example will build a Table and Chair
    using only boxes
  • and will use a STL list to hold onto the
    objects to be
  • displayed.
  • The main reason for this example is to give
    you an idea of
  • what can be done with a simple type of object
    and a little
  • imagination.
  • Author Jerry Heuring
  • Date August 31, 2004
  • /
  • include ltcmathgt
  • include "glut.h"
  • include ltlistgt
  • using namespace std

4
Box Class
  • class Box private double location3
    float color3 bool filled double
    width, height, depthpublic Box(double x
    0.0, double y 0.0, double z 0.0,
  • double width 1.0, double height 1.0, double
    depth 1.0,
  • float red 1.0, float green 1.0, float blue
    1.0) void setFilled(bool f) bool
    isFilled() void setLocation(double x, double
    y, double z) void setColor(float red, float
    green, float blue) void display()

5
Box Constructor
  • BoxBox (double x, double y, double z, double
    width, double height, double depth, float red,
    float green, float blue)
  • location0 x
  • location1 y
  • location2 z
  • color0 red
  • color1 green
  • color2 blue
  • this-gtwidth width
  • this-gtheight height
  • this-gtdepth depth
  • filled true

6
Get/Set Methods
  • void BoxsetFilled(bool f) filled f
  • bool BoxisFilled() return filled
  • void BoxsetLocation ( double x, double y,
    double z) location0 x location1
    y location2 z
  • void BoxsetColor( float r, float g, float b)
    color0 r color1 g color2
    b

7
display method
  • void Boxdisplay () glMatrixMode(GL_MODELVI
    EW) glPushAttrib(GL_ALL_ATTRIB_BITS)
    glColor3f( color0, color1, color2 ) if
    (filled) glPolygonMode(GL_FRONT_AND_BACK,
    GL_FILL) else glPolygonMode(GL_FRONT_AN
    D_BACK, GL_LINE) glLineWidth(4.0) // Wide
    Lines so we can see them
    glBegin(GL_QUAD_STRIP) glVertex3d(
    location0, location1, location2) glVertex
    3d( location0, location1 height,
    location2) glVertex3d( location0width,
    location1, location2) glVertex3d(
    location0width, location1 height,
    location2) glVertex3d( location0width,
    location1, location2depth )

8
  • glVertex3d( location0width, location1
    height, location2 depth) glVertex3d(
    location0, location1, location2
    depth) glVertex3d( location0, location1
    height, location2depth) glVertex3d(
    location0, location1,
    location2) glVertex3d( location0,
    location1 height, location2) glEnd()
    glBegin(GL_QUADS) glVertex3d (location0,
    location1, location2) glVertex3d
    (location0width, location1,
    location2) glVertex3d (location0,
    location1, location2depth)

9
  • glVertex3d (location0width, location1,
    location2depth)
  • glVertex3d (location0, location1height,
    location2)
  • glVertex3d (location0width,
    location1height, location2)
  • glVertex3d (location0, location1height,
    location2depth)
  • glVertex3d (location0width,
    location1height, location2depth)
  • glEnd()
  • glPopAttrib()

10
Prototypes and Globals
  • //
  • // Prototypes...
  • //
  • void buildTable()
  • void buildChair()
  • void initialize()
  • void display()
  • void updatePosition(int)
  • void handleKey(unsigned char ch, int x, int y)
  • /
  • All our objects will be stored in a STL List
  • /
  • static listltBoxgt objectList
  • static double angle 0.0
  • const double M_PI atan(1.0) 4.0

11
main()
  • /
  • The main program builds the objects and hooks
    up routines
  • for handling redisplay, keystrokes and a
    timer.
  • /
  • int main (int argCount, char argValues)
    glutInit(argCount, argValues)
    glutInitDisplayMode(GLUT_RGBA GLUT_DEPTH)
    glutInitWindowSize(700, 700)
    glutCreateWindow("Table Example") initialize()
    buildTable() buildChair()
    glutDisplayFunc(display) glutTimerFunc(500.0,upd
    atePosition, 1) glutKeyboardFunc(handleKey)
    glutMainLoop() return EXIT_SUCCESS

12
initialize()
  • void initialize()
  • /
  • this routine is largely lifted from the
    hoops example.
  • I've added a statement to enable depth
    testing so that
  • Hidden surfaces are handled correctly.
  • / glEnable (GL_DEPTH_TEST)
    glClearColor(1.0, 1.0, 1.0, 1.0) // White for
    the clear color glMatrixMode(GL_PROJECTION)
    glLoadIdentity() glOrtho(-70.0, 70.0,
    -40.0, 100.0, -100.0, 100.0)
    glMatrixMode(GL_MODELVIEW)

13
display()
  • void display()
  • / Use an iterator to step through the list
    and display each piece... We need to do a
    few things here. We will clear a Depth
    Buffer. It is necessary to do the hidden
    surfaces. Also, have added a call to
    gluLookAt to have us circle the object for
    inspection purposes. / listltBoxgtiterator
    iter glMatrixMode(GL_MODELVIEW)
    glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
    glLoadIdentity() gluLookAt(sin(angle M_PI
    / 180.0)3525.0, 15.0, cos (angleM_PI /
    180.0)-40.0, // eyepoint shifts 25.0,
    15.0, 0.0, // look at point 0.0, 1.0, 0.0)
    // up vector

14
  • for (iter objectList.begin() iter !
    objectList.end() iter)
  • iter-gtdisplay()
  • glFlush()

15
buildTable()
  • / Build a table out of boxes. Just a top and
    4 legs. /
  • void buildTable()
  • // Table top
  • objectList.push_back(Box( 0.0, 30.0, 0.0,
    60.0, 2.0, 20.0, 1.0, 0.5, 0.5))
  • // The 4 legs at the corners in a different
    color
  • objectList.push_back(Box( 0.0, 0.0, 0.0,
    2.0, 30.0, 2.0, 0.5, 1.0, 0.5))
  • objectList.push_back(Box( 58.0, 0.0, 0.0,
    2.0, 30.0, 2.0, 0.5, 1.0, 0.5))
  • objectList.push_back(Box( 0.0, 0.0, 18.0,
    2.0, 30.0, 2.0, 0.5, 1.0, 0.5))
  • objectList.push_back(Box( 58.0, 0.0, 18.0,
    2.0, 30.0, 2.0, 0.5, 1.0, 0.5))

16
buildChair()
  • void buildChair() / Creates the
    boxes that make up a chair. Note that the
    chair is always in one place. It might be nice
    to make it possible to set a location...
    / // The seat of the chair
    objectList.push_back(Box(-10.0, 16.0, 2.0,
    16.0, 1.0, 16.0, 0.5, 0.5,
    1.0)) // The two Back legs/supports
    objectList.push_back(Box(-10.0, 0.0, 2.0,
    2.0, 40.0, 2.0, 0.5, 1.0, 1.0))
    objectList.push_back(Box(-10.0, 0.0, 16.0,
    2.0, 40.0, 2.0, 0.5, 1.0, 1.0))

17
  • // The two Front legs (under the table)
  • objectList.push_back(Box( 4.0, 0.0, 2.0,
    2.0, 16.0, 2.0, 0.5, 1.0, 1.0))
    objectList.push_back(Box( 4.0, 0.0, 16.0,
    2.0, 16.0, 2.0, 0.5, 1.0, 1.0))
    // 3 slats up the back of the chair
    objectList.push_back(Box(-10.0, 24.0, 2.0,
    2.0, 3.0, 16.0, 0.5, 1.0, 1.0))
    objectList.push_back(Box(-10.0, 32.0, 2.0,
    2.0, 3.0, 16.0, 0.5, 1.0, 1.0))
    objectList.push_back(Box(-10.0, 40.0, 2.0,
    2.0, 3.0, 16.0, 0.5, 1.0, 1.0))

18
updatePosition()
  • / The timer routine. It shifts the viewing
    angle by 5 degrees and redraws the display.
    Hooking up the Timer function again is
    necessary in windows but not in Unix /
  • void updatePosition(int value) angle
    5.0 if (angle gt 360.0) angle -
    360.0 glutTimerFunc(500.0, updatePosition,
    1) glutPostRedisplay()

19
handleKey()
  • / handle the keystrokes -- currently only 3
    characters are recognized x -- exit
    w -- wireframe f -- filled /void
    handleKey(unsigned char ch, int x, int
    y) listltBoxgtiterator iter if (ch 'x')
    exit(0) else if (ch 'w') for (iter
    objectList.begin() iter !
    objectList.end() iter) iter-gtsetFilled(fal
    se) glutPostRedisplay() else if (ch
    'f') for (iter objectList.begin() iter
    ! objectList.end() iter) iter-gtsetFilled(
    true) glutPostRedisplay()

20
gluLookAt
  • Used to change the viewpoint
  • gluLookAt (double eyex, double eyey, double
    eyez, double xcenter, double ycenter, double
    zcenter, double upx, double upy, double upz)
  • Common Problems
  • forget to load the identity matrix (reset the
    current transform)
  • change it while in Projection Mode

21
Depth Buffer
  • The depth buffer compares the distance each pixel
    is from the eye while drawing objects. If the
    object is closer than the current pixel's depth
    it updates it with the new one. If it is further
    it remains unchanged.
  • 3 Pieces
  • glut needs to allocate a buffer
  • depth processing must be enabled
  • need to clear the depth buffer when we clear the
    screen.

22
Call Lists
  • Lists of primitives and certain changes can be
    combined into a Call List in OpenGL.
  • Advantages
  • Can save time for certain classes of operations
  • Matrix Operations
  • Raster bitmaps and images
  • Lights, Materials, and Lighting Methods
  • Polygon Stipples

23
Basic Calls
  • glGenLists(1)
  • glNewList (GLuint listID, GLenum mode)
  • listID is the id for the list
  • mode is either GL_COMPILE or GL_COMPILE_AND_EXECUT
    E
  • glEndList()
  • glCallList(GLuint listID)

24
Example Snippet
  • GLuint listID
  • listID glGenLists(1)glNewList(listID,
    GL_COMPILE)
  • glBegin(GL_QUAD_STRIP) glVertex3d()
  • glEnd()
  • glEndList()
  • glCallList (listID)

25
What is Really Happening?
  • The system is generating some code for OpenGL to
    output to the screen. It does not keep track of
    any or your variables.
  • For items where OpenGL may need to do a fair
    amount of calculation it may be more efficient to
    store in a call list rather than recompute.

26
Does it Help Us?
  • This really doesn't help us -- yet.
  • Multiple copies of something drawn in the same
    location aren't helpful.
  • Haven't dealt with Materials and Lights.
  • Not working with images

27
When Will it Help Real Soon Now
  • We would like to transform objects
  • Basic Affine Transformations
  • Translation (moving something)
  • Rotation (turning something)
  • Scaling (shrinking or enlarging something)

28
How Does This Help With Call Lists?
  • With Transformations call lists are useful since
    our table could all be 1 cube scaled and
    translated appropriately.
  • Also, transformations can be stored in call
    lists. As we will see a transformation often
    involves a matrix multiplication that could be
    done ahead of time.
Write a Comment
User Comments (0)
About PowerShow.com