Primitives and Attributes - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Primitives and Attributes

Description:

... a C application using OpenGL and GLUT to draw a representation of a locomotive. The locomotive must be 3 dimensional ... Some Locomotive Examples ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 29
Provided by: Jer598
Category:

less

Transcript and Presenter's Notes

Title: Primitives and Attributes


1
Primitives and Attributes
2
An Application With Primitives
  • Objects
  • Pyramid
  • Made up of triangles triangle strip
  • Filled/unfilled
  • Color
  • Hoops
  • Made up of 3 circles (polylines)
  • Color
  • Line Style
  • Line Width

3
OpenGL Calls
  • glLineWidth(float width)
  • Sets linewidth for subsequent lines. Linewidth
    is limited in what it can do no 0.0000001.
  • glLineStipple(int patternScale, unsigned short
    pattern)
  • Uses a bit pattern (pattern) to determine the
    line pattern. 0s and 1s are spaces and lines
    respectively.
  • Can be scaled using the pattern scale to get
    larger versions.
  • Pattern is 16 bits.

4
OpenGL Calls
  • glEnable(GL_LINE_STIPPLE)
  • enables the line stippling not on by default.
  • glPolygonMode(GLenum face, GLenum mode)
  • the face determines which face this is to be
    applied to. One of GL_FRONT, GL_BACK, or
    GL_FRONT_AND_BACK.
  • the mode determines the fill mode. Can be
    GL_FILL, GL_LINE, GL_POINT

5
Polygon Stippling
  • Can apply a pattern to polygons as well. The
    call is
  • glPolygonStipple (GLubyte mask)
  • The mask is an array that defines a 32x32 bit map
    where 1s indicate drawn pixels and 0s indicate
    blank pixels.
  • glEnable (GL_POLYGON_STIPPLE)
  • Needed to enable the polygon stipple off by
    default.

6
Primitives and AttributesThe Program
  • include ltiostreamgt
  • include ltcstdlibgt
  • include ltcmathgt
  • include "glut.h"
  • using namespace std
  • /
  • Primitives and Attributes Example
  • This program is an example of some of the
    primitives and
  • attributes available to the user. It also
    sets up and uses
  • a simple menu to allow the user to
    interactively change the
  • attributes involved.
  • Author Jerry Heuring
  • Date September 3, 2003
  • Revisions
  • Bugs
  • /

7
  • const double PI3.14159
  • /
  • Hoops is an object made up of 3 circles. It
    has been placed
  • here to demonstrate the attributes associated
    with lines and
  • to give an alternative method of handling
    objects (as opposed
  • to global calls).
  • Hoops -- Default Constructor
  • setRadius -- sets the radius/size of the
    hoops.
  • getRadius -- gets the current radius of
    the hoops.
  • setCenter -- sets the location of the center
    of the hoops.
  • getPattern -- gets the pattern being used to
    draw the lines.
  • setPattern -- sets the pattern being used to
    draw the lines.
  • getPatternScale -- gets the scale that is
    being applied to
  • the pattern
  • setPatternScale -- sets the scale to be
    applied to the
  • pattern
  • getLineWidth -- gets the current width of
    the lines being

8
  • class Hoops
  • private
  • float radius
  • float center3
  • float color3
  • float lineWidth
  • int patternScale
  • unsigned short pattern
  • public
  • Hoops()
  • / Constructs the object -- centered at the
    origin
  • with a radius of 1.0. It is originally
    drawn with
  • solid lines.
  • /
  • radius 1.0
  • center0 center1 center2 0.0
  • color0 1.0 color1 0.0 color2 0.0
  • lineWidth 1.0
  • patternScale 1

9
  • void setRadius (double r)
  • // Set the radius of the object -- will take
    effect at
  • // the next display.
  • radius r
  • double getRadius ()
  • // returns the current radius of the object.
  • return radius
  • void setCenter(double x, double y, double z)
  • // Sets the center of the object to a
  • // particular coordinate.
  • center0 x center1 y center2 z
  • unsigned short getPattern()
  • // returns the pattern currently being used to

10
  • void setPattern(unsigned short p)
  • // sets the line drawing pattern
  • // 1 bits are drawn 0 bits are skipped.
  • pattern p
  • void setPatternScale(int s)
  • // The pattern scale is used to stretch out the
  • // pattern. Must be an integer!
  • patternScale s
  • int getPatternScale() // returns the current
    pattern scale.
  • return patternScale
  • double getLineWidth() // returns the current
    linewidth
  • return lineWidth
  • void setLineWidth(double newWidth)
  • // sets the line width -- even though a real
    value
  • // is used for linewidth all values are not
    able to be

