Hello Guys. I’m trying to render a scene and then saving it as 2D texture like a screenshot. In my test scene there is a simple model. That’s what I see:
https://i.imgur.com/JF8iPdQ.png
but when I trying to save the texture in .png file it looks like emply scene:
https://i.imgur.com/8ti8vKw.png
Here is my code:
void Start()
{
scene_ = new Scene(context_); //главное - сцена
scene_->CreateComponent<Octree>();
//-------------------------------------
//Здесь описываем глобальные настройки рендеринга
Node* _pZoneAmbientNode = scene_->CreateChild("Zone");
if (_pZoneAmbientNode != nullptr) {
Zone* _pZoneAmbient = _pZoneAmbientNode->CreateComponent<Zone>();
if (_pZoneAmbient != nullptr) {
_pZoneAmbient->SetBoundingBox(BoundingBox(-2048.0f, 2048.0f));
_pZoneAmbient->SetAmbientColor(Color(1.0f, 1.0f, 1.0f, 1.0f));
//_pZoneAmbient->SetFogColor(Color(1.0f, 1.0f, 1.0f, 1.0f));
//_pZoneAmbient->SetFogStart(0.0f);
//_pZoneAmbient->SetFogEnd(512.0f);
}
}
//-------------------------------
// здесь добавляем ноду для камеры и саму камеру
/*auto cameraNode = scene_->CreateChild("MyCamera");
cameraNode->CreateComponent<Camera>();
cameraNode->SetFarClip(512.0f);
cameraNode->SetPosition(Vector3(0.0f, 0.0f, -5.0f)); //положение камеры
auto viewport = new Viewport(context_, scene_, cameraNode->GetComponent<Camera>()); //какая камера отображается на экране
*/
auto cameraNode = scene_->CreateChild("MyCamera");
if (cameraNode != nullptr){
Camera* _pCamera = cameraNode->CreateComponent<Camera>();
if (_pCamera != nullptr) {
_pCamera->SetFarClip(512.0f);
}
}
auto viewport = new Viewport(context_, scene_, cameraNode->GetComponent<Camera>());
GetSubsystem<Renderer>()->SetViewport(0, viewport);
//----------------------------------------------------------
// здесь создаем текстуру для экспорта в приложение
Texture2D* screenTexture = new Texture2D(context_);
screenTexture->SetSize(512, 512, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET);
//screenTexture->SetFilterMode(FILTER_TRILINEAR);
RenderSurface* _pRenderSurface = screenTexture->GetRenderSurface();
if (_pRenderSurface != nullptr){
SharedPtr<Viewport> _pViewport(new Viewport(context_, scene_, cameraNode->GetComponent<Camera>()));
_pRenderSurface->SetViewport(0, _pViewport);
_pRenderSurface->SetUpdateMode(RenderSurfaceUpdateMode::SURFACE_UPDATEALWAYS);
}
//---------------------------------------------------------------
//Здесь добавляем куб на сцену
auto boxNode = scene_->CreateChild("MyBox");
auto boxObject = boxNode->CreateComponent<StaticModel>();
auto cache = GetSubsystem<ResourceCache>();
boxObject->SetModel(cache->GetResource<Model>("Models/cat.mdl"));
boxNode->SetPosition(Vector3(0.0f, -50.0f, 250.0f));
boxNode->SetRotation(Quaternion(0.0f, 90.0f, -90.0f));
boxObject->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml"));
//---------------------------------------------------------
// добавляем источник освещения
auto lightNode = scene_->CreateChild("MyLight");
auto light = lightNode->CreateComponent<Light>();
light->SetLightType(LIGHT_DIRECTIONAL);
lightNode->SetDirection(Vector3(0.6f, -0.6f, 0.8f));
//----------------------------------
//здесь пишем текстуру в файл (игрушечный код)
GetSubsystem<Renderer>()->Update(1.0f);
GetSubsystem<Renderer>()->Render();
Image* _pImage = new Image(context_);
_pImage->SetSize(512, 512, 3);
unsigned char* _ImageData = new unsigned char[screenTexture->GetDataSize(512, 512)];
screenTexture->GetData(0, _ImageData); //отсюда буфер изображения можно забрать, минуя файл
_pImage->SetData(_ImageData);
_pImage->SavePNG("test.png");
//scene_->TakeScreenShot(_ImageData);
delete[] _ImageData;
}
Thank you !