COMP 175: Computer Graphics March 10, 2015

Slides:



Advertisements
Similar presentations
Today Composing transformations 3D Transformations
Advertisements

Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
Graphics Pipeline.
Viewing and Transformation
Transformations Objectives Understand how transformations work in 2D and 3D Understand the concept of homogenous coordinate system Understand scene graphs.
Objectives Learn to build arbitrary transformation matrices from simple transformations Learn to build arbitrary transformation matrices from simple transformations.
OpenGL (II). How to Draw a 3-D object on Screen?
2IV60 Computer Graphics 2D transformations
2D Transformations. World Coordinates Translate Rotate Scale Viewport Transforms Hierarchical Model Transforms Putting it all together.
Hierarchical Transformations and Models CSE 3541 Matt Boggus.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology 3D Engines and Scene Graphs Spring 2012.
Hierarchical Transformations Hierarchical Models Scene Graphs
CS 4731: Computer Graphics Lecture 9: Introduction to 3D Modeling Emmanuel Agu.
Hierarchy, Modeling, and Scene Graphs Angel: Chapter 10 OpenGL Programming and Reference Guides, other sources. ppt from Angel, AW, etc. CSCI 6360/4360.
C O M P U T E R G R A P H I C S Stuff Jian Chen January 15, 2010 Transformations 1/28 Geometric Transformations By Andries van Dam.
Objectives Review some advanced topics, including Review some advanced topics, including Chapter 8: Implementation Chapter 8: Implementation Chapter 9:
C O M P U T E R G R A P H I C S Stuff CMSC 435 / 634 Transformations 1/29 Geometric Transformations Readings: Chapters 5-6.
COMP 175: Computer Graphics March 24, 2015
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.
The Viewing Pipeline (Chapter 4) 5/26/ Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel.
Geometric Transformations Jehee Lee Seoul National University.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
Geometric transformations The Pipeline
Data Structures for Scenes, The Basics of Scene Graphs Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday,
Foundations of Computer Graphics (Fall 2012) CS 184, Lectures 13,14: Reviews Transforms, OpenGL
Week 4 Lecture 1: Hierarchical Modeling Part 1 Based on Interactive Computer Graphics (Angel) - Chapter 10 1 Angel: Interactive Computer Graphics 5E ©
Hierarchical Modeling. Instance Transformation Start with a prototype object (a symbol) Each appearance of the object in the model is an instance – Must.
CS559: Computer Graphics Lecture 15: Hierarchical Modeling and Shading Li Zhang Spring 2008.
1 Scene Graphs ©Anthony Steed Scene Graph Overview n Building Scene Structures n Traversal n Examples n Instancing and Re-Use n More Transformations.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 4: Transformations 2
Computing & Information Sciences Kansas State University Lecture 19 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 19 of 42 William H. Hsu.
1 Scene Graphs Week 4.2 ©Anthony Steed
Modeling with OpenGL Practice with OpenGL transformations.
Jul 25, 2014IAT 3551 Scene Graphs.  A data structure that stores information about a graphics scene –Each node has information that structures the interpretation.
111/16/ :14 UML Instance Transformation x y z x y z x y z x y z SRT Model Coordinates.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
Transformations Tutorial
1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.
2 COEN Computer Graphics I Evening’s Goals n Discuss viewing and modeling transformations n Describe matrix stacks and their uses n Show basic geometric.
The Camera Analogy ► Set up your tripod and point the camera at the scene (viewing transformation) ► Arrange the scene to be photographed into the desired.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
Geometric Transformations Sang Il Park Sejong University Many slides come from Jehee Lee’s.
Introduction to Computer Graphics: Viewing Transformations Rama C
University of Texas at Austin CS384G - Computer Graphics Fall 2010 Don Fussell Hierarchical Modeling.
CS552: Computer Graphics Lecture 4: 2D Graphics. Recap 2D Graphics Coordinate systems 2D Transformations o Translation o Scaling o Rotation Combining.
Computer Graphics Matrices
CS559: Computer Graphics Lecture 13: Hierarchical Modeling and Curves Li Zhang Spring 2010.
OpenGL and You I Cast, Therefore I Am. Ray Casting Idea is simple, implementation takes some work –Cast rays as if you were the camera –Determine intersection.
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Computer Graphics I, Fall 2008 Hierarchical Modeling I.
CSCE 441: Computer Graphics: Hierarchical Models Jinxiang Chai.
OpenGL LAB III.
- Introduction - Graphics Pipeline
IAT 355 Scene Graphs Feb 23, 2017 IAT 355.
CS451Real-time Rendering Pipeline
Computer Graphics - Hierarchical Models-
Hierarchical and Object-Oriented Graphics
CSCE 441: Computer Graphics: Hierarchical Models
Chapter 4/5 glMatrixMode Modeling Transformations glTranslate glScale
Chapters 5/4 part2 understanding transformations working with matrices
Computer Graphics (Spring 2003)
Projection in 3-D Glenn G. Chappell
Hierarchical and Object-Oriented Graphics
Hierarchical Modeling & Constructive Solid Geometry
Geometric Objects and Transformations (II)
Viewing/Projections I Week 3, Fri Jan 25
Dr. Chih-Kuo Yeh 葉智國 Computer Graphics Dr. Chih-Kuo Yeh 葉智國
Hierarchical Modeling
CSCE 441: Computer Graphics: Hierarchical Models
Presentation transcript:

