Esempio n. 1
0
    def __enter__(self):
        # Save viewport for final rendering
        self._viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)

        # Prepare framebuffer for "original" rendering
        gl.glViewport(0, 0, self.width, self.height)
        self._framebuffers[0].activate()
Esempio n. 2
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj):

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0] # Depth is saved in the first channel

    fbo.deactivate()

    return depth
Esempio n. 3
0
    def render_texture(self, texture):
        self.before_render(texture)

        #print("Rendering %s to texture" % repr(self))
        fbo_size = None
        if self._force_size is not None:
            fbo_size = self._force_size
        elif texture is not None:
            h, w, _ = texture.shape
            fbo_size = w, h
        elif self.preferred_size is not None:
            fbo_size = self.preferred_size
        else:
            assert False, "A preferred size must be set if there is no input texture / no size forced"

        fbo = self.fbo(fbo_size)
        fbo.activate()

        gl.glViewport(0, 0, fbo_size[0], fbo_size[1])
        gl.glClearColor(0.0, 0.0, 0.0, 0.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        self._transform_flipy = True
        self.render(texture, fbo_size)
        fbo.deactivate()
        self.after_render()

        return fbo.color[0]
Esempio n. 4
0
    def __exit__(self, type, value, traceback):
        # Done with "original" rendering
        self._framebuffers[0].deactivate()

        # Actual filtering starts here
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        gl.glDisable(gl.GL_DEPTH_TEST)

        # Apply all filters using ping-pong framebuffers
        index = 0
        for i in range(len(self._programs)-1):
            program = self._programs[i]
            if i == 0: # special case for first rendering
                program['filtered'] = self._framebuffers[0].color[0]
            else:
                program['filtered'] = self._framebuffers[1+index].color[0]
            index = (index + 1) % 2 # ping-pong
            self._framebuffers[index+1].activate()
            self._programs[i].draw(gl.GL_TRIANGLE_STRIP)
            self._framebuffers[index+1].deactivate()

        # Final rendering (no transformation) at original viewport size
        program = self._programs[-1]
        program['filtered'] = self._framebuffers[index+1].color[0]
        gl.glViewport( *self._viewport )
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        gl.glDisable(gl.GL_DEPTH_TEST)
        program.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 5
0
    def on_draw(dt):
        # window.clear()
        global rgb
        extent_shape = (int(shape[0] * ssaa), int(shape[1] * ssaa))
        # Frame buffer object
        color_buf = np.zeros((extent_shape[0], extent_shape[1], 4),
                             np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((extent_shape[0], extent_shape[1]),
                             np.float32).view(gloo.DepthTexture)
        fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
        fbo.activate()

        # OpenGL setup
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, extent_shape[1], extent_shape[0])

        gl.glDisable(gl.GL_CULL_FACE)
        program.draw(gl.GL_TRIANGLES, I)

        rgb = np.zeros((extent_shape[0], extent_shape[1], 4), dtype=np.float32)
        gl.glReadPixels(0, 0, extent_shape[1], extent_shape[0], gl.GL_RGBA,
                        gl.GL_FLOAT, rgb)
        rgb.shape = extent_shape[0], extent_shape[1], 4
        rgb = rgb[::-1, :]
        rgb = np.round(rgb[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255]

        import cv2
        rgb = cv2.resize(rgb, shape, interpolation=cv2.INTER_AREA)

        fbo.deactivate()
Esempio n. 6
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj):

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0] # Depth is saved in the first channel

    fbo.deactivate()

    return depth
Esempio n. 7
0
 def activate(self, clear=False):
     gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
     gl.glViewport(0, 0, self.width, self.height)
     if clear:
         # self._win.clear()
         gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
     gl.glEnable(gl.GL_DEPTH_TEST)
     yield self
