页面滚动效果

时间:2014-05-15 05:41:50

标签: c++ opengl math shader projection

我想在着色器中创建页面滚动效果。所以我有一个yZ为0的XZ平面点。现在我假设一个R半径和Inf的Cylender。高度位于平面上,在Y轴上旋转一定角度。见图:

enter image description here

我想要一个等式,以便纸张可以在给定的XZ方向上滚动球体。

我正在做的是:

float2 currPoint = gl_Vertex.xz;
float2 normDir = normalize(-1, 0); //direction at which paper will start rolling out.
float cylRadius = 1.f;
float dist = sqrt(normDir.x *vi.x * vi.x + normDir.y *vi.y * vi.y);
float beta = dist / cylRadius;

float3 outPos = 0;
outPos.x = currPoint.x + N.x * cylRadius * sin(beta);
outPos.z = cylRadius * (1 -cos(beta));
outPos.y = currPoint.y + N.y * cylRadius * sin(beta); 

但它仅适用于normDir = normalize(-1, 0),在其他情况下结果不符合预期。

1 个答案:

答案 0 :(得分:1)

我得到了这个。我的实现是基于Pawel的页面Flip implimentation(http://nomtek.com/page-flip-3d/

以下是HLSL中的代码。

    float DistToLine(float2 pt1, float2 pt2, float2 testPt)
    {
      float2 lineDir = pt2 - pt1;
      float2 perpDir = float2(lineDir.y, -lineDir.x);
      float2 dirToPt1 = pt1 - testPt;
      return (dot(normalize(perpDir), dirToPt1));
    }

    float3 Roll(float2 pos )  //per vertex
    {
        float time = param1.z ;
        float t = (time);
        float2 A = float2( 0 , 1 );    //tweak these 4 variables for the direction of Roll
        float2 B = float2( 5.f , 1 );  //
        float2 C = float2( 1 , 0 );    //
        float2 D = float2( 0 , 0 );    //

        float2 P1 = lerp( B , A , time ) ;
        float2 P2 = lerp( C , D , time ) ; ;
        float2 N =   normalize( float2(-(P2-P1).y , (P2-P1).x ) );
        float dist = DistToLine(P1 , P2 , float2(pos.x , pos.y) );

        float3 vOut;
        if (dist > 0 )
        {
            float distFromEnd = DistToLine(C , B ,  float2(pos.x , pos.y) ) ;
            float R = lerp( .1 , .13 , distFromEnd );

            float2 p = pos - N * dist;
            float alpha = dist / R;
            float sinAlpha = R * sin(alpha);
            vOut.x = p.x + N.x * sinAlpha;
            vOut.y = p.y + N.y * sinAlpha;
            vOut.z = (1 - cos(alpha)) * R;
        }
        else
        {
            vOut.x = pos.x;
            vOut.y = pos.y;
            vOut.z = 0;
        }
        return vOut;
    }