def getTransform(showTransform, theta): if showTransform == TR_STANDARD: return tr.identity() elif showTransform == TR_ROTATE_ZP: return tr.rotationZ(theta) elif showTransform == TR_ROTATE_ZM: return tr.rotationZ(-theta) elif showTransform == TR_TRANSLATE: return tr.translate(0.3 * np.cos(theta), 0.3 * np.cos(theta), 0) elif showTransform == TR_UNIFORM_SCALE: return tr.uniformScale(0.7 + 0.5 * np.cos(theta)) elif showTransform == TR_NONUNIF_SCALE: return tr.scale(1.0 - 0.5 * np.cos(1.5 * theta), 1.0 + 0.5 * np.cos(2 * theta), 1.0) elif showTransform == TR_REFLEX_Y: return tr.scale(1, -1, 1) elif showTransform == TR_SHEARING_XY: return tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0) else: # This should NEVER happend raise Exception()
def drawCustomSceneGraphNode(node, pipeline, transformName, parentTransform=tr.identity()): # Dibujara los nodos con el pipeline que se encuentre en el atributo curr_pipeline para usar distintos shaders en un solo grafo assert (isinstance(node, CustomSceneGraphNode)) # Composing the transformations through this path newTransform = np.matmul(parentTransform, node.transform) if node.curr_pipeline != None: # cambia el pipeline a usar por el resto del sub grafo pipeline = node.curr_pipeline glUseProgram(pipeline.shaderProgram) # If the child node is a leaf, it should be a GPUShape. # Hence, it can be drawn with drawCall if len(node.childs) == 1 and isinstance(node.childs[0], gs.GPUShape): leaf = node.childs[0] glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, transformName), 1, GL_TRUE, newTransform) glUniform1f(glGetUniformLocation(pipeline.shaderProgram, "green"), node.green) pipeline.drawCall(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: drawCustomSceneGraphNode(child, pipeline, transformName, newTransform)
def findPosition(node, name, parentTransform=tr.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 createBackground(pipeline, i): # Se crea el fondo con un grafo de escena de shapes greenQuad = bs.createColorQuad(0.0, 0.6, 0.0) greyQuad = bs.createColorQuad(0.6, 0.6, 0.6) whiteQuad = bs.createColorQuad(255 / 255, 242 / 255, 0) gpuGrass = createGPUShape(greenQuad, pipeline) gpuStreet = createGPUShape(greyQuad, pipeline) gpuLine = createGPUShape(whiteQuad, pipeline) grassNode = sg.SceneGraphNode("grass") grassNode.transform = tr.scale(4, 2, 1) grassNode.childs = [gpuGrass] streetNode = sg.SceneGraphNode("street") streetNode.transform = tr.scale(1, 2, 1) streetNode.childs = [gpuStreet] lineNode = sg.SceneGraphNode("line") lineNode.transform = tr.scale(0.1, 0.25, 1) lineNode.childs = [gpuLine] trLineNode1 = sg.SceneGraphNode("trline1") trLineNode1.transform = tr.identity() trLineNode1.childs = [lineNode] trLineNode2 = sg.SceneGraphNode("trline2") trLineNode2.transform = tr.translate(0, 0.4, 0) trLineNode2.childs = [lineNode] trLineNode3 = sg.SceneGraphNode("trline3") trLineNode3.transform = tr.translate(0, -0.4, 0) trLineNode3.childs = [lineNode] trLineNode4 = sg.SceneGraphNode("trline4") trLineNode4.transform = tr.translate(0, -0.8, 0) trLineNode4.childs = [lineNode] trLineNode5 = sg.SceneGraphNode("trline5") trLineNode5.transform = tr.translate(0, 0.8, 0) trLineNode5.childs = [lineNode] lineGroupNode = sg.SceneGraphNode("lineGroup") lineGroupNode.transform = tr.translate(0, 0, 0) lineGroupNode.childs = [ trLineNode1, trLineNode2, trLineNode3, trLineNode4, trLineNode5 ] finalNode = sg.SceneGraphNode("final" + str(i)) finalNode.childs = [grassNode, streetNode, lineGroupNode] return finalNode
def findTransform(node, name, parentTransform=tr.identity()): # The name was not found in this path if isinstance(node, gs.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 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 drawSceneGraphNode(node, pipeline, transformName, parentTransform=tr.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 drawCall if len(node.childs) == 1 and isinstance(node.childs[0], gs.GPUShape): leaf = node.childs[0] glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, transformName), 1, GL_TRUE, newTransform) pipeline.drawCall(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, transformName, newTransform)
def createDecorations(pipeline, tex_pipeline, x_range, y_range, n, i_z): # Se crean las decoraciones con un grafo de escena que admite shapes y texturas triangleShape = createTriangle() quadShape = createQuad() whiteQuad = bs.createColorQuad(1, 1, 1) greyQuad = bs.createColorQuad(0.8, 0.8, 0.8) umbrellaShape = createUmbrellaCircle() gpuLeaves = createTextureGPUShape(triangleShape, tex_pipeline, getAssetPath("hojas_arboles.jpg")) gpuTrunk = createTextureGPUShape(quadShape, tex_pipeline, getAssetPath("tronco.jpeg")) gpuWhiteQuad = createGPUShape(whiteQuad, pipeline) gpuGreyQuad = createGPUShape(greyQuad, pipeline) gpuUmbrella = createGPUShape(umbrellaShape, pipeline) leavesNode = csg.CustomSceneGraphNode("hojas") leavesNode.transform = tr.translate(0, 0.5, 0) leavesNode.childs = [gpuLeaves] trunkNode = csg.CustomSceneGraphNode("tronco") trunkNode.transform = tr.matmul( [tr.translate(0, -0.25, 0), tr.scale(0.3, 0.5, 1)]) trunkNode.childs = [gpuTrunk] scaledTreeNode = csg.CustomSceneGraphNode("arbol_escalado") scaledTreeNode.transform = tr.uniformScale(0.3) scaledTreeNode.childs = [trunkNode, leavesNode] treeGroupNode = csg.CustomSceneGraphNode("grupo_arboles") # Se crean arboles en posiciones generadas al azar for i in range(n): x = 100 - x_range * 100 x_rand = rd.randint(0, x) * 0.01 r_x = x_rand + x_range coin = rd.randint(0, 1) if coin: r_x *= -1 r_y = rd.randint(y_range[0] * 100, y_range[1] * 100) * 0.01 treeNode = csg.CustomSceneGraphNode("arbol_" + str(i)) treeNode.transform = tr.translate(r_x, r_y, 0) treeNode.childs = [scaledTreeNode] treeGroupNode.childs += [treeNode] treeGroupNode.childs.sort(reverse=True, key=height) cartBodyNode = csg.CustomSceneGraphNode("ej") cartBodyNode.transform = tr.scale(0.3, 0.5, 1) cartBodyNode.childs = [gpuWhiteQuad] cartInsideNode = csg.CustomSceneGraphNode("inside") cartInsideNode.transform = tr.scale(0.2, 0.3, 1) cartInsideNode.childs = [gpuGreyQuad] umbrellaNode = csg.CustomSceneGraphNode("umbrella") umbrellaNode.transform = tr.rotationZ(math.pi / 8) umbrellaNode.childs = [gpuUmbrella] umbrellaScaled = csg.CustomSceneGraphNode("umbrellaS") umbrellaScaled.transform = tr.scale(0.3, 0.3, 1) umbrellaScaled.childs = [umbrellaNode] umbrellaTranslated = csg.CustomSceneGraphNode("umbrellaT") umbrellaTranslated.transform = tr.translate(-0.1, 0.1, 0) umbrellaTranslated.childs = [umbrellaScaled] cartNode = csg.CustomSceneGraphNode("cart") cartNode.transform = tr.translate(0.8, -0.5, 0) cartNode.childs = [cartBodyNode, cartInsideNode, umbrellaTranslated] regularNode = csg.CustomSceneGraphNode("regular") regularNode.transform = tr.identity() regularNode.childs = [cartNode] regularNode.curr_pipeline = pipeline texNode = csg.CustomSceneGraphNode("tex") texNode.childs = [treeGroupNode] texNode.curr_pipeline = tex_pipeline decNode = csg.CustomSceneGraphNode("decorations" + str(i_z)) decNode.childs = [regularNode, texNode] return decNode
view = tr.lookAt( np.array([5,5,2]), np.array([0,0,0]), np.array([0,0,1]) ) theta = glfw.get_time() axis = np.array([1,-1,1]) axis = axis / np.linalg.norm(axis) model = tr.rotationA(theta, axis) # Drawing axes and cube in a 3D world 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.drawCall(gpuAxis, GL_LINES) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, model) mvpPipeline.drawCall(gpuRainbowCube) 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.identity() else: reflex = tr.scale(-1, 1, 1)
if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # 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.identity() else: reflex = tr.scale(-1, 1, 1) # Drawing the shapes glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul( [tr.translate(tx, ty, 0), tr.scale(0.5, 0.5, 1.0), reflex])) pipeline.drawCall(gpuBoo) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, questionBoxesTransform)
def __init__(self, name): self.name = name self.transform = tr.identity() self.childs = [] self.curr_pipeline = None self.green = 0
theta = glfw.get_time() axis = np.array([1, -1, 1]) axis = axis / np.linalg.norm(axis) model = tr.rotationA(theta, axis) # Drawing axes and cube in a 3D world 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.drawCall(gpuAxis, GL_LINES) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, model) mvpPipeline.drawCall(gpuRainbowCube) 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.identity()
glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, -5, 0)) pipeline.drawCall(gpuYellowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, 0, 5)) pipeline.drawCall(gpuCyanCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, 0, -5)) pipeline.drawCall(gpuPurpleCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) pipeline.drawCall(gpuRainbowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) pipeline.drawCall(gpuAxis, GL_LINES) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) # freeing GPU memory gpuAxis.clear() gpuRedCube.clear() gpuGreenCube.clear() gpuBlueCube.clear()
theta = glfw.get_time() axis = np.array([1, -1, 1]) axis = axis / np.linalg.norm(axis) model = tr.rotationA(theta, axis) # Drawing axes (no texture) glUseProgram(colorShaderProgram.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) colorShaderProgram.drawCall(gpuAxis, GL_LINES) # Drawing dice (with texture, another shader program) glUseProgram(textureShaderProgram.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(textureShaderProgram.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(textureShaderProgram.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(textureShaderProgram.shaderProgram, "model"), 1, GL_TRUE, model) textureShaderProgram.drawCall(gpuDice)
def __init__(self, name): self.name = name self.transform = tr.identity() self.childs = []
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.lookAt(viewPos, np.array([0, 0, 0]), np.array([0, 0, 1])) rotation_theta = glfw.get_time() axis = np.array([1, -1, 1]) #axis = np.array([0,0,1]) axis = axis / np.linalg.norm(axis) model = tr.rotationA(rotation_theta, axis) 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(mvpPipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, "projection"),