Homework 4

Modified
  1. Transformations:
  1. Determine the translation and rotations necessary to rotate by 45° about the line through points P0=(1,4,2) and P1=(2,6,5). Give the rotation solution as sequence of matrix operations and rotation parameters (see the matrix definition on page 214),
  2. Use Octave or other package (Matlab, Mathematica, etc.) to perform operations to produce the concatenated matrix. Verify that point (1.25, 4.5,2.75) is a fixed point in the above transformation (e.g. using Octave). To verify a fixed point, multiply the point by the transformation and you should get the same point as a result.
  3. Rotate the colored cube defined in faces() below in C++ code (see that below for handling hidden surfaces correctly). Define individual transformation matrices concatenated by OpenGL matrix multiplications (i.e. glMultMatrix) and printing of final transformation matrix,
  4. Rotate the colored cube defined in faces() below in C++ code using individual OpenGL rotations about appropriate axis's (i.e. multiple glRotate, etc.) and printing of final transformation matrix,
  5. Implement transformations between coordinate systems where the new basis from x, y and z vectors is: u1=-x, u2=-y, u3=-z; and define the transformation matrix M and (MT)-1 that converts between x, y, z representation and u1, u2, u3. For either c or d above:
    1. convert using M, display cube and print CTM,
    2. convert again using (MT)-1, display cube and print CTM,
    3. explain the results.

 

#include <GL/glut.h>

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
    {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
    {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
    {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
    {1.0,0.0,1.0}, {0.5,0.0,0.5}, {0.0,1.0,1.0}};

void polygon(int a, int b, int c, int d, int color)
{/* draw a polygon via list of vertices */

  glBegin(GL_POLYGON);
    glColor3fv(colors[color]);
    glVertex3fv(vertices[a]);
    glColor3fv(colors[color]);
    glVertex3fv(vertices[b]);
    glColor3fv(colors[color]);
    glVertex3fv(vertices[c]);
    glColor3fv(colors[color]);
    glVertex3fv(vertices[d]);
  glEnd();
}

void faces() {   /* map vertices to faces */

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  polygon(4,5,6,7,1); // front red
  polygon(0,3,2,1,0); // back black
  polygon(2,3,7,6,2); // top yellow
  polygon(0,4,7,3,3); // left green
  polygon(1,2,6,5,4); // right blue
  polygon(0,1,5,4,7); // bottom blue green
  glFlush();
}

 int main(int argc, char **argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
   glutInitWindowSize(500, 500);
   glutCreateWindow("HW4");
   glutDisplayFunc(faces);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-4.0,4.0, -4.0, 4.0, -4.0, 4.0);
   glMatrixMode(GL_MODELVIEW);
   glEnable(GL_DEPTH_TEST);
   glClearColor (1.0, 1.0, 1.0, 0.0);
   glutMainLoop();
}

 

  1. Extend the program that displays the Earth orbiting the Sun. The base display program is available for download; pressing the 'y' key rotates the blue sphere about the yellow sphere center, 'd' rotates the blue sphere about its center.
    1. Change the spheres to colored cubes of appropriate sizes in order to better visual the transformations.
    2. Rotate the Earth system about the z-axis along a line from the center of the Sun to the point clicked with the left mouse button. This effectively tilts the view of the orbital plane to align with the location of the left mouse click.
    3. Rotate the system about the x-axis by pressing the 'x' key for forward steps and 'X' for negative steps. Combinations of 'y' and 'x' should allow viewing the system from the top (bottom) as the Earth circles the Sun. This transformation is concatenated to the translation of part b above.
    4. Add the Moon (cube) to the system, always keeping the same face to the Earth as the Earth rotates (d key). Rotate the Moon about its y-axis when the 'm' key is pressed.
    5. Add a timer that rotates the sun, earth and moon; the keys rotate the earth and moon only. Rotate the sun in the opposite direction of the earth's rotation, keeping the same face of the moon toward the earth.

    There are several useful cube manipulation examples given in Appendix A (Download Appendix A sample programs) or from Question 1 above.

 

Hints

Octave has a fairly robust programming interface that allows matrix functions to be easily defined. For example, the matrix of operations for rotations about the z-axis can be defined simply as:

function result = Rz(t)

   result = [cos(t),  -sin(t),  0,  0;
                   sin(t),  cos(t),   0,  0;
                   0,         0,           1,  0;
                   0,         0,           0,  1];
endfunction

Rz(45*pi/180)

ans =
0.70711 -0.70711 0.00000 0.00000
0.70711 0.70711 0.00000 0.00000
0.00000 0.00000 1.00000 0.00000
0.00000 0.00000 0.00000 1.00000

Saving and Editing

The history of commands entered can be written out to a file, edited and read in by:

history -w '\\directory\\filename'

Use Word, etc. to edit history, write out as text file. Then read into Octave and execute by:

source('\\directory\\filename')

 

EMAIL

  1. To  notifying that assignment is complete with subject: YOUR NAME: B481 and Homework 4.
  2. Place in the OnCourse drop-box a zip file with name HW4.zip containing two directories:
    1. Directory named HW4-1 for Question 1:
      • contains typed answers as a text, Word or HTML document of:
        • 1a,
        • results from 1b,
        • printed transformation matrices for 1c and 1d
        • explanation of 1c and 1d differences, if any.
        • results and explanation of 1e.
      • contains all code files (include exe) for Question 1c, 1d and 1e.
    2. Directory named HW4-2 for Question 2:
      • contains all code files (include exe) for Question 2.
      • Comments on this style of programming for larger models and suggestions for improvement.
  3. Include instructions on executing.