0% found this document useful (0 votes)
7 views17 pages

Hierarchical Modeling

Done

Uploaded by

projectprinting1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

Hierarchical Modeling

Done

Uploaded by

projectprinting1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

2/19/15

Computer  Graphics  
 

Hierarchical  Modeling  

Review: Model to World Frame

M=TRS

1  
2/19/15  

Modeling  with  Transforma9ons  


• The  robot  you  built  so  far  looks  
great  –  un9l  you  try  to  make  it  
move.  

Modeling  with  Transforma9ons  


• When  you  want  to  change  
something,  the  model  falls  apart  
• Reason:  robot  is  constrained,  but  
your  model  is  not  
• Need  a  set  of  parameters  that  
make  it  easy  to  move  our  robot  
through  legal  configura9ons  
– Convenient  for  sta9c  models,  
and  vital  for  anima9on!  
• Key  is  to  structure  the  
transforma9ons  using  a  hierarchy  

2  
2/19/15  

Model  à  World  Scenario    


q Posi9on  and  orient  the  robot  hammer  in  the  world  space:  

upper
arm

lower
arm

hammer
base

Absolute  Transforma9ons    
q Each  part  of  the  object  is  transformed  independently  rela9ve  to  
the  origin:  

Translate  base  by  (5,0,0)  


Translate  lower  arm  by  (5,0,0)  
Translate  upper  arm  by  (5,0,0)  
Translate  hammer  by  (5,0,0)  
…  

3  
2/19/15  

Rela9ve  Transforma9ons    
q BeUer:  transform  each  object  rela-ve  to  its  parent  

Step  1:  Translate  the  base  and  its  descendants  by  (5,0,0)  

Rela9ve  Transforma9ons    

Step  2:  Rotate  the  lower  arm  and  its  descendants  by  -­‐90  degrees  
   about  the  local  y  axis  

4  
2/19/15  

Hierarchical  Transforms  

Ar9culated  Models  
• Rigid  parts  connected  by  joints   rla!

• By  modeling  joint  angles,  you  can     rua!

 do  anima9ons   lua!

• Ques9on  is,  given  all  the  join  angles,     lla!

 how  do  you  draw  the  model?  


rul
!
lul
!

rll
!
lll
!

l-- left!
r-- right!
-u- upper!
-l- !lower!
--a !arm!
--l !leg!

5  
2/19/15  

Making  an  Ar9culated  Arm  


S b
β

a=c
a b c
A B α
A

• A  minimal  2D  jointed  object:  


– Two  pieces,  A  (“forearm”)  and  B  (“upper  arm”)  
– AUach  point  c  on  B  to  point  a  on  A  (“elbow”)  
• Desired  parameters:  
– shoulder  posi9on  S  (point  at  which  b  winds  up)  
– shoulder  angle  β  (A  and  B  rotate  together  about  b)  
– elbow  angle  α  (A  rotates  about  a  =  c)  

Making  an  Arm,  step  1   S b


β

a=c

b c α
A
B want  

a A α

B
A
b a=c

• Start  with  A  and  B  in  their  untransformed  configura9ons    


• First  apply  a  series  of  transforma9ons  to  posi9on  A  
rela-ve  to  its  parent  B  

6  
2/19/15  

Making  an  Arm,  step  2   S b


β

a=c

α
A

• Translate  by  -­‐a,  bringing  a  to  the  origin  

a
A B
  A
a
 
• You  can  see  B  peeking  out  from  behind  A  

Making  an  Arm,  step  3   S b


β

a=c

α
A

• Next,  we  rotate  A  by  the  “elbow”  angle  α


A
B
A α
B
a a c

7  
2/19/15  

Making  an  Arm,  step  4   S b


β

a=c

α
A

• Translate  A  to  to  form  the  elbow  joint  at  c  

A
A B B
a c a c

• We  have  placed  A  rela-ve  to  B  in  its  default  


(untransformed)  posi-on  

S b
Making  an  Arm,  step  5   β

a=c

α
A

• From  now  on,  each  transforma9on  applies  to  both  


A  and  B  (This  is  important!)  
• Translate  by  -­‐a,  bringing  a  to  the  origin  
– A  and  B  move  together,  so  the  elbow  doesn’t  separate!  

A
B A
B
a c
a c

8  
2/19/15  

Making  an  Arm,  step  6   S b


β

a=c

