Hello
I’m trying to implement a kind of CCTV window, showing 3d scene.
For the test, added code bellow in
\cocos2d-x-3.17.2\tests\cpp-tests\Classes\Camera3DTest
Camera3DTest.cpp Camera3DTestDemo::onEnter() - line 351
if (_camera == nullptr)
{
_camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000);
_camera->setCameraFlag(CameraFlag::USER1);
_layer3D->addChild(_camera);
// added code-----------------------------------------------------------------------------
Size fboSize = Size(512,384);
auto fbo = experimental::FrameBuffer::create(1, fboSize.width, fboSize.height);
auto rt = experimental::RenderTarget::create(fboSize.width, fboSize.height);
auto rtDS = experimental::RenderTargetDepthStencil::create(fboSize.width, fboSize.height);
fbo->attachRenderTarget(rt);
fbo->attachDepthStencilTarget(rtDS); // commenting this line will render 3d scene without depth test
fbo->setClearColor(Color4F(0.25,0.25,0.25,1));
_camera->setFrameBufferObject(fbo);
auto sprite_scr = Sprite::createWithTexture(fbo->getRenderTarget()->getTexture());
sprite_scr->setFlippedY(true);
sprite_scr->setPosition(Vec2(s.width/2, s.height/2));
addChild(sprite_scr,-2);
// ----------------------------------------------------------------------------------------
}
It just renders grey window with this code.
but, without depthbuffer by commenting this line
fbo->attachDepthStencilTarget(rtDS);
renders 3d scene without depth test.
It seems like depth buffer is not working.
or Am I missing some setup code?
Do share the part of your code that you actually call the function to clear depth in each new frame.
Eg, if i’m using RenderTexture i’d call this function. I’m using v4 so haven’t really looked into the equivalent of that in Framebuffer (doesn’t exist in v4).
I simply added code in cocos2d-x-3.17.2 / cpp-tests project.
Actual code is from Camera3DTest.cpp / CameraFrameBufferTest
debugging info shows clear depth at FrameBuffer::clearFBO() every frame,
but it seems not clearing properly.
{
std::string filename = "Sprite3DTest/girl.c3b";
auto sprite = Sprite3D::create(filename);
sprite->setScale(1.0);
auto animation = Animation3D::create(filename);
if (animation)
{
auto animate = Animate3D::create(animation);
sprite->runAction(RepeatForever::create(animate));
}
sprite->setPosition(Vec2(100,100));
addChild(sprite);
}
It renders Sprite3D normaly in 2d position with default camera.
and changed Sprite3D camera mask to camera with fbo
{
std::string filename = "Sprite3DTest/girl.c3b";
auto sprite = Sprite3D::create(filename);
sprite->setScale(1.0);
auto animation = Animation3D::create(filename);
if (animation)
{
auto animate = Animate3D::create(animation);
sprite->runAction(RepeatForever::create(animate));
}
// sprite->setPosition(Vec2(100,100)); // [ case1 ] 2d position
sprite->setPosition3D(Vec3(0,0,0)); // [ case2 ] 3d position
addChild(sprite);
sprite->setCameraMask((unsigned short)CameraFlag::USER1); // to camera with fbo
}
It does not render Sprite3D, both [ case1 ], [ case2 ]