bool VTerrainSectorRenderer::SaveToFile(VTerrainSector *pSector)
{
  if (pSector)
    SetSector(pSector);
  if (!m_pSector)
    return false;

  // render into the context, readback results and save to file
  Vision::Game.SetUpdateSceneCount(Vision::Game.GetUpdateSceneCount() + 1);
  m_spContext->GetCamera()->Update();
  IVisVisibilityCollector_cl *pVisColl = m_spContext->GetVisibilityCollector();
  pVisColl->SetPropertiesFromRenderContext(m_spContext);
  pVisColl->OnDoVisibilityDetermination(m_spContext->GetRenderFilterMask());

  m_spContext->SetRecentlyRendered(true);
  m_spContext->Activate();
  VisRenderContext_cl::GlobalTick();
    
  Vision::Renderer.BeginRendering();
  Vision::RenderSceneHelper();

  // get the snapshot from the render target and write it to disk
  char szRelativePathname[FS_MAX_PATH];
  if (pSector->m_Config.GetSectorDiffuseReplacementFilename (szRelativePathname, pSector->m_iIndexX, pSector->m_iIndexY))
  {
    VASSERT_MSG (m_spContext->GetRenderTarget(0)->IsRenderable(), "Render-Target must be a renderable texture object.");
    SaveRenderTarget(m_pSector, (VisRenderableTexture_cl*) m_spContext->GetRenderTarget(0), szRelativePathname);
  }

  Vision::Renderer.EndRendering();

  VisRenderContext_cl::GetMainRenderContext()->Activate();

  return true;
}