Esempio n. 8
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    C['projection'] = glm.ortho(0, width, 0, height, -1, +1)
    i = 0
    for x in [0,width//2, width]:
        for y in [0,height//2, height]:
            C[i]['translate'] = x,y
            i += 1
Esempio n. 9
0
    def render_screen(self, texture, screen_size):
        self.before_render(texture)

        #print("Rendering %s to screen" % repr(self))
        gl.glViewport(0, 0, screen_size[0], screen_size[1])

        self._transform_flipy = False
        self.render(texture, screen_size)
        self.after_render()
Esempio n. 10
0
def draw_color(shape, vertex_buffers, index_buffers, mat_models, mat_views,
               mat_projs, ambient_weight, light_color, bg_color):
    assert (len(vertex_buffers) == len(index_buffers))
    assert (len(vertex_buffers) == len(mat_models))
    assert (len(vertex_buffers) == len(mat_views))
    assert (len(vertex_buffers) == len(mat_projs))

    program = gloo.Program(_color_vertex_code, _color_fragment_code)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4),
                         np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]),
                         np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK)  # Back-facing polygons will be culled
    gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    for i in xrange(len(vertex_buffers)):
        vertex_buffer = vertex_buffers[i]
        index_buffer = index_buffers[i]
        mat_model = mat_models[i]
        mat_view = mat_views[i]
        mat_proj = mat_projs[i]

        program.bind(vertex_buffer)
        program['u_light_eye_pos'] = [0, 0, 0]
        program['light_color'] = [
            light_color[0], light_color[1], light_color[2]
        ]
        program['u_light_ambient_w'] = ambient_weight
        program['u_mv'] = _compute_model_view(mat_model, mat_view)
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view,
                                                    mat_proj)
        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8)  # Convert to [0, 255]

    fbo.deactivate()

    return rgb
Esempio n. 11
0
 def initgl(self):
     if not self.pycuda_initialized:
         self.setup_gl(self.width, self.height)
         self.pycuda_initialized = True
     """Initalize gl states when the frame is created"""
     gl.glViewport(0, 0, self.width, self.height)
     gl.glClearColor(0.0, 0.0, 0.0, 0.0)
     self.dt_history = [1000 / 60]
     self.t0 = time.time()
     self.t_last = self.t0
     self.nframes = 0
Esempio n. 12
0
 def activate(self):
     """prepare to draw:
     increment self.head, invalidate the cpu rep and activate the render target
     """
     gl.glViewport(0, 0, *self.size)
     if self.n:
         # gl.glPushAttrib(gl.GL_VIEWPORT_BIT)
         self.head = (self.head + 1) % self.n
         self._state[self.head].activate()
         if not self.autoread:
             self.cpu_state = None
Esempio n. 13
0
def on_draw(dt):
    simulation.on_draw(dt)
    GUI.on_draw(dt)

    render["texture"][:, gui_height:gui_height+cheight, 0] = simulation.cells
    render["texture"][:, :gui_height, 0] = GUI.pixels

    # gl.glDisable(gl.GL_BLEND)
    # gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glViewport(0, 0, window.width, window.height)
    render.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 14
0
def draw_label(shape, vertex_buffers, index_buffers, mat_models, mat_views,
               mat_projs, labels):
    assert (len(vertex_buffers) == len(index_buffers))
    assert (len(vertex_buffers) == len(mat_models))
    assert (len(vertex_buffers) == len(mat_views))
    assert (len(vertex_buffers) == len(mat_projs))
    assert (labels is not None)
    assert (len(vertex_buffers) == len(labels))

    program = gloo.Program(_label_vertex_code, _label_fragment_code)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4),
                         np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]),
                         np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK)  # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    for i in range(len(vertex_buffers)):
        vertex_buffer = vertex_buffers[i]
        index_buffer = index_buffers[i]
        mat_model = mat_models[i]
        mat_view = mat_views[i]
        mat_proj = mat_projs[i]
        label = labels[i]
        program.bind(vertex_buffer)
        program['u_mv'] = _compute_model_view(mat_model, mat_view)
        # program['u_nm'] = compute_normal_matrix(model, view)
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view,
                                                    mat_proj)
        program['label'] = label
        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    label_map = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT,
                    label_map)
    label_map.shape = shape[0], shape[1], 4
    label_map = label_map[::-1, :]
    label_map = label_map[:, :, 0]
    fbo.deactivate()

    return label_map
