An Application of Display Lists and Transformations - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

An Application of Display Lists and Transformations

Description:

This is an example using many of the concepts we ... It does make motion more palatable. Commonly used in most graphics libraries and applications. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 27
Provided by: geraldh6
Category:

less

Transcript and Presenter's Notes

Title: An Application of Display Lists and Transformations


1
An Application of Display Lists and
Transformations
  • September 13, 2004

2
  • /
  • This is an example using many of the concepts
    we
  • have already dealt with in class -- it is a
    view of
  • a "temple" of sorts. It demonstrates call
    lists,
  • transformations, etc. It is not a very
    accurate.
  • drawing but it will work for this case
  • Author Jerry Heuring
  • Date September 17, 2003
  • /
  • include ltiostreamgt
  • include ltcmathgt
  • include "glut.h"
  • using namespace std

3
Prototypes and Variables
  • // Global Variables (limited to this file)
  • const double pi 4 atan(1.0)
  • static unsigned int cylinderID 0
  • static unsigned int cubeID 0
  • static unsigned int coneID 0
  • static unsigned int coneTreeID 0
  • static unsigned int forestID 0
  • static double viewAngle 0.0
  • // Function Prototypes
  • void drawTemple()
  • void drawPillars()
  • void init()
  • void display()
  • void reshape(int, int)
  • unsigned int drawTrees()
  • void createConeList()
  • void createCylinderList()
  • void createConeTreeList()
  • void createCubeList()

4
Idle()
  • void idle(int value)
  • / Idle is forming a timer function that causes
    the image to
  • rotate on the screen. It could be improved
    but it provides
  • a reasonable wayof seeing all sides.
  • Note -- we call the TimerFunc at the end
    again because
  • windows has a "documented feature" that
    causes the
  • timer to be called only once.
  • /
  • const double fullTurn 360.0
  • const double increment 2.0
  • viewAngle (viewAngle increment)
  • if (viewAngle gt fullTurn) // Reset
    once we rotated 1
  • viewAngle viewAngle - fullTurn
  • glutTimerFunc(static_castltunsigned
    intgt(1000.0/30.0), idle, 1)
  • glutPostRedisplay()

5
Display()
  • void display ()
  • // A simple display module that just calls the
    list 3 times...
  • // be careful to load the Identity or the
    transformations will
  • // continue to accumulate. It uses the depth
    bit and double
  • // buffering to get a reasonable version on the
    screen.
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glClear (GL_COLOR_BUFFER_BIT
    GL_DEPTH_BUFFER_BIT)
  • gluLookAt(0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0,
    1.0, 0.0)
  • glColor3f(1.0, 0.0, 0.0)
  • glTranslated(0.0, 0.0, -16.0)
  • glRotated(viewAngle, 0.0, 1.0, 0.0) // spin
    scene
  • glTranslated(0.0, 0.0, 16.0)
  • drawTemple()
  • glCallList(forestID) // not drawn each time --
    call existing list.
  • glutSwapBuffers() // gflush for double
    buffering

6
Reshape()
  • void reshape(int width, int height)
  • / reshape is called whenever the window changes
    shape -- it
  • redefines the viewport to be the entire
    window and changes
  • the projection to keep pixels "square"
  • /
  • double aspectRatio
  • const double windowSize 14.0
  • glViewport(0, 0, width, height)
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • aspectRatio static_castltdoublegt(width) /
    double(height)
  • if (width lt height)
  • glOrtho(-windowSize, windowSize,
    -windowSize/aspectRatio,
  • windowSize/aspectRatio, -100.0,
    100.0)
  • else
  • glOrtho(-windowSizeaspectRatio,
    windowSizeaspectRatio,
  • -windowSize, windowSize, -100.0, 100.0)

7
Init()
  • void init ()
  • / Init will set up overall parameters and
    establishes all of
  • the basic display lists for this program.
    There are
  • display lists containing a cube, cylinder,
    cone, and
  • "coneTree" not to mention the "forest" list.
  • /
  • glClearColor(0.0, 0.0, 0.0, 0.0)
  • glEnable(GL_DEPTH_TEST)
  • //
  • // set up the display list 's
  • //
  • cylinderID glGenLists(4)
  • cubeID cylinderID1
  • coneID cylinderID2
  • coneTreeID cylinderID3
  • createCylinderList()

