Ejemplo n.º 1
0
def show_window(window, program, loc, loc_color, vertices):

    R = 1.0
    G = 0.0
    B = 0.0

    glfw.show_window(window)

    while not glfw.window_should_close(window):

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        # Draw Triangle
        mat_translation = np.zeros((4, 4), np.float32)
        mat_translation = scale()

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_translation)

        glDrawArrays(GL_TRIANGLES, 0, len(vertices))
        glUniform4f(loc_color, R, G, B, 1.0)  # modifies object color

        glfw.swap_buffers(window)

    glfw.terminate()
Ejemplo n.º 2
0
def show_window(window, program, loc, loc_color, vertices):

    glfw.show_window(window)

    while not glfw.window_should_close(window):

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        # Draw Circle
        mat_translation = np.zeros((4, 4), np.float32)
        mat_translation = translation()

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_translation)

        glUniform4f(loc_color, 0.521, 0.521, 0.521, 1)

        glDrawArrays(GL_TRIANGLE_FAN, 0, len(vertices))

        glfw.swap_buffers(window)

    glfw.terminate()
Ejemplo n.º 3
0
 def show_window(self):
     '''
     Shows renderer window
     :return: None
     '''
     self.is_window_hidden = False
     glfw.show_window(self.__window)
Ejemplo n.º 4
0
    def _on_render(self, env):
        env_info = self._envs[env.id]

        if self._hidden:
            glfw.show_window(self._window)
            self._hidden = False

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, env_info.texture)

        # http://stackoverflow.com/questions/27712437/opengl-how-do-i-affect-the-lighting-on-a-textured-plane
        gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_REPLACE)

        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glBegin(gl.GL_QUADS)
        gl.glTexCoord2f(0.0, 0.0)
        gl.glVertex3f(0.0, 0.0, 0)
        gl.glTexCoord2f(0.0, 1.0)
        gl.glVertex3f(0.0, 1.0, 0)
        gl.glTexCoord2f(1.0, 1.0)
        gl.glVertex3f(1.0, 1.0, 0)
        gl.glTexCoord2f(1.0, 0.0)
        gl.glVertex3f(1.0, 0.0, 0)
        gl.glEnd()
        gl.glDisable(gl.GL_TEXTURE_2D)

        # Swap front and back buffers
        glfw.swap_buffers(self._window)
Ejemplo n.º 5
0
 def init(this):
     if not glfw.init():
         return
     this.windowID = glfw.create_window(640, 480, "Test", None, None)
     glfw.show_window(this.windowID)
     glfw.make_context_current(this.windowID)
     glfw.swap_interval(1)
     glClearColor(0, 0, 0, 1);
Ejemplo n.º 6
0
 def init(this):
     if not glfw.init():
         return
     this.windowID = glfw.create_window(640, 480, "Test", None, None)
     glfw.show_window(this.windowID)
     glfw.make_context_current(this.windowID)
     glfw.swap_interval(1)
     glClearColor(0, 0, 0, 1)
Ejemplo n.º 7
0
 def _vispy_set_visible(self, visible):
     # Show or hide the window or widget
     if self._id is None:
         return
     if visible:
         glfw.show_window(self._id)
         # this ensures that the show takes effect
         self._vispy_update()
     else:
         glfw.hide_window(self._id)
Ejemplo n.º 8
0
def show_window(window, program):

    glfw.show_window(window)
    glfw.set_cursor_pos(window, lastX, lastY)

    glEnable(GL_DEPTH_TEST)

    rotacao_inc = 0

    while not glfw.window_should_close(window):

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glClearColor(1.0, 1.0, 1.0, 1.0)

        if polygonal_mode == True:
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        if polygonal_mode == False:
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        draw_ground(program)
        draw_house(program)
        draw_trees(program)
        draw_frog(program)
        draw_horse(program)
        draw_chair(program)
        draw_screen(program)
        draw_table(program)
        draw_cpu(program)
        draw_wind_mill_body(program)
        draw_wind_mill_circle(program, rotacao_inc)
        draw_sky(program)
        draw_street(program)
        draw_secondary_yard(program)
        draw_floor(program)
        draw_keyboard(program)

        rotacao_inc += 0.1

        mat_view = view()
        loc_view = glGetUniformLocation(program, "view")
        glUniformMatrix4fv(loc_view, 1, GL_FALSE, mat_view)

        mat_projection = projection()
        loc_projection = glGetUniformLocation(program, "projection")
        glUniformMatrix4fv(loc_projection, 1, GL_FALSE, mat_projection)

        glfw.swap_buffers(window)

    glfw.terminate()
