Exemplo n.º 1
0
    def render_frame(self, context):
        window = context.window
        region = context.region
        view = context.region_data

        # Calculate dimensions of the display region.
        pixel_scale = p3d.LVecBase2(1.0 / window.width, 1.0 / window.height)

        dimensions = p3d.LVecBase4(region.x, region.x + region.width,
                               region.y, region.y + region.height)
        dimensions.x *= pixel_scale.x
        dimensions.y *= pixel_scale.x
        dimensions.z *= pixel_scale.y
        dimensions.w *= pixel_scale.y

        self.view_region.set_dimensions(dimensions)

        # Save GL State
        glPushAttrib(GL_ALL_ATTRIB_BITS)
        glPushClientAttrib(~0)
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()

        try:
            # update window
            wp = p3d.WindowProperties()
            wp.set_origin(window.x, window.y)
            wp.set_size(window.width, window.height)
            self.win.request_properties(wp)
            self.engine.open_windows()

            # update camera
            proj_mat = p3d.LMatrix4()
            for i, v in enumerate(view.perspective_matrix):
                proj_mat.set_col(i, p3d.LVecBase4(*v))
            self.view_lens.set_user_mat(proj_mat)
            self.view_lens.set_view_mat(p3d.LMatrix4.ident_mat())

            #draw
            self.fix_gl_state()
            self.engine.render_frame()
        finally:
            # Restore GL State
            glMatrixMode(GL_PROJECTION)
            glPopMatrix()
            glMatrixMode(GL_MODELVIEW)
            glPopMatrix()

            glPopClientAttrib()
            glPopAttrib()
Exemplo n.º 2
0
def test_textnode_frame_actual():
    text = core.TextNode("test")
    text.text = "Test"

    l, r, b, t = 0.1, 0.2, 0.3, 0.4
    text.set_frame_actual(l, r, b, t)

    assert text.has_frame()
    assert not text.is_frame_as_margin()
    assert text.get_frame_as_set() == (l, r, b, t)

    frame_actual = text.get_frame_actual()
    frame_expect = core.LVecBase4(l, r, b, t)
    assert frame_actual == frame_expect
Exemplo n.º 3
0
def test_textnode_card_actual():
    text = core.TextNode("test")
    text.text = "Test"

    l, r, b, t = 0.1, 0.2, 0.3, 0.4
    text.set_card_actual(l, r, b, t)

    assert text.has_card()
    assert not text.is_card_as_margin()
    assert text.get_card_as_set() == (l, r, b, t)

    card_actual = text.get_card_actual()
    card_expect = core.LVecBase4(l, r, b, t)
    assert card_actual == card_expect
Exemplo n.º 4
0
def test_textnode_frame_as_margin():
    text = core.TextNode("test")
    text.text = "Test"

    l, r, b, t = 0.1, 0.2, 0.3, 0.4
    text.set_frame_as_margin(l, r, b, t)

    assert text.has_frame()
    assert text.is_frame_as_margin()
    assert text.get_frame_as_set() == (l, r, b, t)

    frame_actual = text.get_frame_actual()
    frame_expect = core.LVecBase4(text.get_left() - l,
                                  text.get_right() + r,
                                  text.get_bottom() - b,
                                  text.get_top() + t)
    assert frame_actual == frame_expect
Exemplo n.º 5
0
    def init_entity(self, filter_name, entity):
        if filter_name == 'sun':
            sun = entity[Sun]
            light = core.DirectionalLight(entity._uid.name)
            light.priority = sun.priority
            path = base.render.attach_new_node(light)
            path.set_h(sun.azimuth)
            self.lights[entity] = path

        elif filter_name == 'ambient':
            ambient = entity[AmbientLight]
            light = core.AmbientLight(entity._uid.name)
            light.color = core.LVecBase4(*ambient.color, 0) * ambient.intensity
            path = base.render.attach_new_node(light)
            self.lights[entity] = path

        base.render.set_light(path)
Exemplo n.º 6
0
from panda3d import core
import pytest

TEST_COLOR = core.LColor(1, 127 / 255.0, 0, 127 / 255.0)
TEST_COLOR_SCALE = core.LVecBase4(0.5, 0.5, 0.5, 0.5)
TEST_SCALED_COLOR = core.LColor(TEST_COLOR)
TEST_SCALED_COLOR.componentwise_mult(TEST_COLOR_SCALE)
FUZZ = 0.02


@pytest.fixture(scope='session',
                params=[False, True],
                ids=["shader:off", "shader:auto"])
def shader_attrib(request):
    """Returns two ShaderAttribs: one with auto shader, one without."""
    if request.param:
        return core.ShaderAttrib.make_default().set_shader_auto(True)
    else:
        return core.ShaderAttrib.make_off()


@pytest.fixture(
    scope='session',
    params=["mat:off", "mat:empty", "mat:amb", "mat:diff", "mat:both"])
def material_attrib(request):
    """Returns two MaterialAttribs: one with material, one without.  It
    shouldn't really matter what we set them to, since the tests in here do
    not use lighting, and therefore the material should be ignored."""

    if request.param == "mat:off":
        return core.MaterialAttrib.make_off()
