def movement(direction, currentStance, nextStance): cube.isLocked = True if not cube.isActive: if not cube.mapRotated: if cube.willRotate: if direction == 'd': parent_point.transform.rotation = glm.quat( glm.vec3(0, 0, glm.radians(-90))) cube.mapRotated = True else: print('movedXZ') movementXZ(direction, currentStance, nextStance) else: print('movedXZ') movementXZ(direction, currentStance, nextStance) else: if cube.willRotate: if direction == 'a': parent_point.transform.rotation = glm.quat( glm.vec3(0, 0, 0)) cube.mapRotated = False else: print('movedYZ') movementYZ(direction, currentStance, nextStance) else: print('movedYZ') movementYZ(direction, currentStance, nextStance)
def get_node_transformation( self, node_id ) -> glm.mat4: # Get Node transformation matrix using Node ID. matrix = glm.mat4(1.0) if self.gltf.nodes[node_id].matrix: # Convert gltf matrix to glm matrix. matrix = glm.mat4(*self.gltf.nodes[node_id].matrix) print("Node has matrix.") elif self.gltf.nodes[node_id].rotation or self.gltf.nodes[ node_id].scale or self.gltf.nodes[node_id].rotation: # Convert transformation vectors to matrix. print("Node has vectors.") translation = glm.vec3() rotation = glm.quat() scale = glm.vec3(1.0, 1.0, 1.0) if self.gltf.nodes[node_id].translation: translation = glm.vec3(*self.gltf.nodes[node_id].translation) if self.gltf.nodes[node_id].rotation: rotation = glm.quat(*self.gltf.nodes[node_id].rotation) if self.gltf.nodes[node_id].scale: scale = glm.vec3(*self.gltf.nodes[node_id].scale) trans_mat = glm.translate(glm.mat4(1.0), translation) rot_mat = glm.mat4_cast(rotation) scale_mat = glm.scale(glm.mat4(1.0), scale) matrix = trans_mat * rot_mat * scale_mat return matrix
def rotation(self, orig, dest): identityQuat = glm.quat(1.0, 0.0, 0.0, 0.0) epsilon = 0.00001 cosTheta = glm.dot(orig, dest) if cosTheta >= 1.0 - epsilon: #// orig and dest point in the same direction return identityQuat if cosTheta < -1.0 + epsilon: ''' // special case when vectors in opposite directions : // there is no "ideal" rotation axis // So guess one; any will do as long as it's perpendicular to start // This implementation favors a rotation around the Up axis (Y), // since it's often what you want to do. ''' rotationAxis = glm.cross(glm.vec3(0.0, 0.0, 1.0), orig) if glm.length( rotationAxis ) < epsilon: # // bad luck, they were parallel, try again! rotationAxis = glm.cross(glm.vec3(1.0, 0.0, 0.0), orig) rotationAxis = glm.normalize(rotationAxis) return glm.angleAxis(glm.pi(), rotationAxis) #// Implementation from Stan Melax's Game Programming Gems 1 article rotationAxis = glm.cross(orig, dest) s = math.sqrt((1.0 + cosTheta) * 2.0) invs = 1.0 / s return glm.quat(s * 0.5, rotationAxis.x * invs, rotationAxis.y * invs, rotationAxis.z * invs)
def load_blender_gltf_light(g0, n0): nc = g0.model.nodes[n0.children[0]] l0 = nc.extensions.get('KHR_lights_punctual')['light'] l1 = g0.model.extensions['KHR_lights_punctual']['lights'][l0] m0 = glm.mat4_cast(glm.quat(xyzw2wxyz(nc.rotation))) m1 = glm.mat4_cast(glm.quat(xyzw2wxyz(n0.rotation))) if n0.rotation else glm.mat4(1.0) qq = glm.quat_cast(m1 * m0) attenuation = { 'spot': 1 / 10, 'point': 1 / 10, 'directional': 5 / 4, } ambient = 0.001 light = Light( n0.name, qq, n0.scale, n0.translation, l1['type'], l1['color'], l1['intensity'] * attenuation.get(l1['type'], 1.), ambient, l1.get('spot', {}).get('outerConeAngle', math.pi / 4), l1.get('spot', {}).get('innerConeAngle', math.pi / 4 * 0.9), ) light._source = n0 return light
def forward(self, mat=None): if not mat: matrix = glm.mat4_cast(glm.quat(self.rotation)) else: matrix = mat return glm.vec3(matrix[2])
def _generate_nodeview_matrix2( rsm_version: int, node: AbstractNode) -> Tuple[glm.mat4, glm.mat4]: # Transformations which are inherited by children local_transform_matrix = glm.mat4() rsm_node = node.impl # Scaling if len(rsm_node.scale_key_frames) > 0: local_transform_matrix = glm.scale( local_transform_matrix, glm.vec3(rsm_node.scale_key_frames[0].scale)) # Rotation if len(rsm_node.rot_key_frames) > 0: # Animated model key_frame = rsm_node.rot_key_frames[0] quaternion = glm.quat(key_frame.quaternion[3], key_frame.quaternion[0], key_frame.quaternion[1], key_frame.quaternion[2]) local_transform_matrix *= glm.mat4_cast(quaternion) else: # Static model local_transform_matrix = rag_mat4_mul( local_transform_matrix, mat3tomat4(rsm_node.info.offset_matrix)) if node.parent: parent_offset_matrix = mat3tomat4( node.parent.impl.info.offset_matrix) local_transform_matrix = rag_mat4_mul( local_transform_matrix, glm.inverse(parent_offset_matrix)) # Translation if rsm_version >= 0x203 and len(rsm_node.pos_key_frames) > 0: key_frame = rsm_node.pos_key_frames[0] position = glm.vec3(key_frame.position) elif node.parent: position = glm.vec3(rsm_node.info.offset_vector) - \ glm.vec3(node.parent.impl.info.offset_vector) parent_offset_matrix = mat3tomat4(node.parent.impl.info.offset_matrix) position = glm.vec3( glm.inverse(parent_offset_matrix) * glm.vec4(position, 1.0)) else: position = glm.vec3(rsm_node.info.offset_vector) # Transformations which are applied only to this node final_transform_matrix = copy.copy(local_transform_matrix) # Reset translation transformation to `position` final_transform_matrix[3] = glm.vec4(position, 1.0) # Inherit transformations from ancestors parent = node.parent while parent: final_transform_matrix = rag_mat4_mul(final_transform_matrix, parent.local_transform_matrix) parent = parent.parent if node.parent: parent_translation = glm.vec3(node.parent.final_transform_matrix[3]) final_transform_matrix[3] += glm.vec4(parent_translation, 0.0) return (local_transform_matrix, final_transform_matrix)
def makeInitialFallEffect(acceleration=0): parent_point.transform.rotation = glm.quat(glm.vec3(0, 0, 0)) cube.mapRotated = False cube.transform.rotation = glm.quat(glm.vec3(0, 0, 0)) cube.transform.position = glm.vec3(0, 3.0, 0.0) while cube.transform.position.y > 0: cube.transform.position -= glm.vec3(0, acceleration, 0) acceleration += 0.0007 display() cube.transform.position = glm.vec3(cube.transform.position.x, (cube.transform.position.y * 0) - 0.01, cube.transform.position.z) display() cube.rotateDirection = 'none'
def distance(self, other:"Transformation") -> float: td = glm.distance(self.translation, other.translation) sd = glm.distance(self.scale, other.scale) qn : glm.Quat = glm.inverse(self.quaternion) # type: ignore qn = qn * other.quaternion if qn.w<0: qn = -qn qd = glm.length(qn - glm.quat()) return td+sd+qd
def _calculate_node_bounding_box( version: int, node_count: int, node: Node, matrix: glm.mat4 = glm.mat4(1.0)) -> None: parent = node.parent rsm_node = node.impl bbox = BoundingBox() if parent is not None: matrix = glm.translate(matrix, glm.vec3(rsm_node.info.position)) if rsm_node.rot_key_count == 0: if rsm_node.info.rotation_angle > 0.01: matrix = glm.rotate( matrix, glm.radians(rsm_node.info.rotation_angle * 180.0 / math.pi), glm.vec3(rsm_node.info.rotation_axis)) else: quaternion = glm.quat(*rsm_node.rot_key_frames[0].quaternion) matrix *= glm.mat4_cast(glm.normalize(quaternion)) matrix = glm.scale(matrix, glm.vec3(rsm_node.info.scale)) local_matrix = copy.copy(matrix) offset_matrix = mat3tomat4(rsm_node.info.offset_matrix) if node_count > 1: local_matrix = glm.translate(local_matrix, glm.vec3(rsm_node.info.offset_vector)) local_matrix *= offset_matrix for i in range(rsm_node.mesh_vertex_count): x = rsm_node.mesh_vertices[i].position[0] y = rsm_node.mesh_vertices[i].position[1] z = rsm_node.mesh_vertices[i].position[2] v = glm.vec3() v[0] = local_matrix[0][0] * x + local_matrix[1][0] * y + local_matrix[ 2][0] * z + local_matrix[3][0] v[1] = local_matrix[0][1] * x + local_matrix[1][1] * y + local_matrix[ 2][1] * z + local_matrix[3][1] v[2] = local_matrix[0][2] * x + local_matrix[1][2] * y + local_matrix[ 2][2] * z + local_matrix[3][2] for j in range(3): bbox.min[j] = min(v[j], bbox.min[j]) bbox.max[j] = max(v[j], bbox.max[j]) for i in range(3): bbox.offset[i] = (bbox.max[i] + bbox.min[i]) / 2.0 bbox.range[i] = (bbox.max[i] - bbox.min[i]) / 2.0 bbox.center[i] = bbox.min[i] + bbox.range[i] node.bbox = bbox for child in node.children: _calculate_node_bounding_box(version, node_count, child, matrix)
def reset(self): self.origin = glm.vec3() self.position = glm.vec3() self.scale = glm.vec3(1.0, 1.0, 1.0) self.rotation = glm.quat(1.0, 0.0, 0.0, 0.0) self.transform = glm.mat4(1.0) self.inv_transform = glm.mat4(1.0) self.transform_dirty = True self.inv_transform_dirty = True
def movementYZ(direction, currentStance, nextStance): if currentStance == 'none': if nextStance == 'horizontal': if direction == 'd': cube.transform.rotation = glm.quat( glm.vec3(0, glm.radians(90), 0)) cube.transform.position += glm.vec3(0.5, 1.5, 0) else: cube.transform.rotation = glm.quat( glm.vec3(0, glm.radians(-90), 0)) cube.transform.position -= glm.vec3(-0.5, 1.5, 0) if nextStance == 'vertical': if direction == 'w': cube.transform.rotation = glm.quat( glm.vec3(glm.radians(90), 0, 0)) cube.transform.position += glm.vec3(-0.5, 0, -1.5) else: cube.transform.rotation = glm.quat( glm.vec3(glm.radians(-90), 0, 0)) cube.transform.position += glm.vec3(0.5, 0, 1.5) if currentStance == 'vertical': if nextStance == 'vertical': if direction == 'd': # cube.transform.rotation *= glm.quat(glm.vec3(0, 0, glm.radians(90))) cube.transform.position += glm.vec3(1, 0, 0) else: # cube.transform.rotation *= glm.quat(glm.vec3(0, 0, glm.radians(-90))) cube.transform.position -= glm.vec3(1, 0, 0) if nextStance == 'none': if direction == 'w': cube.transform.rotation = glm.quat( glm.vec3(0, 0, glm.radians(90))) cube.transform.position += glm.vec3(-0.5, 0, -1.5) else: cube.transform.rotation = glm.quat( glm.vec3(0, 0, glm.radians(-90))) cube.transform.position += glm.vec3(0.5, 0, 1.5) if currentStance == 'horizontal': if nextStance == 'horizontal': if direction == 'w': # cube.transform.rotation *= glm.quat(glm.vec3(0, glm.radians(90), 0)) cube.transform.position += glm.vec3(0, 0, -1) else: # cube.transform.rotation *= glm.quat(glm.vec3(0, glm.radians(-90), 0)) cube.transform.position -= glm.vec3(0, 0, -1) if nextStance == 'none': if direction == 'd': cube.transform.rotation = glm.quat( glm.vec3(0, 0, glm.radians(90))) cube.transform.position += glm.vec3(-0.5, 1.5, 0) else: cube.transform.rotation = glm.quat( glm.vec3(0, 0, glm.radians(-90))) cube.transform.position -= glm.vec3(0.5, 1.5, 0)
def _generate_nodeview_matrix1(rsm_version: int, node: AbstractNode, is_only_node: bool, model_bbox: BoundingBox) -> glm.mat4: rsm_node = node.impl # Model view # Translation nodeview_matrix = glm.mat4() if node.parent is None: # Z axis is in the opposite direction in glTF nodeview_matrix = glm.rotate(nodeview_matrix, math.pi, glm.vec3(1.0, 0.0, 0.0)) if is_only_node: nodeview_matrix = glm.translate( nodeview_matrix, glm.vec3(-model_bbox.center[0] + model_bbox.range[0], -model_bbox.max[1] + model_bbox.range[1], -model_bbox.min[2])) else: nodeview_matrix = glm.translate( nodeview_matrix, glm.vec3(-model_bbox.center[0], -model_bbox.max[1], -model_bbox.center[2])) else: nodeview_matrix = glm.rotate(nodeview_matrix, -math.pi / 2, glm.vec3(1.0, 0.0, 0.0)) nodeview_matrix = glm.translate(nodeview_matrix, glm.vec3(rsm_node.info.position)) # Figure out the initial rotation if len(rsm_node.rot_key_frames) == 0: # Static model if rsm_node.info.rotation_angle > 0.01: nodeview_matrix = glm.rotate( nodeview_matrix, glm.radians(rsm_node.info.rotation_angle * 180.0 / math.pi), glm.vec3(rsm_node.info.rotation_axis)) else: # Animated model key_frame = rsm_node.rot_key_frames[0] quaternion = glm.quat(key_frame.quaternion[3], key_frame.quaternion[0], key_frame.quaternion[1], key_frame.quaternion[2]) nodeview_matrix *= glm.mat4_cast(quaternion) # Scaling nodeview_matrix = glm.scale(nodeview_matrix, glm.vec3(rsm_node.info.scale)) # Node view if is_only_node: nodeview_matrix = glm.translate(nodeview_matrix, -model_bbox.range) elif node.parent is not None: nodeview_matrix = glm.translate(nodeview_matrix, glm.vec3(rsm_node.info.offset_vector)) nodeview_matrix *= mat3tomat4(rsm_node.info.offset_matrix) return nodeview_matrix
def load_blender_gltf_camera(g0, n0): nc = g0.model.nodes[n0.children[0]] c0 = g0.model.cameras[nc.camera] m0 = glm.mat4_cast(glm.quat(xyzw2wxyz(nc.rotation))) m1 = glm.mat4_cast(glm.quat(xyzw2wxyz(n0.rotation))) qq = glm.quat_cast(m1 * m0) camera = Camera( n0.name, qq, n0.scale, n0.translation, c0.type, **vars(c0.perspective or c0.orthographic) ) camera._source = n0 return camera
def get_state(self): """Get a dictionary of properties defining the object state. Returns: dict: A dictionary of properties. """ return dict( rotation=glm_dumps(glm.quat(self.rotation)), position=glm_dumps(glm.vec3(self.position)), anchor=glm_dumps(glm.vec3(self.anchor)), scale0=glm_dumps(glm.vec3(self.scale0)), )
def __init__( self, name='', anchor=None, scale=None, rotation=None, position=None, ): super().__init__() self.name = name self.anchor = glm.vec3(anchor) if anchor else glm.vec3(0.) self.scale0 = glm.vec3(scale) if scale else glm.vec3(1.) self.rotation = glm.quat(rotation) if rotation else glm.quat( 1., 0., 0., 0.) self.position = glm.vec3(position) if position else glm.vec3(0.) self._itemz = None self._matrix = glm.mat4(1.)
def animate(self, time: float) -> None: self.poses[0].transformation_reset() self.poses[1].transformation_reset() self.poses[2].transformation_reset() angle = math.sin(time * 0.2) * 0.3 q = glm.quat() q = glm.angleAxis(time * 0.3, glm.vec3([0, 0, 1])) * q q = glm.angleAxis(1.85, glm.vec3([1, 0, 0])) * q self.poses[0].transform( Transformation(translation=(0., 0., 0.), quaternion=q)) q = glm.quat() q = glm.angleAxis(angle * 4, glm.vec3([0, 0, 1])) * q self.poses[1].transform( Transformation(translation=(0., 0., -math.cos(4 * angle)), quaternion=q)) q = glm.quat() q = glm.angleAxis(angle * 4, glm.vec3([0, 0, 1])) * q self.poses[2].transform( Transformation(translation=(0., 0., +math.cos(4 * angle)), quaternion=q)) self.pose.update(int(time * 1E5)) pass
def __init__(self, translation: Optional[Tuple[float,float,float]]=None, quaternion : Optional[glm.Quat] = None, scale : Optional[Tuple[float,float,float]]=None ) -> None: if translation is None: self.translation = glm.vec3() pass else: self.translation = glm.vec3(translation) pass if quaternion is None: self.quaternion = glm.quat() pass else: self.quaternion = glm.quat(quaternion) pass if scale is None: self.scale = glm.vec3([1.,1.,1.]) pass else: self.scale = glm.vec3(scale) pass pass
def RecalculateMatrices(self): if self.__posChanged: self._transMat = glm.translate(glm.mat4(1.0), self._pos) self.__posChanged = False if self.__rotChanged: self._rotQuat = glm.quat(glm.radians(self._rot)) self.__rotChanged = False if self.__sizeChanged: self._scaleMat = glm.scale(glm.mat4(1.0), self._size) self.__sizeChanged = False self.matrix = self._transMat * glm.mat4_cast(self._rotQuat) * self._scaleMat
def get_rotation(self, deg=False): """Extract rotation angle and axis from rotation matrix. This method does not separate global and local rotations. For that it is neccessary to keep track of separate model transformations. References: https://stackoverflow.com/questions/17918033/glm-decompose-mat4-into-translation-and-rotation and https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation """ rotation = decompose(self._matrix, rotation=glm.quat()) return q2aa(rotation, deg=deg)
def lerp_to_vec3(source, target, speed, delta_time): sourceQuat = glm.quat(source[0], source[1], source[2], 1.0) targetQuat = glm.quat(target[0], target[1], target[2], 1.0) step = glm.lerp(sourceQuat, targetQuat, 1.0 - delta_time) step *= speed change = glm.vec3(source[0], source[1], source[2]) # check x coord if (abs(source[0] - target[0]) > (speed * 2)): if (target[0] > source[0]): change = glm.vec3(change[0] + abs(step[0]), change[1], change[2]) else: change = glm.vec3(change[0] - abs(step[0]), change[1], change[2]) # check y coord if (abs(source[1] - target[1]) > (speed * 2)): if (target[1] > source[1]): change = glm.vec3(change[0], change[1] + abs(step[1]), change[2]) else: change = glm.vec3(change[0], change[1] - abs(step[1]), change[2]) # # check z coord if (abs(source[2] - target[2]) > (speed * 2)): if (target[2] > source[2]): change = glm.vec3(change[0], change[1], change[2] + abs(step[2])) else: change = glm.vec3(change[0], change[1], change[2] - abs(step[2])) if (abs(source[0] - target[0]) < (speed * 3) and abs(source[1] - target[1]) < (speed * 3) and abs(source[2] - target[2]) < (speed * 3)): return (True, change) else: return (False, change)
def __init__(self, parent: 'Transform' = None, position=None, scale=None, rotation=None): self.__parent = None self.__set_parent(parent) self.children: List[Transform] = [] self.__position = position or glm.vec3(0.0) self.__scale = scale or glm.vec3(1.0) self.__rotation = rotation or glm.quat(glm.vec3(0.0)) self.__transformation_matrix = None self.__transformations_changed = True
def teleportInit(): for tile in fromTeleport: if tile.isActive: if tile.rotateDirection == 'up': if tile.life == 3: if cube.transform.position.x > -20: print('up') tile.transform.position -= glm.vec3(0.1, 0, 0) cube.transform.position -= glm.vec3(0.1, 0, 0) cube.isActive = True else: tile.life = 0 else: if tile.life == 0: parent_point.transform.rotation = glm.quat( glm.vec3(0, 0, 0)) cube.mapRotated = False cube.transform.rotation = glm.quat(glm.vec3(0, 0, 0)) tile.transform.rotation = glm.quat(glm.vec3(0, 0, 0)) tile.transform.position = glm.vec3(0, 20, -1) cube.transform.position = glm.vec3(0, 21, -1) tile.life = 1 else: if cube.transform.position.y > 0: print(cube.transform.position.y) tile.transform.position -= glm.vec3(0, 0.1, 0) cube.transform.position -= glm.vec3(0, 0.1, 0) cube.rotateDirection = 'none' else: tile.transform.position = glm.vec3(0, -1, -1) cube.transform.position = glm.vec3(0, -0.01, -1) tile.isActive = False tile.isLocked = True tile.willRotate = False cube.isActive = False
def readTransform(tTransform): tDx = tDy = tDz = 0 tRx = tRy = tRz = 0 tSx = tSy = tSz = 1 tf = glm.mat4(1) if tTransform is None: return tf if "Position" in tTransform: tPosition = tTransform["Position"] if tPosition is not None: tDx, tDy, tDz = tPosition.get("X", 0), tPosition.get( "Y", 0), tPosition.get("Z", 0) tf = glm.translate(tf, glm.vec3(tDx, tDy, tDz)) if "Rotation" in tTransform: tRotation = tTransform["Rotation"] if tRotation is not None: if "W" in tRotation and tRotation["W"] <= 1.000001: tRx = tRotation["X"] tRy = tRotation["Y"] tRz = tRotation["Z"] tW = tRotation["W"] R = glm.mat4_cast(glm.quat(tRx, tRy, tRz, tW)) else: # only one rotation axis allowed tRx = glm.radians(tRotation["X"]) tRy = glm.radians(tRotation["Y"]) tRz = glm.radians(tRotation["Z"]) Rx = glm.rotate(glm.mat4(1), tRx, glm.vec3(1, 0, 0)) Ry = glm.rotate(glm.mat4(1), tRy, glm.vec3(0, 1, 0)) Rz = glm.rotate(glm.mat4(1), tRz, glm.vec3(0, 0, 1)) R = Rz * Ry * Rx tf = tf * R if "Scale" in tTransform: tScaling = tTransform["Scale"] if tScaling is not None: tSx, tSy, tSz = tScaling.get("X", 0), tScaling.get( "Y", 0), tScaling.get("Z", 0) tf = glm.scale(tf, glm.vec3(tSx, tSy, tSz)) # print("LOG", "Position:", tDx, tDy, tDz, "Rotation:", tRx, tRy, tRz, "Scaling:", tSx, tSy, tSz) return tf
def __init__(self, obj_data: BoundObjData, parent: 'GameObject' = None, position=glm.vec3(0.0), scale=glm.vec3(1.0), rotation=glm.quat(glm.vec3(0.0))): self.obj_data = obj_data self.parent = parent self.position = position self.rotation = rotation self.scale = scale self.__transformation_matrix = None self.__transformations_changed = True
def __init__(self, parent: 'Transform' = None, position=glm.vec3(0.0), scale=glm.vec3(1.0), rotation=glm.quat(glm.vec3(0.0))): self.__parent = None self.parent = parent self.children: List[Transform] = [] # self.game_object: GameObject = None self.position = position self.rotation = rotation self.scale = scale self.__transformation_matrix = None self.__transformations_changed = True
def __init__(self, pos: glm.vec3, size: glm.vec3, rotation: glm.vec3): self._pos = pos self._size = size self._rot = glm.mod(rotation, 360.0) self._rotHint = rotation self.__posChanged = True self.__sizeChanged = True self.__rotChanged = True self.matrix = glm.mat4(1.0) self._transMat = glm.mat4(1.0) self._scaleMat = glm.mat4(1.0) self._rotQuat = glm.quat(self._rot) self.RecalculateMatrices()
def decompose( matrix, scale=None, rotation=None, translation=None, skew=None, perspective=None, ): status = glm.decompose( matrix, scale or glm.vec3(), rotation or glm.quat(), translation or glm.vec3(), skew or glm.vec3(), perspective or glm.vec4() ) return scale or rotation or translation or skew or perspective
def update(self, ind): """ Updates every :class:`~diskovery_buffer.UniformBuffer` stored in ``uniforms`` with new data :param ind: the index indicating which :class:`~diskovery_buffer.Buffer` in each :class:`~diskovery_buffer.UniformBuffer` should be filled with new data """ m = MVPMatrix() m.model = glm.scale(glm.translate(glm.mat4(1.0), self.position) * \ glm.mat4_cast(glm.quat(self.rotation)), self.scale) m.view = _camera.view_matrix m.projection = _camera.proj_matrix self.uniforms[0].update(m.get_data(), ind) if hasattr(self, 'light_scene'): self.uniforms[1].update(_light_scenes[self.light_scene].get_data(), ind)
def decompose_matrix( mat: glm.mat4 ) -> Tuple[Optional[glm.vec3], Optional[glm.quat], Optional[glm.vec3]]: sx = glm.length(glm.vec3(mat[0])) sy = glm.length(glm.vec3(mat[1])) sz = glm.length(glm.vec3(mat[2])) if glm.determinant(mat) < 0.0: sx = -sx translation = glm.vec3(mat[3]) scale = glm.vec3(sx, sy, sz) inv_sx = 1.0 / sx inv_sy = 1.0 / sy inv_sz = 1.0 / sz rot_mat = copy.copy(mat) rot_mat[0][0] *= inv_sx rot_mat[0][1] *= inv_sx rot_mat[0][2] *= inv_sx rot_mat[1][0] *= inv_sy rot_mat[1][1] *= inv_sy rot_mat[1][2] *= inv_sy rot_mat[2][0] *= inv_sz rot_mat[2][1] *= inv_sz rot_mat[2][2] *= inv_sz rot_mat[3] = glm.vec4(0.0, 0.0, 0.0, 1.0) rotation = glm.normalize(glm.quat_cast(rot_mat)) if translation == glm.vec3(): translation = None if rotation == glm.quat(): rotation = None if scale == glm.vec3(): scale = None return (translation, rotation, scale)
def on_left_dclick(self, event: wx.MouseEvent) -> None: id_ = self._hover_id # id_ belongs to viewcube if self._viewcube.hovered: if id_ == 0: # front self._rot_quat = quat() elif id_ == 1: # top self._rot_quat = quat(glm.radians(vec3(90, 0, 0))) elif id_ == 2: # right self._rot_quat = quat(glm.radians(vec3(0, 0, -90))) elif id_ == 3: # bottom self._rot_quat = quat(glm.radians(vec3(-90, 0, 0))) elif id_ == 4: # left self._rot_quat = quat(glm.radians(vec3(0, 0, 90))) elif id_ == 5: # back self._rot_quat = quat(glm.radians(vec3(0, 0, 180))) else: pass else: # nothing selected if id_ == -1: self.core.select_device(-1) self.core.select_point(-1, clear=True) self.select_object(-1) # id_ belongs to camera device elif -1 < id_ < self._num_devices: self.core.select_device(id_) # id_ belongs to proxy object elif id_ > MAX_ID - self._num_objects: self.select_object(MAX_ID - id_) # id_ belongs to action point else: # un-offset ids self.core.select_point(id_ - self._num_devices, clear=True)