α
A
• Next,  rotate  by  the  “shoulder”  angle  -­‐β

– again,  A  and  B  rotate  together  

A b
B
a c β

a=c
A

S
Making  an  Arm,  last  step   b
β

a=c

α
A

• Finally,  translate  by  the  shoulder  posi9on  S,  bringing  the  


arm  where  we  want  it  
S=b

a=c
b A

a=c
A

9  
2/19/15  

Hierarchical  Transforms  
S b
TSR-­‐βT-­‐b   β

a=c

α
A
Upper  arm  B  

TcRαT-­‐a   Apply  all  the  way  down  

Lower  Arm  A

Apply  all  the  way  down  

Ar9culated  Model  Exercise  


• Ar9culated  model  
40  
– rigid  parts  connected  by  joints  
5  

20  

60  

• Given  x,  y,  α,  β,  θ1  and  θ2,  draw  the  scooter.  

10  
2/19/15  

Step  1  
• Write  func9ons  that  draw  parts  in  default  loca9on:  

DrawBody   DrawHandle   DrawWheel   DrawBrake  

40  
5  

20  

60  

Step  2  
• Construct  the  scene  graph  (model  as  a  tree)  
40  
5  

Body
20  

60  

Handle Back Front


Wheel Wheel

Lower Upper
Brake Brake

11  
2/19/15  

Step  3   40  
• Label  each  edge  with  transforma9on   5  
necessary  to  posi9on  child  with  respect  
to  parent,  in  parent’s  default  loca9on   20  

Body 60  

(transform handle
w.r.t. default body)

Handle Back Front


Wheel Wheel

(transform break
w.r.t. default handle)
Lower Upper
Brake Brake

Step  3   40  
• Tree  Labeling   5  
Tx,y  
20  
Body
60  

T30,20R-α
T30Rβ

T-30Rβ

Handle Back Front


Wheel Wheel

T-35Rθ1
T-35Rθ2

Lower Upper
Brake Brake

12  
2/19/15  

Step  4  
• Code  it  up:  
- Make  sure  that  the  transforma9ons   Tx,y
on  each  edge  apply  to  ALL   Body
descendants     T30,20R-α
T30Rβ

T-30Rβ

Handle Back Front
Wheel Wheel
T-35Rθ1 T-35R-θ2
Lower Upper
Brake Brake

Back  to  the  Robot  …  

13  
2/19/15  

Robot  Guy  
l-- left!
r-- right!
-u- upper! rla!

-l- !lower! rua!


--a !arm! lua!

--l !leg! lla!

rul!
lul!

rll!
lll!

Step  1:  Default  Loca9on  


• Set  the  model  matrix  for  each  part  in  default  loca9on  
• Important:  the  point  of  rota9on  should  be  at  the  origin  

head   neck   shoulders  


hips  
4x4x4   2x2x2   6x2x2  
4x2x2  

upper  &  lower  leg   upper  arm   lower  arm   torso  


2x6x2   2x6x2   4x1x1   2x10x2  

• Dimensions:    2*HEAD_X, 2*HEAD_Y, 2*HEAD_Z!


! ! !2*NECK_X, 2*NECK_Y, 2*NECK_Z!
! ! !etc.!
!

14  
2/19/15  

Step  2:  Build  the  Scene  Graph  

Torso  
rla!

Shoulders   Hips   rua!


lua!

lla!

Neck   LUA   RUA   LUL   RUL  


rul
!
lul
!

rll
!
lll
!

Head   LLA   RLA   LLL   RLL  

Step  3:  Label  the  Scene  Graph  


T(x,y,z)  (posi9on  in  scene)  
Torso  
T(0,-­‐TORSO_Y-­‐HIPS_Y,  0)  

Shoulders   Hips  
T(…)*R(lua)  
rla!

Neck   LUA   RUA   LUL   RUL  


rua!
lua!

lla!

Head   LLA   RLA   LLL   RLL   rul


!
lul
!

rll
!
lll
!

15  
2/19/15  

Step  4:  Coding  


struct  ModelAsset  {   struct  ModelInstance  {  
       tdogl::Program*  shaders;          ModelAsset*  asset;  
       tdogl::Texture*  texture;          glm::mat4  model;                //  default  posi9on  
       GLuint  vbo;          glm::mat4  transform;    //  posi9on  rela9ve  to  parent  
       GLuint  vao;          vector  <ModelInstance  *>  child;    
       GLenum  drawType;    
       GLint  drawStart;          void  Draw();  
       GLint  drawCount;          void  Translate(GLfloat  x,  GLfloat  y,  GLfloat  z);  
};            void  Rotate(GLfloat  theta,  vec3  axis);  
};  

