Ejemplo n.º 1
0
def create(game):
    mat = gl.Material('player')

    def rand_color():
        import random
        return gl.col3f(random.random(), random.random(), random.random())

    mat.ambient = rand_color()  #gl.col3f(0.2, 0.2, 0.2)
    #mat.diffuse = gl.col3f('black')
    #mat.specular = gl.col3f('black')
    mat.opacity = .5
    mesh = gl.Spheref(gl.vec3f(0), 1).drawable(game.renderer)
    light = game.renderer.new_light(
        gl.PointLightInfo(
            gl.vec3f(0, 8, -1),
            gl.Color3f("#888"),
            gl.Color3f("#333"),
        ))
    player = game.entity_manager.create('player')
    player.add_component(
        Transform(gl.matrix.translate(gl.mat4f(), gl.vec3f(0, .9, 0)),
                  name='position'), )
    player.add_component(Bindable(light, name='light'))
    player.add_component(Bindable(mat, name='material'))
    player.add_component(Drawable(mesh))
    #def change_colors(ev, delta):
    #    mat.ambiant = rand_color()
    #player.add_component(Controller(change_colors, ['tick']))
    return player
Ejemplo n.º 2
0
def create(game):
    mat = gl.Material('player')
    def rand_color():
        import random
        return gl.col3f(random.random(), random.random(), random.random())
    mat.ambient = rand_color() #gl.col3f(0.2, 0.2, 0.2)
    #mat.diffuse = gl.col3f('black')
    #mat.specular = gl.col3f('black')
    mat.opacity = .5
    mesh = gl.Spheref(gl.vec3f(0), 1).drawable(game.renderer)
    light = game.renderer.new_light(
        gl.PointLightInfo(
            gl.vec3f(0, 8, -1),
            gl.Color3f("#888"),
            gl.Color3f("#333"),
        )
    )
    player = game.entity_manager.create('player')
    player.add_component(
        Transform(
            gl.matrix.translate(gl.mat4f(), gl.vec3f(0, .9, 0)),
            name = 'position'
        ),
    )
    player.add_component(Bindable(light, name = 'light'))
    player.add_component(Bindable(mat, name = 'material'))
    player.add_component(Drawable(mesh))
    #def change_colors(ev, delta):
    #    mat.ambiant = rand_color()
    #player.add_component(Controller(change_colors, ['tick']))
    return player
Ejemplo n.º 3
0
    def test_ortho(self):
        m = gl.mat4f(self.s.model)
        v = gl.mat4f(self.s.view)
        p = gl.mat4f(self.s.projection)
        mvp = gl.mat4f(self.s.mvp)

        self.assertEqual(m, self.s.model)
        self.assertEqual(v, self.s.view)
        self.assertEqual(p, self.s.projection)
        self.assertEqual(mvp, self.s.mvp)

        self.s.ortho(0, 0, 200, 200)

        self.assertEqual(m, self.s.model)
        self.assertEqual(v, self.s.view)
        self.assertNotEqual(p, self.s.projection)
        self.assertNotEqual(mvp, self.s.mvp)
Ejemplo n.º 4
0
    def test_perspective(self):
        m = gl.mat4f(self.s.model)
        v = gl.mat4f(self.s.view)
        p = gl.mat4f(self.s.projection)
        mvp = gl.mat4f(self.s.mvp)

        self.assertEqual(m, self.s.model)
        self.assertEqual(v, self.s.view)
        self.assertEqual(p, self.s.projection)
        self.assertEqual(mvp, self.s.mvp)

        self.s.perspective(angle.deg(45), 1, 0.1, 300)

        self.assertEqual(m, self.s.model)
        self.assertEqual(v, self.s.view)
        self.assertNotEqual(p, self.s.projection)
        self.assertNotEqual(mvp, self.s.mvp)
Ejemplo n.º 5
0
    def test_look_at(self):
        m = gl.mat4f(self.s.model)
        v = gl.mat4f(self.s.view)
        p = gl.mat4f(self.s.projection)
        mvp = gl.mat4f(self.s.mvp)

        self.assertEqual(m, self.s.model)
        self.assertEqual(v, self.s.view)
        self.assertEqual(p, self.s.projection)
        self.assertEqual(mvp, self.s.mvp)

        self.s.look_at(gl.vec3f(0,0,0), gl.vec3f(1,2,3), gl.vec3f(0, 1, 0))

        self.assertEqual(m, self.s.model)
        self.assertNotEqual(v, self.s.view)
        self.assertEqual(p, self.s.projection)
        self.assertNotEqual(mvp, self.s.mvp)
Ejemplo n.º 6
0
def power_matrix(shape):
    return gl.matrix.scale(
        gl.matrix.translate(
          gl.mat4f(),
            gl.vec3f(shape.x, shape.y - shape.h, -.2)
        ),
        gl.vec3f(shape.w, shape.h, 1)
    )
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
 def __render_size(self, size, material, chunks, referential, painter, state):
     for chunk in chunks:
         origin = chunk.node.origin - referential
         if origin.y != 0: continue
         state.model = gl.matrix.scale(
             gl.matrix.translate(
                 gl.mat4f(),
                 gl.vec3f(
                     origin.x * chunk.size,
                     origin.y * chunk.size,
                     origin.z * chunk.size
                 )
             )
             , gl.vec3f(chunk.node.size * chunk.size)
         )
         with painter.bind([material]):
             painter.draw([self.mesh])
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
 def render(self):
     new_time = time.time()
     #self.title.text = "%.2f ms" % ((new_time - self.last_time) * 1000)
     self.last_time = new_time
     with self.renderer.begin(gl.mode_2d) as painter:
         #painter.state.look_at(
         #    gl.vec3f(0,0,-1), gl.vec3f(0, 0, 1), gl.vec3f(0, 1, 0)
         #)
         #painter.state.perspective(
         #    units.deg(45), self.window.width / self.window.height, 0.005, 300.0
         #)
         #painter.state.look_at(
         #    gl.vec3f(self.window.width / 2, self.window.height / 2, -800),
         #    gl.vec3f(self.window.width / 2, self.window.height / 2, 0),
         #    gl.vec3f(0, 1, 0)
         #)
         #painter.state.perspective(
         #    units.deg(45), self.window.width / self.window.height, 0.005, 3000.0
         #)
         painter.state.view = gl.matrix.translate(
             gl.mat4f(), -self.camera.pos +
             gl.vec3f(self.window.width / 2, self.window.height / 2, 0))
         with painter.bind([self.light]):
             painter.draw([self.scene_view])
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
def player_matrix(pos, radius):
    return gl.matrix.rotate(
        gl.matrix.scale(gl.matrix.translate(gl.mat4f(), pos),
                        gl.vec3f(radius, radius, 1)), units.deg(0),
        gl.vec3f(1, 1, 0))
Ejemplo n.º 15
0
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