Fixing bug

This commit is contained in:
songchengjiang 2014-10-13 19:10:42 +08:00
parent 9affecb0e4
commit ecd63db80b
3 changed files with 64 additions and 40 deletions

View File

@ -101,7 +101,7 @@ void BillBoard::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
auto camera = Camera::getVisitingCamera(); auto camera = Camera::getVisitingCamera();
const Mat4& camWorldMat = camera->getNodeToWorldTransform(); const Mat4& camWorldMat = camera->getNodeToWorldTransform();
if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_transform.m, transform.m, sizeof(float) * 16) != 0 || _modeDirty) if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_mvTransform.m, transform.m, sizeof(float) * 16) != 0 || _modeDirty)
{ {
Vec3 camDir; Vec3 camDir;
switch (_mode) switch (_mode)
@ -124,24 +124,38 @@ void BillBoard::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
} }
camDir.normalize(); camDir.normalize();
static Vec3 upAxis(0.0f, 1.0f, 0.0f); //static Vec3 upAxis(0.0f, 1.0f, 0.0f);
Vec3 x, y; //Vec3 x, y;
camWorldMat.transformVector(upAxis, &y); //camWorldMat.transformVector(upAxis, &y);
Vec3::cross(camDir, y, &x); //Vec3::cross(camDir, y, &x);
x.normalize(); //x.normalize();
Vec3::cross(x, camDir, &y); //Vec3::cross(x, camDir, &y);
float xlen = sqrtf(transform.m[0] * transform.m[0] + transform.m[1] * transform.m[1] + transform.m[2] * transform.m[2]); //float xlen = sqrtf(transform.m[0] * transform.m[0] + transform.m[1] * transform.m[1] + transform.m[2] * transform.m[2]);
float ylen = sqrtf(transform.m[4] * transform.m[4] + transform.m[5] * transform.m[5] + transform.m[6] * transform.m[6]); //float ylen = sqrtf(transform.m[4] * transform.m[4] + transform.m[5] * transform.m[5] + transform.m[6] * transform.m[6]);
float zlen = sqrtf(transform.m[8] * transform.m[8] + transform.m[9] * transform.m[9] + transform.m[10] * transform.m[10]); //float zlen = sqrtf(transform.m[8] * transform.m[8] + transform.m[9] * transform.m[9] + transform.m[10] * transform.m[10]);
_billboardTransform.m[0] = x.x * xlen; _billboardTransform.m[1] = x.y * xlen; _billboardTransform.m[2] = x.z * xlen; //_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[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[8] = -camDir.x * zlen; _billboardTransform.m[9] = -camDir.y * zlen; _billboardTransform.m[10] = -camDir.z * zlen;
_billboardTransform.m[12] = transform.m[12]; _billboardTransform.m[13] = transform.m[13]; _billboardTransform.m[14] = transform.m[14]; //_billboardTransform.m[12] = transform.m[12]; _billboardTransform.m[13] = transform.m[13]; _billboardTransform.m[14] = transform.m[14];
_transformDirty = true;
Mat4 parentToWorld = transform * getNodeToParentTransform().getInversed();
Mat4 v = camera->getViewMatrix();
v.inverse();
v.m[12] = v.m[13] = v.m[14] = 0.0f;
Vec3 scl, pos;
_transform.getScale(&scl);
_transform.getTranslation(&pos);
Mat4 sclMat, posMat;
Mat4::createScale(scl, &sclMat);
Mat4::createTranslation(pos, &posMat);
_billboardTransform = posMat * v * sclMat;
_billboardTransform = parentToWorld * _billboardTransform;
const Mat4 &viewMat = camWorldMat.getInversed(); const Mat4 &viewMat = camWorldMat.getInversed();
_zDepthInView = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]); _zDepthInView = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]);
_transform = transform; _mvTransform = transform;
_camWorldMat = camWorldMat; _camWorldMat = camWorldMat;
} }

View File

@ -99,7 +99,7 @@ CC_CONSTRUCTOR_ACCESS:
protected: protected:
Mat4 _camWorldMat; Mat4 _camWorldMat;
Mat4 _transform; Mat4 _mvTransform;
Mat4 _billboardTransform; Mat4 _billboardTransform;
float _zDepthInView; float _zDepthInView;