Ejemplo n.º 9
0
    def open_output_window(self):
        self._pause_event_loop()

        # Select monitor
        self._select_monitor()

        # Open window
        if self.monitor:
            # Fullscreen
            self._select_video_mode()
            x, y = glfw.get_monitor_pos(self.monitor)
            logger.info("Output selected : {} on {} at {},{}".format(
                str(self.video_mode), glfw.get_monitor_name(self.monitor), x,
                y))
            w, h = self.video_mode[0]
            self.pano_renderer.setViewport(w, h)
            self.pano_renderer.setRefreshRate(
                self._convert_rate(30), self._convert_rate(self.video_mode[2]))
            if self.output_window:
                glfw.set_window_monitor(self.output_window, self.monitor, x, y,
                                        w, h, self.video_mode[2])
                glfw.show_window(self.output_window)
                self.pano_renderer.enableOutput(True)
            else:
                self._open_output_window(w, h, self.monitor, self.video_mode)
        else:
            # No monitor available or windowed
            w = self.width / 4
            h = self.height / 4
            self.pano_renderer.setViewport(w, h)

            monitor = glfw.get_primary_monitor()
            if monitor:
                rate = glfw.get_video_mode(monitor)[2]
                self.pano_renderer.setRefreshRate(self._convert_rate(30),
                                                  self._convert_rate(rate))

            if self.output_window:
                self.pano_renderer.enableOutput(True)
                glfw.show_window(self.output_window)
            else:
                self._open_output_window(w, h)
                if not SETTINGS.display in ["window", "windowed"]:
                    # No monitor available
                    glfw.hide_window(self.output_window)

        self._unpause_event_loop()
def display_image(image, zoom=None, size=None, title=None):  # HWC
    # Import OpenGL and glfw.
    import OpenGL.GL as gl
    import glfw

    # Zoom image if requested.
    image = np.asarray(image)
    if size is not None:
        assert zoom is None
        zoom = max(1, size // image.shape[0])
    if zoom is not None:
        image = image.repeat(zoom, axis=0).repeat(zoom, axis=1)
    height, width, channels = image.shape

    # Initialize window.
    if title is None:
        title = 'Debug window'
    global _glfw_window
    if _glfw_window is None:
        glfw.init()
        _glfw_window = glfw.create_window(width, height, title, None, None)
        glfw.make_context_current(_glfw_window)
        glfw.show_window(_glfw_window)
        glfw.swap_interval(0)
    else:
        glfw.make_context_current(_glfw_window)
        glfw.set_window_title(_glfw_window, title)
        glfw.set_window_size(_glfw_window, width, height)

    # Update window.
    glfw.poll_events()
    gl.glClearColor(0, 0, 0, 1)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glWindowPos2f(0, 0)
    gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
    gl_format = {3: gl.GL_RGB, 2: gl.GL_RG, 1: gl.GL_LUMINANCE}[channels]
    gl_dtype = {
        'uint8': gl.GL_UNSIGNED_BYTE,
        'float32': gl.GL_FLOAT
    }[image.dtype.name]
    gl.glDrawPixels(width, height, gl_format, gl_dtype, image[::-1])
    glfw.swap_buffers(_glfw_window)
    if glfw.window_should_close(_glfw_window):
        return False
    return True
Ejemplo n.º 11
0
def show_window(window, program, loc, loc_color, vertices):

    glfw.show_window(window)

    glEnable(GL_DEPTH_TEST)

    while not glfw.window_should_close(window):

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glClearColor(1.0, 1.0, 1.0, 1.0)

        mat_transform = np.zeros((4, 4), np.float32)
        mat_transform = apply_transformations()

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_transform)

        # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        # Drawing cylinder side
        for polygonm in range(0, len(vertices) - (2 * 60), 3):

            random.seed(polygonm)
            R = random.random()
            G = random.random()
            # B = random.random()
            glUniform4f(loc_color, R, G, 1.0, 1.0)
            glDrawArrays(GL_TRIANGLES, polygonm, 3)

        # Drawing cylinder top
        glUniform4f(loc_color, 0.521, 0.521, 1, 1)
        glDrawArrays(GL_TRIANGLE_FAN, len(vertices) - 2 * 60, 60)

        # Drawing cylinder base
        glUniform4f(loc_color, 0.521, 0.521, 1, 1)
        glDrawArrays(GL_TRIANGLE_FAN, len(vertices) - 60, 60)

        glfw.swap_buffers(window)

    glfw.terminate()
