我目前正在学习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,而应该传达的理念。)
但是,这看起来相当不整洁,看起来像程序更多的工作量(因为会有一个额外的矢量数学吨)。
我已经读了一些关于层次建模的内容,但是关于它的所有文献似乎都假定每个孩子总是想要继承所有的父属性。
有没有更好的方法来做到这一点?