panda3d中Scene Graph的介绍

来源:互联网 发布:圣斗士星矢 知乎 编辑:程序博客网 时间:2024/06/11 21:01

        在计算机图形学中,需要用一些基本元素构成一个复杂的场景,因此需要一个数据结构scene graph来存储该场景中的元素的基本关系。每一条边代表两个基本元素之间的相对位置关系等,在一些引擎中scene graph将3d模型存储在线性表中。panda3d存储结构稍微复杂为树状结构,简单的说是自上而下的结构且无回边,即子节点的性质不能影响父节点的性质, panda3d中存储Scene Graph树节点的是PandaNode。他是ModelNode,GeomNode,LightNode三个类的父类,场景中Scene Graph的父节点render根据不同的节点进行渲染,由于三种节点有相同的父节点,所以父节点的一些性质会被子节点继承。注意scene graph中不是只有一棵树,可以有多棵,用attachNewNode语句来完成,读者可以通过下面两个程序显示的结果来更加深入的理解。

代码一:

from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from panda3d.core import Vec3

class Application(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        self.sun=loader.loadModel("smiley")
        self.sun.reparentTo(render)
        self.sun.setScale(4)

        self.earth=loader.loadModel("frowney")
        self.earthCenter=render.attachNewNode("earthCenter")
        self.earth.reparentTo(self.earthCenter)
        #self.earth.reparentTo(render)
        self.earth.setPos(4,0,0)

        self.cam.setPos(0,-100,0)

w=Application()
run()

代码二:

from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from panda3d.core import Vec3

class Application(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        self.sun=loader.loadModel("smiley")
        self.sun.reparentTo(render)
        self.sun.setScale(4)

        self.earth=loader.loadModel("frowney")
        #self.earthCenter=render.attachNewNode("earthCenter")
        #self.earth.reparentTo(self.earthCenter)
        self.earth.reparentTo(self.sun)
        self.earth.setPos(4,0,0)

        self.cam.setPos(0,-100,0)

w=Application()
run()

 

 

结果为

代码1:

代码2: