Esempio n. 1
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Fireworks Demo",
            sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED,
            width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load Textures
        texture = loadTexture(b'res/flare.bmp', gl.GL_ALPHA, gl.GL_REPEAT)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(0.001, 0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)

        # the base system
        self.basesystem = BaseSystem(self.height, texture)

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.angleY = 0.0
        self.camPosZ = 5.0
        self.spacePressed = -1.0
        self.systems = []
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks()-1]
        self.lasttime = self.frames[-1]
Esempio n. 2
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Rain Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLEBUFFERS, 1)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLESAMPLES, 4)
        gl.glClearColor(0.8, 0.8, 0.8, 0.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # gl.glDisable(gl.GL_DEPTH)
        gl.glDisable(gl.GL_DEPTH_TEST)
        # load Textures
        self.txPaving = loadTexture(b'res/paving.bmp',
                                    gl.GL_RGB,
                                    gl.GL_REPEAT,
                                    mipmap=True)
        txSplash = loadTexture(b'res/waterdrops.bmp', gl.GL_ALPHA, gl.GL_CLAMP)
        # init fog
        gl.glEnable(gl.GL_FOG)
        gl.glFogi(gl.GL_FOG_MODE, gl.GL_EXP2)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        self.sizeRatio = self.width / 1440
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        dropRenderer = GLPointRenderer.create()
        dropRenderer.setType(spk.POINT_CIRCLE)
        dropRenderer.setSize(2.0 * self.sizeRatio)
        dropRenderer.enableBlending(True)
        self.dropRenderer = dropRenderer
        rainRenderer = GLLineRenderer.create()
        rainRenderer.setLength(-0.1)
        rainRenderer.enableBlending(True)
        self.rainRenderer = rainRenderer
        splashRenderer = GLQuadRenderer.create()
        splashRenderer.setScale(0.05, 0.05)
        splashRenderer.setTexturingMode(spk.TEXTURE_2D)
        splashRenderer.setTexture(txSplash)
        splashRenderer.enableBlending(True)
        splashRenderer.enableRenderingHint(spk.DEPTH_WRITE, False)
        self.splashRenderer = splashRenderer
        # create Model
        rain_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | spk.FLAG_MASS
        rain_mutable = spk.FLAG_NONE
        rain_random = spk.FLAG_MASS
        rainModel = spk.Model.create(rain_enable, rain_mutable, rain_random)
        rainModel.setParam(spk.PARAM_ALPHA, 0.2)
        rainModel.setImmortal(True)
        self.rainModel = rainModel
        dropModel = spk.Model.create(rain_enable, rain_mutable,
                                     rain_random)  # same as rain
        dropModel.setParam(spk.PARAM_ALPHA, 0.6)
        self.dropModel = dropModel
        splash_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | spk.FLAG_SIZE | spk.FLAG_ANGLE
        splash_mutable = spk.FLAG_SIZE | spk.FLAG_ALPHA
        splash_random = spk.FLAG_SIZE | spk.FLAG_ANGLE
        splashModel = spk.Model.create(splash_enable, splash_mutable,
                                       splash_random)
        splashModel.setParam(spk.PARAM_ANGLE, 0.0, 2.0 * math.pi)
        splashModel.setParam(spk.PARAM_ALPHA, 1.0, 0.0)
        self.splashModel = splashModel
        # create Emitter
        rainZone = spk.Ring.create(spk.Vector3D(0.0, 5.0, 0.0))
        self.rainZone = rainZone
        rainEmitter = spk.SphericEmitter.create(spk.Vector3D(0.0, -1.0, 0.0),
                                                0.0, 0.03 * math.pi)
        rainEmitter.setZone(rainZone)
        self.rainEmitter = rainEmitter
        self.dropEmitter = spk.SphericEmitter.create(
            spk.Vector3D(0.0, 1.0, 0.0), 0.0, 0.2 * math.pi)
        # create Group
        gravity = spk.Vector3D(0.0, -2.0, 0.0)
        rainGroup = spk.Group.create(rainModel, 8000)
        rainGroup.setCustomUpdate(lambda p, t: self.killRain(p, t))
        rainGroup.setRenderer(rainRenderer)
        rainGroup.addEmitter(rainEmitter)
        rainGroup.setFriction(0.7)
        rainGroup.setGravity(gravity)
        self.rainGroup = rainGroup
        dropGroup = spk.Group.create(dropModel, 16000)
        dropGroup.setRenderer(dropRenderer)
        dropGroup.setFriction(0.7)
        dropGroup.setGravity(gravity)
        self.dropGroup = dropGroup
        splashGroup = spk.Group.create(splashModel, 2400)
        splashGroup.setRenderer(splashRenderer)
        self.splashGroup = splashGroup
        # create System
        system = spk.System.create()
        system.addGroup(splashGroup)
        system.addGroup(dropGroup)
        system.addGroup(rainGroup)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.renderEnv = True
        self.angleY = 0.0
        self.angleX = 12.0
        self.posX = 0.0
        self.posZ = 0.0
        self.rainRatio = 0.5
        self.recompute = True
        self.nbParticles = ''
        self.fps = ''
        self.strRainRate = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 3
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Flakes Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        renderer = GLPointRenderer.create(1.0)  # type: GLPointRenderer
        renderer.setBlending(
            spk.BLENDING_ADD
        )  # blending method between particles(additive blending)
        renderer.enableRenderingHint(spk.DEPTH_TEST,
                                     False)  # notifying not to use depth info
        renderer.setAlphaTestThreshold(0.8)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA  # use color channels(RGBA)
        model = spk.Model.create(model_enable)  # type: spk.Model
        model.setParam(spk.PARAM_RED, 1.0)
        model.setParam(spk.PARAM_GREEN, 0.8)
        model.setParam(spk.PARAM_BLUE, 0.3)
        model.setParam(spk.PARAM_ALPHA, 0.4)
        model.setImmortal(True)  # particle is immortal
        self.model = model
        # no Emitter
        # create Modifier
        self.sphere = spk.Sphere.create(spk.Vector3D(), 1.0)
        obstacle = spk.Obstacle.create(self.sphere)  # type: spk.Obstacle
        obstacle.setFriction(0.9)
        obstacle.setBouncingRatio(0.9)
        self.obstacle = obstacle
        # create Group
        self.NB_PARTICLES = [10000, 25000, 50000, 100000, 200000, 500000]
        group = spk.Group.create(model,
                                 self.NB_PARTICLES[-1])  # type: spk.Group
        group.addModifier(obstacle)
        group.setRenderer(renderer)
        group.setGravity(spk.Vector3D(0.0, -0.5, 0.0))
        group.setFriction(0.2)
        self.group = group
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.angleX = 0.0
        self.angleZ = 0.0
        self.camPosZ = 2.75
        self.particleIndex = 2
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 4
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Collision Demo2", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load Texture
        texture = loadTexture(b'res/ball.bmp', gl.GL_RGBA, gl.GL_CLAMP)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        renderer = GLPointRenderer.create(1.0)  # type: GLPointRenderer
        renderer.setType(
            spk.POINT_SPRITE)  # using Point Sprite(require >=OpenGL2.0)
        renderer.setTexture(texture)  # use the texture on drawing
        renderer.enableWorldSize(
            True
        )  # true: unit is adjusted with the screen, false: unit is pixel(fixed)
        GLPointRenderer.setPixelPerUnit(45.0 * math.pi / 180.0, self.height)
        renderer.setSize(0.15)
        renderer.setBlending(
            spk.BLENDING_NONE
        )  # blending method between particles(no blending)
        renderer.enableRenderingHint(spk.ALPHA_TEST, True)
        renderer.setAlphaTestThreshold(0.8)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA  # use color channels(RGBA)
        model = spk.Model.create(model_enable)  # type: spk.Model
        model.setImmortal(True)  # particle is immortal
        self.model = model
        # no Emitter
        # create Modifier
        self.sphere = spk.Sphere.create(spk.Vector3D(), 1.0 - 0.15 / 2.0)
        self.cube = spk.AABox.create(spk.Vector3D(),
                                     spk.Vector3D(1.2, 1.2, 1.2))
        self.obstacle = spk.Obstacle.create(self.sphere, spk.EXIT_ZONE, 0.9,
                                            0.9)  # type: spk.Obstacle
        self.obstacle.setZone(self.sphere)
        collision = spk.Collision.create(0.15, 0.9)
        # create Group
        self.NB_PARTICLES = 250
        group = spk.Group.create(model, self.NB_PARTICLES)  # type: spk.Group
        group.addModifier(self.obstacle)
        group.addModifier(collision)
        group.setRenderer(renderer)
        group.setGravity(spk.Vector3D())
        group.setFriction(0.1)
        self.group = group
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.angleX = 0.0
        self.angleZ = 0.0
        self.camPosZ = 2.75
        self.nbParticles = ''
        self.fps = ''
        self.strZone = 'ZONE: SPHERE'
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 5
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Fire Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load textures
        txFire = loadTexture(b'res/fire2.bmp',
                             gl.GL_ALPHA,
                             gl.GL_CLAMP,
                             mipmap=True)
        txSmoke = loadTexture(b'res/explosion.bmp', gl.GL_ALPHA, gl.GL_CLAMP)
        # lighting
        light_ambient = [0.15, 0.15, 0.25, 1.0]
        light_diffuse = [1.0, 0.75, 0.25, 1.0]
        gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, light_ambient)
        gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, light_diffuse)
        gl.glLightf(gl.GL_LIGHT0, gl.GL_QUADRATIC_ATTENUATION, 20.0)
        mat_ambient = [0.2, 0.2, 0.2, 1.0]
        mat_diffuse = [1.0, 1.0, 1.0, 1.0]
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, mat_ambient)
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, mat_diffuse)
        gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, light_ambient)
        gl.glLightModelf(gl.GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init PyWavefront
        self.scene = pywavefront.Wavefront('./res/SceneFireCamp.obj')

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)

        # create Renderer
        fireRenderer = GLQuadRenderer.create()  # type: GLQuadRenderer
        fireRenderer.setScale(0.3, 0.3)
        fireRenderer.setTexturingMode(spk.TEXTURE_2D)
        fireRenderer.setTexture(txFire)
        fireRenderer.setTextureBlending(gl.GL_MODULATE)
        fireRenderer.setBlending(spk.BLENDING_ADD)
        fireRenderer.enableRenderingHint(spk.DEPTH_WRITE, False)
        fireRenderer.setAtlasDimensions(2, 2)
        self.fireRenderer = fireRenderer
        smokeRenderer = GLQuadRenderer.create()  # type: GLQuadRenderer
        smokeRenderer.setScale(0.3, 0.3)
        smokeRenderer.setTexturingMode(spk.TEXTURE_2D)
        smokeRenderer.setTexture(txSmoke)
        smokeRenderer.setTextureBlending(gl.GL_MODULATE)
        smokeRenderer.setBlending(spk.BLENDING_ALPHA)
        smokeRenderer.enableRenderingHint(spk.DEPTH_WRITE, False)
        smokeRenderer.setAtlasDimensions(2, 2)
        self.smokeRenderer = smokeRenderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)

        # create Model
        fire_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | \
            spk.FLAG_SIZE | spk.FLAG_ANGLE | spk.FLAG_TEXTURE_INDEX
        fire_mutable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_ALPHA | spk.FLAG_ANGLE
        fire_random = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_TEXTURE_INDEX | spk.FLAG_ANGLE
        fire_interpolated = spk.FLAG_SIZE
        fireModel = spk.Model.create(fire_enable, fire_mutable, fire_random,
                                     fire_interpolated)  # type: spk.Model
        fireModel.setParam(spk.PARAM_RED, 0.8, 0.9, 0.8,
                           0.9)  # [min,max]on birth, [min,max]on death
        fireModel.setParam(spk.PARAM_GREEN, 0.5, 0.6, 0.5, 0.6)
        fireModel.setParam(spk.PARAM_BLUE, 0.3)
        fireModel.setParam(spk.PARAM_ALPHA, 0.4, 0.0)  # on birth, on death
        minAngle, maxAngle = 0.0, 2.0 * math.pi
        fireModel.setParam(spk.PARAM_ANGLE, minAngle, maxAngle, minAngle,
                           maxAngle)
        fireModel.setParam(spk.PARAM_TEXTURE_INDEX, 0.0, 4.0)
        fireModel.setLifeTime(1.0, 1.5)
        ip_size = fireModel.getInterpolator(spk.PARAM_SIZE)
        ip_size.addEntry(0.5, 2.0, 5.0)
        ip_size.addEntry(1.0, 0.0)
        self.fireModel = fireModel
        smoke_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | \
            spk.FLAG_SIZE | spk.FLAG_ANGLE | spk.FLAG_TEXTURE_INDEX
        smoke_mutable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_SIZE | spk.FLAG_ANGLE
        smoke_random = spk.FLAG_TEXTURE_INDEX | spk.FLAG_ANGLE
        smoke_interpolated = spk.FLAG_ALPHA
        smokeModel = spk.Model.create(smoke_enable, smoke_mutable,
                                      smoke_random,
                                      smoke_interpolated)  # type: spk.Model
        smokeModel.setParam(spk.PARAM_RED, 0.3, 0.2)
        smokeModel.setParam(spk.PARAM_GREEN, 0.25, 0.2)
        smokeModel.setParam(spk.PARAM_BLUE, 0.2)
        smokeModel.setParam(spk.PARAM_ALPHA, 0.2, 0.0)
        smokeModel.setParam(spk.PARAM_SIZE, 5.0, 10.0)
        smokeModel.setParam(spk.PARAM_TEXTURE_INDEX, 0.0, 4.0)
        smokeModel.setParam(spk.PARAM_ANGLE, 0.0, 2.0 * math.pi, 0.0,
                            2.0 * math.pi)
        smokeModel.setLifeTime(5.0, 5.0)
        ip_alpha = smokeModel.getInterpolator(spk.PARAM_ALPHA)
        ip_alpha.addEntry(0.0, 0.0)
        ip_alpha.addEntry(0.2, 0.2)
        ip_alpha.addEntry(1.0, 0.0)
        self.smokeModel = smokeModel

        # create Emitter
        fireEmitter1 = spk.StraightEmitter.create(spk.Vector3D(0.0, 1.0, 0.0))
        fireEmitter1.setZone(
            spk.Sphere.create(spk.Vector3D(0.0, -1.0, 0.0), 0.5))
        fireEmitter1.setFlow(40)
        fireEmitter1.setForce(1.0, 2.5)
        fireEmitter2 = spk.StraightEmitter.create(spk.Vector3D(1.0, 0.6, 0.0))
        fireEmitter2.setZone(
            spk.Sphere.create(spk.Vector3D(0.15, -1.2, 0.075), 0.1))
        fireEmitter2.setFlow(15)
        fireEmitter2.setForce(0.5, 1.5)
        fireEmitter3 = spk.StraightEmitter.create(spk.Vector3D(
            -0.6, 0.8, -0.8))
        fireEmitter3.setZone(
            spk.Sphere.create(spk.Vector3D(-0.375, -1.15, -0.375), 0.3))
        fireEmitter3.setFlow(15)
        fireEmitter3.setForce(0.5, 1.5)
        fireEmitter4 = spk.StraightEmitter.create(spk.Vector3D(-0.8, 0.5, 0.2))
        fireEmitter4.setZone(
            spk.Sphere.create(spk.Vector3D(-0.255, -1.2, 0.225), 0.2))
        fireEmitter4.setFlow(10)
        fireEmitter4.setForce(0.5, 1.5)
        fireEmitter5 = spk.StraightEmitter.create(spk.Vector3D(0.1, 0.8, -1.0))
        fireEmitter5.setZone(
            spk.Sphere.create(spk.Vector3D(-0.075, -1.2, -0.3), 0.2))
        fireEmitter5.setFlow(10)
        fireEmitter5.setForce(0.5, 1.5)
        smokeEmitter = spk.SphericEmitter.create(spk.Vector3D(0.0, 1.0, 0.0),
                                                 0.0, 0.5 * math.pi)
        smokeEmitter.setZone(spk.Sphere.create(spk.Vector3D(), 1.2))
        smokeEmitter.setFlow(25)
        smokeEmitter.setForce(0.5, 1.0)

        # no Modifier

        # create Group
        fireGroup = spk.Group.create(fireModel, 135)  # type: spk.Group
        fireGroup.addEmitter(fireEmitter1)
        fireGroup.addEmitter(fireEmitter2)
        fireGroup.addEmitter(fireEmitter3)
        fireGroup.addEmitter(fireEmitter4)
        fireGroup.addEmitter(fireEmitter5)
        fireGroup.setRenderer(fireRenderer)
        fireGroup.setGravity(spk.Vector3D(0.0, 3.0, 0.0))
        self.fireGroup = fireGroup
        smokeGroup = spk.Group.create(smokeModel, 135)  # type: spk.Group
        smokeGroup.addEmitter(smokeEmitter)
        smokeGroup.setRenderer(smokeRenderer)
        smokeGroup.setGravity(spk.Vector3D(0.0, 0.4, 0.0))
        self.smokeGroup = smokeGroup

        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(fireGroup)
        system.addGroup(smokeGroup)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.renderEnv = True
        self.angleY = 30.0
        self.angleX = 25.0
        self.camPosZ = 12.0
        self.smoke = True
        self.lightTime = 0
        self.nbParticles = ''
        self.fps = ''
        self.strSmoke = 'SMOKE: ON'
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 6
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Fountain Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.68, 0.85, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        # init fog
        gl.glEnable(gl.GL_FOG)
        gl.glFogi(gl.GL_FOG_MODE, gl.GL_EXP2)
        gl.glFogfv(gl.GL_FOG_COLOR, [0.0, 0.68, 0.85, 1.0])
        gl.glFogf(gl.GL_FOG_DENSITY, 0.04)
        gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST)
        # load Textures
        self.txGrass = loadTexture(b'res/grass.bmp',
                                   gl.GL_RGB,
                                   gl.GL_REPEAT,
                                   mipmap=True)
        self.txFountain = loadTexture(b'res/tile.bmp',
                                      gl.GL_RGB,
                                      gl.GL_REPEAT,
                                      mipmap=True)
        self.txWater = loadTexture(b'res/water.bmp',
                                   gl.GL_RGB,
                                   gl.GL_REPEAT,
                                   mipmap=True)
        txSplash = loadTexture(b'res/waterdrops.bmp', gl.GL_ALPHA, gl.GL_CLAMP)
        # create display lists
        self.createDisplayLists()

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        renderer = GLQuadRenderer.create(1.0)  # type: GLQuadRenderer
        renderer.setScale(0.06, 0.06)
        renderer.setTexturingMode(spk.TEXTURE_2D)
        renderer.setTexture(txSplash)
        renderer.setBlending(spk.BLENDING_ALPHA)
        renderer.enableRenderingHint(spk.DEPTH_WRITE, False)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_ALPHA | spk.FLAG_SIZE | spk.FLAG_ANGLE
        model_mutable = spk.FLAG_ALPHA | spk.FLAG_SIZE | spk.FLAG_ANGLE
        model_random = spk.FLAG_SIZE | spk.FLAG_ANGLE
        model = spk.Model.create(model_enable, model_mutable,
                                 model_random)  # type: spk.Model
        model.setParam(spk.PARAM_ALPHA, 0.2, 0.0)
        model.setParam(spk.PARAM_SIZE, 1.0, 1.0, 2.0, 8.0)
        model.setParam(spk.PARAM_ANGLE, 0.0, 4.0 * math.pi, 0.0, 4.0 * math.pi)
        model.setLifeTime(1.6, 2.2)
        self.model = model
        # create Emitter
        self.emitterZones = []
        self.emitters = []
        # splash to just above(x1)
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1, 0.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(0.0, 1.0, 0.0)))
        # splash from center of fountain(x4)
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1, 0.0)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1, 0.0)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1, 0.0)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1, 0.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(1.0, 3.0, 1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(-1.0, 3.0, -1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(-1.0, 3.0, 1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(1.0, 3.0, -1.0)))
        # splash from the side of fountain(x8)
        self.emitterZones.append(
            spk.Point.create(spk.Vector3D(-1.6, 0.1, -1.6)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(1.6, 0.1, 1.6)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(1.6, 0.1,
                                                               -1.6)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(-1.6, 0.1,
                                                               1.6)))
        self.emitterZones.append(
            spk.Point.create(spk.Vector3D(-2.26, 0.1, 0.0)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(2.26, 0.1,
                                                               0.0)))
        self.emitterZones.append(
            spk.Point.create(spk.Vector3D(0.0, 0.1, -2.26)))
        self.emitterZones.append(spk.Point.create(spk.Vector3D(0.0, 0.1,
                                                               2.26)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(1.0, 2.0, 1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(-1.0, 2.0, -1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(-1.0, 2.0, 1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(1.0, 2.0, -1.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(1.41, 2.0, 0.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(-1.41, 2.0, 0.0)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(0.0, 2.0, 1.41)))
        self.emitters.append(
            spk.StraightEmitter.create(spk.Vector3D(0.0, 2.0, -1.41)))
        self.flow = [
            500.0, 600.0, 600.0, 600.0, 600.0, 900.0, 900.0, 900.0, 900.0,
            900.0, 900.0, 900.0, 900.0
        ]
        self.flowLow = [
            150.0, 200.0, 200.0, 200.0, 200.0, 250.0, 250.0, 250.0, 250.0,
            250.0, 250.0, 250.0, 250.0
        ]
        for i in range(len(self.emitters)):
            self.emitters[i].setZone(self.emitterZones[i])
            self.emitters[i].setFlow(self.flow[i])
            self.emitters[i].setForce(2.5, 4.0)
        self.emitters[0].setForce(3.0, 3.5)
        # no Modifier
        # create Group
        group = spk.Group.create(model, 20000)  # type: spk.Group
        for emitter in self.emitters:
            group.addEmitter(emitter)
        group.setRenderer(renderer)
        group.setCustomUpdate(self.splash)
        group.setGravity(spk.Vector3D(0.0, -2.2, 0.0))
        group.setFriction(0.7)
        self.group = group
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.totalTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.renderEnv = True
        self.highGraphics = True
        self.angleY = 25.0
        self.angleX = 25.0
        self.camPosZ = 8.0
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 7
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Collision Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load Texture
        txParticle = loadTexture(b'res/ball2.bmp', gl.GL_RGBA, gl.GL_CLAMP)
        self.txCrate = loadTexture(b'res/crate.bmp', gl.GL_RGB, gl.GL_REPEAT)
        self.txFloor = loadTexture(b'res/floor.bmp', gl.GL_RGB, gl.GL_REPEAT)
        self.txWall = loadTexture(b'res/wall.bmp', gl.GL_RGB, gl.GL_REPEAT)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # create Renderer
        spk.setRandomSeed(int(time.time()))
        renderer = GLPointRenderer.create(1.0)  # type: GLPointRenderer
        renderer.setType(
            spk.POINT_SPRITE)  # using Point Sprite(require >=OpenGL2.0)
        renderer.setTexture(txParticle)  # use the texture on drawing
        renderer.enableWorldSize(
            True
        )  # true: unit is adjusted with the screen, false: unit is pixel(fixed)
        GLPointRenderer.setPixelPerUnit(45.0 * math.pi / 180.0, self.height)
        renderer.setSize(0.05)
        renderer.setBlending(
            spk.BLENDING_NONE
        )  # blending method between particles(no blending)
        renderer.enableRenderingHint(spk.ALPHA_TEST,
                                     True)  # notifying not to use depth info
        renderer.setAlphaTestThreshold(0.8)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE  # use color channels(RGB)
        model = spk.Model.create(model_enable)  # type: spk.Model
        model.setLifeTime(10.0, 10.0)  # particle's life time is 10[sec]
        self.model = model
        # create Emitter
        emit_dir = spk.Vector3D(0.0, 0.0, -1.0)
        emit_center = spk.Vector3D(0.0, 2.0, 4.475)
        minangle = 0.0 * math.pi  # zenith angle(min)
        maxangle = 0.5 * math.pi  # zenith angle(max)
        emitter = spk.SphericEmitter.create(
            emit_dir, minangle, maxangle)  # type: spk.SphericEmitter
        emitter.setZone(
            spk.Point.create(emit_center),
            True)  # particles are randomly placed in the specified zone
        emitter.setFlow(300)  # max number of particles
        emitter.setForce(1.5, 2.5)  # emittion power
        # create Group
        maxparticle = 3100  # upper limit number of particles
        group = spk.Group.create(model, maxparticle)  # type: spk.Group
        group.addEmitter(emitter)
        group.setRenderer(renderer)
        group.setCustomBirth(self.assignColor)
        group.setGravity(spk.Vector3D(0.0, -0.8, 0.0))
        group.setFriction(0.2)
        self.group = group
        # create Room objects
        self.ROOM_X = 7.0
        self.ROOM_Y = 3.0
        self.ROOM_Z = 9.0
        self.boxes = []
        self.partitions = []
        self.initRoom(group)  # obstacles
        self.createDisplayLists()  # display list for OpenGL drawing
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.enableBoxDrawing = False
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.renderEnv = True
        self.angleY = 90.0
        self.angleX = 45.0
        self.camPosZ = 10.0
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 8
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Basic Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load Texture
        texture = loadTexture(b'res/point.bmp', gl.GL_ALPHA, gl.GL_CLAMP)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        renderer = GLPointRenderer.create(1.0)  # type: GLPointRenderer
        renderer.setType(
            spk.POINT_SPRITE)  # using Point Sprite(require >=OpenGL2.0)
        renderer.setTexture(texture)  # use the texture on drawing
        renderer.setTextureBlending(gl.GL_MODULATE)
        renderer.enableWorldSize(
            True
        )  # true: unit is adjusted with the screen, false: unit is pixel(fixed)
        GLPointRenderer.setPixelPerUnit(45.0 * math.pi / 180.0, self.height)
        renderer.setSize(0.05)
        renderer.setBlending(
            spk.BLENDING_ADD
        )  # blending method between particles(additive blending)
        renderer.enableRenderingHint(spk.DEPTH_WRITE,
                                     False)  # notifying not to use depth info
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA  # use color channels(RGBA)
        model = spk.Model.create(model_enable)  # type: spk.Model
        model.setParam(spk.PARAM_ALPHA,
                       1.0)  # alpha is fixed(RGB are changed later)
        model.setLifeTime(8.0, 8.0)  # particle's life time is 8.0[sec]
        self.model = model
        # create Emitter
        emit_dir = spk.Vector3D(0.0, 1.0, 0.0)
        emit_center = spk.Vector3D(0.0, 0.015, 0.0)
        minangle = 0.1 * math.pi  # zenith angle(min)
        maxangle = 0.1 * math.pi  # zenith angle(max)
        emitter = spk.SphericEmitter.create(
            emit_dir, minangle, maxangle)  # type: spk.SphericEmitter
        emitter.setZone(
            spk.Point.create(emit_center),
            True)  # particles are randomly placed in the specified zone
        emitter.setFlow(1000)  # max number of particles
        emitter.setForce(1.5, 1.5)  # emittion power
        # create Modifier
        groundplane = spk.Plane.create()
        bouncing = 0.6  # bouncing ratio(coefficient of restitution)
        friction = 1.0  # coefficient of friction
        obstacle = spk.Obstacle.create(groundplane, spk.INTERSECT_ZONE,
                                       bouncing, friction)
        # create Group
        maxparticle = 5000  # upper limit number of particles
        group = spk.Group.create(model, maxparticle)  # type: spk.Group
        group.addEmitter(emitter)
        group.addModifier(obstacle)
        group.setRenderer(renderer)
        group.setGravity(spk.Vector3D(0.0, -0.8, 0.0))
        self.group = group
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.angleY = 0.0
        self.angleX = 45.0
        self.camPosZ = 5.0
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 9
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Galaxy Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        gl.glEnable(gl.GL_DEPTH_TEST)
        # load Textures
        texture = loadTexture(b'res/flare.bmp', gl.GL_ALPHA, gl.GL_CLAMP)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useRealStep()  # no adjustment
        # create Renderer
        renderer = GLQuadRenderer.create()  # type: GLQuadRenderer
        renderer.setTexturingMode(spk.TEXTURE_2D)
        renderer.setTexture(texture)
        renderer.setTextureBlending(gl.GL_MODULATE)
        renderer.setScale(0.05, 0.05)
        renderer.setBlending(spk.BLENDING_ADD)
        renderer.enableRenderingHint(spk.DEPTH_WRITE, False)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        galaxy_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | spk.FLAG_SIZE
        galaxy_mutable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE
        galaxy_random = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_SIZE
        galaxy_interpolated = spk.FLAG_ALPHA
        galaxyModel = spk.Model.create(galaxy_enable, galaxy_mutable,
                                       galaxy_random,
                                       galaxy_interpolated)  # type: spk.Model
        galaxyModel.setParam(spk.PARAM_RED, 0.0, 0.3, 0.5, 0.5)
        galaxyModel.setParam(spk.PARAM_GREEN, 0.0, 0.3, 0.5, 0.5)
        galaxyModel.setParam(spk.PARAM_BLUE, 1.0, 0.1)
        galaxyModel.setParam(spk.PARAM_SIZE, 0.1, 5.0)
        galaxyModel.setLifeTime(35.0, 40.0)
        ip_alpha = galaxyModel.getInterpolator(spk.PARAM_ALPHA)
        ip_alpha.addEntry(0.0, 0.0)
        ip_alpha.addEntry(0.95, 0.6, 1.0)
        ip_alpha.addEntry(1.0, 0.0)
        star_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | spk.FLAG_SIZE
        star_mutable = spk.FLAG_NONE
        star_random = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_ALPHA | spk.FLAG_SIZE
        starModel = spk.Model.create(star_enable, star_mutable,
                                     star_random)  # type: spk.Model
        starModel.setParam(spk.PARAM_RED, 0.8, 1.0)
        starModel.setParam(spk.PARAM_GREEN, 0.4, 1.0)
        starModel.setParam(spk.PARAM_BLUE, 0.8, 1.0)
        starModel.setParam(spk.PARAM_ALPHA, 0.2, 1.0)
        starModel.setParam(spk.PARAM_SIZE, 0.1, 5.0)
        starModel.setImmortal(True)
        # create Emitter
        # 2 emitters in diagonal lines on Y=0 plane
        lineEmitter1 = spk.RandomEmitter.create()  # type: spk.RandomEmitter
        lineEmitter1.setZone(
            spk.Line.create(spk.Vector3D(-2.5, 0.0, -2.5),
                            spk.Vector3D(2.5, 0.0, 2.5)))
        lineEmitter1.setFlow(100)
        lineEmitter1.setForce(0.0, 0.01)
        lineEmitter2 = spk.RandomEmitter.create()
        lineEmitter2.setZone(
            spk.Line.create(spk.Vector3D(-2.5, 0.0, 2.5),
                            spk.Vector3D(2.5, 0.0, -2.5)))
        lineEmitter2.setFlow(100)
        lineEmitter2.setForce(0.0, 0.01)
        # create Modifier
        vortex = spk.Vortex.create()  # type: spk.Vortex
        vortex.setRotationSpeed(0.4,
                                False)  # True->[rad/sec], False->[unit/src]
        vortex.setAttractionSpeed(0.04, True)
        vortex.setEyeRadius(0.05)
        vortex.enableParticleKilling(True)  # kill attracted particles
        # create Group
        galaxyGroup = spk.Group.create(galaxyModel, 8000)  # type: spk.Group
        galaxyGroup.addEmitter(lineEmitter1)
        galaxyGroup.addEmitter(lineEmitter2)
        galaxyGroup.setRenderer(renderer)
        galaxyGroup.addModifier(vortex)
        self.galaxyGroup = galaxyGroup
        starGroup = spk.Group.create(starModel, 1000)
        starGroup.setRenderer(renderer)
        skySphere = spk.Sphere(spk.Vector3D(), 16.0)
        starGroup.addParticles(1000,
                               zone=skySphere,
                               velocity=spk.Vector3D(),
                               full=False)
        starGroup.flushAddedParticles()
        # create System
        galaxySystem = spk.System.create()  # type: spk.System
        galaxySystem.addGroup(galaxyGroup)
        self.galaxySystem = galaxySystem
        starSystem = spk.System.create()
        starSystem.addGroup(starGroup)
        self.starSystem = starSystem

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.renderEnv = True
        self.angleY = 0.0
        self.angleX = 45.0
        self.camPosZ = 5.0
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]
Esempio n. 10
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # init SDL
        sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1)
        self.window = sdl2.SDL_CreateWindow(
            b"SPARK(&pyspk) Writing Demo", sdl2.SDL_WINDOWPOS_UNDEFINED,
            sdl2.SDL_WINDOWPOS_UNDEFINED, width, height,
            sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_CaptureMouse(True)

        # init OpenGL
        self.context = sdl2.SDL_GL_CreateContext(self.window)
        sdl2.SDL_GL_SetSwapInterval(0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        surface = sdl2.SDL_GetWindowSurface(self.window)
        gl.glViewport(0, 0, surface.contents.w, surface.contents.h)
        # load texture
        texture = loadTexture(b'res/point.bmp', gl.GL_ALPHA, gl.GL_CLAMP)

        # init FTGL
        if not notext:
            font = FTGL.TextureFont('res/font.ttf')
            font.FaceSize(24)
            self.font = font

        # init SPARK(pyspk)
        # random seed initialization
        spk.setRandomSeed(int(time.time()))
        # step configuration
        spk.System.setClampStep(True, 0.1)  # clamp the step to 100 ms
        spk.System.useAdaptiveStep(
            0.001,
            0.01)  # use an adaptive step from 1ms to 10ms (1000fps to 100fps)
        # create Renderer
        renderer = GLQuadRenderer.create(1.0)  # type: GLQuadRenderer
        renderer.enableBlending(True)
        renderer.setBlendingFunctions(gl.GL_SRC_ALPHA, gl.GL_ONE)
        renderer.setTexturingMode(spk.TEXTURE_2D)
        renderer.setTexture(texture)
        renderer.setTextureBlending(gl.GL_MODULATE)
        renderer.setScale(0.05, 0.05)
        renderer.enableRenderingHint(spk.DEPTH_TEST, False)
        self.renderer = renderer
        self.basicRenderer = GLPointRenderer.create(
            1.0)  # bare renderer for comparison(F4)
        # create Model
        model_enable = spk.FLAG_RED | spk.FLAG_GREEN | spk.FLAG_BLUE | spk.FLAG_ALPHA | spk.FLAG_SIZE
        model_mutable = spk.FLAG_ALPHA | spk.FLAG_SIZE
        model = spk.Model.create(model_enable,
                                 model_mutable)  # type: spk.Model
        model.setParam(spk.PARAM_ALPHA, 5.0,
                       0.0)  # the particles will fade as the die
        model.setParam(spk.PARAM_SIZE, 1.0,
                       15.0)  # the particles will enlarge over time
        model.setLifeTime(5.0, 6.0)
        self.model = model
        # create Emitter
        # We set up a spheric emitter that emits in all direction with a very small force
        # in order to slightly displace the particles
        emitter = spk.RandomEmitter.create()
        emitter.setForce(0.01, 0.01)
        self.emitter = emitter  # uses it at later directly
        # create Group
        group = spk.Group.create(model, 14000)  # type: spk.Group
        group.setRenderer(renderer)
        group.setFriction(
            -0.3)  # negative friction: The particles will accelerate over time
        self.group = group
        # create System
        system = spk.System.create()  # type: spk.System
        system.addGroup(group)
        self.system = system

        print('SPARK FACTORY AFTER INIT :')
        spk.Factory.getInstance().traceAll()

        # init Variables
        self.running = True
        self.paused = False
        self.deltaTime = 0
        self.step = 0
        self.text = 2
        self.renderValue = 0
        self.angleY = 0.0
        self.angleX = 0.0
        self.camPosZ = 5.0
        self.add = False
        self.oldX = 0
        self.oldY = 0
        self.oldZ = 0
        self.offset = 0.0
        self.nbParticles = ''
        self.fps = ''
        self.frames = [sdl2.SDL_GetTicks() - 1]
        self.lasttime = self.frames[-1]

        # This computes the ratio to go from screen coordinates to universe coordinates
        self.screenToUniverse = 2.0 * self.camPosZ * math.tan(
            45.0 * 0.5 * math.pi / 180.0) / self.height