Computer Graphics: Lecture 9: Matrix Stacks and 3dtransformations
Computer Graphics: Lecture 9: Matrix Stacks and 3dtransformations
Computer Graphics: Lecture 9: Matrix Stacks and 3dtransformations
Graphics
Lecture 9: Matrix Stacks and 3DTransformations
Using Translation and Rotation together
(X , Y,+Z) (X , Y,-Z)
+z -z
+X -Y
Drawing a 3d object using 3d coordinates
float A1X=200, A1Y=-200,A1Z=200, A2Z=200, A2X=400, A2Y=-200, A3X=300,
A3Y=0,A3Z=200, A4Y=-150, A4X=200, A4Z=-200;
//RIGHT TRIANGLE
glBegin(GL_LINE_LOOP);
glVertex3f(A1X,A1Y,A1Z);
glVertex3f(A3X,A3Y,A3Z);
glVertex3f(A4X,A4Y,A4Z);
glEnd();
Translating triangle
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3.0);
glLoadIdentity();
CrossCenter();
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,A4Z);
glLoadIdentity();
glTranslatef(-600,0,0);
glColor3f(1,0,0);
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,A4Z);
glFlush();
}
Translating two triangles on Z axis
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3.0);
glLoadIdentity();
CrossCenter();
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,A4Z);
glLoadIdentity();
glTranslatef(0,0,-600);
glColor3f(1,0,0);
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,A4Z);
glFlush();
}
Idle Call Back Function
Use for animation and continuous update
Can use glutTimerFunc or timed callbacks for animations as well
glLoadIdentity();
glRotatef(Angle,0,1,0);
glTranslatef(0,0,-600);
glColor3f(1,0,0);
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,
A4Z);
glFlush();
}
Reshape Callback Function
Used to change the window size at run time
}
Model view and projection matrix stacks
glPopMatrix ( void )
Pops the top matrix off the stack, destroying the contents of the
popped matrix. What was the second-from-the top matrix becomes the
top matrix.
Modelview Matrix stacks
glRotatef(all,0,0,1); glRotatef(90,0,0,1);
glPushMatrix();//left bottom Wing
glPushMatrix();//base ball glTranslatef(-1,0,0);
glTranslatef(0,0,0); glColor3f(1,1,1);
glColor3f(1,1,1); glScalef(2,0.5,-0.5);
glScalef(0.6,0.5,0.7); glutSolidSphere(0.5,50,50);
glutSolidSphere(0.5,50,50); glPopMatrix();
glPopMatrix();//base ball
One more Wing
Rotating the Windmill on Keyboard
Event
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'A':
all = (all + 10) % 360;
glutPostRedisplay();
break;
case 'a':
all = (all + 10) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
Drawing Multiple windmills in a scene
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glTranslatef(-1,-1,-1);
windmill();
glColor3f(0,1,0);
glTranslatef(2,1,-1);
windmill();
glColor3f(1,1,0);
glTranslatef(3,2,-1);
windmill();
glColor3f(1,0,1);
glTranslatef(4,2,-1);
windmill();
glutSwapBuffers();
}
Drawing a Solar System
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glutWireSphere(0.4, 20, 16); /* draw sun */
glTranslatef (0.7, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutWireSphere(0.1, 10, 8); /* draw smaller
planet */
glPopMatrix();
glFlush();
}
Adding another Planet
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glutWireSphere(0.4, 20, 16); /* draw sun */
glTranslatef (0.7, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutWireSphere(0.1, 10, 8); /* draw smaller planet */
glColor3f(1,0,0);
glTranslatef (-1.4, 0.0, 0.0);
glutWireSphere(0.1,10,18);
glPopMatrix();
glFlush();}
Rotation using Keyboard Event Handler
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default: break;}}
Making a Robot Arm
void display(void)
{ glPushMatrix();//outter push
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);