Esempio n. 15
0
 def activate(self, clear=False):
     self._framebuffer.activate()
     gl.glViewport(0, 0, self.width, self.height)
     if clear:
         # color_attachment_flags = []
         # for i in range(len(self._color_attachments)):
         #     color_attachment_flags.append(gl.GL_COLOR_ATTACHMENT0 + i)
         # gl.glDrawBuffers(np.array(color_attachment_flags, dtype=np.uint32))
         gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
     gl.glEnable(gl.GL_DEPTH_TEST)
     yield self
     self._framebuffer.deactivate()
Esempio n. 16
0
    def render(self, size, render_func):
        fbo = self.get_fbo(size)
        fbo.activate()

        gl.glViewport(0, 0, size[0], size[1])
        gl.glClearColor(0.0, 0.0, 0.0, 0.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        render_func()

        fbo.deactivate()
        return fbo.color[0]
Esempio n. 17
0
def on_resize(w, h):
    if w > h:
        x = (w - h) / 2
        y = 0
        w = h
    else:
        x = 0
        y = (h - w) / 2
        h = w
    gl.glViewport(int(x), int(y), int(w), int(h))
    window.dispatch_event('on_draw', 0.0)
    window.swap()
    return True
Esempio n. 18
0
def on_resize(width, height):
    if width > height:
        x = (width - height) / 2
        y = 0
        w = h = height
    else:
        x = 0
        y = (height - width) / 2
        w = h = width
    gl.glViewport(int(x), int(y), int(w), int(h))
    window.dispatch_event('on_draw', 0.0)
    window.swap()
    return True
Esempio n. 19
0
 def draw(self):
     # consume all segments present at start of drawing
     with self.target:
         gl.glViewport(0, 0, *self.target.size)
         gl.glClear(gl.GL_COLOR_BUFFER_BIT)
         n = self.segments.qsize()
         for _ in range(n):
             p, c, s = self.segments.get()
             self.data['a_position'][:] = p
             self.data['a_color'][:] = c
             self.data['a_size'][:] = s
             # gl.glClearColor(0,0,0,0.001)
             # gl.glEnable(gl.GL_BLEND)
             # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
             self.program.draw(gl.GL_POINTS)
Esempio n. 20
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view,
               mat_proj):

    assert type(mat_view) is list, 'Requires list of arrays.'

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4),
                         np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]),
                         np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)

    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    for i in range(len(mat_view)):

        program['u_mv'] = _compute_model_view(mat_model, mat_view[i])
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i],
                                                    mat_proj)

        # Keep the back-face culling disabled because of objects which do not have
        # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser)
        gl.glDisable(gl.GL_CULL_FACE)
        # gl.glEnable(gl.GL_CULL_FACE)
        # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled

        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0]  # Depth is saved in the first channel

    fbo.deactivate()

    return depth
