Exemple #1
0
 def __init__(self):
     self.GPUfloor1 = es.toGPUShape(
         bs.createTextureNormalsCube(os.path.join("img", "rainbow1.png")),
         GL_REPEAT, GL_NEAREST)
     self.GPUfloor2 = es.toGPUShape(
         bs.createTextureNormalsCube(os.path.join("img", "rainbow2.png")),
         GL_REPEAT, GL_NEAREST)
     self.GPUfloor3 = es.toGPUShape(
         bs.createTextureNormalsCube(os.path.join("img", "rainbow3.png")),
         GL_REPEAT, GL_NEAREST)
     self.GPUfloor4 = es.toGPUShape(
         bs.createTextureNormalsCube(os.path.join("img", "rainbow4.png")),
         GL_REPEAT, GL_NEAREST)
     self.GPUfloor5 = es.toGPUShape(
         bs.createTextureNormalsCube(os.path.join("img", "rainbow5.png")),
         GL_REPEAT, GL_NEAREST)
     self.transform = tr.matmul([
         tr.translate(0, 0, -5),
         tr.scale(20, 20, 0.001),
         tr.uniformScale(1)
     ])
     self.r, self.g, self.b = np.random.random_sample(
     ), np.random.random_sample(), np.random.random_sample()
     self.weirdTimer = 0
     self.timer = 0
     self.floorPicker = 0
Exemple #2
0
 def __init__(self):
     self.GPU = deque([
         es.toGPUShape(
             bs.createTextureCube(
                 os.path.join('mod', 'tex', 'game_over_1.png')), GL_REPEAT,
             GL_NEAREST),
         es.toGPUShape(
             bs.createTextureCube(
                 os.path.join('mod', 'tex', 'game_over_2.png')), GL_REPEAT,
             GL_NEAREST),
         es.toGPUShape(
             bs.createTextureCube(
                 os.path.join('mod', 'tex', 'game_over_3.png')), GL_REPEAT,
             GL_NEAREST),
         es.toGPUShape(
             bs.createTextureCube(
                 os.path.join('mod', 'tex', 'game_over_4.png')), GL_REPEAT,
             GL_NEAREST)
     ])
     self.x, self.y, self.z = 0, 310, 100
     self.phi = 0
     self.tick = 0
     self.s = 10
     self.transform = tr.matmul([
         tr.translate(self.x, self.y, self.z),
         tr.scale(self.s, self.s, 0.0001)
     ])
Exemple #3
0
 def update(self):
     self.theta += 0.1
     self.transform = tr.matmul([
         tr.translate(self.x, self.y, self.z),
         tr.uniformScale(0.5),
         tr.rotationZ(self.theta),
         tr.rotationX(np.pi / 4)
     ])
Exemple #4
0
 def __init__(self):
     self.x, self.y, self.z = 0.0, 0.0, -4.5
     self.theta = 0.0
     self.bend = 0.20
     self.front = 0.20
     self.turn = 0
     self.weirdLight = (1.0, 1.0, 1.0)
     self.transform = tr.matmul([tr.translate(self.x,self.y,self.z),tr.uniformScale(0.5),tr.rotationZ(self.theta)])
Exemple #5
0
 def update(self):
     self.GPU.append(self.GPU.popleft())
     self.tick -= 0.1 * np.exp(self.tick / 10)
     self.phi = np.exp(self.tick)
     self.transform = tr.matmul([
         tr.translate(self.x, self.y, self.z),
         tr.rotationX(2 * self.phi),
         tr.rotationZ(self.phi),
         tr.scale(self.s, self.s, 0.0001)
     ])
