目的: 写一套操作摄像机的操作, 与具体引擎无关。
其中使用到了今天刚看的 template 相关重点。
non-const-getter macros, 为了少些代码
#ifndef _CONSTGETERHELPER_H_ #define _CONSTGETERHELPER_H_ #define NON_CONST_GETTER(ClassType, ReturnType, ConstGetter)\ ReturnType ConstGetter(){ return const_cast<const ClassType*>(this)->ConstGetter();} #define NON_CONST_GETTER1(ClassType, ReturnType, ConstGetter,ParamType1)\ ReturnType ConstGetter(ParamType1 param1){ return const_cast<const ClassType*>(this)->ConstGetter(param1);} #define NON_CONST_GETTER2(ClassType, ReturnType, ConstGetter, ParamType1, ParamType2)\ ReturnType ConstGetter(ParamType1 param1, ParamType2 param2){ return const_cast<const ClassType*>(this)->ConstGetter(param1, param2);} #define NON_CONST_GETTER3(ClassType, ReturnType, ConstGetter, ParamType1, ParamType2, ParamType3)\ ReturnType ConstGetter(ParamType1 param1, ParamType2 param2, ParamType3 param3){ return const_cast<const ClassType*>(this)->ConstGetter(param1, param2, param3);} #endif
抽象 Camera, 其中的 namespace 名称是女朋友提的:)
#pragma once #include "ConstGeterHelper.h" namespace Sakula { template<typename _Camera, typename _Vector3, typename _Quaternion> class ICameraAdaptor { public: typedef _Camera CustomCameraType; typedef _Vector3 Vector3Type; typedef _Quaternion QuaternionType; public: virtual Vector3Type getPosition() const=0; virtual void setPosition(const Vector3Type& pos) = 0; virtual QuaternionType getOrientation() const=0; virtual void setOrientation(const QuaternionType& quat) = 0; virtual void destroyCamera() = 0; }; template<typename CameraAdaptorImpl> class CameraAdaptorT : public CameraAdaptorImpl { public: typedef typename CameraAdaptorImpl::CustomCameraType CustomCameraType; typedef typename CameraAdaptorImpl::Vector3Type Vector3Type; typedef typename CameraAdaptorImpl::QuaternionType QuaternionType; public: CameraAdaptorT(CustomCameraType* c) :camera(c) { }; ~CameraAdaptorT() { }; public: using typename CameraAdaptorImpl::getPosition; NON_CONST_GETTER( CameraAdaptorT, Vector3Type, getPosition); Vector3Type getPosition() const { return this->getPosition(); } void setPosition(const Vector3Type& v) { this->setPosition(v); } using typename CameraAdaptorImpl::setOrientation; NON_CONST_GETTER( CameraAdaptorT, QuaternionType, getOrientation); QuaternionType getOrientation() const { return this->getOrientation(); } void setOrientation(const QuaternionType& v) { this->setOrientation(v); } using typename CameraAdaptorImpl::destroyCamera; void destroyCamera() { this->destroyCamera(); } protected: CustomCameraType* camera; }; }
基于 OgreCamera 的实现版本
OgreCameraAdaptor.h
#pragma once namespace Ogre { class Camera; class Quaternion; class Vector3; }; #include "AdaptorT\CameraAdaptorT.h" namespace Sakula { typedef Sakula::CameraAdaptorT< Sakula::ICameraAdaptor<Ogre::Camera, Ogre::Vector3, Ogre::Quaternion> > OgreCameraAdaptorBase; class OgreCameraAdaptor : public OgreCameraAdaptorBase { public: OgreCameraAdaptor(Ogre::Camera* c); ~OgreCameraAdaptor(); public: virtual Vector3Type getPosition()const; virtual void setPosition( const Vector3Type& pos); virtual QuaternionType getOrientation()const; virtual void setOrientation(const QuaternionType& quat); virtual void destroyCamera(); }; }
OgreCameraAdaptor.cpp
#include "stdafx.h" #include "OgreCameraAdaptor.h" #include "OgreVector3.h" #include "OgreQuaternion.h" #include "OgreCamera.h" #include "OgreSceneManager.h" Sakula::OgreCameraAdaptor::OgreCameraAdaptor(Ogre::Camera* c) :OgreCameraAdaptorBase(c) { } Sakula::OgreCameraAdaptor::~OgreCameraAdaptor() { } Sakula::OgreCameraAdaptor::Vector3Type Sakula::OgreCameraAdaptor::getPosition()const { return camera->getPosition(); } void Sakula::OgreCameraAdaptor::setPosition( const Vector3Type& pos) { camera->setPosition(pos); } Sakula::OgreCameraAdaptor::QuaternionType Sakula::OgreCameraAdaptor::getOrientation()const { return camera->getOrientation(); } void Sakula::OgreCameraAdaptor::setOrientation(const QuaternionType& quat) { camera->setOrientation(quat); } void Sakula::OgreCameraAdaptor::destroyCamera() { Ogre::SceneManager* sm = camera->getSceneManager(); sm->destroyCamera(camera); camera = NULL; }
当然,这里没有对 Vector3 和 Quaternion 做适配接口, 实际上我是写了的, 因为各家的 Vector3 和 Quaternion 都不尽相同。计算函数也不一样,所以需要更多的提炼,慢慢写吧,希望年前能写一套比较完善的摄像机操作。