COMP 175: Computer Graphics March 10, 2015 Lecture 07: Scene Graph COMP 175: Computer Graphics March 10, 2015

Refresher: OpenGL Matrix Transformation Pipeline Input: list of 3D coordinates (x, y, z) GL_MODELVIEW Model transform View transform GL_PROJECTION Projection transform Clipping Perspective division Viewport Viewport transform Output: 2D coordinates for each input 3D coordinates

Normalization Transformation - Composite To recap, to normalize a parallel view volume into canonical form, we perform 3 steps: Translate the view volume to the origin Rotation from (u, v, w) to (x, y, z) Scale into (-1, 1) range in x, y and (0, -1) in z Together, we can write this as: 𝑀 𝑜𝑟𝑡ℎ𝑜𝑔𝑜𝑛𝑎𝑙 = 2 𝑤𝑖𝑑𝑡ℎ 0 0 0 0 2 ℎ𝑒𝑖𝑔ℎ𝑡 0 0 0 0 1 𝑓𝑎𝑟 0 0 0 0 1 𝑢 𝑥 𝑢 𝑦 𝑢 𝑧 0 𝑣 𝑥 𝑣 𝑦 𝑣 𝑧 0 𝑤 𝑥 𝑤 𝑦 𝑤 𝑧 0 0 0 0 1 1 0 0 −𝑃 𝑛 𝑥 0 1 0 −𝑃 𝑛 𝑦 0 0 1 −𝑃 𝑛 𝑧 0 0 0 1

In Summary! The full projection matrix: 𝑀 𝑝𝑝 𝑆 𝑥𝑦𝑧 𝑅 𝑢𝑣𝑧2𝑥𝑦𝑧 𝑇 𝑢𝑣𝑤 : 1 0 0 0 0 1 0 0 0 0 −1 𝑐+1 𝑐 𝑐+1 0 0 −1 0 1 tan 𝜃 𝑤 2 𝑓𝑎𝑟 0 0 0 0 1 tan 𝜃 ℎ 2 𝑓𝑎𝑟 0 0 0 0 1 𝑓𝑎𝑟 0 0 0 0 1 𝑢 𝑥 𝑣 𝑥 𝑤 𝑥 0 𝑢 𝑦 𝑣 𝑦 𝑤 𝑦 0 𝑢 𝑧 𝑣 𝑧 𝑤 𝑧 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 0 − 𝑒 𝑥 − 𝑒 𝑦 − 𝑒 𝑧 1 Where: Theta_w is the camera angle in the x direction Theta_h is the camera angle in the y direction Only one of the two needs to be specified. The other can be inferred from the screen’s aspect ratio. In my implementation, camera angle = Theta_h c = -near/far

Putting Everything All Together We know about camera and object modeling transformations now, let’s put them together: 1) 𝑁 𝑝𝑒𝑟𝑠𝑝𝑒𝑐𝑡𝑖𝑣𝑒 = 𝑀 𝑝𝑝 𝑀 𝑝𝑒𝑟𝑠𝑝𝑒𝑐𝑡𝑖𝑣𝑒 2) 𝐶𝑀𝑇𝑀=𝑆𝑅𝑇 The CMTM (Composite Modeling Transformation Matrix) is a composite matrix of all of our object modeling transformations (Scaling, Rotating, Translations, etc) 3) 𝐶𝑇𝑀= 𝑁 𝑝𝑒𝑟𝑠𝑝𝑒𝑐𝑡𝑖𝑣𝑒 ∗𝐶𝑀𝑇𝑀 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

What is the Camera’s ModelView Matrix? glMatrixMode(GL_PROJECTION); glLoadMatrixd(myCamera->getProjectionMatrix()); glMatrixMode(GL_MODELVIEW); glLoadMatrixd(myCamera->getModelViewMatrix()); Matrix M = T-1 * (R * S) * T; glMultMatrixd(M.getValues()); DrawObject(); Hrm.. The camera’s modelview transform is really indistinguishable from the object’s transform matrix as far as OpenGL is concerned. Specifically, they are the same glTranslate, glRotate calls!

