Exemple #1
0
class RenderNode(object):

    def __init__(self, sceneNode):
        super(RenderNode, self).__init__()
        self.children = []
        self.childrenBySceneNode = {}
        self.sceneNode = sceneNode
        self.displayList = DisplayList()          # Recompiled whenever this node's scenegraph node is dirty
                                                  # or node gains or loses children
        self.childNeedsRecompile = True

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.sceneNode)

    _parent = None
    @property
    def parent(self):
        if self._parent:
            return self._parent()

    @parent.setter
    def parent(self, value):
        if value is not None:
            self._parent = weakref.ref(value)
        else:
            self._parent = None

    def addChild(self, node):
        self.children.append(node)
        self._addChild(node)

    def _addChild(self, node):
        self.childrenBySceneNode[node.sceneNode] = node
        node.parent = self
        self.displayList.invalidate()
        self.childNeedsRecompile = True
        if self.parent:
            self.parent.touch()

    def insertNode(self, index, node):
        self.children.insert(index, node)
        self._addChild(node)

    def removeChild(self, node):
        self.childrenBySceneNode.pop(node.sceneNode, None)
        self.children.remove(node)
        self.displayList.invalidate()
        node.parent = None
        self.childNeedsRecompile = True
        if self.parent:
            self.parent.touch()

    def invalidate(self):
        self.displayList.invalidate()
        self.touch()

    def touch(self):
        node = self
        while node:
            node.childNeedsRecompile = True
            node = node.parent

    def getList(self):
        return self.displayList.getList()

    def callList(self):
        self.displayList.call()

    def compile(self):
        if self.childNeedsRecompile:
            for node in self.children:
                if node.sceneNode.visible:
                    node.compile()
            self.childNeedsRecompile = False

        self.displayList.compile(self.draw)

    def draw(self):
        self.drawSelf()
        self.drawChildren()

    def drawChildren(self):
        if len(self.children):
            lists = [node.getList()
                     for node in self.children
                     if node.sceneNode.visible]
            if len(lists):
                lists = numpy.hstack(tuple(lists))
                try:
                    GL.glCallLists(lists)
                except GL.error as e:
                    log.exception("Error calling child lists: %s", e)
                    raise

    def drawSelf(self):
        pass

    def destroy(self):
        for child in self.children:
            child.destroy()
        self.displayList.destroy()
Exemple #2
0
class RenderNode(object):

    def __init__(self, sceneNode):
        super(RenderNode, self).__init__()
        self._parents = []
        self.children = []
        self.childrenBySceneNode = {}
        self.sceneNode = sceneNode
        self.displayList = DisplayList()          # Recompiled whenever this node's scenegraph node is dirty
                                                  # or node gains or loses children
        self.childNeedsRecompile = True

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.sceneNode)

    def addParent(self, obj):
        for parent in self._parents:
            if parent() is obj:
                return

        self._parents.append(weakref.ref(obj))

    def removeParent(self, obj):
        self._parents[:] = [p for p in self._parents
                            if p() is not obj and p() is not None]

    def addChild(self, node):
        self.children.append(node)
        self._addChild(node)

    def _addChild(self, node):
        self.childrenBySceneNode[node.sceneNode] = node
        node.addParent(self)
        self.displayList.invalidate()
        self.childNeedsRecompile = True
        self.notifyParents()

    def insertNode(self, index, node):
        self.children.insert(index, node)
        self._addChild(node)

    def removeChild(self, node):
        self.childrenBySceneNode.pop(node.sceneNode, None)
        self.children.remove(node)
        self.displayList.invalidate()
        node.removeParent(self)
        self.childNeedsRecompile = True
        self.notifyParents()

    def invalidate(self):
        self.displayList.invalidate()
        self.notifyParents()

    def notifyParents(self):
        for p in self._parents:
            parent = p()
            if parent:
                parent.childNeedsRecompile = True
                parent.notifyParents()

    def getList(self):
        return self.displayList.getList()

    if DEBUG_NO_DISPLAYLISTS:
        def callList(self):
            with self.enterStates():
                self.drawSelf()
                self.debugDrawChildren()
    else:
        def callList(self):
            self.displayList.call()

    def compile(self):
        if self.childNeedsRecompile:
            for node in self.children:
                node.compile()
            self.childNeedsRecompile = False

        for state in self.sceneNode.states:
            state.compile()

        if not DEBUG_NO_DISPLAYLISTS:
            self.displayList.compile(self.draw)

    @contextmanager
    def enterStates(self):
        for state in self.sceneNode.states:
            state.enter()
        try:
            yield
        finally:
            for state in reversed(self.sceneNode.states):
                state.exit()

    def draw(self):
        with self.enterStates():
            self.drawSelf()
            self.callChildren()

    def callChildren(self):
        if len(self.children):
            lists = [node.getList()
                     for node in self.children
                     if node.sceneNode.visible]
            if len(lists):
                lists = numpy.hstack(tuple(lists))
                try:
                    GL.glCallLists(lists)
                except GL.error as e:
                    log.exception("Error calling child lists: %s", e)
                    raise

    def debugDrawChildren(self):
        if len(self.children):
            for node in self.children:
                node.callList()


    def drawSelf(self):
        pass

    def dealloc(self):
        for child in self.children:
            child.dealloc()
        self.displayList.dealloc()