Esempio n. 21
0
def on_draw(dt):

    gl.glViewport(0, 0, GridWidth, GridHeight)
    gl.glDisable(gl.GL_BLEND)

    Advect(Velocity.Ping, Velocity.Ping, Obstacles, Velocity.Pong,
           VelocityDissipation)
    Velocity.swap()

    Advect(Velocity.Ping, Temperature.Ping, Obstacles, Temperature.Pong,
           TemperatureDissipation)
    Temperature.swap()

    Advect(Velocity.Ping, Density.Ping, Obstacles, Density.Pong,
           DensityDissipation)
    Density.swap()

    ApplyBuoyancy(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong)
    Velocity.swap()

    ApplyImpulse(Temperature.Ping, ImpulsePosition, ImpulseTemperature)
    ApplyImpulse(Density.Ping, ImpulsePosition, ImpulseDensity)
    ComputeDivergence(Velocity.Ping, Obstacles, Divergence)
    ClearSurface(Pressure.Ping, 0.0)

    for i in range(NumJacobiIterations):
        Jacobi(Pressure.Ping, Divergence, Obstacles, Pressure.Pong)
        Pressure.swap()

    SubtractGradient(Velocity.Ping, Pressure.Ping, Obstacles, Velocity.Pong)
    Velocity.swap()

    gl.glViewport(0, 0, window.width, window.height)
    gl.glClearColor(0, 0, 0, 1)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

    prog_visualize['u_data'] = Density.Ping.texture
    prog_visualize['u_shape'] = Density.Ping.texture.shape[
        1], Density.Ping.texture.shape[0]
    prog_visualize['u_kernel'] = data.get("spatial-filters.npy")
    prog_visualize["Sampler"] = Density.Ping.texture
    prog_visualize["FillColor"] = 0.95, 0.925, 1.00
    prog_visualize["Scale"] = 1.0 / window.width, 1.0 / window.height
    prog_visualize.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 22
0
def on_draw(dt):
    global pingpong

    pingpong = 1 - pingpong
    compute["pingpong"] = pingpong
    render["pingpong"] = pingpong

    gl.glDisable(gl.GL_BLEND)

    framebuffer.activate()
    gl.glViewport(0, 0, cwidth, cheight)
    compute.draw(gl.GL_TRIANGLE_STRIP)
    framebuffer.deactivate()

    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glViewport(0, 0, window.width, window.height)
    render.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 23
0
def on_draw(dt):
    global pingpong

    pingpong = 1 - pingpong
    compute["pingpong"] = pingpong
    render["pingpong"] = pingpong

    gl.glDisable(gl.GL_BLEND)

    framebuffer.activate()
    gl.glViewport(0, 0, cwidth, cheight)
    compute.draw(gl.GL_TRIANGLE_STRIP)
    framebuffer.deactivate()

    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glViewport(0, 0, window.width, window.height)
    render.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 24
0
def draw_label(shape, vertex_buffer, index_buffer, texture, mat_model,
               mat_view, mat_proj, ambient_weight, bg_color, shading,
               inst_ids):

    assert type(mat_view) is list, 'Requires list of arrays.'

    program = gloo.Program(_label_vertex_code, _label_fragment_code)
    program.bind(vertex_buffer)

    # FBO
    color_buf = np.zeros((shape[0], shape[1], 4),
                         np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]),
                         np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)

    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    print('inst ids = ' + str(inst_ids))
    for i in range(len(mat_view)):
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i],
                                                    mat_proj)
        program['inst_id'] = inst_ids[
            i] / 255.  # input instance-id is mapped to range [0,1]

        gl.glDisable(gl.GL_CULL_FACE)

        program.draw(gl.GL_TRIANGLES, index_buffer)

    label = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, label)
    label.shape = shape[0], shape[1], 4
    label = label[::-1, :]
    label = np.round(label[:, :, 0] * 255).astype(
        np.uint8)  # Label is saved in the first channel

    fbo.deactivate()

    return label
