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

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


BEGIN_MESSAGE_MAP(AmbWin, RMWin)
	//{{AFX_MSG_MAP(AmbWin)
	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[]="D:\\mssdk\\samples\\Multimedia\\D3DRM\\media\\torus.x";

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

BOOL AmbWin::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;
	}
	meshbuilder->SetColorRGB( D3DVALUE(0.7), D3DVALUE(0.7), D3DVALUE(1) );
	ScaleMesh( meshbuilder, D3DVALUE(20) );

	// ------- MESH FRAME -------
	LPDIRECT3DRMFRAME l_frame;
	d3drm->CreateFrame( scene, &l_frame );
	l_frame->AddVisual( meshbuilder );
	l_frame->SetPosition( scene,
		D3DVALUE(-12), D3DVALUE(0), D3DVALUE(0) );
	l_frame->SetOrientation( scene,
        D3DVALUE(0), D3DVALUE(), D3DVALUE(1),
        D3DVALUE(1), D3DVALUE(0), D3DVALUE(0) );
	l_frame->SetRotation( scene,
			D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
			D3DVALUE(.01) );
	l_frame->Release();
	l_frame=0;

	// ------- MESH FRAME -------
	LPDIRECT3DRMFRAME r_frame;
	d3drm->CreateFrame( scene, &r_frame );
	r_frame->AddVisual( meshbuilder );
	r_frame->SetPosition( scene,
		D3DVALUE(12), D3DVALUE(0), D3DVALUE(0) );
	r_frame->SetOrientation( scene,
        D3DVALUE(0), D3DVALUE(0), D3DVALUE(1),
        D3DVALUE(1), D3DVALUE(0), D3DVALUE(0) );
	r_frame->SetRotation( scene,
			D3DVALUE(0), D3DVALUE(-1), D3DVALUE(0),
			D3DVALUE(.01) );
	r_frame->Release();
	r_frame=0;

	// ---------AMBIENT LIGHT--------
	LPDIRECT3DRMLIGHT alight;
	d3drm->CreateLightRGB( D3DRMLIGHT_PARALLELPOINT,
			D3DVALUE(1.00), D3DVALUE(1.0), D3DVALUE(1.0), 
			&alight);
//	scene->AddLight( alight );
	LPDIRECT3DRMFRAME lightframe;
	d3drm->CreateFrame( scene, &lightframe );
	lightframe->AddLight( alight );
//	lightframe->SetOrientation( scene,
        //D3DVALUE(-1), D3DVALUE(0), D3DVALUE(1),
        //D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
	alight->Release();
	alight=0;
	lightframe->Release();
	lightframe=0;

	//------ CAMERA-----------
	d3drm->CreateFrame( scene, &camera );
	camera->SetPosition( scene, D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50));
	camera->SetOrientation( scene,
        D3DVALUE(0), D3DVALUE(0), D3DVALUE(50),
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
	d3drm->CreateViewport( device, camera, 0, 0,	
			device->GetWidth(), device->GetHeight(), 
			&viewport);

	return TRUE;
}

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

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

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

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

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

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