11
  • void setColor(float red, float green, float
    blue)
  • // Set the color to use for the lines.
  • color0 red color1 green color2
    blue
  • void display()
  • /
  • Draw the object -- The object is drawn with
    its
  • current attributes. The routine saves the
  • current attributes before drawing and
    restores
  • them at the end so that the changes should
    all
  • be local. Again, there are a number of
    things
  • that could be done to make the routine
  • more efficient.
  • /
  • double angle

12
  • glMatrixMode(GL_MODELVIEW)
  • glPushAttrib(GL_ALL_ATTRIB_BITS)
  • glColor3d(color0, color1, color2)
  • glLineStipple(patternScale, pattern)
  • glEnable(GL_LINE_STIPPLE)
  • glLineWidth(lineWidth)
  • glBegin(GL_LINE_LOOP)
  • for (angle 0.0 angle lt 2.0PI angle
    2.0PI/36)
  • glVertex3d(sin(angle)radiuscenter0,
  • cos(angle)radiuscenter1,center2)
  • glEnd()
  • glBegin(GL_LINE_LOOP)
  • for (angle 0.0 angle lt 2.0PI angle
    2.0PI/36)
  • glVertex3d(sin(angle)radiuscenter0,cente
    r1,
  • cos(angle)radiuscenter2)
  • glEnd()
  • glBegin(GL_LINE_LOOP)
  • for (angle 0.0 angle lt 2.0PI angle
    2.0PI/36)
  • glVertex3d(center0,sin(angle)radiuscente
    r1,

13
  • /
  • Pyramid is an object made up of polygons (in
    this case
  • trianbles) and displayed. It is again set up
    as a
  • demonstration of the use of attributes.
  • Pyramid -- default constructor
  • display -- displays the object in the
    current window.
  • isFilled -- returns true if the triangles for
    the pyramid
  • are currently set to filled
    mode. False
  • otherwise.
  • setFilled -- sets the triangle mode to
    filled for the
  • pyramid.
  • setUnfilled -- sets the triangle mode to
    hollow/unfilled
  • for the pyramid
  • setColor -- takes the color as a set of
    red/green/blue
  • values for the pyramid
  • /

14
  • class Pyramid
  • private
  • float vertices63
  • bool filled
  • float color3
  • public
  • Pyramid()
  • // The default constructor sets up a simple
    pyramid with
  • // one vertex at the origin and the color used
    as white.
  • // It is initially unfilled or hollow.
  • int vertex, coord
  • int initialVertices3 0.0, 0.0, 0.0,
  • 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
    0.0,
  • 0.0, 1.0, 1.0, 0.0, 0.0, 0.0
  • filled false
  • color0 color1 color2 1.0
  • for ( vertex 0 vertex lt 6 vertex)
  • for ( coord 0 coord lt 3 coord )
  • verticesvertexcoord initialVerticesvertex
    coord

15
  • // Access Routines
  • bool isFilled ()
  • // returns true if the current mode for the
    pyramid is
  • // filled. Returns false otherwise.
  • return filled
  • void setFilled()
  • // Sets the polygon mode to filled for the
    pyramid
  • filled true
  • void setUnfilled()
  • // Sets the polygon mode to unfilled for the
    pyramid
  • filled false
  • void setColor (float red, float green, float
    blue)
  • // Set the color to the values sent. Will take
    effect

16
  • void display()
  • / This routine draws the pyramid. It tries to
    save and
  • restore any current attributes using the
  • glPushAttrib()and glPopAttrib() pair of
    calls.
  • This could be shortened using vector calls
    in OpenGL
  • and will probably be modified to do so in
    the future.
  • /
  • int vertex
  • glMatrixMode (GL_MODELVIEW)
  • glPushAttrib(GL_ALL_ATTRIB_BITS)
  • if (isFilled())
  • glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  • else
  • glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  • glColor3f(color0, color1, color2)
  • glBegin(GL_TRIANGLE_STRIP)
  • for (vertex 0 vertex lt 6 vertex)
  • glVertex3f( verticesvertex0,
    verticesvertex1, verticesvertex2)

17
  • // My objects are currently being stored as
    global variables.
  • Pyramid testObject
  • Hoops loopy
  • void initialize()
  • // Set up a orthonormal view and a clear color
    (black).
  • glClearColor(0.0, 0.0, 0.0, 0.0)
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(-3.0, 3.0, -3.0, 3.0, -3.0, 3.0)
  • glMatrixMode(GL_MODELVIEW)
  • void display()
  • // Clear the screen and display the two objects.
  • glMatrixMode(GL_MODELVIEW)
  • glClear(GL_COLOR_BUFFER_BIT)
  • testObject.display()

18
  • void lineMenuHandler(int value)
  • // The handler for the line menu. just gets the
  • // different values and calls the member
    routines.
  • // Again, the real problem here is that the
    routine
  • // needs to be able to get an object to
    manipulate.
  • switch (value)
  • case 1
  • loopy.setColor(1.0, 0.0, 0.0)
  • break
  • case 2
  • loopy.setColor(0.0, 0.0, 1.0)
  • break
  • case 3
  • loopy.setColor(0.0, 1.0, 0.0)
  • break

19
  • case 10
  • loopy.setPattern(0xffff)
  • break
  • case 11
  • loopy.setPattern(0xf00f)
  • break
  • case 12
  • loopy.setPattern(0xcccc)
  • break
  • case 20
  • loopy.setLineWidth(8.0)
  • break
  • case 21
  • loopy.setLineWidth(4.0)
  • break
  • case 22
  • loopy.setLineWidth(1.0)
  • break
  • default

20
  • int createLineMenu()
  • // Create the menu for the line attributes.
    This allows some
  • // of the line attributes to be modified while
    running. It
  • // may have been better to change some of the
    values around // to make handling this easier.
  • int id
  • id glutCreateMenu(lineMenuHandler)
  • glutAddMenuEntry("Red", 1)
  • glutAddMenuEntry("Blue", 2)
  • glutAddMenuEntry("Green", 3)
  • glutAddMenuEntry("Solid Lines", 10)
  • glutAddMenuEntry("Dashed Lines", 11)
  • glutAddMenuEntry("Dotted Lines", 12)
  • glutAddMenuEntry("Thick Lines", 20)
  • glutAddMenuEntry("Medium Lines", 21)
  • glutAddMenuEntry("Thin Lines", 22)
  • return id

21
  • void polygonMenuHandler(int value)
  • // Simple minded handler to take care of the
    menu choices
  • // from the polygon attributes menu. A little
    more judicious
  • // choice of values for the menu items may have
    simplified
  • // this.
  • switch (value)
  • case 1
  • testObject.setFilled()
  • break
  • case 2
  • testObject.setUnfilled()
  • break
  • case 10
  • testObject.setColor(0.0, 1.0, 1.0)
  • break
  • case 11
  • testObject.setColor(1.0, 0.0, 1.0)
  • break
  • case 12

22
  • int createPolygonMenu()
  • // Create a menu for polygon attributes and
    return its
  • // ID to the calling routine.
  • int id
  • id glutCreateMenu(polygonMenuHandler)
  • glutAddMenuEntry("Filled", 1)
  • glutAddMenuEntry("Unfilled", 2)
  • glutAddMenuEntry("Cyan", 10)
  • glutAddMenuEntry("Magenta", 11)
  • glutAddMenuEntry("Yellow", 12)
  • return id

23
  • int main (int argCount, char argValues)
  • glutInit(argCount, argValues)
  • glutInitDisplayMode(GLUT_RGBA)
  • glutInitWindowSize(600, 600)
  • glutInitWindowPosition(20, 20)
  • glutCreateWindow("Main Window")
  • initialize()
  • // Create and attach the two menus controlling
    some of the// attributes
  • createLineMenu()
  • glutAttachMenu(GLUT_LEFT_BUTTON)
  • createPolygonMenu()
  • glutAttachMenu(GLUT_RIGHT_BUTTON)
  • // Move the "loopy" object to the side so we can
    see both // items
  • loopy.setCenter(-1.0, -1.0, 0.0)
  • glutDisplayFunc(display)
  • glutMainLoop()
  • return EXIT_SUCCESS

24
Project 1
  • Write a C application using OpenGL and GLUT to
    draw a representation of a locomotive.
  • The locomotive must be 3 dimensional
  • Not all details need to be included we are
    attempting to get something that is recognizable
    as a locomotive not something we could build a
    locomotive from.
  • You pick the coordinate system
  • Graduate Students
  • Must have 3 windows 1 with the top view, 1 with
    the profile view, and 1 with the front view.

25
Some Locomotive Examples
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com