visiting camera

This commit is contained in:
yangxiao 2014-08-08 11:49:59 +08:00
parent 1e15131b2b
commit 4dce59582b
7 changed files with 24 additions and 36 deletions

View File

@ -1242,7 +1242,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
auto camera = director->getCurrentCamera(); auto camera = Camera::getVisitingCamera();
bool visibleByCamera = camera ? (unsigned short)camera->getCameraFlag() & _cameraMask : true; bool visibleByCamera = camera ? (unsigned short)camera->getCameraFlag() & _cameraMask : true;
int i = 0; int i = 0;

View File

@ -185,12 +185,6 @@ void Scene::addChildToPhysicsWorld(Node* child)
} }
} }
Camera* Scene::getCameraByIndex(int index) const
{
CCASSERT(index < _cameras.size(), "invalid index");
return _cameras[index];
}
#endif #endif
NS_CC_END NS_CC_END

View File

@ -65,10 +65,8 @@ public:
using Node::addChild; using Node::addChild;
virtual std::string getDescription() const override; virtual std::string getDescription() const override;
/** get camera count */ /** get all cameras */
ssize_t getCameraCount() const { return _cameras.size(); } const std::vector<Camera*>& getCameras() const { return _cameras; }
/** get camera */
Camera* getCameraByIndex(int index) const;
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
Scene(); Scene();
@ -82,6 +80,7 @@ protected:
friend class ProtectedNode; friend class ProtectedNode;
friend class SpriteBatchNode; friend class SpriteBatchNode;
friend class Camera; friend class Camera;
friend class Director;
std::vector<Camera*> _cameras; //weak ref to Camera std::vector<Camera*> _cameras; //weak ref to Camera

View File

@ -29,6 +29,8 @@
NS_CC_BEGIN NS_CC_BEGIN
Camera* Camera::_visitingCamera = nullptr;
Camera* Camera::create() Camera* Camera::create()
{ {
Camera* camera = nullptr; Camera* camera = nullptr;
@ -224,17 +226,9 @@ void Camera::onEnter()
{ {
if (_scene == nullptr) if (_scene == nullptr)
{ {
auto parent = getParent(); auto scene = getScene();
while (parent)
{
auto scene = dynamic_cast<Scene*>(parent);
if (scene) if (scene)
{
setScene(scene); setScene(scene);
break;
}
parent = parent->getParent();
}
} }
Node::onEnter(); Node::onEnter();
} }

View File

@ -132,6 +132,8 @@ public:
virtual void onEnter() override; virtual void onEnter() override;
virtual void onExit() override; virtual void onExit() override;
static const Camera* getVisitingCamera() { return _visitingCamera; }
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
Camera(); Camera();
~Camera(); ~Camera();
@ -157,6 +159,10 @@ protected:
float _farPlane; float _farPlane;
mutable bool _viewProjectionDirty; mutable bool _viewProjectionDirty;
unsigned short _cameraFlag; // camera flag unsigned short _cameraFlag; // camera flag
static Camera* _visitingCamera;
friend class Director;
}; };
NS_CC_END NS_CC_END

View File

@ -158,7 +158,6 @@ bool Director::init(void)
initMatrixStack(); initMatrixStack();
_renderer = new Renderer; _renderer = new Renderer;
_currentCamera = nullptr;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
_console = new Console; _console = new Console;
@ -289,18 +288,19 @@ void Director::drawScene()
if (_runningScene) if (_runningScene)
{ {
Camera* defaultCamera = nullptr; Camera* defaultCamera = nullptr;
const auto& cameras = _runningScene->_cameras;
//draw with camera //draw with camera
for (ssize_t i = 0; i < _runningScene->getCameraCount(); i++) for (size_t i = 0; i < cameras.size(); i++)
{ {
_currentCamera = _runningScene->getCameraByIndex((int)i); Camera::_visitingCamera = cameras[i];
if (_currentCamera->getCameraFlag() == CameraFlag::DEFAULT) if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
{ {
defaultCamera = _currentCamera; defaultCamera = Camera::_visitingCamera;
continue; continue;
} }
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _currentCamera->getViewProjectionMatrix()); loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
//visit the scene //visit the scene
_runningScene->visit(_renderer, Mat4::IDENTITY, false); _runningScene->visit(_renderer, Mat4::IDENTITY, false);
@ -311,9 +311,9 @@ void Director::drawScene()
//draw with default camera //draw with default camera
if (defaultCamera) if (defaultCamera)
{ {
_currentCamera = defaultCamera; Camera::_visitingCamera = defaultCamera;
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _currentCamera->getViewProjectionMatrix()); loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
//visit the scene //visit the scene
_runningScene->visit(_renderer, Mat4::IDENTITY, false); _runningScene->visit(_renderer, Mat4::IDENTITY, false);
@ -321,7 +321,7 @@ void Director::drawScene()
popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
} }
_currentCamera = nullptr; Camera::_visitingCamera = nullptr;
_eventDispatcher->dispatchEvent(_eventAfterVisit); _eventDispatcher->dispatchEvent(_eventAfterVisit);
} }

View File

@ -407,8 +407,6 @@ public:
*/ */
float getFrameRate() const { return _frameRate; } float getFrameRate() const { return _frameRate; }
Camera* getCurrentCamera() const { return _currentCamera; }
protected: protected:
void purgeDirector(); void purgeDirector();
bool _purgeDirectorInNextLoop; // this flag will be set to true in end() bool _purgeDirectorInNextLoop; // this flag will be set to true in end()
@ -509,9 +507,6 @@ protected:
/* Renderer for the Director */ /* Renderer for the Director */
Renderer *_renderer; Renderer *_renderer;
/* current camera, only vailid during drawScene */
Camera* _currentCamera;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
/* Console for the director */ /* Console for the director */
Console *_console; Console *_console;