View File

@ -90,31 +90,41 @@ BillBoardTest::BillBoardTest()
_layerBillBorad->addChild(_camera); _layerBillBorad->addChild(_camera);
} }
std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"}; //std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"};
for (unsigned int i = 0; i < 4; ++i) //for (unsigned int i = 0; i < 4; ++i)
{ //{
Layer *layer = Layer::create(); // Layer *layer = Layer::create();
auto billboard = BillBoard::create(imgs[(unsigned int)(CCRANDOM_0_1() * 1 + 0.5)]); // auto billboard = BillBoard::create(imgs[(unsigned int)(CCRANDOM_0_1() * 1 + 0.5)]);
billboard->setScale(0.5f); // billboard->setScale(0.5f);
billboard->setPosition3D(Vec3(0.0f, 0.0f, CCRANDOM_MINUS1_1() * 150.0f)); // billboard->setPosition3D(Vec3(0.0f, 0.0f, CCRANDOM_MINUS1_1() * 150.0f));
billboard->setBlendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED); // billboard->setBlendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED);
billboard->setOpacity(CCRANDOM_0_1() * 128 + 128); // billboard->setOpacity(CCRANDOM_0_1() * 128 + 128);
_billboards.push_back(billboard); // _billboards.push_back(billboard);
layer->addChild(billboard); // layer->addChild(billboard);
_layerBillBorad->addChild(layer); // _layerBillBorad->addChild(layer);
layer->runAction( RepeatForever::create( RotateBy::create( CCRANDOM_0_1(), Vec3(0.0f, 45.0f, 0.0f) ) ) ); // layer->runAction( RepeatForever::create( RotateBy::create( CCRANDOM_0_1(), Vec3(0.0f, 45.0f, 0.0f) ) ) );
} //}
addNewBillBoradWithCoords(Vec3(20,5,0)); {
addNewBillBoradWithCoords(Vec3(60,5,0)); auto sprite = Sprite3D::create("Sprite3DTest/orc.c3t");
addNewBillBoradWithCoords(Vec3(100,5,0)); auto billboard = BillBoard::create("Images/Icon.png");
addNewBillBoradWithCoords(Vec3(140,5,0)); billboard->setScale(0.5f);
addNewBillBoradWithCoords(Vec3(180,5,0)); sprite->addChild(billboard);
addNewAniBillBoradWithCoords(Vec3(-20,0,0)); sprite->runAction( RepeatForever::create( RotateBy::create( 10.0f, Vec3(0.0f, 360.0f, 0.0f) ) ) );
addNewAniBillBoradWithCoords(Vec3(-60,0,0)); _billboards.push_back(billboard);
addNewAniBillBoradWithCoords(Vec3(-100,0,0)); _layerBillBorad->addChild(sprite);
addNewAniBillBoradWithCoords(Vec3(-140,0,0)); }
addNewAniBillBoradWithCoords(Vec3(-180,0,0));
//addNewBillBoradWithCoords(Vec3(20,5,0));
//addNewBillBoradWithCoords(Vec3(60,5,0));
//addNewBillBoradWithCoords(Vec3(100,5,0));
//addNewBillBoradWithCoords(Vec3(140,5,0));
//addNewBillBoradWithCoords(Vec3(180,5,0));
//addNewAniBillBoradWithCoords(Vec3(-20,0,0));
//addNewAniBillBoradWithCoords(Vec3(-60,0,0));
//addNewAniBillBoradWithCoords(Vec3(-100,0,0));
//addNewAniBillBoradWithCoords(Vec3(-140,0,0));
//addNewAniBillBoradWithCoords(Vec3(-180,0,0));
_camera->setPosition3D(Vec3(0, 130, 230)); _camera->setPosition3D(Vec3(0, 130, 230));
_camera->lookAt(Vec3(0,0,100), Vec3(0,1,0)); _camera->lookAt(Vec3(0,0,100), Vec3(0,1,0));