def create_cars(n): """ Create cars. :param n: Number of cars :return: """ # First we scale a car scaled_car = sg.SceneGraphNode('traslated_car') scaled_car.transform = tr.uniform_scale(0.15) scaled_car.childs += [create_car()] # Re-using the previous function _cars = sg.SceneGraphNode('cars') base_name = 'scaled_car' for i in range(n): # A new node is only locating a scaled_car in the scene depending on index i new_node = sg.SceneGraphNode(base_name + str(i)) new_node.transform = tr.translate(0.4 * i - 0.9, 0.9 - 0.4 * i, 0) new_node.childs += [scaled_car] # Now this car is added to the 'cars' scene graph _cars.childs += [new_node] return _cars
def get_center_translation(self): """ Returns center as a translation. :return: Translation matrix """ return tr.translate(self.get_center_x(), self.get_center_y(), self.get_center_z())
def translate(self, tx=0, ty=0, tz=0): """ Translate model. :param tx: :param ty: :param tz: :return: """ self._model = _tr.matmul([_tr.translate(tx, ty, tz), self._model])
def create_car(): """ Create car object. :return: """ gpu_black_quad = es.to_gpu_shape(shapes.create_color_quad(0, 0, 0)) gpu_red_quad = es.to_gpu_shape(shapes.create_color_quad(1, 0, 0)) # Cheating a single wheel wheel = sg.SceneGraphNode('wheel') wheel.transform = tr.uniform_scale(0.2) wheel.childs += [gpu_black_quad] wheel_rotation = sg.SceneGraphNode('wheel_rotation') wheel_rotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts front_wheel = sg.SceneGraphNode('front_wheel') front_wheel.transform = tr.translate(0.3, -0.3, 0) front_wheel.childs += [wheel_rotation] back_wheel = sg.SceneGraphNode('back_wheel') back_wheel.transform = tr.translate(-0.3, -0.3, 0) back_wheel.childs += [wheel_rotation] # Creating the chasis of the car chasis = sg.SceneGraphNode('chasis') chasis.transform = tr.scale(1, 0.5, 1) chasis.childs += [gpu_red_quad] car = sg.SceneGraphNode('car') car.childs += [chasis] car.childs += [front_wheel] car.childs += [back_wheel] traslated_car = sg.SceneGraphNode('traslated_car') traslated_car.transform = tr.translate(0, 0.3, 0) traslated_car.childs += [car] return traslated_car
def create_car(r, g, b): """ Create car model. :param r: :param g: :param b: :return: """ gpu_black_quad = es.to_gpu_shape(shapes.create_color_cube(0, 0, 0)) gpu_chasis_quad = es.to_gpu_shape(shapes.create_color_cube(r, g, b)) # Cheating a single wheel wheel = sg.SceneGraphNode('wheel') wheel.transform = tr.scale(0.2, 0.8, 0.2) wheel.childs += [gpu_black_quad] wheel_rotation = sg.SceneGraphNode('wheel_rotation') wheel_rotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts front_wheel = sg.SceneGraphNode('front_wheel') front_wheel.transform = tr.translate(0.3, 0, -0.3) front_wheel.childs += [wheel_rotation] back_wheel = sg.SceneGraphNode('back_wheel') back_wheel.transform = tr.translate(-0.3, 0, -0.3) back_wheel.childs += [wheel_rotation] # Creating the chasis of the car chasis = sg.SceneGraphNode('chasis') chasis.transform = tr.scale(1, 0.7, 0.5) chasis.childs += [gpu_chasis_quad] # All pieces together car = sg.SceneGraphNode('car') car.childs += [chasis] car.childs += [front_wheel] car.childs += [back_wheel] return car
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()
) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, 'view'), 1, GL_TRUE, view) # Mainloop while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) # Modifying only a specific node in the scene graph wheelRotationNode = sg.find_node(cars, 'wheel_rotation') theta = -10 * glfw.get_time() wheelRotationNode.transform = tr.rotation_z(theta) # Modifying only car 3 car3 = sg.find_node(cars, 'scaled_car3') car3.transform = tr.translate(0.3, 0.5 * np.sin(0.1 * theta), 0) # Uncomment to see the position of scaledCar_3, it will fill your terminal # print('car3_position =', sg.find_position(cars, 'scaled_car3')) # Drawing the Car sg.draw_scene_graph_node(cars, pipeline) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) glfw.terminate()
glGetUniformLocation(pipeline.shaderProgram, 'projection'), 1, GL_TRUE, projection) # 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) # Drawing shapes with different model transformations glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.translate(5, 0, 0)) pipeline.draw_shape(gpuRedCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, tr2.translate(-5, 0, 0)) pipeline.draw_shape(gpuGreenCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, 'model'), 1, GL_TRUE, 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)
controller.theta += 4 * dt if controller.theta >= 2 * np.pi: controller.theta -= 2 * np.pi # Drawing the rotating red quad obj_quad_red.apply_temporal_transform(tr.rotation_z(controller.theta)) obj_quad_red.draw() # Getting the mouse location in opengl coordinates mousePosX = 2 * (controller.mousePos[0] - width / 2) / width mousePosY = 2 * (height / 2 - controller.mousePos[1]) / height # Draw green quad obj_quad_green.apply_temporal_transform( np.matmul(tr.uniform_scale(0.5), tr.translate(mousePosX, mousePosY, 0))) obj_quad_green.draw() # This is another way to work with keyboard inputs # Here we request the state of a given key if glfw.get_key(window, glfw.KEY_LEFT_CONTROL) == glfw.PRESS: obj_quad_blue.apply_temporal_transform( np.matmul(tr.uniform_scale(0.2), tr.translate(-0.6, 0.4, 0.0))) obj_quad_blue.draw() # All "non-pressed" keys are in release state if glfw.get_key(window, glfw.KEY_LEFT_CONTROL) == glfw.RELEASE: obj_quad_yellow.apply_temporal_transform( np.matmul(tr.uniform_scale(0.2), tr.translate(-0.6, 0.6, 0.0))) obj_quad_yellow.draw()
else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Create projection # projection = tr2.ortho(-1, 1, -1, 1, 0.1, 100) projection = tr2.perspective(45, float(width) / float(height), 0.1, 100) # Get camera view matrix view = camera.get_view() # Update target cube object t = tr2.translate(camera.get_center_x(), camera.get_center_y(), camera.get_center_z()) obj_camTarget.apply_temporal_transform(t) # Draw objects obj_axis.draw(view, projection, mode=GL_LINES) obj_camTarget.draw(view, projection) obj_planeL.draw(view, projection) obj_planeR.draw(view, projection) obj_planeS.draw(view, projection) obj_planeB.draw(view, projection) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen glfw.swap_buffers(window) glfw.terminate()
glUseProgram(mvcPipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.85, 0.85, 0.85, 1.0) # As we work in 3D, we need to check which part is in front, # and which one is at the back glEnable(GL_DEPTH_TEST) # Creating shapes on GPU memory gpuAxis = es.to_gpu_shape(shapes.create_axis(7)) redCarNode = create_car(1, 0, 0) blueCarNode = create_car(0, 0, 1) blueCarNode.transform = np.matmul(tr.rotation_z(-np.pi / 4), tr.translate(3.0, 0, 0.5)) # Using the same view and projection matrices in the whole application projection = tr.perspective(45, float(width) / float(height), 0.1, 100) glUniformMatrix4fv( glGetUniformLocation(mvcPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) view = tr.look_at(np.array([5, 5, 7]), np.array([0, 0, 0]), np.array([0, 0, 1])) glUniformMatrix4fv(glGetUniformLocation(mvcPipeline.shaderProgram, 'view'), 1, GL_TRUE, view) # Mainloop while not glfw.window_should_close(window):