C ++ / OpenGL在分级转换中转换父位置

问题描述

我应该创建一个具有父级和子级变换的太阳系。太阳是所有行星的父母,行星是其卫星的父母。一切都会正确渲染并旋转,但是,当从层次上将其渲染到地球时,作为地球子节点的moon_node会渲染到太阳上。我认为问题出在我的DFS或渲染函数中。

我初始化对象并将它们添加为子对象,然后运行DFS以渲染节点。

    std::stack<CelestialBody*> node_stack({ &sun_node });
    std::stack<glm::mat4> matrix_stack({glm::mat4(1.0f)});
    std::vector<CelestialBody*> tempVec;
    std::set<CelestialBody*> visited = {};
    
    //Depth first search by using get_children() and populate node stack,visited nodes get rendered)
    
    
    while(node_stack.size()>0)
    {
        if(visited.count(node_stack.top())==1) {
            node_stack.top()->render(delta_time,camera.GetWorldToClipMatrix(),matrix_stack.top());
            matrix_stack.pop();
            node_stack.pop();
            
        } else {
            tempVec = node_stack.top()->get_children();
            visited.insert(node_stack.top());
            if (tempVec.size() == 0) {
                continue;
            } else {
            for (int j = 0; j < tempVec.size(); j++) {
            matrix_stack.push(node_stack.top()->get_transform());
            }
            for (int i = 0; i < tempVec.size(); i++) {
            node_stack.push(tempVec.at(i));
            }
            }
        }
    }

DFS将X数量的父矩阵推入堆栈,其中X是子数量。经过一些调试后,DFS似乎可以正常工作,但是我可能错了。

void CelestialBody::render(float ellapsed_time,glm::mat4 const& view_projection,glm::mat4 const& parent_transform)
{
    spin_angle += spinSpeed*ellapsed_time;
    orbit_angle += orbit_speed*ellapsed_time;
    glm::mat4 model;
    //Body below (Scaling,spining around y-axis with angle then spinning around z-axis with inclination
    model = glm::scale(glm::mat4(1.0f),sc);
    model = glm::rotate(model,spin_angle,glm::vec3(0.0f,1.0f,0.0f));
    model = glm::rotate(model,spinAxis,0.0f,1.0f));
    //Orbit Below (Rotating according to orbit inclination,spinning around Y-axis at angle,then placing it in orbit with radius)
    model = glm::rotate(model,orbit_inclination,1.0f));
    model = glm::rotate(model,orbit_angle,0.0f));
    model = glm::translate(model,glm::vec3(orbit_radius,0.0f));
    //I use the parent transform to create the orbit matrix of the child,incorrect?
    orbitMatrix = glm::rotate(parent_transform,1.0f));
    orbitMatrix = glm::rotate(orbitMatrix,0.0f));
    orbitMatrix = glm::translate(orbitMatrix,0.0f));
    node.render(view_projection,model);
}

orbitMatrix使用以下方法保存和检索:

glm::mat4 CelestialBody::get_transform() const
{
    return orbitMatrix;
}

我认为我必须使用通过get_transform()发送给孩子的父对象orbitMatrix,并且它是render函数中的最后一个属性,以便进行多重化吗?轨道半径或应用其他平移,但我无法使其正常工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)