2012-01-22 84 views
3

我目前正在学习OpenGL,并获得了我的第一批显示的几何图形和纹理。OpenGL中的建模层次结构

现在我正试图在不同的图形角色之间建立一个父子层次结构模型。 据我所知,它通常是实现这样的:

void Draw(){ 
    glPushMatrix(); 
    <apply all transformations> 
    <draw this model> 
    <for each child c of this actor:> 
     c.Draw(); 
    glPopMatrix(); 
} 

这样的孩子将继承父(规模,位置,旋转)的所有转换。 但有时我只想要应用某些转换(例如,特殊效果演员应与其父项一起移动,但在头盔演员想要继承所有转换时不要使用它的父项旋转)。

的想法,我想出了在流逝应当适用于绘图功能的转换参数:

class Actor{ 
    Vec3 m_pos, m_rot, m_scale; //This object's own transformation data 
    std::vector<Actor*> m_children; 

    void Draw(Vec3 pos, Vec3 rot, Vec3 scale){ 
     (translate by pos) //transform by the given parent data 
     (rotate around rot) 
     (scale by scale) 

     glPushMatrix(); //Apply own transformations 
     (translate by m_pos) 
     (rotate around m_rot) 
     (scale by m_scale) 
     (draw model) 
     glPopMatrix(); //discard own transformations 

     for(Actor a : m_children){ 
      glPushMatrix(); 
      a.Draw(pass m_pos, m_rot and/or m_scale depending on what the current child wants to inherit); 
      glPopMatrix(); 
     } 
    } 
} 

void StartDrawing(){ 
    glLoadIdentity(); 
    //g_wold is the "world actor" which everything major is attached to. 
    g_world.Draw(Vec3(0,0,0), Vec3(0,0,0), Vec3(0,0,0)); 
} 

(伪代码可能是errorneous,而应该传达的理念。)

但是,这看起来相当不整洁,看起来像程序更多的工作量(因为会有一个额外的矢量数学吨)。

我已经读了一些关于层次建模的内容,但是关于它的所有文献似乎都假定每个孩子总是想要继承所有的父属性。

有没有更好的方法来做到这一点?

回答

2

先说明一下:矩阵堆栈在最近的OpenGL版本中已被弃用,用户应该自己控制转换。

你面临的问题是你在渲染图中使用的每一种OpenGL状态。解决这个问题的一个常见方法是为每个状态定义一个附加属性,该属性定义子图是否覆盖其子级中的属性。反过来,孩子们可以保护他们的国家免受父母的压制。这适用于每种OpenGL状态。

你的情况似乎有点奇怪。通常转换意图适用于整个子图。虽然这个方案也可以在这里工作。