def show_window(window, program, loc, loc_color, vertices):

    R = 1.0
    G = 0.0
    B = 0.0

    glfw.show_window(window)

    while not glfw.window_should_close(window):

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        # Draw Triangle
        mat_translation = np.zeros((4, 4), np.float32)
        mat_translation = rotation()

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_translation)

        # modificando a cor do triangulo!
        glUniform4f(loc_color, R, G, B, 1.0)
        glDrawArrays(GL_TRIANGLES, 0, 3)

        # Draw Circle
        mat_translation2 = np.zeros((4, 4), np.float32)
        mat_translation2 = translation()

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_translation2)

        # modificando a cor do circulo!
        glUniform4f(loc_color, 0.521, 0.521, 0.521, 1)
        glDrawArrays(GL_TRIANGLE_FAN, 3, len(vertices))

        glfw.swap_buffers(window)

    glfw.terminate()
Ejemplo n.º 13
0
def open_window(title,
                posX,
                posY,
                width,
                height,
                share=None,
                key_callback=key_callback):
    glfw.window_hint(glfw.VISIBLE, False)
    glfw.window_hint(glfw.DECORATED, False)
    glfw.window_hint(glfw.SAMPLES, 8)
    window = glfw.create_window(width, height, title, None, share)
    if not window:
        return None
    window.name = title  # for compliance with dGraph
    window.classifier = 'window'  # for compliance with dGraph
    glfw.make_context_current(window)
    glfw.swap_interval(
        0
    )  # doesn't seem to be helping fix vsync - actually turning it to 0 does seem to help
    glfw.set_window_pos(window, posX, posY)
    glfw.show_window(window)
    glfw.set_key_callback(window, key_callback)
    return window
Ejemplo n.º 14
0
    global cameraPos, cameraFront, cameraUp
    mat_view = glm.lookAt(cameraPos, cameraPos + cameraFront, cameraUp)
    mat_view = np.array(mat_view)
    return mat_view


def projection():
    global altura, largura
    # perspective parameters: fovy, aspect, near, far
    mat_projection = glm.perspective(glm.radians(45.0), largura / altura, 0.1,
                                     3000.0)
    mat_projection = np.array(mat_projection)
    return mat_projection


glfw.show_window(window)
glfw.set_cursor_pos(window, lastX, lastY)

glEnable(GL_DEPTH_TEST)  ### importante para 3D

#fazer o gollum ficar pulando
jump_speed = 0.35
while not glfw.window_should_close(window):

    glfw.poll_events()

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glClearColor(1.0, 1.0, 1.0, 1.0)

    if polygonal_mode == True:
