def draw_scene_graph_node(node, pipeline, parent_transform=_tr.identity()): """ :param node: :param pipeline: :param parent_transform: :return: """ assert (isinstance(node, SceneGraphNode)) # Composing the transformations through this path new_transform = np.matmul(parent_transform, 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], _GPUShape): leaf = node.childs[0] glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, new_transform) pipeline.draw_shape(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: draw_scene_graph_node(child, pipeline, new_transform)
def find_transform(node, _name, parent_transform=_tr.identity()): """ :param node: :param _name: :param parent_transform: :return: """ # The name was not found in this path if isinstance(node, _GPUShape): return None new_transform = np.matmul(parent_transform, node.transform) # This is the requested node if node.name == _name: return new_transform # All childs are checked for the requested name else: for child in node.childs: found_transform = find_transform(child, _name, new_transform) if isinstance(found_transform, (np.ndarray, np.generic)): return found_transform # No child of this node had the requested name return None
def find_position(node, _name, parent_transform=_tr.identity()): """ :param node: :param _name: :param parent_transform: :return: """ found_transform = find_transform(node, _name, parent_transform) if isinstance(found_transform, (np.ndarray, np.generic)): zero = np.array([[0, 0, 0, 1]], dtype=np.float32).T found_position = np.matmul(found_transform, zero) return found_position return None
def __init__(self, shapes, model=_tr.identity(), enabled=True, shader=None, mode=None): """ Constructor. :param shapes: List or GPUShape object :param model: Basic model transformation matrix :param enabled: Indicates if the shape is enabled or not :param shader: Shader program """ if not isinstance(shapes, list): shapes = [shapes] for i in range(len(shapes)): if not isinstance(shapes[i], _GPUShape): raise Exception('Object {0} of shapes list is not GPUShape instance'.format(i)) if mode is None: mode = _GL_TRIANGLES self._shapes = shapes self._model = model self._modelPrev = None self._enabled = enabled self._shader = shader self._drawMode = mode
if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS: camera_theta += 2 * dt projection = tr.perspective(45, float(width) / float(height), 0.1, 100) camX = 3 * np.sin(camera_theta) camY = 3 * np.cos(camera_theta) viewPos = np.array([camX, camY, 2]) view = tr.look_at( viewPos, np.array([0, 0, 0]), np.array([0, 0, 1]) ) model = tr.identity() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if controller.fillPolygon: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # The axis is drawn without lighting effects if controller.showAxis: glUseProgram(colorPipeline.shaderProgram) glUniformMatrix4fv(glGetUniformLocation(colorPipeline.shaderProgram, 'projection'), 1, GL_TRUE, projection) glUniformMatrix4fv(glGetUniformLocation(colorPipeline.shaderProgram, 'view'), 1, GL_TRUE, view)
# OpenGL polygon mode glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) theta = glfw.get_time() tx = 0.7 * np.sin(0.5 * theta) ty = 0.2 * np.sin(5 * theta) # Derivative of tx give us the direction dtx = 0.7 * 0.5 * np.cos(0.5 * theta) if dtx > 0: reflex = tr.scale(-1, 1, 1) else: reflex = tr.identity() # Create booObj transformation booT = tr.matmul( [tr.translate(tx, ty, 0), tr.scale(0.5, 0.5, 1.0), reflex]) obj_boo.apply_temporal_transform(booT) # Draw shapes obj_boo.draw() obj_question.draw() # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) glfw.terminate()
def __init__(self, _name): self.name = _name self.transform = _tr.identity() self.childs = []
tr2.translate(0, 5, 0)) pipeline.draw_shape(gpuBlueCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.translate(0, -5, 0)) pipeline.draw_shape(gpuYellowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.translate(0, 0, 5)) pipeline.draw_shape(gpuCyanCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.translate(0, 0, -5)) pipeline.draw_shape(gpuPurpleCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.identity()) pipeline.draw_shape(gpuRainbowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.identity()) pipeline.draw_shape(gpuAxis, GL_LINES) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) glfw.terminate()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # The axis is drawn without lighting effects if controller.showAxis: glUseProgram(mvpPipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, 'projection'), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, 'view'), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, 'model'), 1, GL_TRUE, tr.identity()) mvpPipeline.draw_shape(gpuAxis, GL_LINES) # Selecting the shape to display if controller.shape == SHAPE_RED_CUBE: gpuShape = gpuRedCube elif controller.shape == SHAPE_GREEN_CUBE: gpuShape = gpuGreenCube elif controller.shape == SHAPE_BLUE_CUBE: gpuShape = gpuBlueCube elif controller.shape == SHAPE_YELLOW_CUBE: gpuShape = gpuYellowCube elif controller.shape == SHAPE_RAINBOW_CUBE: gpuShape = gpuRainbowCube else: raise Exception()