Exemplo n.º 7
0
    def __init__(self, workingdir, conn_addr):
        ShowBase.__init__(self)
        self.view_lens = p3d.MatrixLens()
        self.cam = p3d.NodePath(p3d.Camera('view'))
        self.cam.node().set_lens(self.view_lens)
        self.cam.node().set_active(True)
        self.cam.reparent_to(self.render)

        self.pipe = p3d.GraphicsPipeSelection.get_global_ptr().make_module_pipe('pandagl')

        self.bg_color = p3d.LVecBase4(0.0, 0.0, 0.0, 1.0)

        p3d.get_model_path().prepend_directory(workingdir)
        self.workingdir = workingdir

        self.texture = p3d.Texture()
        self.win = None
        self.renderer = None
        self.make_offscreen(1, 1)

        self.disableMouse()
        self.setFrameRateMeter(True)

        self.image_width = 1
        self.image_height = 1
        self.image_data = struct.pack('=BBB', 0, 0, 0)

        self.scene = self.render.attach_new_node(p3d.PandaNode("Empty Scene"))

        self.connection = BlenderConnection(conn_addr)

        def set_bg_clear_color(task):
            # Keep bg color working even if DisplayRegions get switched around
            # (e.g., from FilterManager)
            for win in self.graphicsEngine.windows:
                for dispregion in win.display_regions:
                    if dispregion.get_camera() == self.cam:
                        dispregion.set_clear_color_active(True)
                        dispregion.set_clear_color(self.bg_color)
            return task.cont
        self.taskMgr.add(set_bg_clear_color, 'Set BG Clear Color')

        def do_updates(task):
            if not self.connection.running:
                sys.exit()

            latest_scene_update = None
            for update in self.connection.get_updates():
                # print('update: {}'.format(update))
                update_type = update['type']
                if update_type == 'view':
                    self.update_view(
                        update['width'],
                        update['height'],
                        self.load_matrix(update['projection_matrix']),
                        self.load_matrix(update['view_matrix']),
                    )
                elif update_type == 'scene':
                    latest_scene_update = update
                elif update_type == 'background_color':
                    self.bg_color = p3d.LVector4(*update['color'])
                else:
                    raise RuntimeError('Unknown update type: {}'.format(update_type))

            if latest_scene_update is not None:
                self.update_scene(latest_scene_update['path'])

            return task.cont
        self.taskMgr.add(do_updates, 'Updates')

        def image_updates(task):
            if self.texture.has_ram_image():
                #start = time.perf_counter()
                self.connection.send_image(
                    self.texture.get_x_size(),
                    self.texture.get_y_size(),
                    memoryview(self.texture.get_ram_image_as('BGR'))
                )
                #print('Extern: Updated image data in {}ms'.format((time.perf_counter() - start) * 1000))
            return task.cont
        self.taskMgr.add(image_updates, 'Upload Images')
Exemplo n.º 8
0
    def load_matrix(self, mat):
        lmat = p3d.LMatrix4()

        for i in range(4):
            lmat.set_row(i, p3d.LVecBase4(*mat[i * 4: i * 4 + 4]))
        return lmat
Exemplo n.º 9
0
    def __init__(self, workingdir):
        ShowBase.__init__(self)
        self.view_lens = p3d.MatrixLens()
        self.cam = p3d.NodePath(p3d.Camera('view'))
        self.cam.node().set_lens(self.view_lens)
        self.cam.node().set_active(True)
        self.cam.reparent_to(self.render)

        self.pipe = p3d.GraphicsPipeSelection.get_global_ptr(
        ).make_module_pipe('pandagl')

        self.bg_color = p3d.LVecBase4(0.0, 0.0, 0.0, 1.0)

        p3d.get_model_path().prepend_directory(workingdir)
        self.workingdir = workingdir

        self.texture = p3d.Texture()
        self.win = None
        self.rendermanager = None
        self.make_offscreen(1, 1)

        self.disableMouse()
        self.setFrameRateMeter(True)

        self.image_width = 1
        self.image_height = 1
        self.image_data = struct.pack('=BBB', 0, 0, 0)

        # Setup conversion logic
        self.converter = Converter()
        self.conversion_queue = queue.Queue()

        def conversion(task):
            while not self.conversion_queue.empty():
                data = self.conversion_queue.get()
                #print(data)
                if 'extras' in data and 'view' in data['extras']:
                    viewd = data['extras']['view']
                    if 'width' in viewd:
                        width = viewd['width']
                        height = viewd['height']
                        self.make_offscreen(width, height)
                    if 'projection_matrix' in viewd:
                        proj_mat = self.converter.load_matrix(
                            viewd['projection_matrix'])
                        self.view_lens.set_user_mat(proj_mat)
                    if 'view_matrix' in viewd:
                        view_mat = self.converter.load_matrix(
                            viewd['view_matrix'])

                        # Panda wants an OpenGL model matrix instead of an OpenGL view matrix
                        view_mat.invert_in_place()
                        self.view_lens.set_view_mat(view_mat)

                self.converter.update(data)
                bg_color = self.converter.background_color
                self.bg_color = p3d.LVector4(bg_color[0], bg_color[1],
                                             bg_color[2], 1)
                self.view_region.set_clear_color(self.bg_color)
                self.converter.active_scene.reparent_to(self.render)
                #self.render.ls()

            if self.texture.has_ram_image():
                #start = time.perf_counter()
                self.server.image_lock.acquire()
                self.image_width = self.texture.get_x_size()
                self.image_height = self.texture.get_y_size()
                self.image_data = memoryview(
                    self.texture.get_ram_image_as("BGR"))
                self.server.image_lock.release()
                #print('Extern: Updated image data in {}ms'.format((time.perf_counter() - start) * 1000))
                #self.texture.write('tex.png')
            return task.cont

        self.taskMgr.add(conversion, 'Conversion')

        # Setup communication with Blender
        self.server = Server(self.handle_data, self.get_img)
        if USE_THREAD:
            self.server.start()

            def server_mon(task):
                if not self.server.is_alive():
                    print('Server thread has terminated, closing program')
                    sys.exit()
                return task.cont

            self.taskMgr.add(server_mon, 'Server Monitor')
        else:

            def server_task(task):
                self.server.run()
                return task.cont

            self.taskMgr.add(server_task, 'Server Communication')