assignments-hw2answers

Transformations Written Answer key

Question 1

OpenGL provides a “rectangle” command that draws a rectangle in the XY plane (e.g. Z=0):

    glRectf(float x1, float y1, float x2, float y2);

Give a function that draws a rectangle on the XZ plane (Y=0). It should use glRectf and some of the OpenGL transformation functions. You should not use glLoadMatrix (that is, you should build the transformations using the translation, rotation, etc. commands)

One possible solution:
void rectXZ(float x1, float z1, float x2, float z2)
{
glRotatef(90, 1, 0, 0);
glRectf(x1, z1, x2, z2);
}

Question 2

The standard OpenGL coordinate system (called “Normalized Device Coordinates”) put 0,0 at the center of the screen. Has X going to the right, Y going up, and Z coming out of the screen (its right-handed). The range along each axis is from -1 to 1 (so -1,-1,0 is the lower left corner of the screen). You get this coordinate system when you have the identity matrix atop the matrix stack (using glLoadIdentity).

Suppose we want a transformation that makes the lower left corner of the screen (0,0,0) and the upper right corner of the screen (640,480,0). (the Z values should not be changed). There is an OpenGL function called glOrtho for doing this, but we won’t use it in the this question.

Part A Provide a sequence of openGL commands that set up this coordinate system (it should be a sequence of translations, rotations, and scales).

glTranslatef(-1.0, -1.0, 0);
glScalef(1.0 / 320.0, 1.0 / 240.0, 1.0);

Part B Reverse the order of the transformations in your answer to 2A. This program would give a different result. Change the parameters to the functions so that it gives the same transformation as in 2A.

glScalef(1.0 / 320.0, 1.0 / 240.0, 1.0);
glTranslatef(-320.0, -240.0, 0.0);

Question 3

Similar to the previous question, but this time we want a coordinate system that places (0,0,0) at the upper left corner, and (640,480,0) in the lower right. We still want the positive Z axis to point forward (out of the screen) - this is no longer a right handed coordinate system.

Part A Write a sequence of OpenGL transformations (rotate, translate, scale) that achieves this.

glTranslatef(-1.0, 1.0, 0.0)
glScalef(1.0 / 320.0, -1.0 / 240.0, 1.0)
note the negative scale factor in the y-direction
(this will switch the handed-ness of the coordinate system)

Part B Give the 4x4 matrix from this transformation. Hint: its probably easier to figure out the matrix than to multiply the transforms from part A.

1 / 32000-1
0- 1 / 24001
0010
0001

Part C: For this matrix to do the right thing, the initial transform on the matrix stack had to be the identity. If the matrix were the identity and we used the transformations from parts A and B, the following would draw a square in the center of the screen:

    glRectf(310,230,330,250)

Suppose we “applied” this transformation twice (either we repeated the lines of code from part A or we multiplied the matrix from part B on top of the stack twice). Where would the rectangle command draw?

two opposite corners would be:
(-10241/10240, 5759/5760, 0) and
(-10239/10240, 5761/5760, 0)

(you might notice that this is the bug in the sample code for the practice assignment)

Question 4

Consider a 3D transformation M (a 4x4 homogeneous coordinate matrix) that is created by composing a rotation and a uniform scale. M maps the unit X vector to (0,1,1), and the unit Y vector to (0,1,-1).

Part A: Where does M map the unit Z vector to?

(-sqrt(2), 0, 0)

Part B: What is the amount of the scale used to make M?

sqrt(2)

Hint: You don’t want to figure out what the angles of the rotation are. In fact, you might not need to figure out what the transformation is.

Question 5

Here’s a little open GL function that draws a “house shaped” object that is articulated:

    void glHouseArm(float angle1, float angle2) {
        glPushMatrix();
        glRectf(0,0,1,1);
        glTranslatef(0,1,0);
        glRotatef(angle1,0,0,1);
        glRectf(0,0,1,1);
        glTranslatef(0,1,0);
        glRotatef(angle2,0,0,1);
        glBegin(GL_TRIANGLES);
        glVertex2f(0,0);
        glVertex2f(1,0);
        glVertex2f(.5,.5);
        glEnd();
        glPopMatrix();
    }

Part A: Sketch what this would look like if angle1 = angle2 = 0. What would the position of the tip of the triangle be? (0.5, 2.5)

Part B: Sketch what this would look like if angle1 = 90, and angle2 = 45. What would the position of the tip of the triangle be? (-1-sqrt(2)/2, 1)

Part C: Write an equation that computes the position of the tip of the triangle given the two angles.

x = [cos(angle1)*cos(angle2) - cos(angle1)*sin(angle2)-sin(angle1)*sin(angle2)-sin(angle1)*cos(angle2)]/2 - sin(angle1)

y = 1 + [sin(angle1)*cos(angle2)-sin(angle1)*sin(angle2)+cos(angle1)*sin(angle2)+cos(angle1)*cos(angle2)]/2+cos(angle1)

Page last modified on October 19, 2008, at 06:12 PM