Esempio n. 25
0
def CreateObstacles(dest, width, height):
    dest.activate()
    gl.glViewport(0, 0, width, height)
    gl.glClearColor(0, 0, 0, 0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

    T = np.ones((height, width, 3), np.float32).view(gloo.Texture2D)

    T[+1:-1, +1:-1] = 0.0
    T[..., 0] += disc(shape=(GridHeight, GridWidth),
                      center=(GridHeight / 2, GridWidth / 2),
                      radius=32)
    T[..., 2] += -2 * disc(shape=(GridHeight, GridWidth),
                           center=(GridHeight / 2, GridWidth / 2),
                           radius=32)
    prog_fill["Sampler"] = T
    prog_fill.draw(gl.GL_TRIANGLE_STRIP)
    dest.deactivate()
Esempio n. 26
0
def CreateObstacles(dest, width, height):
    dest.activate()
    gl.glViewport(0, 0, width, height)
    gl.glClearColor(0, 0, 0, 0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

    T = np.ones((height,width,3), np.float32).view(gloo.Texture2D)

    T[+1:-1,+1:-1] = 0.0
    T[...,0] += disc(shape = (GridHeight,GridWidth),
                     center = (GridHeight/2,GridWidth/2),
                     radius = 32)
    T[...,2] += -2*disc(shape = (GridHeight,GridWidth),
                        center = (GridHeight/2,GridWidth/2),
                        radius = 32)
    prog_fill["Sampler"] = T
    prog_fill.draw(gl.GL_TRIANGLE_STRIP)
    dest.deactivate()
Esempio n. 27
0
    def __init__(self,
                 model,
                 im_size,
                 texture=None,
                 bg_color=(0.0, 0.0, 0.0, 0.0),
                 ambient_weight=0.5,
                 shading='flat',
                 mode='rgb+depth'):

        # Set texture / color of vertices
        texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32)
        colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5

        # Set the vertex data
        vertices_type = [('a_position', np.float32, 3),
                         ('a_normal', np.float32, 3),
                         ('a_color', np.float32, colors.shape[1]),
                         ('a_texcoord', np.float32, 2)]
        vertices = np.array(
            list(zip(model['pts'], model['normals'], colors, texture_uv)),
            vertices_type)

        # Create buffers
        self.vertex_buffer = vertices.view(gloo.VertexBuffer)
        self.index_buffer = model['faces'].flatten().astype(np.uint32).view(
            gloo.IndexBuffer)
        self.window = app.Window(visible=False)

        self.program = gloo.Program(_normal_vertex_code, _normal_fragment_code)
        self.program.bind(self.vertex_buffer)

        scale = max(im_size)
        shape = (scale, scale)
        color_buf = np.zeros((shape[0], shape[1], 4),
                             np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((shape[0], shape[1]),
                             np.float32).view(gloo.DepthTexture)
        fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
        fbo.activate()
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, shape[1], shape[0])
        gl.glDisable(gl.GL_CULL_FACE)
Esempio n. 28
0
def on_draw(dt):

    framebuffer.activate()
    gl.glViewport(0, 0, w, h)
    window.clear()
    scene.draw(gl.GL_TRIANGLE_STRIP)
    framebuffer.deactivate()

    gl.glViewport(0, 0, zoom * w, zoom * h)
    window.clear()
    output.draw(gl.GL_TRIANGLE_STRIP)

    image = np.zeros((window.height, window.width * 3), dtype=np.uint8)
    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, image)
    image[...] = image[::-1, :]
    # filename = "triangle-sdf-filled.png"
    filename = "triangle-sdf-outlined.png"
    png.from_array(image, 'RGB').save(filename)
