Homework 4 |
Modified: |
- 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),
- 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.
- 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,
- 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,
- 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:
- convert using M, display cube and print CTM,
- convert again using (MT)-1, display cube and print CTM,
- 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();
}
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];
endfunctionRz(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.00000Saving 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')