Exemple #6
0
    def __init__(self):

        gpu_stem = es.toGPUShape(
            bs.createColorQuad(159 / 255, 107 / 255, 61 / 255))
        gpu_skin = es.toGPUShape(bs.create_circle(0.5, (0, 0),
                                                  (1.0, 0.0, 0.0)))
        gpu_leaf = es.toGPUShape(bs.createColorQuad(0.0, 1.0, 0.0))

        stem = sg.SceneGraphNode("stem")
        stem.transform = tr.matmul(
            [tr.uniformScale(0.5),
             tr.scale(0.5, 1, 1),
             tr.translate(0, 1, 0)])
        stem.childs += [gpu_stem]

        skin = sg.SceneGraphNode("skin")
        skin.transform = tr.matmul([tr.scale(1, 1, 1), tr.translate(0, 0, 0)])
        skin.childs += [gpu_skin]

        leaf = sg.SceneGraphNode("leaf")
        leaf.transform = tr.matmul([
            tr.uniformScale(0.5),
            tr.scale(1, 0.5, 1),
            tr.translate(0.25, 3, 0)
        ])
        leaf.childs += [gpu_leaf]

        apple = sg.SceneGraphNode("apple")
        apple.transform = tr.matmul([
            tr.uniformScale(0.75),
            tr.scale(1 / d["n"], 1 / d["n"], 1),
            tr.scale(1, d["w"] / d["h"], 1)
        ])
        apple.childs += [stem, skin, leaf]

        self.x, self.y = randint(0, d["n"] - 2, 2)
        apple_tr = sg.SceneGraphNode('apple_tr')
        apple_tr.transform = tr.translate(self.x, self.y, 0)
        apple_tr.childs += [apple]

        self.model = apple_tr
Exemple #7
0
 def __init__(self):
     self.c = 0
     self.d = 0
     self.go = True
     self.do = True
     self.GPUmenu = es.toGPUShape(
         obj_reader.readOBJ2(
             os.path.join("objects", "snok.obj"),
             os.path.join("objects", "textures", "text.png")), GL_REPEAT,
         GL_NEAREST)
     self.GPUuntitled = es.toGPUShape(
         obj_reader.readOBJ(os.path.join("objects", "untitled.obj"),
                            (1.0, 1.0, 1.0)), GL_REPEAT, GL_NEAREST)
     self.GPUover = es.toGPUShape(
         obj_reader.readOBJ2(
             os.path.join("objects", "gameover.obj"),
             os.path.join("objects", "textures", "text.png")), GL_REPEAT,
         GL_NEAREST)
     self.menuTransform = tr.matmul([
         tr.translate(0.0, 0.0, 8.0),
         tr.uniformScale(2),
         tr.rotationX(np.pi / 2),
         tr.rotationY(3 * np.pi / 2)
     ])
     self.untitledTransform = tr.matmul([
         tr.translate(-2.0, 0.0, 8.0),
         tr.uniformScale(1.5),
         tr.rotationX(np.pi / 2),
         tr.rotationY(3 * np.pi / 2)
     ])
     self.overTransform = tr.matmul([
         tr.translate(0.0, 0.0, 8.0),
         tr.uniformScale(2),
         tr.rotationX(np.pi / 2),
         tr.rotationY(3 * np.pi / 2)
     ])
Exemple #8
0
 def __init__(self):
     self.x, self.y, self.z = 0.0, 0.0, -4.5
     self.exists = False
     self.theta = 0.0
     self.rare = 1
     self.bans = [(None, None, None)]
     self.GPU = es.toGPUShape(
         obj_reader.readOBJ(os.path.join("objects", "record.obj"),
                            (1.0, 1.0, 1.0)), GL_REPEAT, GL_NEAREST)
     self.transform = tr.matmul([
         tr.translate(0.0, 0.0, self.z),
         tr.uniformScale(1),
         tr.rotationZ(self.theta),
         tr.rotationX(np.pi / 4)
     ])
Exemple #9
0
 def move_forward(self):
     #self.time += self.bend / (self.rho * self.s)
     self.time += self.bend
     self.x += self.rho * np.cos(self.phi)
     self.y += self.rho * np.sin(self.phi)
     self.transform = tr.matmul([
         tr.translate(self.x, self.y, self.z),
         tr.uniformScale(self.s / 2),
         tr.rotationA(
             self.time * self.s,
             np.array([
                 np.cos(self.phi + np.pi / 2),
                 np.sin(self.phi + np.pi / 2), 0
             ])),
         tr.rotationZ(self.phi)
     ])
