def render(self): trans_mat = glm.mat4(1.0, dtype=c_float) trans_mat = glm.translate(trans_mat, self.position) loc_trans_mat = glm.mat4(1.0, dtype=c_float) loc_trans_mat = glm.translate(loc_trans_mat, self.local_position) rot_mat = glm.mat4_cast(self.rotation) loc_rot_mat = glm.mat4_cast(self.local_rotation) # push transformations to stack sm.CURRENT_PROGRAM.push() # sm.CURRENT_PROGRAM.model_mat4(glm.mat4(1.0, dtype=c_float).value) sm.CURRENT_PROGRAM.model_mat4(trans_mat.value) sm.CURRENT_PROGRAM.model_mat4(rot_mat.value) sm.CURRENT_PROGRAM.model_mat4(loc_trans_mat.value) sm.CURRENT_PROGRAM.model_mat4(loc_rot_mat.value) # disable depth test if self.disable_depth is True gl.glEnable(gl.GL_DEPTH_TEST) # is_depth_enabled = gl.glIsEnabled(gl.GL_DEPTH_TEST) # if self.disable_depth and is_depth_enabled: # gl.glDisable(gl.GL_DEPTH_TEST) # draw the mesh self._mesh.render() # re-enable depth test if depth test was enabled prior to disabling # if self.disable_depth and is_depth_enabled: # gl.glEnable(gl.GL_DEPTH_TEST) # render children for i in range(len(self._children)): # pushing here separates the transformations of the children from the parent # sm.CURRENT_PROGRAM.push() self._children[i].render() # sm.CURRENT_PROGRAM.pop() # pop transformations from stack sm.CURRENT_PROGRAM.pop()
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 render(self) -> None: """Render ViewCube to canvas.""" if not self.init(): return glViewport(*self._get_viewport()) proj = glm.ortho(-2.3, 2.3, -2.3, 2.3, -2.3, 2.3) mat = glm.lookAt( vec3(0.0, -1.0, 0.0), # position vec3(0.0, 0.0, 0.0), # target vec3(0.0, 0.0, 1.0)) # up modelview = mat * glm.mat4_cast(self.parent.rot_quat) glUseProgram(self.parent.shaders['default']) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(proj)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(modelview)) glBindVertexArray(self._vao) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, ctypes.c_void_p(0)) self._render_highlighted() glBindVertexArray(0) glUseProgram(0)
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 modelview_matrix(self) -> mat4: """Returns a mat4 representing the current modelview matrix.""" mat = glm.lookAt( vec3(0.0, -self._dist * 2.0 / self._zoom, 0.0), # eye vec3(0.0, 0.0, 0.0), # center vec3(0.0, 0.0, 1.0)) # up return glm.translate(mat * glm.mat4_cast(self._rot_quat), self._center)
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 _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 move_local(self, xyz): """Move by given displacement in local coordinate system. Args: xyz (glm.vec3): Displacement. """ rxyz = glm.mat4_cast(self.rotation) * glm.vec4(xyz, 1.) self.position += rxyz.xyz
def _drawOrientationIndicator(self): gloo.set_state(line_width=5) # we want the rotation of the camera but not the translation view = glm.inverse(glm.mat4_cast(self.cam._currentTransform.orientation)) view = glm.scale( view, glm.vec3(0.25)) self._oriProgram['projection'] = self._oriProjection self._oriProgram['view'] = view self._oriProgram.draw('lines')
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 getTransform(self): if self.transform_dirty: translationMat = glm.translate(glm.mat4(1.0), self.position - self.origin) scaleMat = glm.scale(glm.mat4(1.0), self.scale) rotationMat = glm.mat4_cast(self.rotation) self.transform = translationMat * rotationMat * scaleMat self.transform_dirty = False return self.transform
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 mat4(self) -> glm.Mat4: m = glm.mat4_cast(self.quaternion) for i in range(3): m[i][0] *= self.scale[i] m[i][1] *= self.scale[i] m[i][2] *= self.scale[i] pass m[3][0] += self.translation[0] m[3][1] += self.translation[1] m[3][2] += self.translation[2] return m
def get_matrix(self): if self.__transformations_changed: transformation = glm.mat4x4() transformation = glm.translate(transformation, self.position) transformation = transformation * glm.mat4_cast(self.rotation) transformation = glm.scale(transformation, self.scale) self.__transformation_matrix = transformation self.__transformations_changed = False if self.parent is None: return self.__transformation_matrix else: return self.parent.get_matrix() * self.__transformation_matrix
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 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 matrix(self): if self._itemz != [ self.anchor, self.scale0, self.rotation, self.position ]: self._itemz = copy.deepcopy( [self.anchor, self.scale0, self.rotation, self.position]) t0 = glm.translate(_i4, self.position) r0 = t0 * glm.mat4_cast(self.rotation) s0 = glm.scale(r0, self.scale0) a0 = glm.translate(s0, -self.anchor) self._matrix = a0 self._dirty.add('_matrix') return self._matrix
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 render_for_picking(self) -> None: """Render ViewCube for picking pass.""" if not self.init(): return glViewport(*self.viewport) proj = glm.ortho(-2.3, 2.3, -2.3, 2.3, -2.3, 2.3) mat = glm.lookAt( vec3(0.0, -1.0, 0.0), # position vec3(0.0, 0.0, 0.0), # target vec3(0.0, 0.0, 1.0)) # up modelview = mat * glm.mat4_cast(self.parent.rot_quat) glUseProgram(self.parent.shaders['default']) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(proj)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(modelview)) glBindVertexArray(self._vao_picking) glDrawArrays(GL_QUADS, 0, 24) glBindVertexArray(0) glUseProgram(0)
def local_transform(self): mat = glm.translate(glm.mat4(1.0), self.position) return mat * glm.mat4_cast(self.rotation)
def mat4(self): return glm.translate(glm.mat4(1.0), self.position) * glm.scale( glm.mat4(1.0), self.scale) * glm.mat4_cast(self.orientation)
def DIF(data, data_root, filename): ''' convert global feature to DIF feature ''' id_root = 0 id_leftfoot = 19 id_right_foot = 16 if (np.sum(np.abs(data[:, :, 1])) < 0.001): num_actor = 1 else: num_actor = 2 print("num_actor:{}".format(num_actor)) for actor in range(num_actor): for i in range(data.shape[0]): quat_root = glm.quat(data_root[i, 0, actor], data_root[i, 1, actor], data_root[i, 2, actor], data_root[i, 3, actor]) R_root = glm.mat4_cast(quat_root) x_axis = R_root * glm.vec4(1, 0, 0, 1) x_axis_new = np.array([x_axis[0], 0, x_axis[2]]) x_axis_new /= np.linalg.norm(x_axis_new) y_axis_new = np.array([0, 1, 0]) z_axis_new = np.cross(x_axis_new, y_axis_new) R = np.eye(4) R[0:3, 0] = x_axis_new R[0:3, 1] = y_axis_new R[0:3, 2] = z_axis_new T = np.eye(4) T[0, 3] = data[i, 0, actor] T[2, 3] = data[i, 2, actor] T[1, 3] = 0.5 * (data[i, id_leftfoot * 3 + 1, actor] + data[i, id_right_foot * 3 + 1, actor]) M = np.linalg.inv(np.dot(T, R)) rotation_matrix = glm.mat3([ x_axis_new.tolist(), y_axis_new.tolist(), z_axis_new.tolist() ]) rotation_matrix = glm.mat4(rotation_matrix) for j in range(25): #embed() pos = np.array([ data[i, j * 7, actor], data[i, 7 * j + 1, actor], data[i, 7 * j + 2, actor], 1 ]) pos = np.dot(M, pos) quat = glm.quat(data[i, 7 * j + 3, actor], data[i, 7 * j + 4, actor], data[i, 7 * j + 5, actor], data[i, 7 * j + 6, actor]) rotation_camera = glm.mat4_cast(quat) rotation_local = glm.inverse( rotation_matrix) * rotation_camera * rotation_matrix quat_local = glm.quat_cast(rotation_local) # make sure the W part of quaternion is larger than 0 if (quat_local[3] < 0): quat_local = -quat_local #print(np.linalg.norm(data[i,7*j+3:7*j+7])) for k in range(3): data[i, 7 * j + k, actor] = pos[k] for k in range(4): data[i, 7 * j + 3 + k, actor] = quat_local[(3 + k) % 4] #print("###") return data