Camera as a model (0,5,0) (-1,5,0) (0,5,0) (0,0,0) (0,0,0) (1,0,0) For rendering, the middle and right models create the same image. Meaning that we can either think of model transform and view transforms as Inverses of each other

This means that we have to be careful with our coordinate spaces. For this class, there are 3 coordinate spaces that we care about: World Coordinate Space Camera Coordinate Space Object Coordinate Space

For Example: Matrix M = T-1 * (R * S) * T; glMultMatrixd(M.getValues()); DrawObject(); When DrawObject is called, it assumes an Object Coordinate Space That is, when you draw your sphere, cube, cone, cylinder, these objects are drawn at the origin and with specific size and orientation Matrix M transforms a point from the Object Coordinate Space into the World Coordinate Space

In the Case of Camera… The ModelView matrix: 𝑢 𝑥 𝑢 𝑦 𝑢 𝑧 0 𝑣 𝑥 𝑣 𝑦 𝑣 𝑧 0 𝑤 𝑥 𝑤 𝑦 𝑤 𝑧 0 0 0 0 1 1 0 0 −𝑃 𝑛 𝑥 0 1 0 −𝑃 𝑛 𝑦 0 0 1 −𝑃 𝑛 𝑧 0 0 0 1 These matrices bring a data point from the World Coordinate Space into the Camera Coordinate Space

Canonical Camera Coordinate To further bring a point from World Coordinate Space into a Canonical Camera Coordinate Space That is, to “scale” the camera space into a pre-defined size (of -1 to 1) 2 𝑤𝑖𝑑𝑡ℎ 0 0 0 0 2 ℎ𝑒𝑖𝑔ℎ𝑡 0 0 0 0 1 𝑓𝑎𝑟 0 0 0 0 1

Which Space Does a Matrix transform To and From? This is often the most confusing part of 3D graphics. A good way to think about it is to think about the following operation (M is a matrix): Point p = M * origin For example, consider the (Ortho) ModelView Matrix: 𝑀= 𝑢 𝑥 𝑢 𝑦 𝑢 𝑧 0 𝑣 𝑥 𝑣 𝑦 𝑣 𝑧 0 𝑤 𝑥 𝑤 𝑦 𝑤 𝑧 0 0 0 0 1 1 0 0 −𝑃 𝑛 𝑥 0 1 0 −𝑃 𝑛 𝑦 0 0 1 −𝑃 𝑛 𝑧 0 0 0 1 In this case, when applied to the origin, you get back negative (inverse) of the camera’s eye point. Think about it for a second… So let’s say that your camera is at (2, 0, 0) After applying this matrix to the origin, you get back (-2, 0, 0) So it’s as if the coordinate system shifted such that the camera goes to (0, 0, 0) As such, the matrix must be a transform from the world space (xyz) to the camera (uvw) space

Best Thing about Matrices If M transforms from A to B coordinate systems Then M-1 is the inverse that transforms from B to A coordinate systems This is important!! Keep this in mind because for the rest of the class, we will be navigating through coordinate systems! Why? Because certain mathematical operations are MUCH easier in certain spaces. For example, Ray-Object intersection

Refresher: OpenGL Matrix Transformation Pipeline Input: list of 3D coordinates (x, y, z) GL_MODELVIEW Model transform View transform GL_PROJECTION Projection transform Clipping Perspective division Viewport Viewport transform Output: 2D coordinates for each input 3D coordinates

Questions?

Applying Composite Matrices Here’s the thought: Assume that you are given a Transformation Matrix in the form of 𝑇 𝑥𝑦𝑧 𝑅 𝜃 𝑥 𝜃 𝑦 𝜃 𝑧 𝑆 𝑥𝑦𝑧 , and you want to apply it to one of the objects that you created in shapes. How would you do this without the use of OpenGL?

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 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. See Algebra.H for how this is done 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.