Esempio n. 29
0
def on_draw(dt):

    gl.glViewport(0, 0, GridWidth, GridHeight)
    gl.glDisable(gl.GL_BLEND)

    Advect(Velocity.Ping, Velocity.Ping, Obstacles, Velocity.Pong, VelocityDissipation)
    Velocity.swap()

    Advect(Velocity.Ping, Temperature.Ping, Obstacles, Temperature.Pong, TemperatureDissipation)
    Temperature.swap()

    Advect(Velocity.Ping, Density.Ping, Obstacles, Density.Pong, DensityDissipation)
    Density.swap()

    ApplyBuoyancy(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong)
    Velocity.swap()

    ApplyImpulse(Temperature.Ping, ImpulsePosition, ImpulseTemperature)
    ApplyImpulse(Density.Ping, ImpulsePosition, ImpulseDensity)
    ComputeDivergence(Velocity.Ping, Obstacles, Divergence)
    ClearSurface(Pressure.Ping, 0.0)

    for i in range(NumJacobiIterations):
        Jacobi(Pressure.Ping, Divergence, Obstacles, Pressure.Pong)
        Pressure.swap()

    SubtractGradient(Velocity.Ping, Pressure.Ping, Obstacles, Velocity.Pong)
    Velocity.swap()

    gl.glViewport(0,0,window.width,window.height)
    gl.glClearColor(0, 0, 0, 1)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

    prog_visualize['u_data']   = Density.Ping.texture
    prog_visualize['u_shape']  = Density.Ping.texture.shape[1], Density.Ping.texture.shape[0]
    prog_visualize['u_kernel'] = data.get("spatial-filters.npy")
    prog_visualize["Sampler"] = Density.Ping.texture
    prog_visualize["FillColor"] = 0.95, 0.925, 1.00
    prog_visualize["Scale"] =  1.0/window.width, 1.0/window.height
    prog_visualize.draw(gl.GL_TRIANGLE_STRIP)
Esempio n. 30
0
    def _draw_rgb(self, obj_id, mat_model, mat_view, mat_proj):
        """Renders an RGB image.

    :param obj_id: ID of the object model to render.
    :param mat_model: 4x4 ndarray with the model matrix.
    :param mat_view: 4x4 ndarray with the view matrix.
    :param mat_proj: 4x4 ndarray with the projection matrix.
    :return: HxWx3 ndarray with the rendered RGB image.
    """
        # Update the OpenGL program.
        program = self.rgb_programs[obj_id]
        program['u_light_eye_pos'] = [0, 0, 0]  # Camera origin.
        program['u_light_ambient_w'] = self.light_ambient_weight
        program['u_mv'] = _calc_model_view(mat_model, mat_view)
        program['u_nm'] = _calc_normal_matrix(mat_model, mat_view)
        program['u_mvp'] = _calc_model_view_proj(mat_model, mat_view, mat_proj)

        # OpenGL setup.
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClearColor(self.bg_color[0], self.bg_color[1], self.bg_color[2],
                        self.bg_color[3])
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, self.width, self.height)

        # Keep the back-face culling disabled because of objects which do not have
        # well-defined surface (e.g. the lamp from the lm dataset).
        gl.glDisable(gl.GL_CULL_FACE)

        # Rendering.
        program.draw(gl.GL_TRIANGLES, self.index_buffers[obj_id])

        # Get the content of the FBO texture.
        rgb = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        rgb)
        rgb.shape = (self.height, self.width, 4)
        rgb = rgb[::-1, :]
        rgb = np.round(rgb[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255].

        return rgb
Esempio n. 31
0
    def draw_depth(self, shape, vertex_buffer, index_buffer, texture,
                   mat_model, mat_view, mat_proj, ambient_weight, bg_color,
                   shading):
        self.program['u_mv'] = _compute_model_view(mat_model, mat_view)
        self.program['u_mvp'] = _compute_model_view_proj(
            mat_model, mat_view, mat_proj)

        gl.glViewport(0, 0, shape[1], shape[0])

        # Rendering
        self.program.draw(gl.GL_TRIANGLES, index_buffer)

        # Retrieve the contents of the FBO texture
        depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
        gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT,
                        depth)
        depth.shape = shape[0], shape[1], 4
        depth = depth[::-1, :]
        depth = depth[:, :, 0]  # Depth is saved in the first channel

        return depth
