#include <AfxWin.h>
#include <AfxExt.h>
#define INITGUID
#include <ddraw.h>
#include <d3d.h>
#include <d3drm.h>
#include <d3drmwin.h>
#include "ZoomWin.h"
#include "resource.h"

#pragma comment (lib,"ddraw.lib")
#pragma comment (lib,"d3drm.lib")


BEGIN_MESSAGE_MAP(ZoomWin, RMWin)
	//{{AFX_MSG_MAP(ZoomWin)
	ON_COMMAND(ID_RENDER_WIREFRAME, OnRenderWireframe)
	ON_COMMAND(ID_RENDER_FLAT, OnRenderFlat)
	ON_COMMAND(ID_RENDER_GOURAUD, OnRenderGouraud)
	ON_UPDATE_COMMAND_UI(ID_RENDER_FLAT, OnUpdateRenderFlat)
	ON_UPDATE_COMMAND_UI(ID_RENDER_GOURAUD, OnUpdateRenderGouraud)
	ON_UPDATE_COMMAND_UI(ID_RENDER_WIREFRAME, OnUpdateRenderWireframe)
	ON_COMMAND(ID_ANIMATION_LINEAR, OnAnimationLinear)
	ON_UPDATE_COMMAND_UI(ID_ANIMATION_LINEAR, OnUpdateAnimationLinear)
	ON_COMMAND(ID_ANIMATION_SPLINE, OnAnimationSpline)
	ON_UPDATE_COMMAND_UI(ID_ANIMATION_SPLINE, OnUpdateAnimationSpline)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

char meshname[]="swirl.x";
LPDIRECT3DRMFRAME ZoomWin::zoomframe;
LPDIRECT3DRMANIMATION ZoomWin::animation;


ZoomWin::ZoomWin()
{
	meshbuilder=0;
}

BOOL ZoomWin::CreateScene()
{
	HRESULT r;
	scene->SetSceneBackgroundRGB( D3DVALUE(.2), D3DVALUE(.2), D3DVALUE(.2) );
	// ------- MESH --------
	d3drm->CreateMeshBuilder( &meshbuilder );
	r=meshbuilder->Load( meshname, NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
	if (r!=D3DRM_OK)
	{
		CString msg;
		msg.Format( "Failed to load file '%s'\n", meshname );
		AfxMessageBox( msg );
		return FALSE;
	}
    meshbuilder->SetColorRGB(D3DVALUE(.67),
            D3DVALUE(.82), D3DVALUE(.94) );
	ScaleMesh( meshbuilder, D3DVALUE(25) );

    // ------- MATERIAL ÌÀÒÅÐÈÀË ----------
    LPDIRECT3DRMMATERIAL material;
    d3drm->CreateMaterial( D3DVALUE(10), &material );
    meshbuilder->SetMaterial( material );
    material->Release();
    material=0;

    // ------ MESH FRAME ÔÐÅÉÌ ÑÅÒÊÈ ------
    LPDIRECT3DRMFRAME meshframe;
    d3drm->CreateFrame( scene, &meshframe );
    meshframe->SetRotation( scene,
            D3DVALUE(1), D3DVALUE(.4), D3DVALUE(0),
            D3DVALUE(.001) );
    meshframe->AddVisual( meshbuilder );
    meshframe->Release();
    meshframe=0;

    // ------- ANIMATION ÀÍÈÌÀÖÈß --------
    d3drm->CreateAnimation( &animation );
    animation->SetOptions(  D3DRMANIMATION_SPLINEPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION );
    animation->AddPositionKey( D3DVALUE(1),
            D3DVALUE(.4), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(25),
            D3DVALUE(5), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(50),
            D3DVALUE(1), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(75),
            D3DVALUE(10), D3DVALUE(), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(100),
            D3DVALUE(.04), D3DVALUE(0), D3DVALUE(0) );

    d3drm->CreateFrame( scene, &zoomframe );
    animation->SetFrame( zoomframe );

    // --------- LIGHT ÎÑÂÅÙÅÍÈÅ --------
    LPDIRECT3DRMLIGHT dlight;
    d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &dlight );

    LPDIRECT3DRMLIGHT alight;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(.40), D3DVALUE(.40), D3DVALUE(.40),
            &alight );
    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );

    lightframe->AddLight( dlight );
    lightframe->AddLight( alight );

    dlight->Release();
    dlight=0;
    alight->Release();
    alight=0;
    lightframe->Release();
    lightframe=0;

    //------ CAMERA ÊÀÌÅÐÀ ----------
    d3drm->CreateFrame( scene, &camera );
    camera->SetPosition( scene,
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50) );
    d3drm->CreateViewport( device, camera, 0, 0,
            device->GetWidth(), device->GetHeight(),
            &viewport );
    camera->AddMoveCallback( AdjustField, NULL );

    return TRUE;
}

void ZoomWin::OnRenderWireframe() 
{
	if (meshbuilder)
		meshbuilder->SetQuality( D3DRMRENDER_WIREFRAME );
}

void ZoomWin::OnRenderFlat() 
{
	if (meshbuilder)
		meshbuilder->SetQuality( D3DRMRENDER_FLAT );
}

void ZoomWin::OnRenderGouraud() 
{
	if (meshbuilder)
		meshbuilder->SetQuality( D3DRMRENDER_GOURAUD );
}

void ZoomWin::OnUpdateRenderWireframe(CCmdUI* pCmdUI) 
{
	if (meshbuilder)
	{
		D3DRMRENDERQUALITY meshquality = meshbuilder->GetQuality();
		pCmdUI->SetCheck( meshquality==D3DRMRENDER_WIREFRAME );
	}
}

void ZoomWin::OnUpdateRenderFlat(CCmdUI* pCmdUI) 
{
	if (meshbuilder)
	{
		D3DRMRENDERQUALITY meshquality = meshbuilder->GetQuality();
		pCmdUI->SetCheck( meshquality==D3DRMRENDER_FLAT );
	}
}

void ZoomWin::OnUpdateRenderGouraud(CCmdUI* pCmdUI) 
{
	if (meshbuilder)
	{
		D3DRMRENDERQUALITY meshquality = meshbuilder->GetQuality();
		pCmdUI->SetCheck( meshquality==D3DRMRENDER_GOURAUD );
	}
}

void ZoomWin::AdjustField(LPDIRECT3DRMFRAME, void*, D3DVALUE)
{
    static D3DVALUE time;
    time+=D3DVALUE(.1);
    animation->SetTime( time );

    D3DVECTOR pos;
    zoomframe->GetPosition( 0, &pos );

    viewport->SetField( 0.3 );//..pos.x );
}

void ZoomWin::OnAnimationLinear() 
{
	// TODO: Add your command handler code here
    animation->SetOptions( D3DRMANIMATION_LINEARPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION );
	
}

void ZoomWin::OnUpdateAnimationLinear(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
    D3DRMANIMATIONOPTIONS options = animation->GetOptions();
    pCmdUI->SetCheck( options & D3DRMANIMATION_LINEARPOSITION );
	
}

void ZoomWin::OnAnimationSpline() 
{
	// TODO: Add your command handler code here
    animation->SetOptions(  D3DRMANIMATION_SPLINEPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION );
	
}

void ZoomWin::OnUpdateAnimationSpline(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
    D3DRMANIMATIONOPTIONS options = animation->GetOptions();
    pCmdUI->SetCheck( options & D3DRMANIMATION_SPLINEPOSITION );
	
}