Ejemplo n.º 15
0
    def __init__(self, vispy_canvas, **kwargs):
        BaseCanvasBackend.__init__(self, vispy_canvas)
        p = self._process_backend_kwargs(kwargs)
        self._initialized = False

        # Deal with config
        _set_config(p.context.config)
        # Deal with context
        p.context.shared.add_ref('glfw', self)
        if p.context.shared.ref is self:
            share = None
        else:
            share = p.context.shared.ref._id

        glfw.window_hint(glfw.REFRESH_RATE, 0)  # highest possible
        glfw.window_hint(glfw.RESIZABLE, int(p.resizable))
        glfw.window_hint(glfw.DECORATED, int(p.decorate))
        glfw.window_hint(glfw.VISIBLE, 0)  # start out hidden
        glfw.window_hint(glfw.FLOATING, int(p.always_on_top))
        if p.fullscreen is not False:
            self._fullscreen = True
            if p.fullscreen is True:
                monitor = glfw.get_primary_monitor()
            else:
                monitor = glfw.get_monitors()
                if p.fullscreen >= len(monitor):
                    raise ValueError('fullscreen must be <= %s' % len(monitor))
                monitor = monitor[p.fullscreen]
            use_size = glfw.get_video_mode(monitor)[:2]
            if use_size != tuple(p.size):
                logger.debug('Requested size %s, will be ignored to '
                             'use fullscreen mode %s' % (p.size, use_size))
            size = use_size
        else:
            self._fullscreen = False
            monitor = None
            size = p.size

        self._id = glfw.create_window(width=size[0],
                                      height=size[1],
                                      title=p.title,
                                      monitor=monitor,
                                      share=share)
        if not self._id:
            raise RuntimeError('Could not create window')

        glfw.make_context_current(self._id)
        glfw.swap_interval(1 if p.vsync else 0)  # needs a valid context

        _VP_GLFW_ALL_WINDOWS.append(self)
        self._mod = list()

        # Register callbacks
        glfw.set_window_refresh_callback(self._id, self._on_draw)
        glfw.set_window_size_callback(self._id, self._on_resize)
        glfw.set_key_callback(self._id, self._on_key_press)
        glfw.set_char_callback(self._id, self._on_key_char)
        glfw.set_mouse_button_callback(self._id, self._on_mouse_button)
        glfw.set_scroll_callback(self._id, self._on_mouse_scroll)
        glfw.set_cursor_pos_callback(self._id, self._on_mouse_motion)
        glfw.set_window_close_callback(self._id, self._on_close)
        self._vispy_canvas_ = None
        self._needs_draw = False
        self._vispy_canvas.set_current()
        if p.position is not None:
            self._vispy_set_position(*p.position)
        if p.show:
            glfw.show_window(self._id)

        # Init
        self._initialized = True
        self._next_key_events = []
        self._next_key_text = {}
        self._vispy_canvas.set_current()
        self._vispy_canvas.events.initialize()
        self._on_resize(self._id, size[0], size[1])
