void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
{
    /*
     *  In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
     *  but not used the tx, ty value. we simulate the function here
     */
    m_tHelpPoint1.x = 0;
    m_tHelpPoint1.y = 1;
    m_tHelpPoint1 = PointApplyAffineTransform(m_tHelpPoint1, matrix);
    m_tHelpPoint1.x -= matrix.tx;
    m_tHelpPoint1.y -= matrix.ty;

    m_tHelpPoint2.x = 1;
    m_tHelpPoint2.y = 0;
    m_tHelpPoint2 = PointApplyAffineTransform(m_tHelpPoint2, matrix);
    m_tHelpPoint2.x -= matrix.tx;
    m_tHelpPoint2.y -= matrix.ty;

    node.m_fSkewX = -(kdAtan2f(m_tHelpPoint1.y, m_tHelpPoint1.x) - 1.5707964f);
    node.m_fSkewY = kdAtan2f(m_tHelpPoint2.y, m_tHelpPoint2.x);
    node.m_fScaleX = kdSqrtf(matrix.a * matrix.a + matrix.b * matrix.b);
    node.m_fScaleY = kdSqrtf(matrix.c * matrix.c + matrix.d * matrix.d);
    node.x = matrix.tx;
    node.y = matrix.ty;
}
示例#2
0
void LightEffect::prepareForRender(Sprite *sprite, Texture2D *normalmap)
{
    auto gl = getGLProgramState();

    gl->setUniformVec2("u_contentSize", sprite->getContentSize());

    Point posRelToSprite = PointApplyAffineTransform(Point(_lightPos.x, _lightPos.y), sprite->getWorldToNodeAffineTransform());
    gl->setUniformVec3("u_lightPos", Vec3(posRelToSprite.x, posRelToSprite.y, _lightPos.z));

    gl->setUniformTexture("u_normals", normalmap);

	int opacity = sprite->getOpacity();
    SpriteFrame *frame = sprite->getSpriteFrame();
    Size untrimmedSize = frame->getOriginalSize();
    Size trimmedSize = frame->getRect().size;
    Vec2 framePos = frame->getRect().origin;
    Size texSize = frame->getTexture()->getContentSize();
    
    // set sprite position in sheet
    gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height));
    gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height));
    gl->setUniformInt("u_spriteRotated", frame->isRotated());
	gl->setUniformFloat("u_spriteOpacity", (float)opacity / 255.0f);
    
    // set offset of trimmed sprite
    Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2;
    Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x)
                                           : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y);
    gl->setUniformVec2("u_spriteOffset", cornerOffset);

}
示例#3
0
EffectSpriteLamp::EffectSpriteLamp()
{
    if (ShaderTestDemo2::init()) {
        
        auto s = Director::getInstance()->getWinSize();
        _sprite = EffectSprite::create("Images/elephant1_Diffuse.png");
        //auto contentSize = _sprite->getContentSize();
        _sprite->setPosition(Vec2(s.width/2, s.height/2));
        addChild(_sprite);
        
        auto lampEffect = EffectNormalMapped::create("Images/elephant1_Normal.png");
        
        Vec3 pos(150,150, 50);
        _lightSprite = Sprite::create("Images/ball.png");
        this->addChild(_lightSprite);
        _lightSprite->setPosition(Vec2(pos.x, s.height- pos.y));
        Mat4 mat = _sprite->getNodeToWorldTransform();
        Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
        lampEffect->setLightColor(Color4F(1,1,1,1));
        lampEffect->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
        lampEffect->setKBump(2);
        _sprite->setEffect(lampEffect);
        _effect = lampEffect;
        auto listerner = EventListenerTouchAllAtOnce::create();
        listerner->onTouchesBegan = CC_CALLBACK_2(EffectSpriteLamp::onTouchesBegan, this);
        listerner->onTouchesMoved = CC_CALLBACK_2(EffectSpriteLamp::onTouchesMoved, this);
        listerner->onTouchesEnded = CC_CALLBACK_2(EffectSpriteLamp::onTouchesEnded, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listerner, this);
    }
}
    bool LightsHolder::init(std::vector<LightEffect *> effectsArray, Sprite *image) {
        if (initGLProgramState("Assets/image/makkajai/Shaders3D/pointlight.frag")) {
            _effectsArray = effectsArray;
            int idx = 0;

            sprite = image;
            for (auto &ef : _effectsArray) {

                if (ef != nullptr) {

                    v3Color[idx] = Vec3(ef->getLightColor().r, ef->getLightColor().g, ef->getLightColor().b) / 255;

                    lightCutoffRadius[idx] = ef->getLightCutoffRadius();
                    lightHalfRadius[idx] = ef->getLightHalfRadius();

                    Point posRelToSprite = PointApplyAffineTransform(Point(ef->getLightPos().x, ef->getLightPos().y),
                            sprite->getWorldToNodeAffineTransform());
                    _lightPos[idx] = Vec3(posRelToSprite.x, posRelToSprite.y, ef->getLightPos().z);


                    brightness[idx] = ef->getBrightness();
                }
                idx++;
            }
            _lightsNumber = idx;
            setLightColor();
            setAmbientLightColor(cocos2d::Color3B(55, 55, 55));
            setLightCutoffRadius();
            setLightHalfRadius();
            //  setBrightness();
            return true;
        }
        return false;
    }
