2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCCamera.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCString.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCGL.h"
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCDrawingPrimitives.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Camera::Camera(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Camera::~Camera(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
const char* Camera::description(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return String::createWithFormat("<Camera | center = (%.2f,%.2f,%.2f)>", _centerX, _centerY, _centerZ)->getCString();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Camera::init(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
restore();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Camera::restore(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_eyeX = _eyeY = 0.0f;
|
|
|
|
_eyeZ = getZEye();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_centerX = _centerY = _centerZ = 0.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_upX = 0.0f;
|
|
|
|
_upY = 1.0f;
|
|
|
|
_upZ = 0.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
kmMat4Identity(&_lookupMatrix);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Camera::locate(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dirty)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
kmVec3 eye, center, up;
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
kmVec3Fill(&eye, _eyeX, _eyeY , _eyeZ);
|
|
|
|
kmVec3Fill(¢er, _centerX, _centerY, _centerZ);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
kmVec3Fill(&up, _upX, _upY, _upZ);
|
|
|
|
kmMat4LookAt(&_lookupMatrix, &eye, ¢er, &up);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-08-08 16:31:44 +08:00
|
|
|
kmGLMultMatrix(&_lookupMatrix);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Camera::getZEye(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return FLT_EPSILON;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::setEye(float fEyeX, float fEyeY, float fEyeZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_eyeX = fEyeX;
|
|
|
|
_eyeY = fEyeY;
|
|
|
|
_eyeZ = fEyeZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::setCenter(float fCenterX, float fCenterY, float fCenterZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_centerX = fCenterX;
|
|
|
|
_centerY = fCenterY;
|
|
|
|
_centerZ = fCenterZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::setUp(float fUpX, float fUpY, float fUpZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_upX = fUpX;
|
|
|
|
_upY = fUpY;
|
|
|
|
_upZ = fUpZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::getEye(float *pEyeX, float *pEyeY, float *pEyeZ) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
*pEyeX = _eyeX;
|
|
|
|
*pEyeY = _eyeY;
|
|
|
|
*pEyeZ = _eyeZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::getCenter(float *pCenterX, float *pCenterY, float *pCenterZ) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
*pCenterX = _centerX;
|
|
|
|
*pCenterY = _centerY;
|
|
|
|
*pCenterZ = _centerZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
void Camera::getUp(float *pUpX, float *pUpY, float *pUpZ) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
*pUpX = _upX;
|
|
|
|
*pUpY = _upY;
|
|
|
|
*pUpZ = _upZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|