Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alpha channel if RenderFlags.RGBA is set #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyrender/offscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def render(self, scene, flags=RenderFlags.NONE, seg_node_map=None):
if flags & RenderFlags.DEPTH_ONLY:
retval = depth
else:
color = self._renderer.read_color_buf()
color = self._renderer.read_color_buf(flags=flags)
retval = color, depth

# Make the platform not current
Expand Down
8 changes: 5 additions & 3 deletions pyrender/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def render_text(self, text, x, y, font_name='OpenSans-Regular',
# Draw text
font.render_string(text, x, y, scale, align)

def read_color_buf(self):
def read_color_buf(self, flags=RenderFlags.NONE):
"""Read and return the current viewport's color buffer.

Alpha cannot be computed for an on-screen buffer.
Expand All @@ -232,11 +232,13 @@ def read_color_buf(self):
width, height = self.viewport_width, self.viewport_height
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0)
glReadBuffer(GL_FRONT)
color_buf = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
fmt = GL_RGBA if flags & RenderFlags.RGBA else GL_RGB
color_buf = glReadPixels(0, 0, width, height, fmt, GL_UNSIGNED_BYTE)

# Re-format them into numpy arrays
color_im = np.frombuffer(color_buf, dtype=np.uint8)
color_im = color_im.reshape((height, width, 3))
channels = 4 if flags & RenderFlags.RGBA else 3
color_im = color_im.reshape((height, width, channels))
color_im = np.flip(color_im, axis=0)

# Resize for macos if needed
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/test_offscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import trimesh

from pyrender import (OffscreenRenderer, PerspectiveCamera, DirectionalLight,
SpotLight, Mesh, Node, Scene)
SpotLight, Mesh, Node, Scene, RenderFlags)


def test_offscreen_renderer(tmpdir):
Expand Down Expand Up @@ -83,10 +83,12 @@ def test_offscreen_renderer(tmpdir):
_ = scene.add(cam, pose=cam_pose)

r = OffscreenRenderer(viewport_width=640, viewport_height=480)
color, depth = r.render(scene)

assert color.shape == (480, 640, 3)
assert depth.shape == (480, 640)
assert np.max(depth.data) > 0.05
assert np.count_nonzero(depth.data) > (0.2 * depth.size)
for channels, flags in ((3, RenderFlags.NONE), (4, RenderFlags.RGBA)):
color, depth = r.render(scene, flags=flags)
assert color.shape == (480, 640, channels)
assert depth.shape == (480, 640)
assert np.max(depth.data) > 0.05
assert np.count_nonzero(depth.data) > (0.2 * depth.size)

r.delete()