示例#5
0
文件: CCSkin.cpp 项目: 0x0c/cocos2d-x
AffineTransform Skin::getNodeToWorldTransformAR() const
{
    AffineTransform displayTransform = _transform;
    Point anchorPoint =  _anchorPointInPoints;

    anchorPoint = PointApplyAffineTransform(anchorPoint, displayTransform);

    displayTransform.tx = anchorPoint.x;
    displayTransform.ty = anchorPoint.y;

    return AffineTransformConcat(displayTransform, _bone->getArmature()->getNodeToWorldTransform());
}
示例#6
0
void EffectNormalMapped::updateUniforms()
{
    if(_pointLight && _sprite)
    {
        Size winSize=Director::getInstance()->getWinSize();
        getGLProgramState()->setUniformFloat("u_kBump", _kBump);
        Mat4 mat= _pointLight->getNodeToWorldTransform();
        Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(mat.m[12], mat.m[13]),_sprite->parentToNodeTransform());
        getGLProgramState()->setUniformVec4("u_lightPosInLocalSpace", Vec4(lightPosInLocalSpace.x,lightPosInLocalSpace.y,mat.m[14],1));
        const Color3B &col = _pointLight->getDisplayedColor();
        getGLProgramState()->setUniformVec3("u_diffuseL", Vec3(col.r/255.0f,col.g/255.0f,col.b/255.0f));
    }
}
示例#7
0
void EffectSpriteLamp::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
    for ( auto &item: touches )
    {
        auto touch = item;
        auto s = Director::getInstance()->getWinSize();
        Point loc_winSpace = touch->getLocationInView();
        _lightSprite->setPosition(Vec2( loc_winSpace.x, s.height - loc_winSpace.y));
        Vec3 pos(loc_winSpace.x,loc_winSpace.y, 50);
        Mat4 mat = _sprite->getNodeToWorldTransform();
        Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
        ((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
    }
}
void LightsHolder::prepareForRender(Sprite *_sprite, Texture2D *normalmap)
{
    int idx=0;
    for(auto &ef : _effectsArray) {
     
        if (ef != nullptr)
        {
            Point posRelToSprite = PointApplyAffineTransform(Point(ef->getLightPos().x, ef->getLightPos().y),_sprite->getWorldToNodeAffineTransform());
            _lightPos[idx]=Vec3(posRelToSprite.x, posRelToSprite.y, ef->getLightPos().z);
            brightness[idx]=ef->getBrightness();
    
        }
        idx++;
    }
    
    auto gl = getGLProgramState();

    gl->setUniformVec2("u_contentSize", _sprite->getContentSize());


 

    gl->setUniformVec3v("u_lightPos",_lightsNumber,_lightPos);

    gl->setUniformTexture("u_normals", normalmap);

    SpriteFrame *frame = _sprite->getSpriteFrame();
    Size untrimmedSize = frame->getOriginalSize();
    Size trimmedSize = frame->getRect().size;
    Vec2 framePos = frame->getRect().origin;
    Size texSize = frame->getTexture()->getContentSize();
    
    // set sprite position in sheet
    gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height));
    gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height));
    
    gl->setUniformFloatv("u_brightness",_lightsNumber, brightness);
    
    gl->setUniformInt("u_spriteRotated", frame->isRotated());
    gl->setUniformInt("u_lightsNumber", _lightsNumber);
    
    // set offset of trimmed sprite
    Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2;
    Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x)
                                           : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y);
    gl->setUniformVec2("u_spriteOffset", cornerOffset);

}
void ColliderDetector::updateTransform(AffineTransform &t)
{
    if (!m_bActive)
    {
        return;
    }

    for(auto object : *m_pColliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;
        ContourData *contourData = colliderBody->getContourData();

        b2PolygonShape* b2shape = nullptr;
        if (m_pB2Body != nullptr)
        {
            b2shape = (b2PolygonShape *)colliderBody->getB2Fixture()->GetShape();
        }

        cpPolyShape* cpshape = nullptr;
        if (m_pCPBody != nullptr)
        {
            cpshape = (cpPolyShape *)colliderBody->getShape();
        }

        int num = contourData->m_tVertexList.count();
        ContourVertex2 **vs = (ContourVertex2 **)contourData->m_tVertexList.data->arr;

#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
        ContourVertex2 **cvs = (ContourVertex2 **)colliderBody->getCalculatedVertexList()->data->arr;
#endif

        for (int i = 0; i < num; i++)
        {
            helpPoint.setPoint( vs[i]->x,  vs[i]->y);
            helpPoint = PointApplyAffineTransform(helpPoint, t);


#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
            cvs[i]->x = helpPoint.x;
            cvs[i]->y = helpPoint.y;
#endif

            if ( b2shape != nullptr )
            {
                b2Vec2 &bv = b2shape->m_vertices[i];
                bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
            }

            if ( cpshape != nullptr )
            {
                cpVect v ;
                v.x = helpPoint.x;
                v.y = helpPoint.y;
                cpshape->verts[i] = v;
            }
        }

		if ( cpshape != nullptr )
		{
			cpConvexHull(num, cpshape->verts, nullptr, nullptr, 0);
			for (int i = 0; i < num; i++)
			{
				cpVect b = cpshape->verts[(i + 1) % cpshape->numVerts];
				cpVect n = cpvnormalize(cpvperp(cpvsub(b, cpshape->verts[i])));

				cpshape->planes[i].n = n;
				cpshape->planes[i].d = cpvdot(n, cpshape->verts[i]);
			}
		}
    }
}