8
Init() createCubeList()
  • createCubeList()
  • createConeList()
  • createConeTreeList()
  • // The forestID is the id for a call list
    consisting of a
  • // number of trees. It is split off into a
    separate routine.
  • forestID drawTrees()
  • void createCubeList()
  • // Create one with a cube in it. The cube is
    based on the xz
  • // plane by using a translation on the existing
    glut cube.
  • glNewList(cubeID, GL_COMPILE)
  • glPushMatrix()
  • glTranslated(0.0, 0.5, 0.0)
  • glutWireCube(1.0)
  • glPopMatrix()
  • glEndList()

9
createConeList()
  • void createConeList()
  • // The cone is rotated from the glut version so
    it is
  • // pointing up the y axis. The center of the
    base is
  • // on the xz plane.
  • glNewList(coneID, GL_COMPILE)
  • glPushMatrix()
  • glRotated(-90.0, 1.0, 0.0, 0.0)
  • glutWireCone(1.0, 1.0, 12, 3)
  • glPopMatrix()
  • glEndList()

10
createCylinderList
  • void createCylinderList()
  • double angle
  • // Create a cylinder in a display list. The
    cylinder's base
  • // will be centered on the origin and it will be
    a unit in
  • // diameter and a unit high. It will become
    multiple things
  • // using transformations.
  • glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  • glNewList(cylinderID, GL_COMPILE)
  • glBegin(GL_TRIANGLE_STRIP)
  • for (angle 0.0 angle lt (2.0 pi) angle
    angle pi/6.0)
  • glVertex3d(sin(angle), 0.0, cos(angle))
  • glVertex3d(sin(angle), 1.0, cos(angle))
  • glEnd()
  • glEndList()

11
createConeTreeList()
  • void createConeTreeList()
  • // The cone tree just stacks a cone on a
    cylinder to make a
  • // tree. The tree is 3 units high by default.
  • glNewList(coneTreeID, GL_COMPILE)
  • glPushMatrix()
  • glPushAttrib(GL_LINE_BIT)
  • glColor3d(0.3, 0.1, 0.1) // Brown???
  • glPushMatrix()
  • glScaled(0.5, 1.0, 0.5) // Thin down the
    cylinder glCallList(cylinderID) // for a trunk
    at the moment.
  • glPopMatrix()
  • glTranslated(0.0, 1.0, 0.0)// increase the
    cone size and
  • glScaled(1.0, 2.0, 1.0)// put it on top of the
    cylinder.
  • glColor3d(0.0, 1.0, 0.5) // green-ish
  • glCallList(coneID)
  • glPopAttrib()
  • glPopMatrix()
  • glEndList()

12
drawTrees()
  • unsigned int drawTrees()
  • / Randomly draw trees around the "temple"
    without
  • hopefully putting any of them IN the temple.
  • /
  • unsigned int listID
  • int treeCount 0
  • const int numberOfTrees 10
  • double xPosition, zPosition, treeHeight
  • // This routine draws the trees but needs to be
    called only
  • // once during which it makes a callList.
  • listID glGenLists(1)
  • glNewList(listID, GL_COMPILE)
  • while (treeCount lt numberOfTrees)
  • xPosition rand()40 - 20 // randomly place
    the base
  • zPosition rand()30 -1.0
  • // make sure it isn't IN the temple

13
drawTrees() -- contd
  • if (!(xPosition lt 6 xPosition gt -6
  • zPosition gt -34 zPosition lt 2))
  • glPushMatrix()
  • glTranslated(xPosition, -2.0, zPosition)
  • treeHeight rand() 10 / 2.0// randomly
    scale
  • // the tree's height
  • glScaled(treeHeight / 1.5 , treeHeight,
  • treeHeight/1.5)
  • glCallList(coneTreeID)
  • glPopMatrix()
  • treeCount
  • glEndList()
  • return listID

14
drawTemple()
  • void drawTemple()
  • // Draw the temple -- calls a separate routine
    for the
  • // pillars and places a flat roof and steps
  • // leading up the the temple.
  • drawPillars()
  • glColor3d(0.8, 0.8, 0.8)
  • glPushMatrix() // Draw the base
  • glTranslated(0.0, -1.0, -15.5)
  • glScaled(12.0, 1.0, 32.0)
  • glCallList(cubeID)
  • glPopMatrix()
  • glPushMatrix() // A step
  • glTranslated(0.0, -2.0, -16.5)
  • glScaled(16.0, 1.0, 36.0)
  • glCallList(cubeID)
  • glPopMatrix()