Esempio n. 32
0
def on_draw(dt):

    surface_ref.set_array(Density.ping_array)
    kernel_function(np.int32(400), np.int32(400), block=(16,16,1), grid=((400+1)//16+1,(400+1)//16+1))

    gl.glViewport(0,0,window.width,window.height)
    gl.glClearColor(0, 0, 0, 1)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

    global t
    t += dt
    prog_visualize['u_data']   = Density.Ping.texture
    prog_visualize['t'] = t
    prog_visualize['u_shape']  = Density.Ping.texture.shape[1], Density.Ping.texture.shape[0]
    prog_visualize['u_kernel'] = data.get("spatial-filters.npy")
    prog_visualize["Sampler"] = Density.Ping.texture
    prog_visualize["FillColor"] = 0.95, 0.925, 1.00
    prog_visualize["Scale"] =  1.0/window.width, 1.0/window.height
    prog_visualize.draw(gl.GL_TRIANGLE_STRIP)
    Density.swap()
Esempio n. 33
0
File: viewer.py Progetto: hesom/npbg
    def on_draw(self, dt):
        self.last_view_matrix = self.get_next_view_matrix(
            self.n_frame, self.t_elapsed)

        self.last_frame = self.render_frame(self.last_view_matrix)

        if self.out_buffer_location == 'torch':
            cpy_tensor_to_texture(self.last_frame, self.screen_tex_cuda)

        self.window.clear()

        gl.glDisable(gl.GL_CULL_FACE)

        # ensure viewport size is correct (offline renderer could change it)
        gl.glViewport(0, 0, self.viewport_size[0], self.viewport_size[1])

        self.screen_program.draw(gl.GL_TRIANGLE_STRIP)

        self.n_frame += 1
        self.t_elapsed += dt

        if self.args.nearest_train:
            ni = nearest_train(
                self.scene_data['view_matrix'],
                np.linalg.inv(self.scene_data['model3d_origin'])
                @ self.last_view_matrix)
            label = self.scene_data['camera_labels'][ni]
            assert self.args.gt, 'you must define path to gt images'
            path = self.args.gt.replace('*', str(label))
            if not os.path.exists(path):
                print(f'{path} NOT FOUND!')
            elif self.last_gt_image != path:
                self.last_gt_image = path
                img = cv2.imread(path)
                max_side = max(img.shape[:2])
                s = 1024 / max_side
                img = cv2.resize(img, None, None, s, s)
                cv2.imshow('nearest train', img)
            cv2.waitKey(1)
Esempio n. 34
0
def on_draw(dt):
    global framebuffer, offset_index

    offset_name = list(offsets.keys())[offset_index]
    offset = offsets[offset_name]

    gl.glViewport(0, 0, width, height)
    framebuffer_2.activate()
    window.clear()
    framebuffer_2.deactivate()

    for dx, dy in offset:
        framebuffer_1.activate()
        window.clear()
        scene["offset"] = (2 * dx - 1) / width, (2 * dy - 1) / height
        scene.draw(gl.GL_TRIANGLE_STRIP)
        # scene.draw(gl.GL_LINE_LOOP)
        framebuffer_1.deactivate()

        framebuffer_2.activate()
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE)
        gl.glBlendColor(0, 0, 0, 1 / len(offset))
        ssaa.draw(gl.GL_TRIANGLE_STRIP)
        gl.glDisable(gl.GL_BLEND)
        framebuffer_2.deactivate()

    gl.glViewport(0, 0, window.width, window.height)
    window.clear()
    final.draw(gl.GL_TRIANGLE_STRIP)

    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, framebuffer)
    framebuffer[...] = framebuffer[::-1, :]
    # filename = "triangle-ssaa-outlined-%s.png" % offset_name
    filename = "triangle-ssaa-filled-%s.png" % offset_name
    png.from_array(framebuffer, 'RGB').save(filename)
    offset_index += 1
Esempio n. 35
0
def draw_color(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj,
               ambient_weight, bg_color):

    program = gloo.Program(_color_vertex_code, _color_fragment_code)
    program.bind(vertex_buffer)
    program['u_light_eye_pos'] = [0, 0, 0]
    program['u_light_ambient_w'] = ambient_weight
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    # program['u_nm'] = compute_normal_matrix(model, view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255]

    fbo.deactivate()

    return rgb
