Exemple #1
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
Exemple #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
Exemple #3
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
Exemple #4
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
    def draw(self, projection, view, width, height):
        model_matrix = self._transform.matrix

        # Set uniforms
        self._program["u_projection"] = projection
        # Note: OpenGL matrix multiplication works on column-major oriented storage (as least for pre-multiplication).
        # Also glumpy.glm is using column-major assumption for its operations.
        view_model_matrix = np.transpose(
            np.matmul(np.transpose(view), np.transpose(model_matrix)))
        self._program["u_view_model_matrix"] = view_model_matrix
        # self._program["u_view"] = view
        # self._program["u_model"] = self._model
        view_model_normal_matrix = np.transpose(
            np.linalg.inv(view_model_matrix))
        self._program["u_view_model_normal_matrix"] = view_model_normal_matrix
        self._program["u_color_scale"] = self._color_scale
        self._program["u_normal_scale"] = self._normal_scale
        self._program["u_depth_scale"] = self._depth_scale
        if self._shader_type == self.PHONG:
            self._program["u_light_position"] = self._light_position
            self._program[
                "u_light_ambient_intensity"] = 0.4 * self._light_intensity
            self._program[
                "u_light_diffuse_intensity"] = 0.4 * self._light_intensity
            self._program[
                "u_light_specular_intensity"] = 0.2 * self._light_intensity
            self._program["u_material_ambient"] = self._material
            self._program["u_material_diffuse"] = self._material
            self._program["u_material_specular"] = self._material
            self._program["u_material_shininess"] = 32

        with self._program.activate():
            # Bind index buffer and draw
            if self.use_depth_test:
                gl.glEnable(gl.GL_DEPTH_TEST)
            else:
                gl.glDisable(gl.GL_DEPTH_TEST)
            if self.use_face_culling:
                gl.glFrontFace(gl.GL_CCW)
                gl.glCullFace(gl.GL_BACK)
                gl.glEnable(gl.GL_CULL_FACE)
            else:
                gl.glDisable(gl.GL_CULL_FACE)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._gl_index_buffer)
            gl.glDrawElements(gl.GL_TRIANGLES, 3 * len(self._faces),
                              opengl_utils.get_gl_type(self._faces), None)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0)
Exemple #6
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
Exemple #7
0
    def render(self, scene, cull_face=True):
        self.fbo.activate()

        gl.glEnable(gl.GL_PROGRAM_POINT_SIZE)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glShadeModel(gl.GL_FLAT)

        if cull_face:
            gl.glEnable(gl.GL_CULL_FACE)
            gl.glCullFace(gl.GL_BACK)
        else:
            gl.glDisable(gl.GL_CULL_FACE)

        gl.glClearColor(*self.clear_color)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, self.viewport_size[0], self.viewport_size[1])

        if scene.draw_points:
            scene.program.draw(gl.GL_POINTS)
        else:
            assert scene.index_buffer is not None
            scene.program.draw(gl.GL_TRIANGLES, scene.index_buffer)

        if self.out_buffer_location == 'torch':
            frame = cpy_texture_to_tensor(self.color_buf_cuda,
                                          self.out_buf).clone()
        elif self.out_buffer_location == 'opengl':
            frame = self.out_buf
        else:
            gl.glReadPixels(0, 0, self.viewport_size[0], self.viewport_size[1],
                            gl.GL_RGB, gl.GL_FLOAT, self.out_buf)
            frame = self.out_buf.copy()

        self.fbo.deactivate()

        return frame