Applying Composite Matrices with Camera glMatrixMode(GL_PROJECTION); glLoadMatrixd(myCamera->getProjectionMatrix().getValues()); glMatrixMode(GL_MODELVIEW); glLoadMatrixd(myCamera->getModelViewMatrix().getValues()); Matrix M = T * (R * S); glMultMatrixd(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();

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 Ms = Ts * (Rs * Ss); glMultMatrixd(Ms.getValues()); mySpher.draw(); What is wrong with this?

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(); Matrix Ms = Ts * (Rs * Ss); glMultMatrixd(Ms.getValues()); mySpher.draw();

Drawing Multiple Objects – Being Paranoid… glMatrixMode(GL_PROJECTION); glLoadIdentity(); glLoadMatrixd(myCamera->getProjectionMatrix().getValues()); glMatrixMode(GL_MODELVIEW); glLoadMatrixd(myCamera->getModelViewMatrix().getValues()); glPushMatrix(); Matrix Mc = Tc * (Rc * Sc); glMultMatrixd(Mc.getValues()); myCube.draw(); glPopMatrix(); Matrix Ms = Ts * (Rs * Ss); glMultMatrixd(Ms.getValues()); mySpher.draw();

Questions?

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

Hierarchical Transform If we think of the Robot Arm as a hierarchy of objects… Then an object’s position and orientation will be based on its parents, grand parents, grand grand parents, etc. This hierarchical representation is called a “Scene Graph”

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.

We had seen earlier how the OpenGL Matrix Stack could work. Recall earlier, when we tried to draw 2 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();

OpenGL Matrix Stack Combine that with the use of glPushMatrix and glPopMatrix note: CTM = current transformation matrix

OpenGL Matrix Stack drawRobotArm() { // push to copy the CTM so we can restore it at the end of the routine glPushMatrix(); // CTM2 = CTM1 Matrix T = trans_mat(5,0,0); glMultMatrix(T); // CTM2 = CTM2 * T drawBase(); Matrix R = rot_mat(‐90, 0,1,0); glPushMatrix(); // CTM3 = CTM2 glMultMatrix(R); // CTM3 = CTM3*R drawLowerArm(); drawUpperArm(); drawHammer(); glPopMatrix(); // pop off CTM3 glPopMatrix(); // pop off CTM2 // now back in the base coordinate system of CTM1 }

As you might have noticed, each object in the Robot Arm can have: Scene Graph As you might have noticed, each object in the Robot Arm can have: An object description (cone, sphere, cube, etc.) A transformation description (scale, rotate, translate) Additional attribute information (such as appearance information: color, texture, etc.) One parent (and ONLY one parent) Some number of children (or none at all)

A Slightly More Complex Case ROBOT transformation upper body lower body head trunk arm base stanchion In this case, notice that there are groupings of objects… So we’ll also need to consider an “abstract” node such that we can apply a transform to it.

Scene Graph Represents a transformation We will iteratively apply the transforms “upwards”, starting at the bottom…

Applying Transforms 1. Leaves of the tree are standard (unit size) primitives

Applying Transforms 2. We apply (local) transforms to them 1. Leaves of the tree are standard (unit size) primitives

Applying Transforms 3. This results in sub-groups 2. We apply (local) transforms to them 1. Leaves of the tree are standard (unit size) primitives

Applying Transforms 4. We apply the transform to the sub-groups 3. This results in sub-groups 2. We apply (local) transforms to them 1. Leaves of the tree are standard (unit size) primitives

Transformation and Scene Graphs As discussed earlier, this results in a cumulative effect as one moves up the tree. In OpenGL: For o1: CTM = 𝑀 1 For o2: CTM = 𝑀 2 𝑀 3 For o3: CTM = 𝑀 2 𝑀 4 𝑀 5 For a vertex v in o3, v’ in world coordinate would be: 𝑣 ′ =𝑀 2 𝑀 4 𝑀 5 𝑣 object nodes (geometry) transformation nodes group nodes 𝑴 𝟏 𝑴 𝟑 𝑴 𝟐 𝑴 𝟒 𝑴 𝟓

Reusing Objects / Trees group3 obj3 obj4 𝑻 𝟓 𝑻 𝟔 𝑻 𝟒 root 𝑻 𝟎 group1 𝑻 𝟏 𝑻 𝟐 obj1 𝑻 𝟑 group2 obj2 You can create reusable sub-trees group3 is used twice in the example above The transforms within group3 don’t change (T5 and T6) but in each instance on the lft it is applied with different CTMs 𝑇 0 𝑇 1 vs. 𝑇 0 𝑇 2 𝑇 4

Representing the SceneGraph as a SceneFile There is a bit of intricacy in the SceneGraph, can we represent it as a text-based SceneFile? Note a few properties: The Scene Graph is a DAG (directed acyclic graph) The structure is inherently hierarchical There are components that should be reusable There are three main types: Transform nodes Object nodes Group nodes

One Example: Scene Description Language (SDL) def leg{ push translate 0 .15 0 scale .01 .15 .01 cube pop } def table{ translate 0 .3 0 scale .3 .01 .3 translate .275 0 .275 use leg translate 0 0 -.55 translate -.55 0 .55 Pop Calling this object: push translate 0.4 0 0.4 use table pop

Using XML Assignment 4 uses an XML structure. Here’s an example of what your scenefile will look like: <transblock> <rotate x="0" y="1" z="0" angle="60"/> <scale x=".5" y=".5" z=".5"/> <object type="tree"> <translate x="0" y="2" z="0"/> <scale x="1" y=".5" z="1"/> <object type="primitive" name="sphere"> <diffuse r="1" g="1" b="0"/> </object> </transblock>

Questions?