-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_graph2.py
96 lines (72 loc) · 2.83 KB
/
scene_graph2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Daniel Calderon, CC3501, 2019-1
A simple scene graph class and functionality
v2.0 - Enhanced to work on 3D environments
"""
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
import transformations2 as tr2
import easy_shaders as es
# A simple class to handle a scene graph
# Each node represents a group of objects
# Each leaf represents a basic figure (GPUShape)
# To identify each node properly, it MUST have a unique name
class SceneGraphNode:
def __init__(self, name):
self.name = name
self.transform = tr2.identity()
self.childs = []
def findNode(node, name):
# The name was not found in this path
if isinstance(node, es.GPUShape):
return None
# This is the requested node
if node.name == name:
return node
# All childs are checked for the requested name
else:
for child in node.childs:
foundNode = findNode(child, name)
if foundNode != None:
return foundNode
# No child of this node had the requested name
return None
def findTransform(node, name, parentTransform=tr2.identity()):
# The name was not found in this path
if isinstance(node, es.GPUShape):
return None
newTransform = np.matmul(parentTransform, node.transform)
# This is the requested node
if node.name == name:
return newTransform
# All childs are checked for the requested name
else:
for child in node.childs:
foundTransform = findTransform(child, name, newTransform)
if isinstance(foundTransform, (np.ndarray, np.generic)):
return foundTransform
# No child of this node had the requested name
return None
def findPosition(node, name, parentTransform=tr2.identity()):
foundTransform = findTransform(node, name, parentTransform)
if isinstance(foundTransform, (np.ndarray, np.generic)):
zero = np.array([[0, 0, 0, 1]], dtype=np.float32).T
foundPosition = np.matmul(foundTransform, zero)
return foundPosition
return None
def drawSceneGraphNode(node, pipeline, parentTransform=tr2.identity()):
# assert (isinstance(node, SceneGraphNode))
# Composing the transformations through this path
newTransform = np.matmul(parentTransform, node.transform)
# If the child node is a leaf, it should be a GPUShape.
# Hence, it can be drawn with drawShape
if len(node.childs) == 1 and isinstance(node.childs[0], es.GPUShape):
leaf = node.childs[0]
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, newTransform)
pipeline.drawShape(leaf)
# If the child node is not a leaf, it MUST be a SceneGraphNode,
# so this draw function is called recursively
else:
for child in node.childs:
drawSceneGraphNode(child, pipeline, newTransform)