void SCRenderToTexture::SetPainter(const String &sValue)
{
	if (m_sPainter != sValue) {
		m_sPainter = sValue;

		// Setup the surface painter
		if (m_pSurfaceTextureBuffer) {
			SurfacePainter *pSurfacePainter = GetSceneContext()->GetRendererContext().GetRenderer().CreateSurfacePainter(m_sPainter);
			m_pSurfaceTextureBuffer->SetPainter(pSurfacePainter);
			if (pSurfacePainter && pSurfacePainter->IsInstanceOf("PLScene::SPScene")) {
				// Do NOT let the renderer update this surface by default, WE update it inside DrawPre()
				m_pSurfaceTextureBuffer->SetActive(false);

				// THIS is the scene root :)
				SPScene *pPainter = static_cast<SPScene*>(pSurfacePainter);
				pPainter->SetRootContainer(this);

				// Tell the surface scene painter about the 'conrete scene'
				SceneNode *pSceneNode = GetByName(m_sSceneName);
				if (pSceneNode && pSceneNode->IsContainer())
					pPainter->SetSceneContainer(static_cast<SceneContainer*>(pSceneNode));
				else
					pPainter->SetSceneContainer(nullptr);

				// Set default scene renderer
				pPainter->SetDefaultSceneRenderer(m_sSceneRenderer);
			}
		}
	}
}
Beispiel #2
0
//[-------------------------------------------------------]
//[ Private virtual PLEngine::EngineApplication functions ]
//[-------------------------------------------------------]
void Application67::OnCreateScene(SceneContainer &cContainer)
{
	// Create a scene container with our 'concrete sound scene' using the default sound API
	SceneNode *pSceneContainerNode = cContainer.Create("PLSound::SCSound", "SoundScene");
	if (pSceneContainerNode && pSceneContainerNode->IsInstanceOf("PLScene::SceneContainer")) {
		SceneContainer *pSceneContainer = static_cast<SceneContainer*>(pSceneContainerNode);

		// Protect this important container!
		pSceneContainer->SetProtected(true);

		// Populate the scene container
		// Setup scene surface painter
		SurfacePainter *pPainter = GetPainter();
		if (pPainter && pPainter->IsInstanceOf("PLScene::SPScene")) {
			SPScene *pSPScene = static_cast<SPScene*>(pPainter);
			pSPScene->SetRootContainer(cContainer.GetContainer());
			pSPScene->SetSceneContainer(pSceneContainer);

			// Get the scene context
			SceneContext *pSceneContext = GetSceneContext();
			if (pSceneContext) {
				// Create us a scene renderer
				SceneRenderer *pSceneRenderer = pSceneContext->GetSceneRendererManager().Create("2DGame");
				if (pSceneRenderer) {
					// Add begin scene renderer pass
					pSceneRenderer->Create("PLCompositing::SRPBegin", "Begin", "TextureFormat=\"R8G8B8A8\" Flags=\"Inactive\"");

					// Add our own scene renderer pass
					pSceneRenderer->Create("SRP2DGame", "2DGame");

					// Add post processing scene renderer pass
					pSceneRenderer->Create("PLCompositing::SRPPostProcessing", "PostProcessing");

					// Add end scene renderer pass
					pSceneRenderer->Create("PLCompositing::SRPEnd", "End");

					// Make this scene renderer to the default scene renderer of our scene surface painter
					pSPScene->SetDefaultSceneRenderer(pSceneRenderer->GetName());
				}
			}
		}

		// Set scene container
		SetScene(pSceneContainer);

		// Start the game
		Restart();
	}
}
void SCRenderToTexture::SetSceneRenderer(const String &sValue)
{
	if (m_sSceneRenderer != sValue) {
		m_sSceneRenderer = sValue;

		// Setup the surface painter
		if (m_pSurfaceTextureBuffer) {
			SurfacePainter *pSurfacePainter = m_pSurfaceTextureBuffer->GetPainter();
			if (pSurfacePainter && pSurfacePainter->IsInstanceOf("PLScene::SPScene")) {
				SPScene *pPainter = static_cast<SPScene*>(pSurfacePainter);
				pPainter->SetDefaultSceneRenderer(m_sSceneRenderer);
			}
		}
	}
}
/**
*  @brief
*    Creates/recreates the texture surface
*/
void SCRenderToTexture::CreateSurfaceTexture()
{
	// Get the renderer
	Renderer &cRenderer = GetSceneContext()->GetRendererContext().GetRenderer();

	// Delete the old render to texture buffer surface
	if (m_pSurfaceTextureBuffer) {
		delete m_pSurfaceTextureBuffer;
		m_pSurfaceTextureBuffer = nullptr;
	}

	// Delete the old resulting texture
	Texture *pTexture = m_pTextureHandler->GetResource();
	if (pTexture) {
		pTexture->Delete();
		m_pTextureHandler->SetResource(nullptr);
	}

	// Get the desired texture buffer format
	TextureBuffer::EPixelFormat nFormat;
	switch (m_nFormat) {
		case 0:
			nFormat = TextureBuffer::R8G8B8;
			break;

		case 1:
			nFormat = TextureBuffer::R8G8B8A8;
			break;

		default:
			nFormat = TextureBuffer::R8G8B8A8;
			break;
	}

	// Create 'render to texture buffer' surface
	if (m_bCube) {
		m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBufferCube(m_nWidth, nFormat, m_nSurfaceFlags);
	} else {
		// If possible, we use a standard 2D texture buffer
		if (cRenderer.GetCapabilities().bTextureBufferNonPowerOfTwo || (Math::IsPowerOfTwo(m_nWidth) && Math::IsPowerOfTwo(m_nHeight)))
			m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBuffer2D(Vector2i(m_nWidth, m_nHeight), nFormat, m_nSurfaceFlags);
		else
			m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBufferRectangle(Vector2i(m_nWidth, m_nHeight), nFormat, m_nSurfaceFlags);
	}
	if (m_pSurfaceTextureBuffer) {
		// Setup the surface painter
		SurfacePainter *pSurfacePainter = cRenderer.CreateSurfacePainter(m_sPainter);
		m_pSurfaceTextureBuffer->SetPainter(pSurfacePainter);
		if (pSurfacePainter && pSurfacePainter->IsInstanceOf("PLScene::SPScene")) {
			// Do NOT let the renderer update this surface by default, WE update it inside DrawPre()
			m_pSurfaceTextureBuffer->SetActive(false);

			// THIS is the scene root :)
			SPScene *pPainter = static_cast<SPScene*>(pSurfacePainter);
			pPainter->SetRootContainer(this);

			// Tell the surface scene painter about the 'conrete scene'
			SceneNode *pSceneNode = GetByName(m_sSceneName);
			pPainter->SetSceneContainer((pSceneNode && pSceneNode->IsContainer()) ? static_cast<SceneContainer*>(pSceneNode) : nullptr);

			// Set default scene renderer
			pPainter->SetDefaultSceneRenderer(m_sSceneRenderer);
		}
	}

	// Add the texture
	TextureManager &cTextureManager = cRenderer.GetRendererContext().GetTextureManager();
	// If there's already a texture with this name we have to get another, still free resource name
	if (cTextureManager.GetByName(m_sTextureName)) {
		// Find an unused resource name
		String sName = m_sTextureName + "_0";
		for (uint32 i=1; cTextureManager.GetByName(sName); i++)
			sName = m_sTextureName + '_' + static_cast<int>(i);

		// We have found an unused name
		m_sTextureName = sName;
	}
	if (m_pSurfaceTextureBuffer && m_pSurfaceTextureBuffer->GetTextureBuffer()) {
		pTexture = cTextureManager.CreateTexture(m_sTextureName, *m_pSurfaceTextureBuffer->GetTextureBuffer());
		if (pTexture)
			m_sTextureName = pTexture->GetName();
		m_pTextureHandler->SetResource(pTexture);
	}
}