CS 381  >  Test 2 Solutions

CS 381, Fall 2003
Test 2 Solutions

Problem 1 [4 pts total]

Scalar, point, vector. Classify each of the following quantities as a scalar, point, or vector quantity. In each part, circle the correct answer.

1a. [1 pt]

Location.

Answer

Scalar Point Vector

1b. [1 pt]

Force.

Answer

Scalar Point Vector
Force has a direction.

1c. [1 pt]

Brightness.

Answer

Scalar Point Vector

1d. [1 pt]

Velocity.

Answer

Scalar Point Vector
Velocity has a direction; speed does not.

Problem 2 [5 pts total]

Vector computations. Let u = (–1, 0, 1), and let v = (0,–1,–2). Compute each of the following. Partial credit can be given only if work is shown.

2a [1 pt]

u + v.

Answer

u + v = (–1, 0, 1) + (0,–1,–2) = ([–1]+0, 0+1, 1+2) = (–1, 1, 3).

2b [1 pt]

3u.

Answer

3u = 3(–1, 0, 1) = (3·[–1], 3·0, 3·1) = (–3, 0, 3).

2c [1 pt]

u · v.

Answer

u · v = (–1, 0, 1) · (0,–1,–2) = [-1]·0 + 0·1 + 1·2 = 0 + 0 + 2 = 2.

Remember that the dot product of two vectors is a scalar.

2d [1 pt]

|u|.

Answer

|u| = |(–1, 0, 1)| = sqrt[(–1)2 + 02 + 12] = sqrt[1 + 0 + 1] = sqrt[2].

Remember that the length of a vectors is a scalar.

2e [1 pt]

Find the unit vector whose direction is the same as that of u.

Answer

In the previous part, we found |u| = sqrt[2]. So now we multiply 1/sqrt[2] by u:
[1/sqrt[2]] u = [1/sqrt[2]] (–1, 0, 1) = ([1/sqrt[2]]·[–1], [1/sqrt[2]]·0, [1/sqrt[2]]·1) = (–1/sqrt[2], 0, 1/sqrt[2]).

An easy check: a correct answer must have length 1.


Problem 3 [3 pts total]

Homogeneous form.

3a [1 pt]

Express the vector (1, 2, 3) in homogeneous form.

Answer

We add a 4th coordinate equal to 1: (1, 2, 3, 1).

3b [2 pts]

What does homogeneous form allow us to do that is difficult/impossible/inconvenient with normal 3-D vectors?

Answer

Homogeneous form allows us to express all the usual transformations in the same format: as a 4×4 matrix, where a transformation is applied using 4-D matrix-vector multiplication, and the final answer is determined using a 4th-coordinate division.

Problem 4 [2 pts]

The function call “glTranslated(2., 3., 4.);” constructs a transformation. How is this transformation represented internally? Give the numbers.

Answer

The transformation is represented as a 4×4 matrix. The matrix stored will be the current matrix multiplied by the following.
(
 1  0  0  2
0 1 0 3
0 0 1 4
0 0 0 1
)

Problem 5 [3 pts]

A certain C++ function, prototyped as “void drawobject();”, draws an object using OpenGL. Write C++ code that uses this function to draw the object in red, triple the usual size, rotated 20° (you pick the axis!), and moved to the point (3, 4, 5). Your code should make no permanent changes to any OpenGL transformations. ou may assume that drawobject is written in a “reasonable” manner, that it uses no OpenGL color or lighting commands, and that the matrix mode is GL_MODELVIEW.

Answer

glPushMatrix();
   glColor3d(1., 0., 0.);      // red; this can go anywhere BEFORE the drawobject call
   glTranslated(3., 4., 5.);   // Comes BEFORE rotate & scale
   glRotated(20., 0., 0., 1.); // Last 3 params can be anything but 0, 0, 0
   glScaled(3., 3., 3.);
   drawobject();
glPopMatrix();


Problem 6 [4 pts total]

VR and Immersion.

6a [2 pts]

In VR, what is meant by “immersion”?

Answer

Immersion means filling the senses with the output of the VR environment. Generally, we refer primarily to the visual sense, in which case immersion means filling the user’s vision with the visual output from the VR display, so that essentially nothing of the “real world” can be seen.

This is generally done either by surrounding the user with large screens or using a head-mounted display that blocks the real world.

6b [2 pts]

How does immersion help to create the sense of “presence”?

Answer

Presence is the users’ feeling that they are inside the virtual environment. Immersion helps create this by blocking external stimuli that would remind users where they actually are.

Problem 7 [5 pts total]

Picking via Selection Mode.

7a [3 pts]

Briefly explain how picking via “selection mode” works.

Answer

In selection mode, OpenGL does no rendering; rather, names are inserted into the pipeline along with primitives, and OpenGL creates a list of the names of those primitives that were not discarded during clipping.

To do picking via selection mode, set the clipping region to be a very small rectangle around the pointing-device location. Give each object a name, and send this name through the pipeline with each primitive making up the object. Then the list returned by OpenGL is the list of all objects that are at the mouse position. The closest such, if any, is the object picked.

7b [2 pts]

Give one advantage of this method over simple extent testing.

Answer

