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

read to pixels function? #3

Open
ofZach opened this issue Jun 13, 2022 · 3 comments
Open

read to pixels function? #3

ofZach opened this issue Jun 13, 2022 · 3 comments

Comments

@ofZach
Copy link

ofZach commented Jun 13, 2022

one nice thing would be a read to pixels or read to image option -- thinking especially in the case of drawing to an FBO for saving out an image, etc...

@aferriss
Copy link
Owner

That's a good idea! Thanks for the suggestion :)

@ofZach
Copy link
Author

ofZach commented Jun 26, 2022

if useful here's a quick test -- probably needs to account for floating point or other fbo settings

readToPixels(){
		const gl = this.gl;
		const size = this.width*this.height*4;

		// maybe this could be optimized, if repeatedly read
		var data =  new Uint8Array(size);				

		gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
		gl.readPixels(0, 0, this.width, this.height, gl.RGBA,gl.UNSIGNED_BYTE, data);
		gl.bindFramebuffer(gl.FRAMEBUFFER, null);
		return data;
	}

@ofZach
Copy link
Author

ofZach commented Jun 26, 2022

and a little proof of concept for turning into an image.

var data = fbo.readToPixels();

 var data = fbo.readToPixels();
 var img = createImage(fbo.width, fbo.height);
 for (i = 0; i < fbo.width; i++){
   for (j = 0; j < fbo.height; j++){
     var pos = (j * fbo.width*4) + i * 4;
     img.set(i,j, [data[pos], data[pos+1], data[pos+2],255]);
   }
 }
 img.updatePixels();
 img.save('outputTest', 'png');

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

No branches or pull requests

2 participants