Nore camera

This commit is contained in:
Ricardo Quesada 2013-12-20 16:33:31 -08:00
parent f55513fa48
commit d8ad5291f4
10 changed files with 168 additions and 115 deletions

View File

@ -36,11 +36,7 @@ NS_CC_BEGIN
void ActionCamera::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
Camera *camera = target->getCamera();
camera->getCenter(&_centerXOrig, &_centerYOrig, &_centerZOrig);
camera->getEye(&_eyeXOrig, &_eyeYOrig, &_eyeZOrig);
camera->getUp(&_upXOrig, &_upYOrig, &_upZOrig);
_targetTransformCopy = target->getNodeToParentTransform();
}
ActionCamera* ActionCamera::clone() const
@ -56,6 +52,65 @@ ActionCamera * ActionCamera::reverse() const
// FIXME: This conversion isn't safe.
return (ActionCamera*)ReverseTime::create(const_cast<ActionCamera*>(this));
}
void ActionCamera::restore()
{
_eyeX = _eyeY = 0.0f;
_eyeZ = FLT_EPSILON;
_centerX = _centerY = _centerZ = 0.0f;
_upX = 0.0f;
_upY = 1.0f;
_upZ = 0.0f;
_dirty = false;
kmMat4Identity(&_lookupMatrix);
if( _target )
_target->setNodeToParentTransform(_targetTransformCopy);
}
void ActionCamera::setEye(float x, float y, float z)
{
_eyeX = x;
_eyeY = y;
_eyeZ = z;
updateTransform();
}
void ActionCamera::setCenter(float centerX, float centerY, float centerZ)
{
_centerX = centerX;
_centerY = centerY;
_centerZ = centerZ;
updateTransform();
}
void ActionCamera::setUp(float upX, float upY, float upZ)
{
_upX = upX;
_upY = upY;
_upZ = upZ;
updateTransform();
}
void ActionCamera::updateTransform()
{
kmVec3 eye, center, up;
kmVec3Fill(&eye, _eyeX, _eyeY , _eyeZ);
kmVec3Fill(&center, _centerX, _centerY, _centerZ);
kmVec3Fill(&up, _upX, _upY, _upZ);
kmMat4LookAt(&_lookupMatrix, &eye, &center, &up);
_target->setNodeToParentTransform(_lookupMatrix);
}
//
// OrbitCamera
//
@ -121,26 +176,21 @@ void OrbitCamera::update(float dt)
float za = _radZ + _radDeltaZ * dt;
float xa = _radX + _radDeltaX * dt;
float i = sinf(za) * cosf(xa) * r + _centerXOrig;
float j = sinf(za) * sinf(xa) * r + _centerYOrig;
float k = cosf(za) * r + _centerZOrig;
float i = sinf(za) * cosf(xa) * r + _centerX;
float j = sinf(za) * sinf(xa) * r + _centerY;
float k = cosf(za) * r + _centerZ;
_target->getCamera()->setEye(i,j,k);
setEye(i,j,k);
}
void OrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimuth)
{
float ex, ey, ez, cx, cy, cz, x, y, z;
float r; // radius
float s;
Camera* camera = _target->getCamera();
camera->getEye(&ex, &ey, &ez);
camera->getCenter(&cx, &cy, &cz);
x = ex-cx;
y = ey-cy;
z = ez-cz;
float x = _eyeX - _centerX;
float y = _eyeY - _centerY;
float z = _eyeZ - _centerZ;
r = sqrtf( powf(x,2) + powf(y,2) + powf(z,2));
s = sqrtf( powf(x,2) + powf(y,2));
@ -155,7 +205,7 @@ void OrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimut
else
*azimuth = asinf(y/s);
*newRadius = r / Camera::getZEye();
*newRadius = r / FLT_EPSILON;
}
NS_CC_END

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#define __CCCAMERA_ACTION_H__
#include "CCActionInterval.h"
#include "kazmath/kazmath.h"
NS_CC_BEGIN
@ -48,15 +49,16 @@ public:
* @js ctor
*/
ActionCamera()
:_centerXOrig(0)
,_centerYOrig(0)
,_centerZOrig(0)
,_eyeXOrig(0)
,_eyeYOrig(0)
,_eyeZOrig(0)
,_upXOrig(0)
,_upYOrig(0)
,_upZOrig(0)
:_centerX(0)
,_centerY(0)
,_centerZ(0)
,_eyeX(0)
,_eyeY(0)
,_eyeZ(FLT_EPSILON)
,_upX(0)
,_upY(1)
,_upZ(0)
,_dirty(false)
{}
/**
* @js NA
@ -70,17 +72,26 @@ public:
virtual ActionCamera *clone() const override;
protected:
float _centerXOrig;
float _centerYOrig;
float _centerZOrig;
float _eyeXOrig;
float _eyeYOrig;
float _eyeZOrig;
void restore();
void setEye(float x, float y, float z);
void setCenter(float x, float y, float z);
void setUp(float x, float y, float z);
void updateTransform();
float _upXOrig;
float _upYOrig;
float _upZOrig;
kmMat4 _lookupMatrix;
kmMat4 _targetTransformCopy;
float _centerX;
float _centerY;
float _centerZ;
float _eyeX;
float _eyeY;
float _eyeZ;
float _upX;
float _upY;
float _upZ;
bool _dirty;
};
/**
@ -112,7 +123,7 @@ public:
* @js NA
* @lua NA
*/
~OrbitCamera(){}
virtual ~OrbitCamera(){}
/** initializes a OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX */
bool initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX);

