File: BlendedAnimations.cpp

package info (click to toggle)
libwildmagic 5.13-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 89,572 kB
  • ctags: 26,264
  • sloc: cpp: 210,924; csh: 637; sh: 95; makefile: 36
file content (240 lines) | stat: -rw-r--r-- 7,387 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.1.0 (2010/04/14)

#include "BlendedAnimations.h"

WM5_WINDOW_APPLICATION(BlendedAnimations);

//----------------------------------------------------------------------------
BlendedAnimations::BlendedAnimations ()
    :
    WindowApplication3("SampleGraphics/BlendedAnimations", 0, 0, 768, 768,
        Float4(0.5f, 0.0f, 1.0f, 1.0f)),
    mTextColor(1.0f, 1.0f, 1.0f, 1.0f),
    mManager(ThePath + "Data/", "Biped"),
    mAnimTime(0.0),
    mUpArrowPressed(false),
    mShiftPressed(false)
{
    // Set animation information.  The counts differ in debug and release
    // builds because of the differing frame rates of those builds.
#ifdef _DEBUG
    int idleWalkCount = 100;
    int walkCount = 10;
    int walkRunCount = 100;
    mAnimTimeDelta = 0.01;
#else
    int idleWalkCount = 1000;
    int walkCount = 100;
    int walkRunCount = 1000;
    mAnimTimeDelta = 0.001;
#endif

    // The idle head turning occurs too frequently (frequency = 1 in the
    // original model).  Reduce the turning by half.
    mManager.SetIdle(0.5, 0.0);

    // The walk and run cycles must be aligned properly for blending.  A
    // phase of 0.2 for the run cycle aligns the biped feet.
    mManager.SetRun(1.0, 0.2);

    // The initial state is 'idle'.
    mManager.Initialize(idleWalkCount, walkCount, walkRunCount);
}
//----------------------------------------------------------------------------
bool BlendedAnimations::OnInitialize ()
{
    if (!WindowApplication3::OnInitialize())
    {
        return false;
    }

    CreateScene();

    // Center-and-fit for camera viewing.
    mCamera->SetFrustum(60.0f, GetAspectRatio(), 1.0f, 1000.0f);
    APoint camPosition(-60.0f, -60.0f, 90.0f);
    AVector camDVector(1.0f, 1.0f, -1.0f);
    camDVector.Normalize();
    AVector camUVector(0.5f, 0.5f, 1.0f);
    camUVector.Normalize();
    AVector camRVector = camDVector.Cross(camUVector);
    mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);

    // Initial update of objects.
    mScene->Update(mAnimTime);

    InitializeCameraMotion(0.01f, 0.01f);
    InitializeObjectMotion(mScene);
    return true;
}
//----------------------------------------------------------------------------
void BlendedAnimations::OnTerminate ()
{
    mScene = 0;
    mWireState = 0;
    WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void BlendedAnimations::OnIdle ()
{
    MeasureTime();

    if (MoveObject())
    {
        mScene->Update(mAnimTime);
    }

    Update();

    if (mRenderer->PreDraw())
    {
        mRenderer->ClearBuffers();
        mRenderer->Draw(mVisibleSet);

        mRenderer->Draw(8, 16, mTextColor,
            "Press UP-ARROW to transition from idle to walk.");

        mRenderer->Draw(8, 40, mTextColor,
            "Press SHIFT-UP-ARROW to transition from walk to run.");

        char message[128];
        sprintf(message, "time = %6.4lf", mAnimTime);
        mRenderer->Draw(128, GetHeight()-8, mTextColor, message);

        DrawFrameRate(8, GetHeight()-8, mTextColor);

        mRenderer->PostDraw();
        mRenderer->DisplayColorBuffer();
    }

    UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool BlendedAnimations::OnKeyDown (unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'w':
    case 'W':
        mWireState->Enabled = !mWireState->Enabled;
        return true;
    }

    return WindowApplication3::OnKeyDown(key, x, y);
}
//----------------------------------------------------------------------------
bool BlendedAnimations::OnSpecialKeyDown (int key, int, int)
{
    if (key == KEY_UP_ARROW)
    {
        mUpArrowPressed = true;
    }
    else if (key == KEY_SHIFT)
    {
        mShiftPressed = true;
    }
    return true;
}
//----------------------------------------------------------------------------
bool BlendedAnimations::OnSpecialKeyUp (int key, int, int)
{
    if (key == KEY_UP_ARROW)
    {
        mUpArrowPressed = false;
    }
    else if (key == KEY_SHIFT)
    {
        mShiftPressed = false;
    }
    return true;
}
//----------------------------------------------------------------------------
void BlendedAnimations::CreateScene ()
{
    mWireState = new0 WireState();
    mRenderer->SetOverrideWireState(mWireState);

    mScene = new0 Node();
    mScene->AttachChild(mManager.GetRoot());

    // Create a floor to walk on.
    VertexFormat* vformat = VertexFormat::Create(3,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);

    mFloor = StandardMesh(vformat).Rectangle(2, 2, 1024.0f, 2048.0f);
    VertexBufferAccessor vba(mFloor);
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        Float2& tcoord = vba.TCoord<Float2>(0, i);
        tcoord[0] *= 64.0f;
        tcoord[1] *= 256.0f;
    }

    std::string textureName = Environment::GetPathR("Grating.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(textureName);
    texture->GenerateMipmaps();
    mFloor->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture,
        Shader::SF_LINEAR_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT));

    mScene->AttachChild(mFloor);

    ComputeVisibleSet(mScene);
}
//----------------------------------------------------------------------------
void BlendedAnimations::ComputeVisibleSet (Spatial* object)
{
    TriMesh* mesh = DynamicCast<TriMesh>(object);
    if (mesh)
    {
        mVisibleSet.Insert(mesh);
        return;
    }

    Node* node = DynamicCast<Node>(object);
    if (node)
    {
        for (int i = 0; i < node->GetNumChildren(); ++i)
        {
            Spatial* child = node->GetChild(i);
            if (child)
            {
                ComputeVisibleSet(child);
            }
        }
    }
}
//----------------------------------------------------------------------------
void BlendedAnimations::Update ()
{
    // Animate the biped.
    mManager.Update(mUpArrowPressed, mShiftPressed);
    mScene->Update(mAnimTime);
    mAnimTime += mAnimTimeDelta;

    // Give the impression the floor is moving by translating the texture
    // coordinates.  For this demo, the texture coordinates are modified in
    // the vertex buffer.  Generally, you could write a vertex shader with
    // time and velocity as inputs, and then compute the new texture
    // coordinates in the shader.
#ifdef _DEBUG
    float adjust = mManager.GetSpeed()/16.0f;
#else
    float adjust = mManager.GetSpeed()/160.0f;
#endif
    VertexBufferAccessor vba(mFloor);
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        Float2& tcoord = vba.TCoord<Float2>(0, i);
        tcoord[1] -= adjust;
    }
    mRenderer->Update(mFloor->GetVertexBuffer());
}
//----------------------------------------------------------------------------