15
  • glPushMatrix() // another step...
  • glTranslated(0.0, -3.0, -17.5)
  • glScaled(20.0, 1.0, 40.0)
  • glCallList(cubeID)
  • glPopMatrix()
  • glPushMatrix() // the "roof" for the moment.
  • glTranslated(0.0, 10.0, -15.5)
  • glScaled(12.0, 1.0, 32.0)
  • glCallList(cubeID)
  • glPopMatrix()
  • return

16
drawPillars()
  • void drawPillars()
  • /
  • Draw the pillars down the two sides. We may
    also want
  • to draw some on the other sides or in the
    middle but it
  • doesn't do that yet...
  • /
  • const double height 10.0
  • const double centerOffset 5.0
  • double z
  • glPushAttrib(GL_LINE_BIT)
  • glColor3d(0.4, 0.4, 0.4) // gray
  • for (z 0.0 z gt -30.0 z z - 2.0)

17
drawPillars() contd
  • /
  • The pillars could be scaled and placed in a
    call list
  • themselves rather than scaling each one
    separately -- I
  • would probably do it but for right now this
    is working
  • out okay.
  • /
  • glPushMatrix()
  • glTranslated(-centerOffset, 0.0, z)
  • glScaled(0.5, height, 0.5)
  • glCallList(cylinderID)
  • glPopMatrix()
  • glPushMatrix()
  • glTranslated(centerOffset, 0.0, z)
  • glScaled(0.5, height, 0.5)
  • glCallList(cylinderID)
  • glPopMatrix()
  • glPopAttrib()

18
Main Program
  • int main (int argCount, char argValues)
  • // Initialize the package (takes arguments from
  • // the command line)
  • glutInit (argCount, argValues)
  • //
  • // New flags
  • // GLUT_DEPTH -- depth buffering (needed for
    hidden surfaces)
  • // GLUT_DOUBLE -- double buffering (needed --
    smooth rotation)
  • //
  • glutInitDisplayMode ( GLUT_RGBA GLUT_DEPTH
    GLUT_DOUBLE)
  • glutInitWindowSize (500, 500)
  • glutInitWindowPosition (0, 0)
  • glutCreateWindow ("trivial temple")
  • init()
  • glutReshapeFunc (reshape)
  • glutDisplayFunc (display)
  • glutTimerFunc(static_castltunsigned
    intgt(1000.0/30.0), idle, 1)

19
Main() contd
  • /
  • Start responding to events.
  • /
  • glutMainLoop ( )
  • return EXIT_SUCCESS

20
Other Tricks
  • Virtual Functions
  • Placeholders in a class no routine supplied in
    class but expected to be provided by descendents.
  • Can provide a uniform group of calls or
    interface for a group of classes.
  • Helps with polymorphic types.

21
Drawable.h
  • ifndef __DRAWABLE_H__
  • define __DRAWABLE_H__
  • class Drawable
  • public
  • virtual void draw() 0
  • endif

22
Other Classes can build on the base class
  • class Cone public Drawable
  • private
  • double radius, height
  • public
  • Cone() radius 0.5 height
    1.0 double getRadius() return radius
    double getHeight() return height void
    draw()
  • glPushMatrix() glRotated(-90.0, 0.0, 1.0,
    0.0)
  • glutWireCone(radius, height, 12, 4)
  • glPopMatrix()
  • void setRadius(double radius) this-gtradius
    radius
  • void setHeight(double height) this-gtheight
    height

23
Advantages
  • You can treat an entire group of things
    identically.
  • Can inherit code from the base class if it
    applies to multiple subclasses.
  • Can enforce a minimal set of routines for a class
    so that other routines can plan on using them.

24
Double Buffering
  • Use two buffers front and back.
  • Front buffer is the one displayed on the screen.
  • Back buffer is used to draw to.
  • When back buffer is complete the two buffers are
    switched.
  • No time to draw the screen just one refresh
    cycle.
  • No intermediate forms shown.

25
Double Buffering -- more
  • There is no speed up due to double buffering.
  • It does make motion more palatable.
  • Commonly used in most graphics libraries and
    applications.

26
What Needs to Change
  • Add GLUT_DOUBLE to the glutInitDisplayMode()
  • glutSwapBuffers() instead of glFlush().
Write a Comment
User Comments (0)
About PowerShow.com