CS 381  >  Week 5 Review Problems

CS 381, Fall 2003
Week 5 Review Problems

Below are some problems related to the material covered in class recently. Answers are in the Answers section, below. Do not turn these in. We may certainly discuss any of these problems in class, if you want.

You can expect Test 2 (on Friday, November 7) to contain problems similar to some of these.

Problems

Note: On the test you will be expected to do problems like these without books or notes.
  1. What, specifically, does OpenGL selection mode allow you to do? Hint: Nothing to do with picking, really.
     
  2. What is a “selection buffer” (or “select buffer”)?
     
  3. What is the effect of a glColor* command on primitives drawn while selection mode is in effect? Hint: Think about it ....
     
  4. a. Briefly explain how to picking via “selection mode” works. b. Give one advantage of this method over extent testing.
     
  5. a. When using selection mode, what change needs to be made to the code that draws objects in the scene? b. What is the main OpenGL function involved in this change that will occur in the scene-drawing code?
     
  6. We are writing a doubly buffered GLUT program that uses selection mode to do picking. Therefore, we write a scene-drawing function that is called from both the display function and the picking function. The following actions should probably be performed outside this scene-drawing function. For each, explain why.
    1. Clearing the viewport.
    2. Flushing.

     
  7. a. What is a “widget”? b. List 4 common types of widgets.
     
  8. What implications does a 2 1/2-D interface have when designing the appearance of a widget?
     
  9. a. Why might it be problematic for a C++ widget class to have a copy constructor or a copy assignment operator? b. Give a solution to this problem.
     
  10. In the Button class, along with many/most other well-written C++ classes, the declaration is kept in a separate file from most of the code. Why is this done?
     
  11. Why is the ability to hide itself a good (and very nearly required) capability for a widget to have?
     
  12. How does a “widget manager” make life easier for programmers?
     
  13. a. How can C++ inheritance/polymorphism be used effectively when creating many different types of widgets? b. How can these ideas simplify the design and use of a widget manager?
     
  14. Suppose we are writing a program to draw an animated scene. How can we make sure that objects in our scene move at the same speed on all systems?
     
  15. Your friend Egbert thinks “glRotated” is a past-tense verb. Comment, please.
     
  16. List the three main OpenGL transformation commands, and briefly explain what each does.
     
  17. Briefly explain the use of glPushMatrix and glPopMatrix that was discussed in class.
     
  18. Suppose you are drawing a square. The four glVertex2d calls have paramters (–1, –1), (1, –1), (1, 1), and (–1, 1). Now, write code that draws this square expanded by a given size factor (stored in the variable factor), but still uses the glVertex2d calls given above. You may assume that “glMatrixMode(GL_MODELVIEW)” has been done.
     

Answers

  1. OpenGL selection mode allows you to get a list of the names of those primitives that were not discarded during clipping.
  2. The selection buffer is an array of GLuint’s in which OpenGL will store the results of selection mode: the hit records generated.
  3. Of course, glColor* sets the current drawing color. This has no effect on primitives drawn while selection mode is in effect.
    1. To do picking via selection mode,
      • Set the clipping region to be a small area around the mouse position.
      • Set selection mode and draw the scene, giving names to the relevant objects.
      • Exit selection mode, and get a list of the names of those objects that were not discarded during clipping.
      • One of these (generally the front-most one) is the object picked.
    2. Here are two advantages of selection-mode picking over extent testing:
      • Existing drawing code (+ names) is all that is necessary to describe the scene; no need to figure out the extent of an object.
      • Easier to do for objects with complex shapes, 3-D objects, etc.
    1. When using selection mode, “names” need to be given to primitives.
    2. The primary OpenGL function we use to give an object a name is glLoadName. Another acceptable answer would be glPushName.
    1. We should not clear the viewport in the scene-drawing function, because this function is called by the picking code, and we do not want to clear the viewport there.
    2. This program is doubly buffered. Thus, in our display function, we need to flush with glutSwapBuffers. However, in our picking function we do not want to swap; we flush with glFlush. Since we want to flush in two different ways, depending on where the scene-drawing function is called, we will need to flush outside that function.
    Note: We could pass a bool to the scene-drawing function, telling whether we are actually displaying or not (it is not a GLUT callback; we get to decide the parameter list; wow!). If so, clear the viewport and swap. If not, just flush. In things were done this way, then these operations would be performed inside the scene-drawing function.
    1. A widget (or control) is a region on the screen that responds to mouse actions in a way that sets options, performs actions, etc.
    2. Here are six common types of widgets:
      • Button
      • Checkbox
      • Radio buttons
      • Text box
      • Menu pop-up
      • Scroll bar
  4. That an interface is 2 1/2-D suggests that “in” (away from the user) and “out” (toward the user) should have some visual meaning. For example, when a button is clicked, it could have a pressed-in appearance, in contrast to its normal pushed-out appearance. Similar ideas apply to checkboxes, menu pop-ups, etc.
    1. If a widget class has a copy constructor or copy assignment operator, then a problematic situation occurs when a copy of a widget is made while the widget is being used. For example, suppose the widget is a button, and it is is pressed in. Are there now two buttons that are pressed in?
    2. The solution I chose for the Button class was to disable the copy constructor and copy assignment operator by declaring them private and not defining them. If you can think of another solution, I would love to hear it.
  5. Keeping declaration separate from implementation is a good thing because:
  6. Here are two reasons why the ability to hide is a very good thing for a widget to be able to do.
  7. Widget managers make life easier because they help ... well ... manage widgets. In particular, they generally make it much easier to add new widgets to a program: just tell the manager about the new widgets, and all the bookkeeping details are taken care of.
    1. A good use of inheritance/polymorphism in the design of widgets is to create an abstract base widget class, from which many concrete classes, implementing specific widgets, can be derived. Member functions that all widgets share (display, for example), can be implemented as virtual functions.
    2. Inheritance & polymorphism simplify the design and use of a widget manager, because they make it possible for a manager to function without knowing what kind of widgets it is managing. It simply holds a number of base-class pointers; all function calls are handled via virtual dispatch. Thus, when creating a new widget, one simply gives the manager its address; no other information is required. And new kinds of widgets can be created and used without telling the manager about them.
  8. We can keep speed system-independent by using the system clock. Each time a frame is generated, read the clock and subtract the prior reading, resulting in the elapsed time since the previous frame. Then move all objects in the scene by an amount proportional to the elapsed time.
  9. Egbert is wrong again. The “d” “glRotated” (and “glTranslated” and “glScaled”) means GLdouble, just as it does in “glVertex2d”. Another form is “glRotatef”.
  10. The three major OpenGL transformation commands are:
    glTranslate*
    Translate, that is, move all points by a certain amount in a certain direction.
    glRotate*
    Rotate about a line through the origin.
    glScale*
    Scale (resize) about the origin.
  11. When we set up and use an OpenGL transformation, we generally surround the code with a glPushMatrix-glPopMatrix pair. The first command saves the old transformation; the second restores it after we are done. This prevents our transformation code from messing up other parts of the program (or messing up itself).
  12. glPushMatrix();  // Assume model/view mode and save transformation
       glScaled(factor, factor, factor);  // Resize square
       glBegin(GL_QUADS);  // Draw the square in the usual way
          glVertex2d(-1, -1);
          glVertex2d( 1, -1);
          glVertex2d( 1,  1);
          glVertex2d(-1,  1);
       glEnd();
    glPopMatrix();  // Restore old transformation


CS 381, Fall 2003: Week 5 Review Problems / Last update: 27 Oct 2002 / Glenn G. Chappell / ffggc@uaf.edu