Ejemplo n.º 16
0
def show_window(window, program, loc, loc_color, vertices, buffer):

    R = 1.0
    G = 0.0
    B = 0.0

    glfw.show_window(window)

    while not glfw.window_should_close(window):

        global angle

        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        #########################
        ########## SKY ##########
        #########################

        # Transformation matrix
        mat_tranformation = scale(1, 1)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_tranformation)

        # Checks if sun is between the 3rd and 4th quadrants or not
        # If so, we change the sky color to night
        # (because the moon will be showing on)
        if int(angle) % 360 > 90 and int(angle) % 360 < 270:
            glUniform4f(loc_color, 0, 0.062, 0.478, 1.0)
        else:
            glUniform4f(loc_color, 0.098, 0.611, 0.921, 1.0)

        # Drawing
        glDrawArrays(GL_TRIANGLE_STRIP, 75, 4)

        #########################
        ########## MOON #########
        #########################

        # Transformation matrix
        mat_rotation_sol = rotation(angle)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_rotation_sol)

        # Drawing
        glUniform4f(loc_color, 0.709, 0.717, 0.768, 1.0)
        glDrawArrays(GL_TRIANGLE_FAN, 783, 64)

        #########################
        ########## SUN ##########
        #########################

        # Transformation matrix
        mat_rotation_sol = rotation(angle)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_rotation_sol)

        # Drawing
        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # first square
        glDrawArrays(GL_TRIANGLE_STRIP, 14, 4)

        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # second square
        glDrawArrays(GL_TRIANGLE_STRIP, 18, 4)

        #########################
        ######### FIELD #########
        #########################

        # Transformation matrix
        mat_tranformation = scale(1, 1)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, mat_tranformation)

        # Drawing
        glUniform4f(loc_color, 0.368, 0.662, 0.356, 1.0)
        glDrawArrays(GL_TRIANGLE_STRIP, 71, 4)

        #########################
        ######### HOUSE #########
        #########################

        glUniform4f(loc_color, 0.858, 0.454, 0.101, 1.0)  # bigger roof
        glDrawArrays(GL_TRIANGLES, 3, 3)

        glUniform4f(loc_color, 0.101, 0.207, 0.858, 1.0)  # smaller roof
        glDrawArrays(GL_TRIANGLES, 0, 3)

        glUniform4f(loc_color, 0.101, 0.207, 0.858, 1.0)  # base
        glDrawArrays(GL_TRIANGLE_STRIP, 6, 4)

        glUniform4f(loc_color, 0.721, 0.321, 0.196, 1.0)  # door
        glDrawArrays(GL_TRIANGLE_STRIP, 10, 4)

        #########################
        ######### TREE ##########
        #########################

        glUniform4f(loc_color, 0.721, 0.321, 0.196, 1.0)  # trunk
        glDrawArrays(GL_TRIANGLE_STRIP, 57, 4)

        glUniform4f(loc_color, 0.129, 0.270, 0.156, 1.0)  # top
        glDrawArrays(GL_TRIANGLE_STRIP, 61, 3)

        #########################
        ###### BIRD HOUSE #######
        #########################

        glUniform4f(loc_color, 0.721, 0.321, 0.196, 1.0)  # rectangle
        glDrawArrays(GL_TRIANGLE_STRIP, 64, 4)

        glUniform4f(loc_color, 0.0, 0.0, 0.0, 1.0)  # roof
        glDrawArrays(GL_TRIANGLE_STRIP, 68, 3)

        glUniform4f(loc_color, 0.2, 0.2, 0.2, 1.0)  # house's entrance
        glDrawArrays(GL_TRIANGLE_FAN, 207, 64)

        glUniform4f(loc_color, 0.0, 0.0, 0.0, 1.0)  # house's entrance 2
        glDrawArrays(GL_TRIANGLE_FAN, 271, 64)

        #########################
        ######### MAN ###########
        #########################

        # Translation and Scale matrices
        man_translation_mat = translation(t_x_man, t_y_man)
        man_scale_mat = scale(e_x_man, e_y_man)

        mtm = man_translation_mat.reshape((4, 4))
        msm = man_scale_mat.reshape((4, 4))

        man_transformation_mat = np.matmul(mtm, msm).reshape((1, 16))

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, man_transformation_mat)

        # Drawing
        glUniform4f(loc_color, 0.0, 0.0, 1.0, 1.0)  # head
        glDrawArrays(GL_TRIANGLE_FAN, 335, 64)

        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # torso
        glDrawArrays(GL_TRIANGLE_STRIP, 34, 4)

        glUniform4f(loc_color, 0.0, 0.894, 0.5, 1.0)  # legs
        glDrawArrays(GL_TRIANGLE_STRIP, 38, 4)

        glUniform4f(loc_color, 0.0, 1.0, 0.0, 1.0)  # right arm
        glDrawArrays(GL_TRIANGLE_STRIP, 43, 4)

        glUniform4f(loc_color, 0.0, 1.0, 0.0, 1.0)  # left arm
        glDrawArrays(GL_TRIANGLE_STRIP, 47, 4)

        #########################
        ######### BIRD ##########
        #########################

        # Translation matrix
        bird_translation_mat = translation(t_x_bird, t_y_bird)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, bird_translation_mat)

        # Drawing
        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # body
        glDrawArrays(GL_TRIANGLE_FAN, 79, 64)

        glUniform4f(loc_color, 0.0, 0.0, 0.0, 1.0)  # eye
        glDrawArrays(GL_TRIANGLE_FAN, 143, 64)

        bird_wings_swing(vertices, buffer)

        glUniform4f(loc_color, 0.858, 0.796, 0, 1.0)  # wing
        glDrawArrays(GL_TRIANGLE_STRIP, 22, 4)

        glUniform4f(loc_color, 0.921, 0.603, 0.098, 1.0)  # beak
        glDrawArrays(GL_TRIANGLES, 31, 3)

        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # tail 1
        glDrawArrays(GL_LINE_STRIP, 25, 2)

        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # tail 2
        glDrawArrays(GL_LINE_STRIP, 27, 2)

        glUniform4f(loc_color, 0.960, 0.894, 0, 1.0)  # tail 3
        glDrawArrays(GL_LINE_STRIP, 29, 2)

        #########################
        ######## CLOUDS #########
        #########################

        cloud_movement()

        # Translation matrix
        clouds_translation_mat = translation(t_x_clouds, t_y_clouds)

        loc = glGetUniformLocation(program, "mat_transformation")
        glUniformMatrix4fv(loc, 1, GL_TRUE, clouds_translation_mat)

        # Changes clouds color according to the angle position
        # (if the angle is between the 3rd and 4th quadrants)
        if int(angle) % 360 > 90 and int(angle) % 360 < 270:
            glUniform4f(loc_color, 0.529, 0.537, 0.592, 1.0)
        else:
            glUniform4f(loc_color, 1, 1, 1, 1.0)

        # first cloud
        glDrawArrays(GL_TRIANGLE_FAN, 399, 64)
        glDrawArrays(GL_TRIANGLE_FAN, 463, 64)
        glDrawArrays(GL_TRIANGLE_FAN, 527, 64)

        # second cloud
        glDrawArrays(GL_TRIANGLE_FAN, 591, 64)
        glDrawArrays(GL_TRIANGLE_FAN, 655, 64)
        glDrawArrays(GL_TRIANGLE_FAN, 719, 64)

        glfw.swap_buffers(window)

    glfw.terminate()
