Exemple #1
0
def draw_scene(scene):
    global framecount
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    if scene is None:
        return
    glUseProgram(0)
    if scene.current_player_ent is None:
        transform = Transform(None)
    else:
        transform = scene.current_player_ent.transform
    cam_transform = transform.clone()
    cam_transform.translate_local(np.array([0, 30, 0]))
    perspective_mat = glm.perspective(glm.radians(90.0),
                                      screen_utils.aspect_ratio(), 0.1,
                                      100000.0)
    tmat = cam_transform._get_translation_matrix()
    rmat = cam_transform._get_rotation_matrix(
    )  # fine as long as we never pitch
    a = tmat.dot(rmat.dot(matrix.create_translation_matrix([1, 0, 0])))
    b = tmat.dot(rmat.dot(matrix.create_translation_matrix([0, 1, 0])))
    cam_vec = glm.vec3((matrix.translation_from_matrix(
        cam_transform.to_model_view_matrix()))[:3])
    point_at = glm.vec3(matrix.translation_from_matrix(a)[:3])
    up_vec = glm.normalize(
        glm.vec3(matrix.translation_from_matrix(b)[:3]) - cam_vec)
    view_mat = glm.lookAt(cam_vec, point_at, up_vec)
    model_mat = np.identity(
        4, dtype='float32')  # by default, no transformations applied
    update_all_uniform('modelViewMatrix', [1, GL_FALSE, model_mat])
    update_all_uniform('viewMatrix', [1, GL_FALSE, np.array(view_mat)])
    update_all_uniform('projectionMatrix',
                       [1, GL_FALSE, np.array(perspective_mat)])

    update_all_uniform('time', [framecount / screen_utils.MAX_FPS])  # seconds

    light_pos = [
        np.sin(framecount * 0.01) * 5,
        np.cos(framecount * 0.01) * 5,
        np.cos(framecount * 0.001)
    ]
    update_all_uniform('light_pos', light_pos)

    scene.bind_scene_vao()
    for element in scene.entities:
        if element is None:
            continue
        if element.is_renderable() and element.mesh.is_scene_mesh:
            element.mesh.render_scene_mesh(scene, element.transform)

    for element in scene.entities:
        if element is None:
            continue
        if element.is_renderable():
            if not element.mesh.is_scene_mesh:
                element.mesh.render(element.transform)
    framecount += 1
 def render_with_transform(self, transform: Transform):
     tmat = transform._get_translation_matrix()
     rmat = transform._get_rotation_matrix()
     a = tmat.dot(rmat.dot(matrix.create_translation_matrix([1, 0, 0])))
     b = tmat.dot(rmat.dot(matrix.create_translation_matrix([0, 1, 0])))
     cam_vec = glm.vec3((matrix.translation_from_matrix(
         transform.to_model_view_matrix()))[:3])
     point_at = glm.vec3(matrix.translation_from_matrix(a)[:3])
     up_vec = glm.normalize(
         glm.vec3(matrix.translation_from_matrix(b)[:3]) - cam_vec)
     view_mat = glm.lookAt(cam_vec, point_at, up_vec)
     update_all_uniform('viewMatrix', [1, GL_FALSE, np.array(view_mat)])
     self._do_render()
 def translate_local(self, trans_vec: np.ndarray):
     if not len(trans_vec) == 3:
         raise Exception("Invalid translation vector!")
     position_mat = self._get_translation_matrix().dot(
         self._get_rotation_matrix().dot(
             matrix.create_translation_matrix(trans_vec)))
     self.set_translation(matrix.translation_from_matrix(position_mat))
 def _get_translation_matrix(self):
     trans_mat = matrix.create_translation_matrix(self._translation)
     return trans_mat
def render(scene: Scene):
    global framecount, flatscreen
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glUseProgram(0)
    cam_transform = scene.active_camera.transform.clone()

    reflector = fbos.find_fbo_by_type(FBO.REFLECTION)
    reflection_render = FBORenderer(reflector, scene)
    cam_transform.set_translation(-cam_transform.get_translation())
    reflection_render.render_with_transform(cam_transform)
    cam_transform.set_translation(-cam_transform.get_translation())

    perspective_mat = glm.perspective(glm.radians(90.0),
                                      screen_utils.aspect_ratio(), 0.1,
                                      100000.0)
    tmat = cam_transform._get_translation_matrix()
    rmat = cam_transform._get_rotation_matrix(
    )  # fine as long as we never pitch
    a = tmat.dot(rmat.dot(matrix.create_translation_matrix([1, 0, 0])))
    b = tmat.dot(rmat.dot(matrix.create_translation_matrix([0, 1, 0])))
    cam_vec = glm.vec3((matrix.translation_from_matrix(
        cam_transform.to_model_view_matrix()))[:3])
    point_at = glm.vec3(matrix.translation_from_matrix(a)[:3])
    up_vec = glm.normalize(
        glm.vec3(matrix.translation_from_matrix(b)[:3]) - cam_vec)
    view_mat = glm.lookAt(cam_vec, point_at, up_vec)
    model_mat = np.identity(
        4, dtype='float32')  # by default, no transformations applied
    update_all_uniform('modelViewMatrix', [1, GL_FALSE, model_mat])
    update_all_uniform('viewMatrix', [1, GL_FALSE, np.array(view_mat)])
    update_all_uniform('projectionMatrix',
                       [1, GL_FALSE, np.array(perspective_mat)])

    time = framecount / screen_utils.MAX_FPS
    update_all_uniform('time', [time])  # seconds
    # update_all_uniform('hasPlane', [True])
    # update_all_uniform('plane', [1, 0, 0, np.sin(time)*5])

    light_pos = [
        np.sin(framecount * 0.01) * 3, 5,
        np.cos(framecount * 0.01) * 3
    ]
    update_all_uniform('light_pos', light_pos)

    # scene.render()
    # """
    # render to the behind-screen
    screen_fbo = fbos.find_fbo_by_type(FBO.FINAL_RENDER_OUTPUT)
    screen_fbo.prepare_render()
    scene.render()
    screen_fbo.return_to_screen()

    # now apply the post-processed shader
    if flatscreen is None:
        flatscreen = Mesh.create_blank_square()
        flatscreen.find_shader('post_processing')
        flatscreen.create_material()
        flatscreen.material.add_fbo_texture(screen_fbo, GL_COLOR_ATTACHMENT0)
        flatscreen.material.add_fbo_texture(reflector, GL_COLOR_ATTACHMENT0)
    flatscreen.render(Transform.zero())
    # """
    framecount += 1