Hierarchical Modeling & Constructive Solid Geometry COM337 COMPUTER GRAPHICS Hierarchical Modeling & Constructive Solid Geometry
Introduction When defining a complex object or system, it is easier to first specify the subparts, then, describe how the subparts come together E.g., a bicycle can be described by first describing a frame, wheels, fenders, handlebars, seat, chain, and pedals then positioning these components to form the bicycle
Basic Modeling Concepts Modeling is the creation and manipulation of a system representation A representation of a system is called a model of the system A model can be graphical or purely descriptive (such as a set of equations that describe relationships of system parameters) Graphical models are often called geometric models We will use the term “model” mostly to mean a geometric model
System Representations The example graphical representation of a logic circuit on the right illustrates features common to many system models Component parts are shown as geometric structures (symbols) Relationships between symbols are represented with a network of connecting lines
System Representations Repeated positioning of a few basic symbols (such as the two and gate symbols) is a common method for building complex models Each such occurrence of a symbol in a model is called an instance of that symbol In many cases symbols are dictated but in other cases symbols can be any convenient geometric pattern
System Representations The information needed to construct and manipulate a model can be stored in data structures, and/or can be specified with procedures In general, a model specification will contain both data structures and procedures, although some models are defined completely with data structures and others use only procedural specifications In solid modeling, information from data structures (defining coordinates and other things) is used with few procedures On the other hand, a weather model may need mostly procedures to calculate plots of temperature and pressure variations.
Symbol Hierarchies Many models can be organized as a hierarchy of symbols Basic elements for the model can be used to form composite objects (modules), which themselves can be grouped to form higher level objects, and so on …
Hierarchical Models Suppose that we wish to build a model of an automobile that we can animate The model can be composed of 5 parts (the chassis and the four wheels) each of which is described using standard primitives
Hierarchical Models 2 frames of a simple animation of the model are shown below We can write a program to generate this animation by noting that if each wheel has a radius r, then a 360-degree rotation of a wheel corresponds to the car moving forward (or backward) a distance of 2πr Program could contain one function to generate each wheel and another to generate the chassis All functions could use the same input such as the desired speed and direction of the car
Hierarchical Models This program is just the kind that we do NOT want to write It is linear and does not show the relationships among the components of the car There are 2 relationships that we want to exploit We cannot separate the movement of the car from the movement of the wheels All wheels are identical and merely located in different places with different orientations
Hierarchical Models
Hierarchical Models
Stack-Based Traversal in Application Code Imagine that we will use a function called figure to do the drawing The MV matrix, M, when this function is called will determine the position of the figure The root node (first node) will draw the torso (using M) Next node is the head: function head will be called with MV updated to MMh Then, as we continue with left-upper arm, left-lower arm, right arm, left leg, and right leg, MV will be updated to MMlua, MMluaMlll, Mrua, …
Stack-Based Traversal in Application Code After processing a node, if we go back up to its parent, we need to also roll back the transformation matrix. This can easily be done by making use of a stack to store (push) copies of matrices and by recovering with popping. Our code will have transformations mixed with pushes and pops of the MV matrix
Stack-Based Traversal in Application Code var modelViewMatrix = mat4(); var mvStack = [ ]; function figure() { mvStack.push(modelViewMatrix); torso(); modelviewMatrix = mult(modelViewMatrix, translate); modelViewMatrix = mult(modelViewMatrix, rotate); head(); modelViewMatrix = mvStack.pop(); leftUpperArm(); modelViewMatrix = mvStack.pop(); mvStack.push(modelViewMatrix); modelviewMatrix = mult(modelViewMatrix, translate); modelViewMatrix = mult(modelViewMatrix, rotate); leftLowerArm(); rightUpperArm(); ⁞
Stack-Based Traversal in Application Code function torso() { instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5*torsoHeight, 0.0)); mult(instanceMatrix, scale4(torsoWidth, torsoHeight, torsoWidth)); gl.uniformMatrix4fv( modelViewMatrixLoc, false, flatten(instance)); for (var i = 0; i < 6; ++i) { gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4); } Functions for individual parts can be as on the right The textbook first explains a simpler example for a robot arm with 3 parts Have a look at that example first and try to complete this hierarchical model If you can complete it, you already have the code for one of the bonuses in the 2nd assignment
Stack-Based Application Code vs. Alternative The example approach has limitations The code relies on programmer to deal with stacks (push and pops) The code developed for the example object will not easily extend to another object Building the model and rendering it is all together, we’d prefer to separate them A more general and powerful method to work with hierarchies is to implement the tree as a data structure and to render it with a traversal algorithm (See textbook)
Hierarchical Models Summary Combine smaller/simpler shapes to construct complex objects and scenes Stored in trees or similar data structures Operations are based on traversal of the tree Can keep other information together with the geometry in tree such as transformations, attributes, bounding boxes, etc.
Scene Graphs Directed Acyclic Graphs (DAGs) to represent scenes and complex objects Nodes: Grouping nodes, Transform nodes, Level of Detail (LoD) nodes, Attribute nodes, State nodes Leaves: Geometric object descriptions
Constructive Solid Geometry (CSG) Combine multiple shapes with set operations (intersection, union, set difference) to construct new shapes
CSG
Implementing CSG Ray casting methods are used for rendering and finding properties of volumes constructed with CSG Rays are intersected with objects Intersections are sorted according to the distance Depending on the operation, extents of the new object can be found
Implementing CSG Ray casting methods are used for rendering and finding properties of volumes constructed with CSG Rays are intersected with objects Intersections are sorted according to the distance Depending on the operation, extents of the new object can be found