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
- 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:
- ๐๐๐๐๐ ๐๐๐๐ก๐๐ฃ๐ = ๐๐๐๐๐๐๐๐ ๐๐๐๐ก๐๐ฃ๐
- ๐ถ๐๐๐ = ๐๐
๐
- The CMTM (Composite Modeling Transformation Matrix) is a composite matrix
of all of our object modeling transformations (Scaling, Rotating, Translations,
etc)
- ๐ถ๐๐ = ๐๐๐๐๐ ๐๐๐๐ก๐๐ฃ๐ โ ๐ถ๐๐๐
- 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:
all the children of the root
node (base)
the children of the upper
arm
Relative Transformation
โข In this sense, all the transformations are
relative to an objectโs parents / ancestors.