Exemple #10
0
    def __init__(self, path):

        gpu_part = es.toGPUShape(bs.createTextureQuad(path), GL_REPEAT,
                                 GL_NEAREST)

        part = sg.SceneGraphNode("part")
        part.transform = tr.matmul([
            tr.scale(1, d["w"] / d["h"], 1),
            tr.scale(1 / d["n"], 1 / d["n"], 1)
        ])
        part.childs += [gpu_part]

        part_tr = sg.SceneGraphNode("part_tr")
        part_tr.transform = tr.identity()
        part_tr.childs += [part]

        self.model = part_tr
Exemple #11
0
    def mainMenu(self, pipeline, projection, view, phong_pipeline):
        glUseProgram(pipeline.shaderProgram)

        self.menuTransform = tr.matmul([
            tr.translate(2.0, 0.0, 8.0 + self.c * 2),
            tr.rotationY(-0.25 + self.c * 0.5),
            tr.uniformScale(2),
            tr.rotationX(np.pi / 2),
            tr.rotationY(3 * np.pi / 2)
        ])
        if self.go:
            self.c += 0.001
            if self.c >= 1.0:
                self.go = False
        else:
            self.c -= 0.001
            if self.c <= 0.0:
                self.go = True

        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "La"), 0.85,
                    0.85, 0.85)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ld"), 0.0,
                    0.0, 0.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ls"), 0.2,
                    0.2, 0.2)

        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ka"), 1.0,
                    1.0, 1.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Kd"), 0.0,
                    0.0, 0.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ks"), 1.0,
                    0.0, 1.0)

        glUniform3f(
            glGetUniformLocation(pipeline.shaderProgram, "lightPosition"), 0,
            0, 50)
        glUniform3f(
            glGetUniformLocation(pipeline.shaderProgram, "viewPosition"), 0.0,
            0.0, 0)
        glUniform1ui(glGetUniformLocation(pipeline.shaderProgram, "shininess"),
                     100)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram,
                                 "constantAttenuation"), 0.001)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram, "linearAttenuation"),
            0.0001)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram,
                                 "quadraticAttenuation"), 0.0001)

        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
            self.menuTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "projection"), 1,
            GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE,
            view)
        pipeline.drawShape(self.GPUmenu)

        glUseProgram(phong_pipeline.shaderProgram)

        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "La"),
                    0.0, 0.0, 0.0)
        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "Ld"),
                    1.0, 1.0, 1.0)
        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "Ls"),
                    0.0, 0.0, 0.0)

        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "Ka"),
                    0.0, 0.0, 0.0)
        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "Kd"),
                    1.0, 1.0, 1.0)
        glUniform3f(glGetUniformLocation(phong_pipeline.shaderProgram, "Ks"),
                    0.0, 0.0, 0.0)

        glUniform3f(
            glGetUniformLocation(phong_pipeline.shaderProgram,
                                 "lightPosition"), 0, 0, 50)
        glUniform3f(
            glGetUniformLocation(phong_pipeline.shaderProgram, "viewPosition"),
            0.0, 0.0, 0)
        glUniform1ui(
            glGetUniformLocation(phong_pipeline.shaderProgram, "shininess"),
            100)
        glUniform1f(
            glGetUniformLocation(phong_pipeline.shaderProgram,
                                 "constantAttenuation"), 0.001)
        glUniform1f(
            glGetUniformLocation(phong_pipeline.shaderProgram,
                                 "linearAttenuation"), 0.0001)
        glUniform1f(
            glGetUniformLocation(phong_pipeline.shaderProgram,
                                 "quadraticAttenuation"), 0.0001)

        glUniformMatrix4fv(
            glGetUniformLocation(phong_pipeline.shaderProgram, "model"), 1,
            GL_TRUE, self.untitledTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(phong_pipeline.shaderProgram, "projection"),
            1, GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(phong_pipeline.shaderProgram, "view"), 1,
            GL_TRUE, view)
        phong_pipeline.drawShape(self.GPUuntitled)
