首页 > 代码库 > WildMagic 简单图元(一)

WildMagic 简单图元(一)

#include <Wm5WindowApplication3.h>
#include <Wm5VisualEffectInstance.h>
using namespace Wm5;


class MyFrame : public WindowApplication3
{
    WM5_DECLARE_INITIALIZE;
    WM5_DECLARE_TERMINATE;
public:
    MyFrame();
    virtual bool OnInitialize();
    virtual void OnTerminate();
    virtual void OnIdle();
private:
    NodePtr  mScene;
    Culler      mCuller;
};
WM5_REGISTER_INITIALIZE(MyFrame);
WM5_REGISTER_TERMINATE(MyFrame);

WM5_WINDOW_APPLICATION(MyFrame)

MyFrame::MyFrame()
    :WindowApplication3("Ocean", 0, 0, 1024, 800, Float4(0.0, 0.0, 1.0, 1.0))
{

}

bool MyFrame::OnInitialize()
{
    if (!WindowApplication3::OnInitialize())
    {
        return false;
    }
   mCamera->SetFrustum(60.0f, GetAspectRatio(), 0.1f, 1000.0f);
    APoint camPosition(16.0f, 0.0f, 0.0f);
    AVector camDVector(-1.0f, 0.0f, 0.0f);
    AVector camUVector(0.0f, 0.0f, 1.0f);
    AVector camRVector = camDVector.Cross(camUVector);
    mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);
    mScene = new0 Node;
    VertexFormat * vFormat = VertexFormat::Create(2,
                                                  VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
                                                  VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    TriMesh * mesh = StandardMesh(vFormat).Rectangle(32, 32, 16.0f, 16.0f);
    VisualEffectInstance * instance = VertexColor3Effect::CreateUniqueInstance();
    mesh->SetEffectInstance(instance);
    mScene->AttachChild(mesh);
    mScene->Update();
    mCuller.SetCamera(mCamera);
    mCuller.ComputeVisibleSet(mScene);
    InitializeCameraMotion(0.01f, 0.001f);
    InitializeObjectMotion(mScene);
    return true;
}

void MyFrame::OnTerminate()
{
    WindowApplication3::OnTerminate();
}

void MyFrame::OnIdle()
{
    MeasureTime();
    if (MoveCamera())
    {
        mCuller.ComputeVisibleSet(mScene);
    }
    if (MoveObject())
    {
        mScene->Update();
        mCuller.ComputeVisibleSet(mScene);
    }
    if (mRenderer->PreDraw())
    {
        mRenderer->ClearBuffers();
        mRenderer->Draw(mCuller.GetVisibleSet());
        DrawFrameRate(8, GetHeight() - 8, Float4(0.0, 1.0, 0.0, 1.0));
        mRenderer->PostDraw();
        mRenderer->DisplayColorBuffer();
    }
    UpdateFrameCount();
}