def test_fill_rect_rgb(self): s = Surface(100, 100) s.fill_rect(gl.recti(0, 0, 10, 10), gl.col3f("red")) s.fill_rect(gl.recti(0, 90, 10, 100), gl.col3f("green")) s.fill_rect(gl.recti(90, 0, 100, 10), gl.col3f("blue")) s.fill_rect(gl.recti(90, 90, 100, 100), gl.col3f("white")) self._compare_surface(s, "test_fill_rect_rgb")
def __init__(self): self.window = cube.system.create_window("Test mesh", 640, 480, cube.gl.renderer.Name.OpenGL) w = h = 1 m = Mesh() m.mode = Mode.quads m.append( Kind.vertex, vec3f(0, 0, 0), vec3f(w, 0, 0), vec3f(w, 0, h), vec3f(0, 0, h), # vec3f(w, h, 0), # vec3f(-w, h, 0), # vec3f(-w, -h, 0), # vec3f(w, -h, 0), Kind.color, col3f("whitesmoke"), col3f("whitesmoke"), col3f("whitesmoke"), col3f("#000042"), ) print(m) r = self.window.renderer self.view = m.view(r) self.shader = r.new_shader_program( [r.new_vertex_shader([vertex_shader]), r.new_fragment_shader([fragment_shader])] ) self.running = False self.window.inputs.on_quit.connect(self.stop) self.window.inputs.on_resize.connect(self._on_resize) self.window.poll() self._on_resize(640, 480)
def create(game): mat = gl.Material('ground') mat.ambient = gl.col3f('black') mat.diffuse = gl.col3f('brown') mat.specular = gl.col3f('gray') mat.shininess = 1000 mat.shading_model = gl.material.ShadingModel.gouraud mat.add_texture("ground.bmp", gl.TextureType.ambient, gl.TextureMapping.uv, gl.StackOperation.add, gl.TextureMapMode.wrap, gl.BlendMode.basic) mesh = gl.Mesh() mesh.mode = gl.DrawMode.quads size = 40 step = 1.0 / size for i in (i / size for i in range(size)): x = i for j in (i / size for i in range(size)): y = 1.0 - j mesh.kind = gl.ContentKind.vertex mesh.append(gl.vec3f(x, y, 0)) mesh.append(gl.vec3f(x, y - step, 0)) mesh.append(gl.vec3f(x + step, y - step, 0)) mesh.append(gl.vec3f(x + step, y, 0)) mesh.kind = gl.ContentKind.tex_coord0 mesh.append(gl.vec2f(i, j)) mesh.append(gl.vec2f(i, j + step)) mesh.append(gl.vec2f(i + step, j + step)) mesh.append(gl.vec2f(i + step, j)) mesh.kind = gl.ContentKind.normal mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) light = game.renderer.new_light( gl.PointLightInfo( gl.vec3f(0, 2, -1), gl.Color3f("#888"), gl.Color3f("#333"), )) ground = game.entity_manager.create("ground") ground.add_component( cubeapp.game.entity.component.Transform( gl.matrix.rotate( gl.matrix.translate(gl.matrix.scale(gl.mat4f(), gl.vec3f(20)), gl.vec3f(-.5, 0, 0)), units.deg(-90), gl.vec3f(1, 0, 0)))) ground.add_component(cubeapp.game.entity.component.Bindable(mat)) ground.add_component(cubeapp.game.entity.component.Drawable(mesh)) return ground
def __init__(self, renderer): self.renderer = renderer self.__materials = {} for lod in range(8): mat = gl.Material('ground-%s' % lod) mat.ambient = gl.col3f('#' + ('%s' % (lod + 1)) * 3) self.__materials[lod] = mat.bindable(renderer) mesh = gl.Mesh() mesh.mode = gl.DrawMode.quads size = 16 step = 1.0 / size for i in (i / size for i in range(size)): x = i for j in (i / size for i in range(size)): y = 1.0 - j mesh.kind = gl.ContentKind.vertex mesh.append(gl.vec3f(x, 0, y - step)) mesh.append(gl.vec3f(x, 0, y)) mesh.append(gl.vec3f(x + step, 0, y)) mesh.append(gl.vec3f(x + step, 0, y - step)) mesh.kind = gl.ContentKind.tex_coord0 mesh.append(gl.vec2f(i, j + step)) mesh.append(gl.vec2f(i + step, j + step)) mesh.append(gl.vec2f(i + step, j)) mesh.append(gl.vec2f(i, j)) mesh.kind = gl.ContentKind.normal mesh.append(gl.vec3f(0, 1, 0)) mesh.append(gl.vec3f(0, 1, 0)) mesh.append(gl.vec3f(0, 1, 0)) mesh.append(gl.vec3f(0, 1, 0)) self.mesh = mesh.drawable(self.renderer) self.__chunks = {}
def create(game, x, z): mat = gl.Material('player') mat.ambient = gl.col3f('#100') mat.diffuse = gl.col3f('#f55') mat.specular = gl.col3f('#f00') mat.shininess = .5 mat.shading_model = gl.material.ShadingModel.gouraud mesh = gl.Spheref(gl.vec3f(0), 1).drawable(game.renderer) entity = game.entity_manager.create('Darkling') transform = entity.add_component( Transform(gl.matrix.translate(gl.mat4f(), gl.vec3f(x, 1, z)))) entity.add_component(Bindable(mat)) entity.add_component(Drawable(mesh)) return entity
def create(game, x, z): mat = gl.Material('player') mat.ambient = gl.col3f('#100') mat.diffuse = gl.col3f('#f55') mat.specular = gl.col3f('#f00') mat.shininess = .5 mat.shading_model = gl.material.ShadingModel.gouraud mesh = gl.Spheref(gl.vec3f(0), 1).drawable(game.renderer) entity = game.entity_manager.create('Darkling') transform = entity.add_component( Transform(gl.matrix.translate(gl.mat4f(), gl.vec3f(x, 1, z))) ) entity.add_component(Bindable(mat)) entity.add_component(Drawable(mesh)) return entity
def __call__(self, ev, delta): tiles = pos_to_tiles(self.board, ev.new, ev.player.radius) ev.player.force = gl.vec3f(0) self.current_tiles = tiles colliding = [] for tile in tiles: controllers = self.cells.get(tile, []) for controller in controllers: controller(self.board, tile, ev.player, colliding) for tile in self.last_colliding: if tile not in colliding: self.cells.get(tile)[0].prepare(self.board, tile) self.last_colliding = colliding tile_w, tile_h = self.board.tile_width, self.board.tile_height border = gl.vec2f(self.board.border, self.board.border) for i, tile in enumerate(colliding): self.cells.get(tile)[0].set_color(self.board, tile, gl.col3f('purple')) tile_pos = gl.vec2f(tile.x * tile_w, tile.y * tile_h) + border print("PIF!", i, ev.new, tile, tile_pos, self.board.border, tile_w, tile_h) if ev.new.x >= tile_pos.x and \ ev.new.x < tile_pos.x + tile_w: if ev.new.y + ev.player.radius >= tile_pos.y \ and ev.new.y < tile_pos.y + tile_h: print("YEAH!", i, ev.new, tile, tile_pos, self.board.border, tile_w, tile_h) if ev.player.velocity.y > 0: ev.player.velocity.y *= -.5 if abs(ev.player.velocity.y) < 50: ev.player.force = -ev.player.gravity ev.player.velocity.y = 0 ev.player.velocity.x *= .9 if abs(ev.player.velocity.x) < 5: ev.player.velocity.x = 0 ev.player.pos.y = tile_pos.y - ev.player.radius elif ev.new.y - ev.player.radius <= tile_pos.y + tile_h and \ ev.new.y > tile_pos.y: print("BIM") if ev.player.velocity.y < 0: ev.player.velocity.y *= -1 ev.player.pos.y = tile_pos.y + tile_h + ev.player.radius if ev.new.y >= tile_pos.y and \ ev.new.y < tile_pos.y + tile_h: if ev.new.x > ev.old.x and \ ev.new.x + ev.player.radius >= tile_pos.x and \ ev.new.x < tile_pos.x + tile_w: if ev.player.velocity.x > 0: ev.player.velocity.x *= -.9 ev.player.pos.x = tile_pos.x - ev.player.radius elif ev.new.x - ev.player.radius <= tile_pos.x + tile_w and \ ev.new.x > tile_pos.x: if ev.player.velocity.x < 0: ev.player.velocity.x *= -.9 ev.player.pos.x = tile_pos.x + tile_w + ev.player.radius
def test_update_ambiant(self, painter): material = Material() material.ambiant = gl.col3f(1,0,0) mesh = gl.Spheref(gl.vec3f(0), 1).drawable(self.renderer) painter.state.look_at( gl.vec3f(0, .8, 10), gl.vec3f(0, 0, 0), gl.vec3f(0, 1, 0) ) painter.state.perspective( angle.deg(35), 200 / 200, 0.005, 30.0 ) bindable = material.bindable(self.renderer) s = painter.push_state() s.translate(1, 0, 0) with painter.bind([bindable]): painter.draw([mesh]) painter.pop_state() s = painter.push_state() s.translate(-1, 0, 0) material.ambiant = gl.col3f(0, 1, 1) with painter.bind([bindable]): painter.draw([mesh])
def create_cube(self): mat = gl.Material('cube') mat.ambient = gl.col3f('#100') mat.diffuse = gl.col3f('#f55') mat.specular = gl.col3f('#f00') mat.shininess = 5 mat.shading_model = gl.material.ShadingModel.gouraud mesh = cube.gl.Cube3f(cube.gl.vec3f(), 1).drawable(self.renderer) entity = self.entity_manager.create('Cube') transform = entity.add_component( Transform(gl.matrix.scale(gl.mat4f(), gl.vec3f(4))) ) entity.add_component(Bindable(mat)) entity.add_component(Drawable(mesh)) class Animate: channels = ['tick'] velocity = 10 def __call__(self, ev, delta): transform.node.rotate(units.deg(45) * delta * self.velocity, gl.vec3f(1, .5, 0)) entity.add_component(Controller(Animate())) return entity
def create(shape, entity_manager): mat = gl.Material('power') mat.ambient = gl.col3f('#888') mat.diffuse = gl.col3f('#000') mat.specular = gl.col3f('#000') #mat.shininess = 0 #mat.shading_model = gl.material.ShadingModel.gouraud mat.add_color( gl.ShaderParameterType.vec3, gl.StackOperation.add ) mesh = gl.Mesh() mesh.mode = gl.DrawMode.quads mesh.kind = gl.ContentKind.vertex mesh.append(gl.vec3f(0, 0, 0)) mesh.append(gl.vec3f(0, 1, 0)) mesh.append(gl.vec3f(1, 1, 0)) mesh.append(gl.vec3f(1, 0, 0)) mesh.kind = gl.ContentKind.color mesh.append(gl.col3f('red')) mesh.append(gl.col3f('green')) mesh.append(gl.col3f('green')) mesh.append(gl.col3f('red')) mesh.kind = gl.ContentKind.normal mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) power = entity_manager.create('power', shape = shape) transform = power.add_component(Transform(power_matrix(shape))) power.add_component(Bindable(mat)) power.add_component(Drawable(mesh)) refill = Controller(Refill(power)) def start_power(ev, delta): power.add_component(refill) power.add_component(Controller(start_power, ['start-power'])) def stop_power(ev, delta): power.remove_component(refill) power.shape.h = 1 transform.node.value = power_matrix(power.shape) power.add_component(Controller(stop_power, ['stop-power'])) return power
def create(game): mat = gl.Material('ground') mat.ambient = gl.col3f('black') mat.diffuse = gl.col3f('brown') mat.specular = gl.col3f('gray') mat.shininess = 1000 mat.shading_model = gl.material.ShadingModel.gouraud mat.add_texture( "ground.bmp", gl.TextureType.ambient, gl.TextureMapping.uv, gl.StackOperation.add, gl.TextureMapMode.wrap, gl.BlendMode.basic ) mesh = gl.Mesh() mesh.mode = gl.DrawMode.quads size = 40 step = 1.0 / size for i in (i / size for i in range(size)): x = i for j in (i / size for i in range(size)): y = 1.0 - j mesh.kind = gl.ContentKind.vertex mesh.append(gl.vec3f(x, y, 0)) mesh.append(gl.vec3f(x, y - step, 0)) mesh.append(gl.vec3f(x + step, y - step, 0)) mesh.append(gl.vec3f(x + step, y, 0)) mesh.kind = gl.ContentKind.tex_coord0 mesh.append(gl.vec2f(i, j)) mesh.append(gl.vec2f(i, j + step)) mesh.append(gl.vec2f(i + step, j + step)) mesh.append(gl.vec2f(i + step, j)) mesh.kind = gl.ContentKind.normal mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) mesh.append(gl.vec3f(0, 0, 1)) light = game.renderer.new_light( gl.PointLightInfo( gl.vec3f(0, 2, -1), gl.Color3f("#888"), gl.Color3f("#333"), ) ) ground = game.entity_manager.create("ground") ground.add_component( cubeapp.game.entity.component.Transform( gl.matrix.rotate( gl.matrix.translate( gl.matrix.scale(gl.mat4f(), gl.vec3f(20)), gl.vec3f(-.5, 0, 0) ), units.deg(-90), gl.vec3f(1, 0, 0) ) ) ) ground.add_component( cubeapp.game.entity.component.Bindable(mat) ) ground.add_component( cubeapp.game.entity.component.Drawable(mesh) ) return ground
def rand_color(): import random return gl.col3f(random.random(), random.random(), random.random())
def create(game, size, border, tile_width, tile_height, rows): mat = gl.Material('ground') mat.ambient = gl.col3f('#888') mat.diffuse = gl.col3f('#000') mat.specular = gl.col3f('#000') #mat.shininess = 0 #mat.shading_model = gl.material.ShadingModel.gouraud mat.add_color( gl.ShaderParameterType.vec3, gl.StackOperation.add ) coords = [] colors = [] indices = list(range(size.x * size.y * 4)) for i in range(size.x): x = i * tile_width + border for j in range(size.y): y = j * tile_height + border coords.extend([ gl.vec3f(x, y, 0), gl.vec3f(x, y + tile_height, 0), gl.vec3f(x + tile_width, y + tile_height, 0), gl.vec3f(x + tile_width, y, 0), ]) colors.extend([ gl.col3f('#356712'), gl.col3f('#356712'), gl.col3f('#356712'), gl.col3f('#356712'), ]) ground_vb = game.renderer.new_vertex_buffer([ gl.make_vba( gl.ContentKind.vertex, coords, gl.ContentHint.static_content ), gl.make_vba( gl.ContentKind.color, colors, gl.ContentHint.static_content ) ]) ground_ib = game.renderer.new_index_buffer( gl.make_vba( gl.ContentKind.index, indices, gl.ContentHint.static_content ) ) # Set initial color #colors_attr = ground_vb[1] #colors_attr[0] = gl.col3f('blue') #colors_attr[1] = gl.col3f('blue') #ground_vb.reload(1) board = game.entity_manager.create( 'board', ib = ground_ib, vb = ground_vb, size = size, tile_height = tile_height, tile_width = tile_width, border = border, rows = rows, event_manager = game.event_manager, ) transform = board.add_component( Transform( gl.matrix.translate( #gl.matrix.scale( gl.mat4f(), # gl.vec3f(size, size, 1) #), gl.vec3f(-.5, -.5, 0) ) ) ) board.add_component(Bindable(mat)) board.add_component(Drawable(DrawGround(board, len(indices)))) board.add_component(Controller(CellManager(board = board))) return board
board.event_manager.push( Event('set-board-friction', friction = self.friction) ) class Wall: def prepare(self, board, tile): pass def __call__(self, board, tile, player, colliding): colliding.append(tile) # Mapping between ascii codes and controllers # Multiple controllers can be set per character, their __call__ method will # be triggered whenever the player cross a particular cell. cell_controllers = { '@': (Cell(gl.col3f('red')), ), '#': (Cell(gl.col3f('white')), Wall()), 'x': ( Cell(gl.col3f('#2342ff')), SetBoardFriction(friction = 5), ), } class CellManager(Controller): """Centralize all cell controllers""" channels = ['player-moved'] def __init__(self, board): self.cells = {} self.current_tiles = None self.board = board y = 0