Exemple #12
0
    def gameOver(self, pipeline, projection, view):
        glUseProgram(pipeline.shaderProgram)

        self.overTransform = tr.matmul([
            tr.translate(0.0, 0.0, 8.0 + self.d * 2),
            tr.rotationY(-0.25 + self.d * 0.5),
            tr.rotationX(-0.25 + self.d * 0.5),
            tr.uniformScale(2),
            tr.rotationX(np.pi / 2),
            tr.rotationY(3 * np.pi / 2)
        ])
        if self.do:
            self.d += 0.001
            if self.d >= 1.0:
                self.do = False
        else:
            self.d -= 0.001
            if self.d <= 0.0:
                self.do = True

        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "La"), 0.85,
                    0.85, 0.85)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ld"), 0.0,
                    0.0, 0.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ls"), 0.2,
                    0.2, 0.2)

        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ka"), 1.0,
                    1.0, 1.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Kd"), 0.0,
                    0.0, 0.0)
        glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "Ks"), 1.0,
                    0.0, 1.0)

        glUniform3f(
            glGetUniformLocation(pipeline.shaderProgram, "lightPosition"), 0,
            0, 50)
        glUniform3f(
            glGetUniformLocation(pipeline.shaderProgram, "viewPosition"), 0.0,
            0.0, 0)
        glUniform1ui(glGetUniformLocation(pipeline.shaderProgram, "shininess"),
                     100)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram,
                                 "constantAttenuation"), 0.001)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram, "linearAttenuation"),
            0.0001)
        glUniform1f(
            glGetUniformLocation(pipeline.shaderProgram,
                                 "quadraticAttenuation"), 0.0001)

        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
            self.overTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "projection"), 1,
            GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE,
            view)
        pipeline.drawShape(self.GPUover)
