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
def create_color_plane_from_curve(_curve, triangulate, r, g, b, center=None): """ Creates a plane from a curve and a center. :param _curve: Curve vertex list :param triangulate: Create plane from curve triangulation :param center: Center position :param r: Red color :param g: Green color :param b: Blue color :return: Merged shape :rtype: AdvancedGPUShape """ shapes = [] # Use delaunay triangulation if triangulate: k = [] for i in _curve: k.append((i[0], i[1])) tri = earclip(k) for i in tri: x1, y1 = i[0] x2, y2 = i[1] x3, y3 = i[2] shape = bs.create_triangle_color((x1, y1, 0), (x2, y2, 0), (x3, y3, 0), r, g, b) shapes.append(es.to_gpu_shape(shape)) else: if center is None: center = _curve[0] for i in range(0, len(_curve) - 1): x1, y1 = _curve[i] x2, y2 = _curve[(i + 1) % len(_curve)] c1, c2 = center shape = bs.create_triangle_color((x1, y1, 0), (x2, y2, 0), (c1, c2, 0), r, g, b) shapes.append(es.to_gpu_shape(shape)) return AdvancedGPUShape(shapes)
textureFlatPipeline = es.SimpleTextureFlatShaderProgram() textureGouraudPipeline = es.SimpleTextureGouraudShaderProgram() texturePhongPipeline = es.SimpleTexturePhongShaderProgram() # This shader program does not consider lighting colorPipeline = es.SimpleModelViewProjectionShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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(4)) gpuTextureCube = es.to_gpu_shape(shapes.create_texture_normals_cube('example_data/bricks.jpg'), GL_REPEAT, GL_LINEAR) t0 = glfw.get_time() camera_theta = np.pi / 4 # Create light obj_light = Light(position=[5, 5, 5], color=[1, 1, 1]) # Mainloop while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events()
# A simple shader program with position and texture coordinates as inputs. pipeline = es.SimpleTextureTransformShaderProgram() # Telling OpenGL to use our shader program glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.25, 0.25, 0.25, 1.0) # Enabling transparencies glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Creating shapes on GPU memory gpuBoo = es.to_gpu_shape( shapes.create_texture_quad('example_data/boo.png'), GL_REPEAT, GL_NEAREST) gpuQuestionBox = es.to_gpu_shape( shapes.create_texture_quad('example_data/question_box.png', 10, 1), GL_REPEAT, GL_NEAREST) # Create objets obj_boo = AdvancedGPUShape(gpuBoo, shader=pipeline) obj_question = AdvancedGPUShape(gpuQuestionBox, shader=pipeline) # Apply object transform obj_question.scale(sx=-2, sy=0.2) obj_question.translate(ty=-0.8) # Mainloop while not glfw.window_should_close(window):
# Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Creating shader programs for textures and for colores colorShaderProgram = es.SimpleModelViewProjectionShaderProgram() phongPipeline = es.SimplePhongShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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) # Create models gpuAxis = es.to_gpu_shape(shapes.create_axis(1)) obj_axis = AdvancedGPUShape(gpuAxis, shader=colorShaderProgram) # Create cilynder, the objective is create many cuads from the bottom, top and # mantle. The cilynder is parametrized using an angle theta, a radius r and # the height h = 1 r = 0.25 # Latitude and longitude of the cylinder, latitude subdivides theta, longitude # subdivides h lat = 20 lon = 20 # Angle step dang = 2 * np.pi / lat
# Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Creating shader programs for textures and for colores phongPipeline = es.SimplePhongShaderProgram() colorShaderProgram = es.SimpleModelViewProjectionShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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) # Create models gpuAxis = es.to_gpu_shape(shapes.create_axis(1)) obj_axis = AdvancedGPUShape(gpuAxis, shader=colorShaderProgram) # Create the grid of the system vertex_grid = [] nx = 40 ny = 40 zlim = [0, 0] for i in range(nx): # x for j in range(ny): # y xp = -1 + 2 / (nx - 1) * j yp = -1 + 2 / (ny - 1) * i zp = f(xp, yp, 4) # Function number 4 zlim = [min(zp, zlim[0]), max(zp, zlim[1])] vertex_grid.append([xp, yp, zp])
# Assembling the shader program pipeline = es.SimpleModelViewProjectionShaderProgram() # Telling OpenGL to use our shader program glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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(bs.create_axis(7)) gpuRedCube = es.to_gpu_shape(bs.create_color_cube(1, 0, 0)) gpuGreenCube = es.to_gpu_shape(bs.create_color_cube(0, 1, 0)) gpuBlueCube = es.to_gpu_shape(bs.create_color_cube(0, 0, 1)) gpuYellowCube = es.to_gpu_shape(bs.create_color_cube(1, 1, 0)) gpuCyanCube = es.to_gpu_shape(bs.create_color_cube(0, 1, 1)) gpuPurpleCube = es.to_gpu_shape(bs.create_color_cube(1, 0, 1)) gpuRainbowCube = es.to_gpu_shape(bs.create_rainbow_cube()) t0 = glfw.get_time() camera_theta = np.pi / 4 # Mainloop while not glfw.window_should_close(window): # Using GLFW to check for input events
# Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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) # Create shader colorShaderProgram = es.SimpleTransformShaderProgram() # Creating shapes on GPU memory gpuCube = es.to_gpu_shape(shapes.create_rainbow_cube()) cube = AdvancedGPUShape(gpuCube, shader=colorShaderProgram) # Mainloop while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # 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) # Clearing the screen in both, color and depth
glfw.set_scroll_callback(window, scroll_callback) # Create shader basicShader = es.SimpleTransformShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 1.0) # Creating shapes on GPU memory blueQuad = shapes.create_color_quad(0, 0, 1) redQuad = shapes.create_color_quad(1, 0, 0) yellowQuad = shapes.create_color_quad(1, 1, 0) greenQuad = shapes.create_color_quad(0, 1, 0) # Create objects obj_quad_blue = AdvancedGPUShape(es.to_gpu_shape(blueQuad), shader=basicShader) obj_quad_red = AdvancedGPUShape(es.to_gpu_shape(redQuad), shader=basicShader) obj_quad_yellow = AdvancedGPUShape(es.to_gpu_shape(yellowQuad), shader=basicShader) obj_quad_green = AdvancedGPUShape(es.to_gpu_shape(greenQuad), shader=basicShader) # Apply permanent transforms obj_quad_red.translate(tx=0.5) obj_quad_red.uniform_scale(0.5) # Polyfon fill mode glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
flatPipeline = es.SimpleFlatShaderProgram() gouraudPipeline = es.SimpleGouraudShaderProgram() phongPipeline = es.SimplePhongShaderProgram() # This shader program does not consider lighting mvpPipeline = es.SimpleModelViewProjectionShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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(4)) gpuRedCube = es.to_gpu_shape(shapes.create_color_normals_cube(1, 0, 0)) gpuGreenCube = es.to_gpu_shape(shapes.create_color_normals_cube(0, 1, 0)) gpuBlueCube = es.to_gpu_shape(shapes.create_color_normals_cube(0, 0, 1)) gpuYellowCube = es.to_gpu_shape(shapes.create_color_normals_cube(1, 1, 0)) gpuRainbowCube = es.to_gpu_shape(shapes.create_rainbow_normals_cube()) t0 = glfw.get_time() camera_theta = np.pi / 4 # Projection matrix # projection = tr.ortho(-1, 1, -1, 1, 0.1, 100) projection = tr.perspective(45, float(width) / float(height), 0.1, 100) # Create light obj_light = Light(shader=phongPipeline,
# Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Creating shader programs for textures and for colores textureShaderProgram = es.SimpleTextureModelViewProjectionShaderProgram() colorShaderProgram = es.SimpleModelViewProjectionShaderProgram() # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 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) # Create models gpuAxis = es.to_gpu_shape(bs.create_axis(1)) obj_axis = AdvancedGPUShape(gpuAxis, shader=colorShaderProgram) # Create one side of the wall vertices = [[1, 0], [0.9, 0.4], [0.5, 0.5], [0, 0.5]] curve = catrom.get_spline_fixed(vertices, 10) obj_planeL = create_color_plane_from_curve(curve, False, 0.6, 0.6, 0.6, center=(0, 0)) obj_planeL.uniform_scale(1.1) obj_planeL.rotation_x(np.pi / 2) obj_planeL.rotation_z(-np.pi / 2)
# Assembling the shader program (pipeline) with both shaders mvcPipeline = es.SimpleModelViewProjectionShaderProgram() # Telling OpenGL to use our shader program 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'),