Esempio n. 36
0
    def _draw_depth(self, obj_id, mat_model, mat_view, mat_proj):
        """Renders a depth image.

    :param obj_id: ID of the object model to render.
    :param mat_model: 4x4 ndarray with the model matrix.
    :param mat_view: 4x4 ndarray with the view matrix.
    :param mat_proj: 4x4 ndarray with the projection matrix.
    :return: HxW ndarray with the rendered depth image.
    """
        # Update the OpenGL program.
        program = self.depth_programs[obj_id]
        program['u_mv'] = _calc_model_view(mat_model, mat_view)
        program['u_mvp'] = _calc_model_view_proj(mat_model, mat_view, mat_proj)

        # OpenGL setup.
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClearColor(0.0, 0.0, 0.0, 0.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, self.width, self.height)

        # Keep the back-face culling disabled because of objects which do not have
        # well-defined surface (e.g. the lamp from the lm dataset).
        gl.glDisable(gl.GL_CULL_FACE)

        # Rendering.
        program.draw(gl.GL_TRIANGLES, self.index_buffers[obj_id])

        # Get the content of the FBO texture.
        depth = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        depth)
        depth.shape = (self.height, self.width, 4)
        depth = depth[::-1, :]
        depth = depth[:, :, 0]  # Depth is saved in the first channel

        return depth
Esempio n. 37
0
def draw_color(shape, vertex_buffer, index_buffer, texture, mat_model, mat_view,
               mat_proj, ambient_weight, bg_color, shading):

    # Set shader for the selected shading
    if shading == 'flat':
        color_fragment_code = _color_fragment_flat_code
    else: # 'phong'
        color_fragment_code = _color_fragment_phong_code

    program = gloo.Program(_color_vertex_code, color_fragment_code)
    program.bind(vertex_buffer)
    program['u_light_eye_pos'] = [0, 0, 0] # Camera origin
    program['u_light_ambient_w'] = ambient_weight
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_nm'] = _compute_normal_matrix(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)
    if texture is not None:
        program['u_use_texture'] = int(True)
        program['u_texture'] = texture
    else:
        program['u_use_texture'] = int(False)
        program['u_texture'] = np.zeros((1, 1, 4), np.float32)

    # Frame buffer object
    color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D)
    depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture)
    fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
    fbo.activate()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # gl.glEnable(gl.GL_BLEND)
    # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    # gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)
    # gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST)
    # gl.glDisable(gl.GL_LINE_SMOOTH)
    # gl.glDisable(gl.GL_POLYGON_SMOOTH)
    # gl.glEnable(gl.GL_MULTISAMPLE)

    # Keep the back-face culling disabled because of objects which do not have
    # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser)
    gl.glDisable(gl.GL_CULL_FACE)
    # gl.glEnable(gl.GL_CULL_FACE)
    # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255]

    fbo.deactivate()

    return rgb
Esempio n. 38
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    cube['projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0)
    pixelate.viewport = 0, 0, width, height
Esempio n. 39
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    projection = glm.ortho(0, width, 0, height, -1, +1)
    program['u_projection'] = projection
Esempio n. 40
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    update_grid(width, height)
Esempio n. 41
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
Esempio n. 42
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    C['projection'] = glm.ortho(0, width, 0, height, -1, +1)
    C['translate'] = width//2,height//2
Esempio n. 43
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    projection = glm.perspective(45.0, width / float(height), 1.0, 1000.0)
    program["u_projection"] = projection
Esempio n. 44
0
def on_resize(width, height):
    gl.glViewport(0, 0, width, height)
    program["u_size"] = width, height
Esempio n. 45
0
 def on_resize(self, width, height):
     """" Default resize handler that set viewport """
     gl.glViewport(0, 0, width, height)
     self.dispatch_event('on_draw', 0.0)
     self.swap()