Exemple #13
0
def vista():

    # Initialize glfw
    if not glfw.init(): sys.exit()

    WIDTH = 1000
    HEIGHT = 800

    window = glfw.create_window(WIDTH, HEIGHT, "DISCO SNOK! 3D", None, None)

    if not window:
        glfw.terminate()
        sys.exit()

    glfw.make_context_current(window)

    ctrl = Controller()
    glfw.set_key_callback(window, ctrl.on_key)

    # Assembling the shader program (pipeline) with all shaders
    pipeline = es.SimpleModelViewProjectionShaderProgram()  # SIMPLE PIPELINE
    texture_pipeline = es.SimpleTextureModelViewProjectionShaderProgram(
    )  # TEXTURE PIPELINE
    lighting_pipeline = ls.SimpleTexturePhongShaderProgram(
    )  # LIGHTING PIPELINE
    phong_pipeline = ls.SimplePhongShaderProgram()  # PHONG PIPELINE

    # Telling OpenGL to use our shader program
    glUseProgram(pipeline.shaderProgram)

    # Setting up the clear screen color
    glClearColor(0.0, 0.0, 0.0, 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)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

    # Using the same view and projection matrices in the whole application
    projection = tr.perspective(45, float(WIDTH) / float(HEIGHT), 0.1, 100)

    # MODELOS
    floor = Floor()
    vinyl = Vinyl()
    snake = Snake()
    ctrl.snake = snake.snake_parts[0]
    snake.objective = vinyl
    snake.floor = floor
    vinyl.bans = snake.positions
    screens = Screens()
    camera = Camera()
    camera.snakeView = snake.snake_parts[0]
    ctrl.camera = camera

    wall = bs.createTextureCube(os.path.join("img", "clouds.png"))
    GPUwall = es.toGPUShape(wall, GL_REPEAT, GL_NEAREST)
    wallTransform = tr.matmul(
        [tr.translate(0, 0, 0),
         tr.uniformScale(50),
         tr.uniformScale(1)])
    building = bs.createTextureCube(os.path.join("img", "building.png"))
    GPUbuilding = es.toGPUShape(building, GL_REPEAT, GL_NEAREST)
    buildingTransform = tr.matmul(
        [tr.translate(0, 0, -10.01),
         tr.scale(20, 20, 10),
         tr.uniformScale(1)])
    bottom = bs.createColorCube(0.0, 0.0, 0.0)
    GPUbottom = es.toGPUShape(bottom, GL_REPEAT, GL_NEAREST)
    bottomTransform = tr.matmul(
        [tr.translate(0, 0, -22),
         tr.scale(49.9, 49.9, 1),
         tr.uniformScale(1)])

    limitFPS = 1.0 / 30.0

    lastTime = 0
    timer = 0

    deltaTime = 0
    nowTime = 0

    frames = 0
    updates = 0

    playsound(os.path.join("sound", "Conga_2.mp3"), block=False)

    while not glfw.window_should_close(window):
        # Using GLFW to check for input events
        glfw.poll_events()

        # Clearing the screen in both, color and depth
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Main Menu
        if not ctrl.gameStart:
            view = tr.lookAt(
                np.array([0, 0, 20]),  # eye
                np.array([0.0001, 0, 0]),  # at
                np.array([0, 0, 1])  # up
            )

            glUseProgram(texture_pipeline.shaderProgram)
            wallTransform0 = tr.matmul([
                tr.translate(0, 0, 0),
                tr.scale(22, 22, 0.001),
                tr.uniformScale(1),
                tr.rotationZ(3 * np.pi / 2)
            ])
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram, "model"),
                1, GL_TRUE, wallTransform0)
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram,
                                     "projection"), 1, GL_TRUE, projection)
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram, "view"),
                1, GL_TRUE, view)
            texture_pipeline.drawShape(GPUwall)

            screens.mainMenu(lighting_pipeline, projection, view,
                             phong_pipeline)
            glfw.swap_buffers(window)
            continue
        elif ctrl.gameStart and lastTime == 0:
            lastTime = glfw.get_time()
            timer = lastTime

        # GAME OVER
        if not snake.alive:
            view = tr.lookAt(
                np.array([0, 0, 20]),  # eye
                np.array([0.0001, 0, 0]),  # at
                np.array([0, 0, 1])  # up
            )

            glUseProgram(texture_pipeline.shaderProgram)
            wallTransform0 = tr.matmul([
                tr.translate(0, 0, 0),
                tr.scale(22, 22, 0.001),
                tr.uniformScale(1),
                tr.rotationZ(3 * np.pi / 2)
            ])
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram, "model"),
                1, GL_TRUE, wallTransform0)
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram,
                                     "projection"), 1, GL_TRUE, projection)
            glUniformMatrix4fv(
                glGetUniformLocation(texture_pipeline.shaderProgram, "view"),
                1, GL_TRUE, view)
            texture_pipeline.drawShape(GPUwall)

            screens.gameOver(lighting_pipeline, projection, view)
            glfw.swap_buffers(window)
            continue

        # Calculamos el dt
        nowTime = glfw.get_time()
        deltaTime += (nowTime - lastTime) / limitFPS
        lastTime = nowTime

        if not vinyl.exists:
            vinyl.spawn()

        while deltaTime >= 1.0:
            vinyl.update()
            snake.move()
            floor.timer += 1
            if floor.timer % 20 == 0:
                floor.floorPicker += 1
            if floor.weirdTimer > 0:
                if floor.weirdTimer % 10 == 0:
                    floor.update()
                    snake.weirdLight = (floor.r, floor.g, floor.b)
                    snake.lightChange()
                floor.weirdTimer -= 1
                if floor.weirdTimer == 0:
                    snake.weirdLight = (1.0, 1.0, 1.0)
                    snake.lightChange()
            updates += 1
            deltaTime -= 1.0

        view = camera.view()

        if timer > 2.0:
            snake.collisions()

        glUseProgram(texture_pipeline.shaderProgram)

        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "model"), 1,
            GL_TRUE, wallTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "projection"),
            1, GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "view"), 1,
            GL_TRUE, view)
        texture_pipeline.drawShape(GPUwall)

        glUseProgram(texture_pipeline.shaderProgram)

        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "model"), 1,
            GL_TRUE, buildingTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "projection"),
            1, GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(texture_pipeline.shaderProgram, "view"), 1,
            GL_TRUE, view)
        texture_pipeline.drawShape(GPUbuilding)

        glUseProgram(pipeline.shaderProgram)

        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
            bottomTransform)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "projection"), 1,
            GL_TRUE, projection)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE,
            view)
        pipeline.drawShape(GPUbottom)

        glUseProgram(lighting_pipeline.shaderProgram)
        floor.draw(lighting_pipeline, projection, view)
        snake.draw(lighting_pipeline, projection, view)
        vinyl.draw(phong_pipeline, projection, view)

        frames += 1

        # Once the render is done, buffers are swapped, showing only the complete scene.
        glfw.swap_buffers(window)

        if glfw.get_time() - timer > 1.0:
            timer += 1
            # print("FPS: ",frames," Updates: ",updates)
            updates = 0
            frames = 0

    glfw.terminate()
