basically i want to get an image pixel at a specified Vec2(x, y) location which is at the center of my screen which is were my sprite is positioned.
the code executes but i get an empty pixel array … all zeros and it shouldn’t be zeroes because i have a sprite in the middle of my screen.
rt = RenderTexture::create(winSize.width , winSize.height );
rt->retain();
GLubyte buf[4];// image format is rgba8888
// tried converting the coordinates from points to pixels but it didn't work
//auto pixelPos = CC_POINT_POINTS_TO_PIXELS(center);
rt->beginWithClear(0, 0, 0, 0);
sprite->visit();
glReadPixels(center.x, center.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buf);
rt->end();
_director->getRenderer()->render();
// buf {r:0, g:0, b:0, a:0} it should contain my color values but instead nothing?
PS: using newImage() is not an option for me becuase i want only one pixel.
You have to wait until the RenderTexture is actually rendered. So if you move the glReadPixels call until the next frame you should get valid colors. I think it’s possibly you can just move it to after the “force” ->render() call, but yeah I’m pretty sure the sprite hasn’t been rendered into the frame buffer that you’re reading from.
to simplify things i made my whole screen red and i tried to get the first pixel at Vec2(0, 0) because as far as i know (0, 0) coordinate does not need any conversion.
GLubyte pixel[4];
rt->beginWithClear(0, 0, 0, 0);
spr1->visit();
rt->end();
_director->getRenderer()->render();
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
and oddly it didn’t work? same result i didn’t get my pixel … just zeros?
Ah, yeah you’re rendering into a RenderTexture frame buffer, but then calling glReadPixels on the screen frame buffer. Try setting the background of the scene to green or something (so that you can see it display green when you run the game.
If the RenderTexture is being output already because you’ve added it into the scene, then you should see red being displayed (now that you updated the clear color).
You can look at ccUtils.cpp to see how glReadPixels used for capturing the screen.
You may still need to wait for the next frame (or longer) which is what captureScreen/onScreenCaptured does in ccUtils.cpp. It looks like they use the event Director::EVENT_AFTER_DRAW and add a callback that reads the pixels when that event has been signaled.
There’s other discussions of issues. One even about a chipset (specific android devices) that may or may not support this command.
Here’s discussion about reading pixel for touch testing:
Pixel perfect touch or how to determine if a pixel of a sprite is transparent .
i tried this and it worked … i got my pixel which was at the center.
GLubyte pixel[4];
auto pixelPos = CC_POINT_POINTS_TO_PIXELS(center);
rt->beginWithClear(0, 0, 0, 0);
spr1->visit();
_director->getRenderer()->render();
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(pixelPos.x, pixelPos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
rt->end();
but another problem poped up?
now aftre i get the pixel my whole screen becomes black.
Ok i Solved the problem by using CustomCommand:
auto pixelPos = CC_POINT_POINTS_TO_PIXELS(center);
cmd = new CustomCommand();
cmd->func = [=]() {
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(pixelPos.x, pixelPos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
};
rt->beginWithClear(0, 0, 0, 0);
spr1->visit();
_director->getRenderer()->addCommand(cmd);
rt->end();
4 Likes