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

readPixels() with format RGB outputs zero #265

Open
UlyssesZh opened this issue Jul 28, 2023 · 4 comments
Open

readPixels() with format RGB outputs zero #265

UlyssesZh opened this issue Jul 28, 2023 · 4 comments
Labels

Comments

@UlyssesZh
Copy link

const width = 4;
const height = 4;
const gl = require('gl')(width, height, {preserveDrawingBuffer: true});
gl.clearColor(1, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);

const pixelsRgba = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixelsRgba);
console.log(pixelsRgba);

const pixelsRgb = new Uint8Array(width * height * 3);
gl.readPixels(0, 0, width, height, gl.RGB, gl.UNSIGNED_BYTE, pixelsRgb);
console.log(pixelsRgb);

Output:

Uint8Array(64) [
  255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
  255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
  255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
  255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
  255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
  255, 0, 0, 255
]
Uint8Array(48) [
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0
]

The RGBA output is correct, but the RGB output is incorrect.

@UlyssesZh UlyssesZh changed the title readPixels() with format RGBA outputs zero readPixels() with format RGB outputs zero Jul 28, 2023
@dhritzkiv dhritzkiv added the bug label Mar 2, 2024
@maximilianmachnik
Copy link

Im facing the same issue. Any updates on this?

@trevorparscal
Copy link

I also bumped into this. Looking briefly at the code, 4-channel color seems to be baked-into the readPixels function. I don't know the code very well, but I might take a crack at it if no one more familiar with the code can. I'm piping data between GL and a video encoder, so 25% overhead is a significant issue.

@UlyssesZh
Copy link
Author

Please react this issue with a thumb up emoji to make the contributors more aware of this issue if this issue is important to you.

I opened the issue long time ago, and it is still relevant to me. For now, I have to use RGBA output and convert it to RGB format in JS before piping it to video encoder.

@ankushlohiya001
Copy link

Because of this, doesn't modify the array,
and is marked as invalid operation.

https://github.com/stackgl/headless-gl/blob/254da5180eb123dd410313c66d581a98bb4266a0/src/javascript/webgl-rendering-context.js#L2890C1-L2896C8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants