Merge pull request #5049 from ricardoquesada/node_camera_fixes

Camera and Node fixes
This commit is contained in:
Ricardo Quesada 2014-01-13 12:54:33 -08:00
commit a94a68771c
6 changed files with 67 additions and 84 deletions

View File

@ -33,6 +33,12 @@ NS_CC_BEGIN
//
// CameraAction
//
ActionCamera::ActionCamera()
{
kmVec3Fill(&_center, 0, 0, 0);
kmVec3Fill(&_eye, 0, 0, FLT_EPSILON);
kmVec3Fill(&_up, 0, 1, 0);
}
void ActionCamera::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
@ -54,53 +60,39 @@ ActionCamera * ActionCamera::reverse() const
void ActionCamera::restore()
{
_eyeX = _eyeY = 0.0f;
_eyeZ = FLT_EPSILON;
kmVec3Fill(&_center, 0, 0, 0);
kmVec3Fill(&_eye, 0, 0, FLT_EPSILON);
kmVec3Fill(&_up, 0, 1, 0);
}
_centerX = _centerY = _centerZ = 0.0f;
_upX = 0.0f;
_upY = 1.0f;
_upZ = 0.0f;
void ActionCamera::setEye(const kmVec3& eye)
{
_eye = eye;
updateTransform();
}
void ActionCamera::setEye(float x, float y, float z)
{
_eyeX = x;
_eyeY = y;
_eyeZ = z;
kmVec3Fill(&_eye, x, y, z);
updateTransform();
}
void ActionCamera::setCenter(float centerX, float centerY, float centerZ)
void ActionCamera::setCenter(const kmVec3& center)
{
_centerX = centerX;
_centerY = centerY;
_centerZ = centerZ;
_center = center;
updateTransform();
}
void ActionCamera::setUp(float upX, float upY, float upZ)
void ActionCamera::setUp(const kmVec3& up)
{
_upX = upX;
_upY = upY;
_upZ = upZ;
_up = up;
updateTransform();
}
void ActionCamera::updateTransform()
{
kmVec3 eye, center, up;
kmVec3Fill(&eye, _eyeX, _eyeY , _eyeZ);
kmVec3Fill(&center, _centerX, _centerY, _centerZ);
kmVec3Fill(&up, _upX, _upY, _upZ);
kmMat4 lookupMatrix;
kmMat4LookAt(&lookupMatrix, &eye, &center, &up);
kmMat4LookAt(&lookupMatrix, &_eye, &_center, &_up);
Point anchorPoint = _target->getAnchorPointInPoints();
@ -199,9 +191,9 @@ void OrbitCamera::update(float dt)
float za = _radZ + _radDeltaZ * dt;
float xa = _radX + _radDeltaX * dt;
float i = sinf(za) * cosf(xa) * r + _centerX;
float j = sinf(za) * sinf(xa) * r + _centerY;
float k = cosf(za) * r + _centerZ;
float i = sinf(za) * cosf(xa) * r + _center.x;
float j = sinf(za) * sinf(xa) * r + _center.y;
float k = cosf(za) * r + _center.z;
setEye(i,j,k);
}
@ -211,9 +203,9 @@ void OrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimut
float r; // radius
float s;
float x = _eyeX - _centerX;
float y = _eyeY - _centerY;
float z = _eyeZ - _centerZ;
float x = _eye.x - _center.x;
float y = _eye.y - _center.y;
float z = _eye.z - _center.z;
r = sqrtf( powf(x,2) + powf(y,2) + powf(z,2));
s = sqrtf( powf(x,2) + powf(y,2));

View File

@ -50,17 +50,7 @@ public:
/**
* @js ctor
*/
ActionCamera()
:_centerX(0)
,_centerY(0)
,_centerZ(0)
,_eyeX(0)
,_eyeY(0)
,_eyeZ(FLT_EPSILON)
,_upX(0)
,_upY(1)
,_upZ(0)
{}
ActionCamera();
/**
* @js NA
* @lua NA
@ -72,23 +62,28 @@ public:
virtual ActionCamera * reverse() const override;
virtual ActionCamera *clone() const override;
/* sets the Eye value of the Camera */
void setEye(const kmVec3 &eye);
void setEye(float x, float y, float z);
/* returns the Eye value of the Camera */
const kmVec3& getEye() const { return _eye; }
/* sets the Center value of the Camera */
void setCenter(const kmVec3 &center);
/* returns the Center value of the Camera */
const kmVec3& getCenter() const { return _center; }
/* sets the Up value of the Camera */
void setUp(const kmVec3 &up);
/* Returns the Up value of the Camera */
const kmVec3& getUp() const { return _up; }
protected:
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 _centerX;
float _centerY;
float _centerZ;
float _eyeX;
float _eyeY;
float _eyeZ;
float _upX;
float _upY;
float _upZ;
kmVec3 _center;
kmVec3 _eye;
kmVec3 _up;
};
/**

View File

@ -103,7 +103,7 @@ Node::Node(void)
, _anchorPointInPoints(Point::ZERO)
, _anchorPoint(Point::ZERO)
, _contentSize(Size::ZERO)
, _additionalTransformDirty(false)
, _useAdditionalTransform(false)
, _transformDirty(true)
, _inverseDirty(true)
// children (lazy allocs)
@ -1189,10 +1189,9 @@ const kmMat4& Node::getNodeToParentTransform() const
// vertex Z
_transform.mat[14] = _vertexZ;
if (_additionalTransformDirty)
if (_useAdditionalTransform)
{
kmMat4Multiply(&_transform, &_transform, &_additionalTransform);
_additionalTransformDirty = false;
}
_transformDirty = false;
@ -1211,14 +1210,14 @@ void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
{
CGAffineToGL(additionalTransform, _additionalTransform.mat);
_transformDirty = true;
_additionalTransformDirty = true;
_useAdditionalTransform = true;
}
void Node::setAdditionalTransform(const kmMat4& additionalTransform)
{
_additionalTransform = additionalTransform;
_transformDirty = true;
_additionalTransformDirty = true;
_useAdditionalTransform = true;
}

View File

@ -1266,7 +1266,9 @@ public:
Point convertTouchToNodeSpaceAR(Touch * touch) const;
/**
* Sets the additional transform.
* Sets an additional transform matrix to the node.
*
* In order to remove it, set the Identity Matrix to the additional transform.
*
* @note The additional transform will be concatenated at the end of getNodeToParentTransform.
* It could be used to simulate `parent-child` relationship between two nodes (e.g. one is in BatchNode, another isn't).
@ -1289,7 +1291,7 @@ public:
spriteA->setPosition(Point(200, 200));
// Gets the spriteA's transform.
AffineTransform t = spriteA->getNodeToParentTransform();
auto t = spriteA->getNodeToParentTransform();
// Sets the additional transform to spriteB, spriteB's postion will based on its pseudo parent i.e. spriteA.
spriteB->setAdditionalTransform(t);
@ -1421,12 +1423,13 @@ protected:
Size _contentSize; ///< untransformed size of the node
kmMat4 _modelViewTransform; ///< ModelView transform of the Node.
// "cache" variables are allowed to be mutable
mutable kmMat4 _additionalTransform; ///< transform
mutable kmMat4 _transform; ///< transform
mutable kmMat4 _inverse; ///< inverse transform
kmMat4 _modelViewTransform; ///< ModelView transform of the Node.
mutable bool _additionalTransformDirty; ///< The flag to check whether the additional transform is dirty
bool _useAdditionalTransform; ///< The flag to check whether the additional transform is dirty
mutable bool _transformDirty; ///< transform dirty flag
mutable bool _inverseDirty; ///< inverse transform dirty flag

View File

@ -1021,7 +1021,18 @@ CameraTest2::CameraTest2()
_sprite2->setPosition( Point(3*s.width/4, s.height/2) );
_sprite2->setScale(0.5);
scheduleUpdate();
kmVec3 eye, center, up;
kmVec3Fill(&eye, 150, 0, 200);
kmVec3Fill(&center, 0, 0, 0);
kmVec3Fill(&up, 0, 1, 0);
kmMat4 lookupMatrix;
kmMat4LookAt(&lookupMatrix, &eye, &center, &up);
_sprite1->setAdditionalTransform(lookupMatrix);
_sprite2->setAdditionalTransform(lookupMatrix);
}
std::string CameraTest2::title() const
@ -1034,22 +1045,6 @@ std::string CameraTest2::subtitle() const
return "Both images should look the same";
}
void CameraTest2::update(float dt)
{
kmVec3 eye, center, up;
kmVec3Fill(&eye, 150, 0, 200);
kmVec3Fill(&center, 0, 0, 0);
kmVec3Fill(&up, 0, 1, 0);
kmMat4 lookupMatrix;
kmMat4LookAt(&lookupMatrix, &eye, &center, &up);
_sprite1->setAdditionalTransform(lookupMatrix);
_sprite2->setAdditionalTransform(lookupMatrix);
}
///
/// main
///

View File

@ -184,7 +184,6 @@ public:
virtual void onExit() override;
protected:
virtual void update(float dt) override;
CameraTest2();
Sprite *_sprite1;