View File

@ -216,17 +216,17 @@ void GridBase::afterDraw(cocos2d::Node *target)
Director *director = Director::getInstance();
director->setProjection(_directorProjection);
if (target->getCamera()->isDirty())
{
Point offset = target->getAnchorPointInPoints();
//
// XXX: Camera should be applied in the AnchorPoint
//
kmGLTranslatef(offset.x, offset.y, 0);
target->getCamera()->locate();
kmGLTranslatef(-offset.x, -offset.y, 0);
}
// if (target->getCamera()->isDirty())
// {
// Point offset = target->getAnchorPointInPoints();
//
// //
// // XXX: Camera should be applied in the AnchorPoint
// //
// kmGLTranslatef(offset.x, offset.y, 0);
// target->getCamera()->locate();
// kmGLTranslatef(-offset.x, -offset.y, 0);
// }
GL::bindTexture2D(_texture->getName());

View File

@ -32,7 +32,6 @@ THE SOFTWARE.
#include "CCString.h"
#include "ccCArray.h"
#include "TransformUtils.h"
#include "CCCamera.h"
#include "CCGrid.h"
#include "CCDirector.h"
#include "CCScheduler.h"
@ -106,7 +105,6 @@ Node::Node(void)
, _additionalTransformDirty(false)
, _transformDirty(true)
, _inverseDirty(true)
, _camera(nullptr)
// children (lazy allocs)
// lazy alloc
, _ZOrder(0)
@ -168,8 +166,6 @@ Node::~Node()
CC_SAFE_RELEASE(_eventDispatcher);
// attributes
CC_SAFE_RELEASE(_camera);
CC_SAFE_RELEASE(_shaderProgram);
CC_SAFE_RELEASE(_userObject);
@ -402,17 +398,6 @@ ssize_t Node::getChildrenCount() const
return _children.size();
}
/// camera getter: lazy alloc
Camera* Node::getCamera()
{
if (!_camera)
{
_camera = new Camera();
}
return _camera;
}
/// isVisible getter
bool Node::isVisible() const
{
@ -1207,6 +1192,12 @@ const kmMat4& Node::getNodeToParentTransform() const
return _transform;
}
void Node::setNodeToParentTransform(const kmMat4& transform)
{
_transform = transform;
_transformDirty = false;
}
void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
{
CGAffineToGL(additionalTransform, _additionalTransform.mat);

View File

@ -43,7 +43,6 @@
NS_CC_BEGIN
class Camera;
class GridBase;
class Point;
class Touch;
@ -98,7 +97,6 @@ class EventListener;
- position
- scale (x, y)
- rotation (in degrees, clockwise)
- Camera (an interface to gluLookAt )
- GridBase (to do mesh transformations)
- anchor point
- size
@ -120,18 +118,14 @@ class EventListener;
-# The node will be translated (position)
-# The node will be rotated (rotation)
-# The node will be scaled (scale)
-# The node will be moved according to the camera values (camera)
Order in transformations with grid enabled
-# The node will be translated (position)
-# The node will be rotated (rotation)
-# The node will be scaled (scale)
-# The grid will capture the screen
-# The node will be moved according to the camera values (camera)
-# The grid will render the captured screen
Camera:
- Each node has a camera. By default it points to the center of the Node.
*/
class CC_DLL Node : public Object
@ -834,19 +828,6 @@ public:
/// @} end of Shader Program
/**
* Returns a camera object that lets you move the node using a gluLookAt
*
@code
Camera* camera = node->getCamera();
camera->setEye(0, 0, 415/2);
camera->setCenter(0, 0, 0);
@endcode
*
* @return A Camera object that lets you move the node using a gluLookAt
*/
virtual Camera* getCamera();
/**
* Returns whether or not the node accepts event callbacks.
*
@ -1202,6 +1183,11 @@ public:
virtual const kmMat4& getNodeToParentTransform() const;
virtual AffineTransform getNodeToParentAffineTransform() const;
/**
* Sets the Transformation matrix manually.
*/
virtual void setNodeToParentTransform(const kmMat4& transform);
/** @deprecated use getNodeToParentTransform() instead */
CC_DEPRECATED_ATTRIBUTE inline virtual AffineTransform nodeToParentTransform() const { return getNodeToParentAffineTransform(); }
@ -1437,8 +1423,6 @@ protected:
mutable bool _transformDirty; ///< transform dirty flag
mutable bool _inverseDirty; ///< inverse transform dirty flag
Camera *_camera; ///< a camera
int _ZOrder; ///< z-order value that affects the draw order
Vector<Node*> _children; ///< array of children nodes

View File

@ -135,7 +135,6 @@ void NodeGrid::visit()
// self draw,currently we have nothing to draw on NodeGrid, so there is no need to add render command
this->draw();
// Uses std::for_each to improve performance.
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) {
(*it)->visit();
}

