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

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


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


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

BOOL DecalWin::CreateScene()
{
    //------ ÄÅÊÀËÛ --------
    LPDIRECT3DRMTEXTURE texture1, texture2;
    HRSRC texture_id;
    texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE1),
       "TEXTURE" );
    d3drm->LoadTextureFromResource( texture_id, &texture1 );
    texture1->SetDecalOrigin( 64, 64 );

    texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE2),
        "TEXTURE" );
    d3drm->LoadTextureFromResource( texture_id, &texture2 );
    texture2->SetDecalOrigin( 64, 64 );

    //-------- ÔÐÅÉÌÛ --------
    LPDIRECT3DRMFRAME dummyframe1;
    d3drm->CreateFrame( scene, &dummyframe1 );
    dummyframe1->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(0.05) );

    LPDIRECT3DRMFRAME dummyframe2;
    d3drm->CreateFrame( scene, &dummyframe2 );
    dummyframe2->SetRotation(scene,
            D3DVALUE(1), D3DVALUE(0), D3DVALUE(0),
            D3DVALUE(-0.05) );

    LPDIRECT3DRMFRAME orbitframe1;
    d3drm->CreateFrame( dummyframe1, &orbitframe1 );
    orbitframe1->SetPosition( dummyframe1,
            D3DVALUE(2), D3DVALUE(0), D3DVALUE(0) );
    orbitframe1->AddVisual( texture1 );

    LPDIRECT3DRMFRAME orbitframe2;
    d3drm->CreateFrame( dummyframe2, &orbitframe2 );
    orbitframe2->SetPosition( dummyframe2,
            D3DVALUE(0), D3DVALUE(-2), D3DVALUE(0) );
    orbitframe2->AddVisual( texture2 );

    texture1->Release();
    texture1=0;
    texture2->Release();
    texture2=0;
    dummyframe1->Release();
    dummyframe1=0;
    dummyframe2->Release();
    dummyframe2=0;
    orbitframe1->Release();
    orbitframe1=0;
    orbitframe2->Release();
    orbitframe2=0;

    //--------- ÎÑÂÅÙÅÍÈÅ --------
    LPDIRECT3DRMLIGHT light;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(1.0),D3DVALUE(1.0), D3DVALUE(1.0),
            &light );
    scene->AddLight( light );
    light->Release();
    light=0;

    //--------- ÎÁËÀÑÒÜ ÏÐÎÑÌÎÒÐÀ --------
    d3drm->CreateFrame( scene, &camera );
    camera->SetPosition( scene,
            D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(-6.0) );
    d3drm->CreateViewport( device, camera,
            0, 0,
            device->GetWidth(), device->GetHeight(),
            &viewport );
    return TRUE;
}


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

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

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

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

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

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