axmol/cocos/3d/CCBillBoard.cpp

180 lines
6.6 KiB
C++
Raw Normal View History

2014-08-15 16:15:29 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
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 "3d/CCBillBoard.h"
#include "2d/CCSpriteFrameCache.h"
#include "base/CCDirector.h"
#include "2d/CCCamera.h"
2014-08-15 16:15:29 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/CCGLProgramCache.h"
2014-08-15 16:15:29 +08:00
NS_CC_BEGIN
2014-09-04 16:26:11 +08:00
BillBoard::BillBoard()
2014-08-29 17:11:18 +08:00
: _zDepthInView(0.0f)
2014-09-05 14:59:14 +08:00
, _mode(Mode::VIEW_POINT_ORIENTED)
2014-09-04 16:26:11 +08:00
, _modeDirty(false)
2014-08-15 16:15:29 +08:00
{
2014-09-17 18:01:18 +08:00
Node::setAnchorPoint(Vec2(0.5f,0.5f));
2014-08-15 16:15:29 +08:00
}
2014-09-04 16:26:11 +08:00
BillBoard::~BillBoard()
2014-08-15 16:15:29 +08:00
{
}
2014-09-04 16:26:11 +08:00
BillBoard* BillBoard::createWithTexture(Texture2D *texture, Mode mode)
2014-08-15 16:15:29 +08:00
{
2014-09-04 16:26:11 +08:00
BillBoard *billborad = new (std::nothrow) BillBoard();
2014-08-15 16:15:29 +08:00
if (billborad && billborad->initWithTexture(texture))
{
billborad->_mode = mode;
2014-08-15 16:15:29 +08:00
billborad->autorelease();
return billborad;
}
CC_SAFE_DELETE(billborad);
return nullptr;
}
2014-09-04 16:26:11 +08:00
BillBoard* BillBoard::create(const std::string& filename, Mode mode)
2014-08-15 16:15:29 +08:00
{
2014-09-04 16:26:11 +08:00
BillBoard *billborad = new (std::nothrow) BillBoard();
2014-08-15 16:15:29 +08:00
if (billborad && billborad->initWithFile(filename))
{
billborad->_mode = mode;
2014-08-15 16:15:29 +08:00
billborad->autorelease();
return billborad;
}
CC_SAFE_DELETE(billborad);
return nullptr;
}
2014-09-04 16:26:11 +08:00
BillBoard* BillBoard::create(const std::string& filename, const Rect& rect, Mode mode)
2014-08-15 16:15:29 +08:00
{
2014-09-04 16:26:11 +08:00
BillBoard *billborad = new (std::nothrow) BillBoard();
2014-08-15 16:15:29 +08:00
if (billborad && billborad->initWithFile(filename, rect))
{
billborad->_mode = mode;
2014-08-15 16:15:29 +08:00
billborad->autorelease();
return billborad;
}
CC_SAFE_DELETE(billborad);
return nullptr;
}
2014-09-04 16:26:11 +08:00
BillBoard* BillBoard::create(Mode mode)
2014-08-15 16:15:29 +08:00
{
2014-09-04 16:26:11 +08:00
BillBoard *billborad = new (std::nothrow) BillBoard();
2014-08-15 16:15:29 +08:00
if (billborad && billborad->init())
{
billborad->_mode = mode;
2014-08-15 16:15:29 +08:00
billborad->autorelease();
return billborad;
}
CC_SAFE_DELETE(billborad);
return nullptr;
}
2014-09-04 16:26:11 +08:00
void BillBoard::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
2014-08-15 16:15:29 +08:00
{
auto camera = Camera::getVisitingCamera();
2014-09-03 15:28:15 +08:00
const Mat4& camWorldMat = camera->getNodeToWorldTransform();
2014-10-13 19:10:42 +08:00
if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_mvTransform.m, transform.m, sizeof(float) * 16) != 0 || _modeDirty)
2014-08-29 17:11:18 +08:00
{
2014-10-14 15:08:36 +08:00
Vec3 anchorPoint(_anchorPointInPoints.x , _anchorPointInPoints.y , 0.0f);
Mat4 localToWorld = transform;
localToWorld.translate(anchorPoint);
Vec3 camDir;
switch (_mode)
{
2014-09-05 14:59:14 +08:00
case Mode::VIEW_POINT_ORIENTED:
2014-10-14 15:08:36 +08:00
camDir = Vec3(localToWorld.m[12] - camWorldMat.m[12], localToWorld.m[13] - camWorldMat.m[13], localToWorld.m[14] - camWorldMat.m[14]);
break;
2014-09-05 14:59:14 +08:00
case Mode::VIEW_PLANE_ORIENTED:
camWorldMat.transformVector(Vec3(0.0f, 0.0f, -1.0f), &camDir);
break;
default:
2014-09-05 14:59:14 +08:00
CCASSERT(false, "invalid billboard mode");
break;
}
2014-09-04 16:26:11 +08:00
_modeDirty = false;
2014-09-03 15:28:15 +08:00
if (camDir.length() < MATH_TOLERANCE)
{
camDir.set(camWorldMat.m[8], camWorldMat.m[9], camWorldMat.m[10]);
}
camDir.normalize();
2014-12-04 15:19:41 +08:00
Quaternion rotationQuaternion;
this->getNodeToWorldTransform().getRotation(&rotationQuaternion);
// fetch the rotation angle of z
float rotationZ = atan2(2*(rotationQuaternion.w*rotationQuaternion.z + rotationQuaternion.x*rotationQuaternion.y),
(1 - 2* (rotationQuaternion.y*rotationQuaternion.y + rotationQuaternion.z *rotationQuaternion.z)));
Mat4 rotationMatrix;
rotationMatrix.setIdentity();
rotationMatrix.rotateZ(rotationZ);
Vec3 upAxis = Vec3(rotationMatrix.m[4],rotationMatrix.m[5],rotationMatrix.m[6]);
2014-10-14 15:08:36 +08:00
Vec3 x, y;
camWorldMat.transformVector(upAxis, &y);
Vec3::cross(camDir, y, &x);
x.normalize();
Vec3::cross(x, camDir, &y);
y.normalize();
float xlen = sqrtf(localToWorld.m[0] * localToWorld.m[0] + localToWorld.m[1] * localToWorld.m[1] + localToWorld.m[2] * localToWorld.m[2]);
float ylen = sqrtf(localToWorld.m[4] * localToWorld.m[4] + localToWorld.m[5] * localToWorld.m[5] + localToWorld.m[6] * localToWorld.m[6]);
float zlen = sqrtf(localToWorld.m[8] * localToWorld.m[8] + localToWorld.m[9] * localToWorld.m[9] + localToWorld.m[10] * localToWorld.m[10]);
_billboardTransform.m[0] = x.x * xlen; _billboardTransform.m[1] = x.y * xlen; _billboardTransform.m[2] = x.z * xlen;
_billboardTransform.m[4] = y.x * ylen; _billboardTransform.m[5] = y.y * ylen; _billboardTransform.m[6] = y.z * ylen;
_billboardTransform.m[8] = -camDir.x * zlen; _billboardTransform.m[9] = -camDir.y * zlen; _billboardTransform.m[10] = -camDir.z * zlen;
_billboardTransform.m[12] = localToWorld.m[12]; _billboardTransform.m[13] = localToWorld.m[13]; _billboardTransform.m[14] = localToWorld.m[14];
_billboardTransform.translate(-anchorPoint);
2014-09-03 15:28:15 +08:00
const Mat4 &viewMat = camWorldMat.getInversed();
2014-10-14 15:08:36 +08:00
_zDepthInView = -(viewMat.m[2] * _billboardTransform.m[12] + viewMat.m[6] * _billboardTransform.m[13] + viewMat.m[10] * _billboardTransform.m[14] + viewMat.m[14]);
2014-10-13 19:10:42 +08:00
_mvTransform = transform;
2014-09-03 15:28:15 +08:00
_camWorldMat = camWorldMat;
2014-08-29 17:11:18 +08:00
}
2014-08-15 16:15:29 +08:00
2014-09-03 09:02:32 +08:00
//FIXME: frustum culling here
2014-08-15 16:15:29 +08:00
{
2014-09-03 18:19:12 +08:00
_quadCommand.init(_zDepthInView, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, _billboardTransform);
2014-09-18 12:00:44 +08:00
_quadCommand.setTransparent(true);
renderer->addCommand(&_quadCommand);
}
2014-08-15 16:15:29 +08:00
}
2014-09-04 16:26:11 +08:00
void BillBoard::setMode( Mode mode )
{
_mode = mode;
2014-09-04 16:26:11 +08:00
_modeDirty = true;
}
2014-09-04 16:26:11 +08:00
BillBoard::Mode BillBoard::getMode() const
{
return _mode;
}
NS_CC_END