Exemple #14
0
    def draw(self, pipeline, apple):

        for j in range(len(self.grid)):
            for i in range(len(self.grid)):

                if self.grid[j][i] < 0:
                    continue

                elif self.time - self.grid[j][i] < self.length:

                    if abs(apple.x - self.x) < 2 and abs(apple.y - self.y) < 2:
                        self.head = self.head3
                    elif abs(apple.x - self.x) < 3 and abs(apple.y -
                                                           self.y) < 3:
                        self.head = self.head2
                    elif abs(apple.x - self.x) < 4 and abs(apple.y -
                                                           self.y) < 4:
                        self.head = self.head1
                    else:
                        self.head = self.head0

                    # head
                    if self.grid[j][i] == self.time:

                        # down
                        if self.ddx == 0 and self.ddy == -1:
                            if d["n"] % 2 == 0:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.head.model, pipeline,
                                                  "transform")

                        # right
                        elif self.ddx == 1 and self.ddy == 0:
                            if d["n"] % 2 == 0:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.head.model, pipeline,
                                                  "transform")

                        # up
                        elif self.ddx == 0 and self.ddy == 1:
                            if d["n"] % 2 == 0:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])

                            sg.drawSceneGraphNode(self.head.model, pipeline,
                                                  "transform")

                        # left
                        elif self.ddx == -1 and self.ddy == 0:
                            if d["n"] % 2 == 0:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(3 * np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.head.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(3 * np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])

                            sg.drawSceneGraphNode(self.head.model, pipeline,
                                                  "transform")

                    else:

                        # vertical body
                        if 0 < j < d["n"] - 3 and (self.grid[j][i] == self.grid[j+1][i]+1 and self.grid[j][i] == self.grid[j-1][i]-1) or \
                            (self.grid[j][i] == self.grid[j+1][i]-1 and self.grid[j][i] == self.grid[j-1][i]+1):
                            if d["n"] % 2 == 0:
                                self.body.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])

                            else:
                                self.body.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    #tr.scale(d["h"]/d["w"], 1, 1),
                                    tr.translate(0, 0, 0)
                                ])

                            sg.drawSceneGraphNode(self.body.model, pipeline,
                                                  "transform")

                        # horizontal body
                        elif 0 < i < d["n"] - 3 and (self.grid[j][i] == self.grid[j][i+1]+1 and self.grid[j][i] == self.grid[j][i-1]-1) or \
                            (self.grid[j][i] == self.grid[j][i+1]-1 and self.grid[j][i] == self.grid[j][i-1]+1):
                            if d["n"] % 2 == 0:
                                self.body.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.body.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.body.model, pipeline,
                                                  "transform")

                        # top left curve
                        elif 0 < i and j < d["n"] - 3 and (self.grid[j][i] == self.grid[j+1][i]+1 and self.grid[j][i] == self.grid[j][i-1]-1) or \
                            (self.grid[j][i] == self.grid[j+1][i]-1 and self.grid[j][i] == self.grid[j][i-1]+1):
                            if d["n"] % 2 == 0:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.curve.model, pipeline,
                                                  "transform")

                        # top right curve
                        elif i < d["n"] - 3 and j < d["n"] - 3 and (self.grid[j][i] == self.grid[j+1][i]+1 and self.grid[j][i] == self.grid[j][i+1]-1) or \
                            (self.grid[j][i] == self.grid[j+1][i]-1 and self.grid[j][i] == self.grid[j][i+1]+1):
                            if d["n"] % 2 == 0:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.curve.model, pipeline,
                                                  "transform")

                        # bottom left curve
                        elif 0 < i and 0 < j and (self.grid[j][i] == self.grid[j-1][i]+1 and self.grid[j][i] == self.grid[j][i-1]-1) or \
                            (self.grid[j][i] == self.grid[j-1][i]-1 and self.grid[j][i] == self.grid[j][i-1]+1):
                            if d["n"] % 2 == 0:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(3 * np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            else:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0),
                                    tr.rotationZ(3 * np.pi / 2),
                                    tr.scale(d["w"] / d["h"], d["h"] / d["w"],
                                             1),
                                    tr.translate(0, 0, 0)
                                ])
                            sg.drawSceneGraphNode(self.curve.model, pipeline,
                                                  "transform")

                        # bottom right curve
                        elif i < d["n"] - 3 and 0 < j and (self.grid[j][i] == self.grid[j-1][i]+1 and self.grid[j][i] == self.grid[j][i+1]-1) or \
                            (self.grid[j][i] == self.grid[j-1][i]-1 and self.grid[j][i] == self.grid[j][i+1]+1):
                            if d["n"] % 2 == 0:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                        (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                        d["w"] / d["h"], 0),
                                ])

                            else:
                                self.curve.model.transform = tr.matmul([
                                    tr.translate(
                                        (i - d["n"] // 2 + 1) / d["n"],
                                        (j - d["n"] // 2 + 1) / d["n"] *
                                        d["w"] / d["h"], 0)
                                ])

                            sg.drawSceneGraphNode(self.curve.model, pipeline,
                                                  "transform")

                        else:
                            if d["n"] % 2 == 0:
                                self.green.model.transform = tr.translate(
                                    (i - d["n"] // 2 + 1 + 0.5) / d["n"],
                                    (j - d["n"] // 2 + 1 + 0.5) / d["n"] *
                                    d["w"] / d["h"], 0)
                            else:
                                self.green.model.transform = tr.translate(
                                    (i - d["n"] // 2 + 1) / d["n"],
                                    (j - d["n"] // 2 + 1) / d["n"] * d["w"] /
                                    d["h"], 0)
                            sg.drawSceneGraphNode(self.green.model, pipeline,
                                                  "transform")
        self.ddx = self.dx
        self.ddy = self.dy
Exemple #15
0
 def move(self):
     self.transform = tr.matmul([tr.translate(self.x,self.y,self.z),tr.uniformScale(0.5),tr.rotationZ(self.theta)])
Exemple #16
0
 def move(self):
     self.x += self.front*np.cos(self.theta)
     self.y += self.front*np.sin(self.theta)
     self.transform = tr.matmul([tr.translate(self.x,self.y,self.z),tr.uniformScale(0.5),tr.rotationZ(self.theta)])
Exemple #17
0
 def draw(self, pipeline):
     self.model.transform = tr.matmul(
         [tr.rotationX(2 * self.phi),
          tr.rotationZ(self.phi)])
     sg.drawSceneGraphNode(self.model, pipeline, "transform")
                                 "quadraticAttenuation"), 0.0011)

        # Setting MVP of OBJ
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "projection"),
            1,
            GL_TRUE,
            projection,
        )
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE,
            view)
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "model"),
            1,
            GL_TRUE,
            tr.matmul([
                tr.rotationX(np.pi / 2),
                tr.translate(1.5, -0.25, 0),
                tr.uniformScale(_controller.size),
            ]),
        )

        # Drawing given OBJ.
        pipeline.drawShape(gpuOBJ)

        # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
        glfw.swap_buffers(window)

    glfw.terminate()