Ejemplo n.º 17
0
def main():
    # Initialize the library
    if not glfw.init():
        return

    # Create a windowed mode window and its OpenGL context
    window = glfw.create_window(640, 480, "Hello World", None, None)
    if not window:
        glfw.terminate()
        return

    # Make the window's context current
    glfw.make_context_current(window)

    # Capturing mouse ang keyboard events
    glfw.set_key_callback(window, key_event)
    glfw.set_mouse_button_callback(window, mouse_event)

    vertex_code = """
        attribute vec2 position;
        void main(){
            gl_Position = vec4(position,0.0,1.0);
        }
        """

    fragment_code = """
        void main(){
            gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
        }
        """

    # Request a program and shader slots from GPU
    program = glCreateProgram()
    vertex = glCreateShader(GL_VERTEX_SHADER)
    fragment = glCreateShader(GL_FRAGMENT_SHADER)

    # Set shaders source
    glShaderSource(vertex, vertex_code)
    glShaderSource(fragment, fragment_code)

    # Compile shaders
    glCompileShader(vertex)
    if not glGetShaderiv(vertex, GL_COMPILE_STATUS):
        error = glGetShaderInfoLog(vertex).decode()
        print(error)
        raise RuntimeError("Erro de compilacao do Vertex Shader")

    # Compile fragment shaders
    glCompileShader(fragment)
    if not glGetShaderiv(fragment, GL_COMPILE_STATUS):
        error = glGetShaderInfoLog(fragment).decode()
        print(error)
        raise RuntimeError("Erro de compilacao do Fragment Shader")

    # Attach shader objects to the program
    glAttachShader(program, vertex)
    glAttachShader(program, fragment)

    # Build program
    glLinkProgram(program)
    if not glGetProgramiv(program, GL_LINK_STATUS):
        print(glGetProgramInfoLog(program))
        raise RuntimeError('Linking error')

    # Make program the default program
    glUseProgram(program)

    # preparando espaço para 3 vértices usando 2 coordenadas (x,y)
    vertices = np.zeros(5, [("position", np.float32, 2)])

    points = 5
    angle = 2 * np.pi / 5
    radius = 0.5

    for i in range(5):
        x = radius * np.sin(i * angle)
        y = radius * np.cos(i * angle)
        vertices[2 * i % 5] = ([x, y])

    # preenchendo as coordenadas de cada vértice
    '''
    vertices['position'] = [
                                ( 0.0, 0.0), # vertice 0
                                (+0.5,+0.5), # vertice 1
                                (-0.5,-0.5), # vertice 2
                                (-1.0,-1.0), # vertice 3
                                (-0.7,-0.2), # vertice 4
                                (-0.5,-0.2)  # vertice 5
                            ] 
    '''

    # Request a buffer slot from GPU
    buffer = glGenBuffers(1)
    # Make this buffer the default one
    glBindBuffer(GL_ARRAY_BUFFER, buffer)

    # Upload data
    glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_DYNAMIC_DRAW)
    glBindBuffer(GL_ARRAY_BUFFER, buffer)

    # Bind the position attribute
    # --------------------------------------
    stride = vertices.strides[0]
    offset = ctypes.c_void_p(0)

    loc = glGetAttribLocation(program, "position")
    glEnableVertexAttribArray(loc)

    glVertexAttribPointer(loc, 2, GL_FLOAT, False, stride, offset)

    glfw.show_window(window)

    # Loop until the user closes the window
    while not glfw.window_should_close(window):
        # Render here, e.g. using pyOpenGL

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # limpa a cor de fundo da janela e preenche com outra no sistema RGBA
        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        glDrawArrays(GL_LINE_LOOP, 0, 5)

        # Poll for and process events
        glfw.poll_events()

    glfw.terminate()