Exemple #3
0
class RenderNode(object):

    def __init__(self, sceneNode):
        super(RenderNode, self).__init__()
        self._parents = []
        self.children = []
        self.childrenBySceneNode = {}
        self.sceneNode = sceneNode
        self.displayList = DisplayList()          # Recompiled whenever this node's scenegraph node is dirty
                                                  # or node gains or loses children
        self.childNeedsRecompile = True

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.sceneNode)

    def addParent(self, obj):
        for parent in self._parents:
            if parent() is obj:
                return

        self._parents.append(weakref.ref(obj))

    def removeParent(self, obj):
        self._parents[:] = [p for p in self._parents
                            if p() is not obj and p() is not None]

    def addChild(self, node):
        self.children.append(node)
        self._addChild(node)

    def _addChild(self, node):
        self.childrenBySceneNode[node.sceneNode] = node
        node.addParent(self)
        self.displayList.invalidate()
        self.childNeedsRecompile = True
        self.notifyParents()

    def insertNode(self, index, node):
        self.children.insert(index, node)
        self._addChild(node)

    def removeChild(self, node):
        self.childrenBySceneNode.pop(node.sceneNode, None)
        self.children.remove(node)
        self.displayList.invalidate()
        node.removeParent(self)
        self.childNeedsRecompile = True
        self.notifyParents()

    def invalidate(self):
        self.displayList.invalidate()
        self.notifyParents()

    def notifyParents(self):
        for p in self._parents:
            parent = p()
            if parent:
                parent.childNeedsRecompile = True
                parent.notifyParents()

    def getList(self):
        return self.displayList.getList()

    def callList(self):
        self.displayList.call()

    def compile(self):
        if self.childNeedsRecompile:
            for node in self.children:
                if node.sceneNode.visible:
                    node.compile()
            self.childNeedsRecompile = False

        for state in self.sceneNode.states:
            state.compile()
        self.displayList.compile(self.draw)

    @contextmanager
    def enterStates(self):
        for state in self.sceneNode.states:
            state.enter()
        try:
            yield
        finally:
            for state in reversed(self.sceneNode.states):
                state.exit()

    def draw(self):
        with self.enterStates():
            self.drawSelf()
            self.drawChildren()

    def drawChildren(self):
        if len(self.children):
            lists = [node.getList()
                     for node in self.children
                     if node.sceneNode.visible]
            if len(lists):
                lists = numpy.hstack(tuple(lists))
                try:
                    GL.glCallLists(lists)
                except GL.error as e:
                    log.exception("Error calling child lists: %s", e)
                    raise

    def drawSelf(self):
        pass

    def destroy(self):
        for child in self.children:
            child.destroy()
        self.displayList.destroy()
Exemple #4
0
class RenderNode(object):
    def __init__(self, sceneNode):
        super(RenderNode, self).__init__()
        self.children = []
        self.childrenBySceneNode = {}
        self.sceneNode = sceneNode
        self.displayList = DisplayList(
        )  # Recompiled whenever this node's scenegraph node is dirty
        # or node gains or loses children
        self.childNeedsRecompile = True

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.sceneNode)

    _parent = None

    @property
    def parent(self):
        if self._parent:
            return self._parent()

    @parent.setter
    def parent(self, value):
        if value is not None:
            self._parent = weakref.ref(value)
        else:
            self._parent = None

    def addChild(self, node):
        self.children.append(node)
        self._addChild(node)

    def _addChild(self, node):
        self.childrenBySceneNode[node.sceneNode] = node
        node.parent = self
        self.displayList.invalidate()
        self.childNeedsRecompile = True
        if self.parent:
            self.parent.touch()

    def insertNode(self, index, node):
        self.children.insert(index, node)
        self._addChild(node)

    def removeChild(self, node):
        self.childrenBySceneNode.pop(node.sceneNode, None)
        self.children.remove(node)
        self.displayList.invalidate()
        node.parent = None
        self.childNeedsRecompile = True
        if self.parent:
            self.parent.touch()

    def invalidate(self):
        self.displayList.invalidate()
        self.touch()

    def touch(self):
        node = self
        while node:
            node.childNeedsRecompile = True
            node = node.parent

    def getList(self):
        return self.displayList.getList()

    def callList(self):
        self.displayList.call()

    def compile(self):
        if self.childNeedsRecompile:
            for node in self.children:
                if node.sceneNode.visible:
                    node.compile()
            self.childNeedsRecompile = False

        self.displayList.compile(self.draw)

    def draw(self):
        self.drawSelf()
        self.drawChildren()

    def drawChildren(self):
        if len(self.children):
            lists = [
                node.getList() for node in self.children
                if node.sceneNode.visible
            ]
            if len(lists):
                lists = numpy.hstack(tuple(lists))
                try:
                    GL.glCallLists(lists)
                except GL.error as e:
                    log.exception("Error calling child lists: %s", e)
                    raise

    def drawSelf(self):
        pass

    def destroy(self):
        for child in self.children:
            child.destroy()
        self.displayList.destroy()