Computer Graphics: Lecture 9: Matrix Stacks and 3dtransformations

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Computer

Graphics
Lecture 9: Matrix Stacks and 3DTransformations
Using Translation and Rotation together

First rotate, then translate =>

First translate, then rotate =>


Three Dimensional Coordinates

Drawing using glVertex3f(x ,y ,z )


Z Axis Object Placement
+Y -X

(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;

//BOTTOM TRIANGLE FACE //FRONT FACE TRIANGLE


glBegin(GL_LINE_LOOP);
glBegin(GL_LINE_LOOP);
glVertex3f(A1X,A1Y,A1Z);
glVertex3f(A1X,A1Y,A1Z); glVertex3f(A2X,A2Y,A2Z);
glVertex3f(A2X,A2Y,A2Z); glVertex3f(A3X,A3Y,A3Z);
glEnd();
glVertex3f(A4X,A4Y,A4Z);
glEnd();

//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

void idle( void )


{
/* change something */
t += dt;
glutPostRedisplay();
}
glutIdleFunc( idle );//Called in Main
Using Idle Callback function
GLfloat Angle=0;
void idle()
{
Angle+=0.01;
if(Angle>360)
{
Angle=Angle-360;
}
glutPostRedisplay();
}
glRotatef(Angle,0,1,0);//Rotate the object continuously
without ant keyboard function
Using Idle Callback function
GLfloat Angle=0;
void idle()
{
Angle+=0.01;
if(Angle>360)
{
Angle=Angle-360;
}
glutPostRedisplay();
}
glRotatef(Angle,0,1,0);//Rotate the object continuously
without ant keyboard function
Using Idle Callback function
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,0);
glTranslatef(0,0,600);
Original1(A1X,A1Y,A1Z,A2X,A2Y,A2Z,A3X,A3Y,A3Z,A4X,A4Y,
A4Z);

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

void reshape (int w, int h)


{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
}
glutReshapeFunc(reshape);//Register Callback
Matrix Stacks
OpenGL uses matrix stacks mechanism to manage transformation
hierarchy. OpenGL provides matrix stacks for each type of supported
matrix to store matrices.

Model-view matrix stack(Stack used when drawing/modeling)


Projection matrix stack(Stack used when setting camera values)

Current matrix is always the topmost matrix of the stack When we


manipulate the current matrix, we actually manipulate the topmost matrix.
We can control the current matrix by using push and pop operations.
Enabling Matrix Stacks
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-640.0, 640.0, -480.0, 480.0,480,-480);
glMatrixMode(GL_MODELVIEW);

}
Model view and projection matrix stacks

Push and Pop Functions are used to


manipulate model-view Matrix stack
Manipulating Matrix stacks
glPushMatrix ( void )
Pushes all matrices in the current stack down one level. The topmost
matrix is copied, so its contents are duplicated in both the top and
second-from-the top matrix.

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

Top most value of the


Stack is the current
matrix of size 4x4
Manipulating Modelview Matrix stacks by using
PushMatrix() function
Code
glPushMatrix();
glTranslatef(0,0,0);
glutSolidCube(1.0);

glPushMatrix() function Pushes all


matrices in the current stack down one
level. The topmost matrix is copied, so its
contents are duplicated in both the top
and second-from-the top matrix.
Manipulating Modelview Matrix stacks by using
PushMatrix() function
Code
glPushMatrix();
glTranslatef(0,0,0);
glutSolidCube(1.0);

glPushMatrix() function Pushes all


matrices in the current stack down one
level. The topmost matrix is copied, so its
contents are duplicated in both the top
and second-from-the top matrix.
Manipulating Modelview Matrix stacks by using
PopMatrix() function
Code
glPushMatrix();
glTranslatef(0,0,0);
glutSolidCube(1.0);
glPopMatrix();

glPopMatrix() function 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.
.
Drawing a Windmill
Drawing a Windmill
Objects used for drawing

•Three glutSolidSphere() functions for the wings

•One Solid sphere function for the center base

•One glutSolidCube() function for the vertical cube


Drawing a Windmill
glPushMatrix(); glRotatef(30,0,0,1);
glPushMatrix();//Drawing the bottom cube glPushMatrix();//right bottom Wing
glTranslatef(0,-1.5,-0.2); glTranslatef(1,0,0);
glScalef(0.2,3,0); glColor3f(1,1,1);
glColor3f(1,1,1); glScalef(2,0.5,-0.5);
glutSolidCube(1.0); glutSolidSphere(0.5,50,50);
glPopMatrix(); glPopMatrix();

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);

glPushMatrix();//inner push 1 of Shoulder


glScalef (2.0, 0.4, 1.0);//Same Size of both elbow and Shoulder
glutWireCube (1.0);//Shoulder Cube
glPopMatrix();//pop 1

glTranslatef (1.0, 0.0, 0.0);


glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();//inner push 2 of shoulder


glScalef (2.0, 0.4, 1.0);//Same Size of both elbow and Shoulder
glutWireCube (1.0);//Elbow Cube
glPopMatrix();//pop 1
glPopMatrix();
Making the Shoulder and elbow move
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay();
break;
case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
THANKS!
Any questions?
You can find me at anumkaleem@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy