gpt4 book ai didi

c++ - OpenGL 中的建模层次结构

转载 作者:可可西里 更新时间:2023-11-01 09:31:00 26 4
gpt4 key购买 nike

我目前正在学习 OpenGL 并获得了我的第一组几何体和纹理显示。

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

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

这样,子级将继承父级的所有变换(缩放、位置、旋转)。但有时我只想应用某些变换(例如,特效 Actor 应该与其父级一起移动,但不使用它的父级旋转,而 Helm Actor 想要继承所有变换)。

我想出的想法是传递应该应用于绘图函数的转换参数:

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) );
}

(伪代码。可能有误,但应该能表达意思。)

但这看起来相当不整洁,而且看起来程序的工作量要大得多(因为会有大量额外的 vector 数学运算)。

我读过一些关于层次建模的文章,但所有关于它的文献似乎都假设每个 child 总是想继承所有父属性。

有没有更好的方法?

最佳答案

首先附注:矩阵堆栈在最近的 OpenGL 版本中已弃用,用户应自行控制转换。

您面临的问题与您在渲染图中使用的各种 OpenGL 状态有关。解决此问题的一种常见方法(例如在 OpenSceneGraph 中)是为每个状态定义一个附加属性,该属性定义子图是否覆盖其子项中的属性。反过来, children 可以保护他们的状态不被 parent 覆盖。这适用于每种 OpenGL 状态。

你的情况似乎有点奇怪。通常,转换旨在应用于整个子图。尽管此方案在这里也适用。

关于c++ - OpenGL 中的建模层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com