From fc1dbf49fb0e09e2897cb39e7b9857f96b5b24f0 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Wed, 28 Jan 2015 15:21:57 +0800 Subject: [PATCH] fix billboard --- cocos/3d/CCBillBoard.cpp | 23 ++++++++++++++++------- cocos/3d/CCBillBoard.h | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cocos/3d/CCBillBoard.cpp b/cocos/3d/CCBillBoard.cpp index 67f2a6816a..6ae4e793ae 100644 --- a/cocos/3d/CCBillBoard.cpp +++ b/cocos/3d/CCBillBoard.cpp @@ -102,6 +102,9 @@ void BillBoard::visit(Renderer *renderer, const Mat4& parentTransform, uint32_t { return; } + bool visibleByCamera = isVisitableByVisitingCamera(); + if (!visibleByCamera && _children.empty()) + return; uint32_t flags = processParentFlags(parentTransform, parentFlags); @@ -119,7 +122,7 @@ void BillBoard::visit(Renderer *renderer, const Mat4& parentTransform, uint32_t director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform); - bool visibleByCamera = isVisitableByVisitingCamera(); + int i = 0; @@ -158,7 +161,7 @@ bool BillBoard::calculateBillbaordTransform() const Mat4& camWorldMat = camera->getNodeToWorldTransform(); //TODO: use math lib to calculate math lib Make it easier to read and maintain - if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || _transformDirty || _modeDirty) + if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_mvTransform.m, _modelViewTransform.m, sizeof(float) * 16) != 0 || _modeDirty || true) { //Rotate based on anchor point Vec3 anchorPoint(_anchorPointInPoints.x , _anchorPointInPoints.y , 0.0f); @@ -190,12 +193,18 @@ bool BillBoard::calculateBillbaordTransform() 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); + ///FIXME: maybe should keep rotation along z axis +// if (rotationQuaternion.z > 0) +// { +// //rotate z only, keep it +// // 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))); +// rotationMatrix.rotateZ(rotationZ); +// } Vec3 upAxis = Vec3(rotationMatrix.m[4],rotationMatrix.m[5],rotationMatrix.m[6]); Vec3 x, y; @@ -217,7 +226,7 @@ bool BillBoard::calculateBillbaordTransform() billboardTransform.m[12] = localToWorld.m[12]; billboardTransform.m[13] = localToWorld.m[13]; billboardTransform.m[14] = localToWorld.m[14]; billboardTransform.translate(-anchorPoint); - _modelViewTransform = billboardTransform; + _mvTransform = _modelViewTransform = billboardTransform; _camWorldMat = camWorldMat; diff --git a/cocos/3d/CCBillBoard.h b/cocos/3d/CCBillBoard.h index b2f5ac8937..13862a97ee 100644 --- a/cocos/3d/CCBillBoard.h +++ b/cocos/3d/CCBillBoard.h @@ -105,6 +105,7 @@ protected: bool calculateBillbaordTransform(); Mat4 _camWorldMat; + Mat4 _mvTransform; Mode _mode; bool _modeDirty;