View File

@ -119,13 +119,13 @@ void TransitionScene::finish()
_inScene->setPosition(Point(0,0));
_inScene->setScale(1.0f);
_inScene->setRotation(0.0f);
_inScene->getCamera()->restore();
// _inScene->getCamera()->restore();
_outScene->setVisible(false);
_outScene->setPosition(Point(0,0));
_outScene->setScale(1.0f);
_outScene->setRotation(0.0f);
_outScene->getCamera()->restore();
// _outScene->getCamera()->restore();
//[self schedule:@selector(setNewScene:) interval:0];
this->schedule(schedule_selector(TransitionScene::setNewScene), 0);

View File

@ -4,6 +4,8 @@
static std::function<Layer*()> createFunctions[] = {
CL(ActionOrbit),
CL(ActionManual),
CL(ActionMove),
CL(ActionRotate),
@ -1226,31 +1228,47 @@ void ActionOrbit::onEnter()
auto action1 = Sequence::create(
orbit1,
orbit1->reverse(),
NULL);
nullptr);
auto orbit2 = OrbitCamera::create(2,1, 0, 0, 180, -45, 0);
auto action2 = Sequence::create(
orbit2,
orbit2->reverse(),
NULL);
nullptr);
auto orbit3 = OrbitCamera::create(2,1, 0, 0, 180, 90, 0);
auto action3 = Sequence::create(
orbit3,
orbit3->reverse(),
NULL);
nullptr);
_kathia->runAction(RepeatForever::create(action1));
_tamara->runAction(RepeatForever::create(action2));
_grossini->runAction(RepeatForever::create(action3));
auto camera1 = Node::create();
auto camera2 = Node::create();
auto camera3 = Node::create();
auto move = MoveBy::create(3, Point(100,-100));
auto move_back = move->reverse();
auto seq = Sequence::create(move, move_back, NULL);
auto rfe = RepeatForever::create(seq);
_kathia->runAction(rfe);
_tamara->runAction(rfe->clone() );
_grossini->runAction( rfe->clone() );
this->addChild(camera1);
this->addChild(camera2);
this->addChild(camera3);
_kathia->removeFromParent();
_tamara->removeFromParent();
_grossini->removeFromParent();
camera1->addChild(_kathia);
camera2->addChild(_tamara);
camera3->addChild(_grossini);
camera1->runAction(RepeatForever::create(action1));
camera2->runAction(RepeatForever::create(action2));
camera3->runAction(RepeatForever::create(action3));
// auto move = MoveBy::create(3, Point(100,-100));
// auto move_back = move->reverse();
// auto seq = Sequence::create(move, move_back, NULL);
// auto rfe = RepeatForever::create(seq);
// _kathia->runAction(rfe);
// _tamara->runAction(rfe->clone() );
// _grossini->runAction( rfe->clone() );
}
std::string ActionOrbit::subtitle() const

View File

@ -604,10 +604,10 @@ CameraZoomTest::CameraZoomTest()
sprite = Sprite::create(s_pathGrossini);
addChild( sprite, 0);
sprite->setPosition( Point(s.width/4*1, s.height/2) );
cam = sprite->getCamera();
cam->setEye(0, 0, 415/2);
cam->setCenter(0, 0, 0);
// cam = sprite->getCamera();
// cam->setEye(0, 0, 415/2);
// cam->setCenter(0, 0, 0);
// CENTER
sprite = Sprite::create(s_pathGrossini);
addChild( sprite, 0, 40);
@ -625,17 +625,17 @@ CameraZoomTest::CameraZoomTest()
void CameraZoomTest::update(float dt)
{
Node *sprite;
Camera *cam;
// Camera *cam;
_z += dt * 100;
sprite = getChildByTag(20);
cam = sprite->getCamera();
cam->setEye(0, 0, _z);
// cam = sprite->getCamera();
// cam->setEye(0, 0, _z);
sprite = getChildByTag(40);
cam = sprite->getCamera();
cam->setEye(0, 0, -_z);
// cam = sprite->getCamera();
// cam->setEye(0, 0, -_z);
}
std::string CameraZoomTest::title() const

View File

@ -135,9 +135,9 @@ TMXOrthoTest::TMXOrthoTest()
child->getTexture()->setAntiAliasTexParameters();
}
float x, y, z;
map->getCamera()->getEye(&x, &y, &z);
map->getCamera()->setEye(x-200, y, z+300);
// float x, y, z;
// map->getCamera()->getEye(&x, &y, &z);
// map->getCamera()->setEye(x-200, y, z+300);
}
void TMXOrthoTest::onEnter()