• Draw:  
 shaders-­‐>setUniform("model",  transform  *  model);  
           glBindVertexArray(asset-­‐>vao);  
           glDrawArrays(asset-­‐>drawType,  asset-­‐>drawStart,  asset-­‐>drawCount);  
• Translate:  
   transform  =  transform  *  translate(mat4(),  vec3(x,  y,  z));  
   for  (int  i  =  0;  i  <  child.size();  i++)  
                   child[i]-­‐>Translate(x,  y,  z);  
 

Step  4:  Coding  


enum{TORSO=0,  SHOULDERS,  NECK,  HEAD,  LUA,  LLA,  RUA,  RLA,  HIPS,  LUL,  LLL,  RUL,  RLL,  QUIT};  
sta9c  GLfloat  theta[QUIT]  =  {0.0,  0.0,  0.0,  0.0,  20.0,20.0,-­‐80.0,-­‐40.0,0.0,  20.0,40.0,-­‐50.0,30.0};  
ModelInstance  robot[QUIT];  
 
/*  InitRobot  sets  the  model  matrix  for  each  body  part  in  default  posi-on  and  builds  the  scene  graph.    
         Called  only  ONCE  in  AppMain  */  
sta9c  void  InitRobot()  
{  
           robot[TORSO].model  =  scale(mat4(),  vec3(…));  
           robot[SHOULDERS].model  =  scale(mat4(),  vec3(…));  
           …  
           robot[TORSO].child.push_back(&robot[SHOULDERS]);  
           …  
}  
/*  DrawRobot  draws  the  robot  in  a  posi-on  controlled  by  angle  array  theta.  Called  once  per  frame.  */  
void  DrawRobot()  
{  
       robot[TORSO].Translate(…);  
       robot[TORSO].Draw();  
       robot[SHOULDERS].Translate(…);  
       robot[SHOULDERS].Draw();  
       …  
}  
 
 
 
 
 
 
 

16  
2/19/15  

Hands-­‐on  Ac9vity  1  
• Understand  the  use  of  assets  and  instances  in  05_asset_instance  
• Download  robotmain.cpp  from  the  class  website  and  replace  
main.cpp  from  05_asset_instance  by  robotmain.cpp.  Do  not  remove  
the  original  main.cpp  (only  the  reference  to  it).  You  will  no9ce  that  
robotmain.cpp  does  not  include  any  header  files  –  copy  them  from  the  
original  main.cpp!
• Note  the  changes  in  ModelInstance  to  support  a  hierarchical  structure  
• Make  sure  you  understand  the  implementa9on  of  ModelInstance  
func9ons,  and  how  Translate  and  Rotate  apply  to  all  descendants  
• The  func9ons  manipula9ng  the  robot  are  InitRobot  (incomplete),  
ResetRobot  (complete)  and  DrawRobot  (incomplete).    
 

         Study  and  complete  the  implementa9on  of  these  func9ons.    

Hands-­‐on  Ac9vity  2  
• GLFW  is  a  library  that  allow  us  to  create  a  window,  and  receive  mouse  
and  keyboard  input  in  a  cross-­‐pla{orm  way:  
– Ini9alize  it  with  glfwInit !
– Create  a  window  with  glfwCreateWindow!
– Clean  up  with  glfwTerminate!
• One  way  to  handle  keyboard  events  is  to  use  the  glfwGetKey  func9on  
– It  indicates  whether  a  key  is  pressed  or  not  
• TO  DO:  complete  the  Update  func9on  to  handle  keyboard  input  and  
change  the  robot  posi9on  interac9vely:  
– Select  the  joint  angle  to  alter  based  on  the  key  pressed:  1  for  LUA,  2  for  LLA,  
3  for  RUA,  4  for  RLA,  5  for  LUL,  6  for  LLL,  7  for  RUL,  8  for  RLL  
– Increase  the  joint  angle  with  the  UP  key,  and  decrease  it  with  the  DOWN  key  
– Call  Update  just  before  Render  to  adjust  the  robot  posi9on  

• You  may  search  Google  for  GLFW_KEY_UP  to  find  out  what  other  
predefined  keys  are  available      

17  

You might also like