在Ogre中设置固定流水线的用户自定义裁剪平面的方法

转载请注明出处!Pulas

http://www.cnblogs.com/pulas/archive/2012/02/16/2354542.html

首先自定义一个场景管理器的监听器.

头文件:

#ifndef __CustomSceneManagerListener_H__
#define __CustomSceneManagerListener_H__

#include <Ogre.h>

class CCustomSceneManagerListener : public Ogre::SceneManager::Listener
{
public:
	CCustomSceneManagerListener();
	~CCustomSceneManagerListener();
 
public:
	/** Called prior to searching for visible objects in this SceneManager.
	@remarks
		Note that the render queue at this stage will be full of the last
		render's contents and will be cleared after this method is called.
	@param source The SceneManager instance raising this event.
	@param irs The stage of illumination being dealt with. IRS_NONE for 
		a regular render, IRS_RENDER_TO_TEXTURE for a shadow caster render.
	@param v The viewport being updated. You can get the camera from here.
	*/
	virtual void preFindVisibleObjects(Ogre::SceneManager* source, Ogre::SceneManager::IlluminationRenderStage irs, Ogre::Viewport* v);

public:
	/** Sets the user clipping region.
	*/
	virtual void setClipPlanes(const Ogre::PlaneList& clipPlanes);

	/** Add a user clipping plane. */
	virtual void addClipPlane (const Ogre::Plane &p);
	/** Add a user clipping plane. */
	virtual void addClipPlane (Ogre::Real A, Ogre::Real B, Ogre::Real C, Ogre::Real D);

	/** Clears the user clipping region.
	*/
	virtual void resetClipPlanes();

	void enableClipPlane(bool bEnable);

	bool isClipPlaneEnabled();

protected:
	// Recording user clip planes
	Ogre::PlaneList m_ClipPlanes;

	bool m_bEnableClipPlane;
};

#endif
 
实现文件:
#include "stdafx.h"
#include "CustomSceneManagerListener.h"
using namespace Ogre;

CCustomSceneManagerListener::CCustomSceneManagerListener() : m_bEnableClipPlane(true)
{
}

CCustomSceneManagerListener::~CCustomSceneManagerListener()
{
}

void CCustomSceneManagerListener::preFindVisibleObjects(Ogre::SceneManager* source, Ogre::SceneManager::IlluminationRenderStage irs, Ogre::Viewport* v)
{
	if (m_bEnableClipPlane)
	{
		Ogre::Root::getSingletonPtr()->getRenderSystem()->setClipPlanes(m_ClipPlanes);
	}
}

//---------------------------------------------------------------------
void CCustomSceneManagerListener::addClipPlane (const Plane &p)
{
	m_ClipPlanes.push_back(p);
}
//---------------------------------------------------------------------
void CCustomSceneManagerListener::addClipPlane (Real A, Real B, Real C, Real D)
{
	addClipPlane(Plane(A, B, C, D));
}
//---------------------------------------------------------------------
void CCustomSceneManagerListener::setClipPlanes(const PlaneList& clipPlanes)
{
	if (clipPlanes != m_ClipPlanes)
	{
		m_ClipPlanes = clipPlanes;
	}
}
//---------------------------------------------------------------------
void CCustomSceneManagerListener::resetClipPlanes()
{
	if (!m_ClipPlanes.empty())
	{
		m_ClipPlanes.clear();
	}
}

void CCustomSceneManagerListener::enableClipPlane( bool bEnable )
{
	m_bEnableClipPlane = bEnable;
}

bool CCustomSceneManagerListener::isClipPlaneEnabled()
{
	return m_bEnableClipPlane;
}
 
在初始化完Ogre后,添加自定义裁剪面:
m_pCustomSceneManagerListener = new CCustomSceneManagerListener;
mSceneMgr->addListener(m_pCustomSceneManagerListener);

Ogre::Plane SectionClipPlane;
SectionClipPlane.normal = Ogre::Vector3::NEGATIVE_UNIT_Y;
SectionClipPlane.d = -4.9;
m_pCustomSceneManagerListener->addClipPlane(SectionClipPlane);
原文地址:https://www.cnblogs.com/pulas/p/2354542.html