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 __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 setUp(self): super().setUp() x, y, w, h = (0, 0, 200, 200) self.vb = self.renderer.new_vertex_buffer( [ gl.make_vba( gl.ContentKind.vertex, [gl.vec2f(x, y), gl.vec2f(x, y + h), gl.vec2f(x + w, y + h), gl.vec2f(x + w, y)], gl.ContentHint.static_content, ), gl.make_vba( gl.ContentKind.tex_coord0, [gl.vec2f(0, 0), gl.vec2f(0, 1), gl.vec2f(1, 1), gl.vec2f(1, 0)], gl.ContentHint.static_content, ), ] ) self.indices = self.renderer.new_index_buffer( gl.make_vba(gl.ContentKind.index, [0, 1, 2, 3], gl.ContentHint.static_content) ) from os import path self.texture = self.renderer.new_texture( gl.Surface(Path(path.join(path.dirname(__file__), "texture_test.bmp"))) ) self.texture.generate_mipmap() self.texture.min_filter_bilinear(gl.TextureFilter.linear) self.texture.mag_filter(gl.TextureFilter.linear)
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, tag=None, id_=None, class_=None, renderer=None, x=0, y=0, w=0, h=0, min_size=None, max_size=None, prefered_size=None): if not tag: raise Exception("Invalid tag name") self._tag = tag self._id = self._gen_id(tag, id_) self._class = class_ self._position = vec2f(x, y) self._size = vec2f(w, h) self._stylesheet = DEFAULT_STYLESHEET self.__renderer = renderer if prefered_size is None: prefered_size = self._size self.__size_hints = self._compute_size_hints( prefered_size, max_size, min_size, ) super().__init__(self._id)
def setUp(self): super().setUp() x, y, w, h = ( 10, 10, 180, 180 ) self.vb = self.renderer.new_vertex_buffer([ gl.make_vba( gl.ContentKind.vertex, [ gl.vec3f(x, y, 0), gl.vec3f(x, y + h, 0), gl.vec3f(x + w, y + h, 0), gl.vec3f(x + w, y, 0), ], gl.ContentHint.static_content ), gl.make_vba( gl.ContentKind.tex_coord0, [ gl.vec2f(0, 0), gl.vec2f(0, 1), gl.vec2f(1, 1), gl.vec2f(1, 0), ], gl.ContentHint.static_content ) ]) self.indices = self.renderer.new_index_buffer( gl.make_vba( gl.ContentKind.index, [ 0, 1, 2, 3], gl.ContentHint.static_content ) )
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 cube(self): if self.__cube is not None: return self.__cube self.__cube = gl.Mesh() vertices = [ # front face (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5), (0.5, 0.5, 0.5), (0.5, -0.5, 0.5), # back face (-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5), (0.5, -0.5, -0.5), ] colors = [ # front face is blue (0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1), # back face is red (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), ] tex_coords = [(0, 1), (0, 0), (1, 0), (1, 1), (1, 1), (1, 0), (0, 0), (0, 1)] indices = [ 0, 2, 1, 0, 3, 2, 4, 3, 0, 4, 7, 3, 4, 1, 5, 4, 0, 1, 3, 6, 2, 3, 7, 6, 1, 6, 5, 1, 2, 6, 7, 5, 6, 7, 4, 5, ] self.__cube.mode = gl.DrawMode.triangles for i in indices: self.__cube.kind = gl.ContentKind.vertex self.__cube.append(gl.vec3f(*vertices[i])) self.__cube.kind = gl.ContentKind.color self.__cube.append(gl.Color3f(*colors[i])) self.__cube.kind = gl.ContentKind.tex_coord0 self.__cube.append(gl.vec2f(*tex_coords[i])) return self.__cube