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

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


BEGIN_MESSAGE_MAP(JadeWin, RMWin)
	//{{AFX_MSG_MAP(JadeWin)
	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\\sphere1.x";

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

BOOL JadeWin::CreateScene()
{
	HRESULT r;

	scene->SetSceneBackgroundRGB( D3DVALUE(231./255), D3DVALUE(231./255),
        D3DVALUE(255./255) );

	//-------- ÑÅÒÊÀ --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
	meshbuilder->SetPerspective( TRUE );
    r=meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL,
        NULL );
    if (r!=D3DRM_OK)
    {
        TRACE("meshbuilder->Load() failed\n");
        return FALSE;
    }
	meshbuilder->Scale( D3DVALUE(1), D3DVALUE(1), D3DVALUE(.5) );
    ScaleMesh( meshbuilder, D3DVALUE(20) );

    //-------- ÒÅÊÑÒÓÐÀ --------
    LPDIRECT3DRMTEXTURE texture;
    HRSRC texture_id = FindResource( NULL,
        MAKEINTRESOURCE(IDR_JADETEXTURE), "TEXTURE" );
    r = d3drm->LoadTextureFromResource( texture_id, &texture );
    if (r!=D3DRM_OK)
    {
        TRACE("d3drm->LoadTextureFromResource() failed\n");
        return FALSE;
    }
    meshbuilder->SetTexture( texture );
    texture->Release();
    texture=0;
/*
    //-------- ÏÎÊÐÛÒÈÅ --------
    D3DRMBOX box;
    meshbuilder->GetBox( &box );
    D3DVALUE w=box.max.x-box.min.x;
    D3DVALUE h=box.max.y-box.min.y;

    LPDIRECT3DRMWRAP wrap;
    d3drm->CreateWrap(D3DRMWRAP_FLAT, scene,
        D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0),  // íà÷àëî ïîêðûòèÿ
        D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0),  // z îñü ïîêðûòèÿ
        D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),  // y îñü ïîêðûòèÿ
        D3DVALUE(0.5), D3DVALUE(0.5),                 // íà÷àëî òåêñòóðû
        D3DDivide(1,w), D3DDivide(1,h),               // ìàñøòàá òåêñòóðû
        &wrap);
    wrap->Apply( meshbuilder );
    wrap->Release();
    wrap=0;
*/

	LPDIRECT3DRMMESH mesh;
	meshbuilder->CreateMesh( &mesh );
	meshbuilder->Release();
	meshbuilder=0;

    //------- ÔÐÅÉÌ ÑÅÒÊÈ ----------
    LPDIRECT3DRMFRAME meshframe;
    d3drm->CreateFrame( scene, &meshframe );
	meshframe->SetOrientation( scene,
        D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0));
    meshframe->AddVisual( mesh );
    meshframe->AddMoveCallback( MoveTexture, NULL );
    meshframe->AddMoveCallback( MoveFrame, NULL );
    meshframe->Release();
	mesh->Release();

    //---------- ÑÂÅÒ -----------
    LPDIRECT3DRMLIGHT light;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(1),D3DVALUE(1), D3DVALUE(1),
            &light );
    scene->AddLight( light );
    light->Release();
    light=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 JadeWin::OnRenderWireframe() 
{
	if (meshbuilder)
		meshbuilder->SetQuality( D3DRMRENDER_WIREFRAME );
}

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

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

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

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

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

void JadeWin::MoveFrame(LPDIRECT3DRMFRAME frame, void*, D3DVALUE)
{
    static int x=0;
    static int y=0;
    static int xi=1;
    static int yi=1;
    if ( x<-50 || x>50)
        xi=-xi;
    if (y<-35 || y>35)
        yi=-yi;
    x+=xi;
    y+=yi;
    frame->SetOrientation( NULL,
            D3DVALUE(x), D3DVALUE(y), D3DVALUE(50),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
}

void JadeWin::MoveTexture(LPDIRECT3DRMFRAME frame,
    void*, D3DVALUE)
{
    static D3DVALUE xtex;
    xtex+=D3DVALUE(.002);

    LPDIRECT3DRMVISUALARRAY visualarray;
    frame->GetVisuals( &visualarray );
    int nvisuals = visualarray->GetSize();
    for ( int i = 0; i < nvisuals; i++ )
    {
        LPDIRECT3DRMVISUAL visual;
        visualarray->GetElement( i, &visual );
        LPDIRECT3DRMMESH mesh;
        if (visual->QueryInterface( IID_IDirect3DRMMesh,
            (void**)&mesh) == 0)
        {
            D3DRMBOX box;
            mesh->GetBox( &box );
            D3DVALUE w=box.max.x-box.min.x;
            D3DVALUE h=box.max.y-box.min.y;

            LPDIRECT3DRMWRAP wrap;
            d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL,
                    D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0),
                    D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0),
                    D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),
                    xtex, /*D3DVALUE(0.5)*/ xtex,             // texture origin
                    D3DDivide(1,w), D3DDivide(1,h),  // texture scale
                    &wrap );
            wrap->Apply( mesh );
            wrap->Release();
            wrap=0;
            mesh->Release();
            mesh=0;
        }
        visual->Release();
        visual=0;
    }
    visualarray->Release();
    visualarray=0;
}
