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

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


BEGIN_MESSAGE_MAP(MultiViewWin, RMWin)
	//{{AFX_MSG_MAP(MultiViewWin)
	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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

char meshname[]="land3.x";

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

BOOL MultiViewWin::CreateScene()
{
	HRESULT r;
	// ------- 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;
	}
	ScaleMesh( meshbuilder, D3DVALUE(25) );

	// ------- MESH FRAME -------
	LPDIRECT3DRMFRAME meshframe;
	d3drm->CreateFrame( scene, &meshframe );
	meshframe->AddVisual( meshbuilder );
	meshframe->SetRotation( scene,
			D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
			D3DVALUE(.1) );
	meshframe->Release();
	meshframe=0;

	// ---------AMBIENT LIGHT--------
	LPDIRECT3DRMLIGHT alight;
	d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
			D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00), 
			&alight);

	scene->AddLight( alight );
	alight->Release();
	alight=0;

	// ---------DIRECTIONAL LIGHT--------
	LPDIRECT3DRMLIGHT dlight;
	d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL,
			D3DVALUE(0.4), D3DVALUE(0.4), D3DVALUE(0.4), 
			&dlight);

	LPDIRECT3DRMFRAME dlightframe;
	d3drm->CreateFrame( scene, &dlightframe );
	dlightframe->AddLight( dlight );
	dlightframe->SetOrientation( scene, 
			D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
			D3DVALUE(0), D3DVALUE(1), D3DVALUE(0));
	dlight->Release();
	dlight=0;
	dlightframe->Release();
	dlightframe=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);

	return TRUE;
}

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

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

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

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

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

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