Docsity

Scene Graph - Introduction to Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download Scene Graph - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

Lecture 14:

Scene Graph

Refresher: OpenGL Matrix

Transformation Pipeline

  • Input: list of 3D coordinates (x, y, z)
  • GL_MODELVIEW
    • Model transform (earlier)
    • View transform
  • GL_PROJECTION
    • Projection transform
    • Clipping
    • Perspective division
  • Viewport
    • Viewport transform
  • Output: 2D coordinates for each

input 3D coordinates

In Summary!

โ€ข The full projection matrix: ๐‘€๐‘๐‘๐‘†๐‘ฅ๐‘ฆ๐‘ง๐‘…๐‘ข๐‘ฃ๐‘ง2๐‘ฅ๐‘ฆ๐‘ง๐‘‡๐‘ข๐‘ฃ๐‘ค:

1

tan ๐œƒ๐‘ค 2 ๐‘“๐‘Ž๐‘Ÿ

0 0 0

0

1

tan ๐œƒ 2 โ„Ž ๐‘“๐‘Ž๐‘Ÿ

0 0

0 0

1 ๐‘“๐‘Ž๐‘Ÿ

0 0 0 0 1

โ€ข Important!! Remember to homogenize your points

after you apply this transform

Putting Everything All Together

  • We know about camera and object modeling transformations now, letโ€™s

put them together:

    1. ๐‘๐‘๐‘’๐‘Ÿ๐‘ ๐‘๐‘’๐‘๐‘ก๐‘–๐‘ฃ๐‘’ = ๐‘€๐‘๐‘๐‘€๐‘๐‘’๐‘Ÿ๐‘ ๐‘๐‘’๐‘๐‘ก๐‘–๐‘ฃ๐‘’
    1. ๐ถ๐‘€๐‘‡๐‘€ = ๐‘†๐‘…๐‘‡
    • The CMTM (Composite Modeling Transformation Matrix) is a composite matrix
of all of our object modeling transformations (Scaling, Rotating, Translations,
etc)
    1. ๐ถ๐‘‡๐‘€ = ๐‘๐‘๐‘’๐‘Ÿ๐‘ ๐‘๐‘’๐‘๐‘ก๐‘–๐‘ฃ๐‘’ โˆ— ๐ถ๐‘€๐‘‡๐‘€
    • The CTM (Composite Transformation Matrix) is the combination of all our
camera and modeling transformations
  • In OpenGL it is referred to as the ModelViewProjection Matrix
  • Model: Modeling Transformations
  • View: Camera translate/rotate
  • Projection: Frustum scaling/unhinging

Applying Composite Matrices

Matrix M = T * (R * S);

for (i=0; i<numTriangles; i++) {

Triangle t = MyObject->triangles[i]; glBegin(GL_TRIANGLES); for (j=0; j<3; j++) { Position p = t->vertex[j]->GetPosition(); Position newP = M * p; glVertex3d (newP.x, newP.y, newP.z); } glEnd();

}

โ€ข Yikes! Matrix multiplication in software. SLOW!

Applying Composite Matrices with

OpenGL

Matrix M = T * (R * S);

glLoadMatrixd(M.getValues());

for (i=0; i<numTriangles; i++) {

Triangle t = MyObject->triangles[i];

glBegin(GL_TRIANGLES);

for (j=0; j<3; j++) {

Position p = t->vertex[j]->GetPosition();

glVertex3d (newP.x, newP.y, newP.z);

glEnd();

  • Much betterโ€ฆ Now weโ€™re using the graphics card to do the

computation for us.

Be careful! Read the
OpenGL spec to see
how you are
arranging your
matrix orientation
and make sure that
itโ€™s consistent with
OpenGL.
You might need to
do a transpose

Drawing Multiple Objects

โ€ข Letโ€™s say that you now have 2 objects, myCube

and mySphere.

โ€ข Each object needs to be transformed

differently:

โ€“ For myCube, the transforms are ๐‘€๐‘ = ๐‘‡๐‘๐‘…๐‘๐‘†๐‘

โ€“ For mySphere, the transforms are: ๐‘€๐‘  = ๐‘‡๐‘ ๐‘…๐‘ ๐‘†๐‘ 

โ€ข Letโ€™s see how we will write this code:

Drawing Multiple Objects

glMatrixMode(GL_PROJECTION);

glLoadMatrixd(myCamera->getProjectionMatrix().getValues());

glMatrixMode(GL_MODELVIEW);

glLoadMatrixd(myCamera->getModelViewMatrix().getValues());

Matrix Mc = Tc * (Rc * Sc);

glMultMatrixd(Mc.getValues());

myCube.draw();

Matrix Ms = Ts * (Rs * Ss);

glMultMatrixd(Ms.getValues());

mySpher.draw();

Drawing Multiple Objects

glMatrixMode(GL_PROJECTION);

glLoadMatrixd(myCamera->getProjectionMatrix().getValues());

glMatrixMode(GL_MODELVIEW);

glLoadMatrixd(myCamera->getModelViewMatrix().getValues());

Matrix Mc = Tc * (Rc * Sc);

glMultMatrixd(Mc.getValues());

myCube.draw();

Matrix Mc_Inv = Mc.Inverse();

glMultMatrixd(Mc_Inv.getValues());

Matrix Ms = Ts * (Rs * Ss);

glMultMatrixd(Ms.getValues());

mySpher.draw();

What aboutโ€ฆ

Drawing Multiple Objects

glMatrixMode(GL_PROJECTION);
glLoadMatrixd(myCamera->getProjectionMatrix().getValues());
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(myCamera->getModelViewMatrix().getValues());
glPushMatrix();
Matrix Mc = Tc * (Rc * Sc);
glMultMatrixd(Mc.getValues());
myCube.draw();
glPopMatrix();
glPushMatrix();
Matrix Ms = Ts * (Rs * Ss);
glMultMatrixd(Ms.getValues());
mySpher.draw();
glPopMatrix();

Hierarchical Transform

โ€ข 3D objects are often constructed in a way that

have dependency

โ€“ This means that an objectโ€™s position, orientation,

etc. will depend on its parents position,

orientation, etc.

upper arm

Hierarchical Transform

โ€ข While you can compute how the hammer will

move based on lower armโ€™s rotation, this is

really a pain.

โ€“ Youโ€™ll need to calculate the rotation of the lower

arm.

โ€“ Followed by the upper arm.

โ€“ And finally the hammer. upper arm

Basic Approach

โ€ข Letโ€™s say that you want

to move the Robotic Arm

to the right (in +x axis)

by 5 units, and the upper

arm rotated by some

amount.

โ€“ Using the Scene Graph,

this could mean that we

iteratively apply:

  • The same Translation to

all the children of the root

node (base)

  • The same Rotation to all

the children of the upper

arm

Relative Transformation

โ€ข In this sense, all the transformations are

relative to an objectโ€™s parents / ancestors.