Selection-mode picking works well regardless of how complex the shapes of objects are. Thus, it is more suitable than extent testing when dealing with complex shapes and 3-D objects, whose extents may be difficult to describe.


Problem 8 [3 pts total]

Matrices and Vectors.

8a [2 pts]

Perform the following matrix-vector multiplication.
(
 1  0  0  0
0 1 0 0
0 0 1 0
0 0 k 0
) (
x
y
z
1
) .

Answer

(
 1  0  0  0
0 1 0 0
0 0 1 0
0 0 k 0
) (
x
y
z
1
)  =  (
x + 0·y + 0·z + 0·1
x + 1·y + 0·z + 0·1
x + 0·y + 1·z + 0·1
x + 0·y + k·z + 0·1
)  =  (
x
y
z
kz
) .

8b [1 pt]

The result of the above transformation will be converted to something else before it emerges from the pipeline. What quantity will emerge? (This was explained in more detail at the time of the test.)

Answer

What will emerge is a 3-D vector. We compute this from the above homogeneous form using a 4th-coordinate division.
(
x
y
z
kz
)  →  (
x / kz
y / kz
z / kz
)  =  (
x / kz
y / kz
1 / k
) .

Problem 9 [4 pts total]

Copying Widgets.

9a [2 pts]

Why might it be problematic for a C++ widget class to have a copy constructor?

Answer

One reason this might a problem is if a widget is copied during the time it is handling a mouse click. For example, for a button, what happens if it is copied while it is depressed? Are there now two depressed buttons?

Other answers are possible.

9b [2 pts]

Give a solution to this problem. (Hint: “Don’t declare a copy constructor” is not a solution, because then the compiler will write one for you.)

Answer

The solution that I used was to disable the copy constructor (and, for the same reason, copy assignment). This is done by declaring the copy constructor private, and then not defining (implementing) it.

Note: Strictly speaking, it is enough simply to declare the copy constructor private, whether or not we write the function body. Then nothing except for class members and friends can use it; if these do not use it, then it never gets used. However, since we do not want to use it at all, it is safer to do both: declare private and do not define. This way, it definitely never gets used, even accidentally by our own code. And, in any case, there is no point in writing the function body if we never want to call the function.


Problem 10 [4 pts total]

Widgets and Inheritance.

10a [2 pts]

How can object-oriented inheritance and polymorphism be used effectively when creating many different types of widgets?

Answer

Create a widget base class, with virtual member functions for each action that every type of widget should be able to do: draw itself, handle a mouse click, etc. Then actual widget classes will all be derived from this base class. This method allows one to deal with a widget, without knowing what type of widget it is.

10b [2 pts]

How can these ideas simplify the design and use of a “widget manager”.

Answer

If the above scheme is used, then a widget manager can deal with every widget as if it were an instance of the base class. The widget manager does not need to know anything about the properties of the various types of widgets, and new types of widgets can be added without altering the manager.

Problem 11 [3 pts total]

Transformations in the Pipeline. On their way through the OpenGL geometry pipeline, points & vectors pass through three transformations.

11a [1 pt]

In which phase of the pipeline do these transformations occur?

Answer

The transformations occur in what I have named the “vertex processing” phase.

11b [2 pts]

Name the three transformations. For each, briefly explain how it is typically used.

Answer

Model/view
Used to place objects (and the camera and lights) in the world.
Projection
Used to project the scene onto a 2-D plane, usually while maintaining depth information.
Viewport
Used to map the projected image into the frame buffer.


Problem 12 [4 pts total]

Moving Objects and the Camera.

12a [2 pts]

Suppose we want to move an object forward (–z direction) 3 units. Give the (single) OpenGL function call that will accomplish this, and explain where in the code this call should occur.

Answer

glTranslated(0., 0., -3.);
This call should occur just before the object is drawn.

12b [2 pts]

Suppose we want to move the camera forward (–z direction) 3 units. Give the (single) OpenGL function call that will accomplish this, and explain where in the code this call should occur. Be careful!

Answer

glTranslated(0., 0., 3.);  // Positive!
This call should occur at the beginning of the transformation-setting code.

Problem 13 [3 pts]

We divided advanced viewing interfaces into two categories. For each category, name it, and indicate how being in this category affects the internal implementation of an interface.

Answer

View the world
In interfaces in this category, the action takes place at the location being viewed. Thus, the translation moving all objects out to where they can be seen belongs permanently at the beginning of the transformation-setting code, and so should generally not be included in a saved viewing matrix.
Move in the world
In interfaces in this category, the action takes place at the camera location. Thus, the translation moving all objects out to where they can be seen belongs with the other camera motions, and so should generally be included in a saved viewing matrix.

Problem 14 [3 pts]

Briefly explain how we can use projection techniques to draw shadows.

Answer

One way to think of a shadow is as the projection of the shape of an object onto another object. Thus, if we generate the appropriate projection, and include it as part of the model/view transformation, then we can use the code that draws the object to draw its shadow as well.

That is the basic idea. There are other issues to deal with, of course: coloring the shadow properly, depth-buffering errors, how to project onto a non-planar object, how to clip the shadow so it lies within the boundaries of the object it is projected on, etc.


CS 381, Fall 2003: Test 2 Solutions / Last update: 17 Nov 2003 / Glenn G. Chappell / ffggc@uaf.edu