Ejemplo n.º 18
0
def show_window(window, program):

    glfw.show_window(window)
    glfw.set_cursor_pos(window, lastX, lastY)

    glEnable(GL_DEPTH_TEST)

    rotacao_inc = 0
    moon_angle = 0.1

    while not glfw.window_should_close(window):

        glfw.poll_events()

        # clear the screen color and set to black
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glClearColor(0, 0, 0, 1)

        # check if polygonal mode is on
        if polygonal_mode == True:
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        if polygonal_mode == False:
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        # draws objects
        draw_ground(program)
        draw_house(program)
        draw_trees(program)
        draw_frog(program)
        draw_horse(program)
        draw_chair(program)
        draw_screen(program)
        draw_table(program)
        draw_cpu(program)
        draw_wind_mill_body(program)
        draw_wind_mill_circle(program, rotacao_inc)
        draw_sky(program)
        draw_street(program)
        draw_secondary_yard(program)
        draw_floor(program)
        draw_keyboard(program)

        # increases rotacao_inc to rotate the windmill circle
        rotacao_inc += 0.1

        # increases moon_angle to rotate the moon
        moon_angle += 0.3

        draw_moon(70 * np.cos(np.deg2rad(moon_angle)),
                  70, 70 * np.sin(np.deg2rad(moon_angle)), program)

        mat_view = view()
        loc_view = glGetUniformLocation(program, "view")
        glUniformMatrix4fv(loc_view, 1, GL_FALSE, mat_view)

        mat_projection = projection()
        loc_projection = glGetUniformLocation(program, "projection")
        glUniformMatrix4fv(loc_projection, 1, GL_FALSE, mat_projection)

        # updates the camera position on GPU to calcula specular reflection

        # recover viesPos variable position on GPU
        loc_view_pos = glGetUniformLocation(program, "viewPos")

        # camera position (x, y, z)
        glUniform3f(loc_view_pos, cameraPos[0], cameraPos[1], cameraPos[2])

        glfw.swap_buffers(window)

    glfw.terminate()