// RnHeightField.h : Declaration of the CRnHeightField

/**
Simple height field mesh generator. Node takes hieght map encoded in a bitmap and generates height field mesh from it.

Example shows how to work with bitmaps and meshes, it also uses mesh fn set to calculate vertex normals.

You can find height field object saved into SDK examples library (Height field object)

*/

#pragma once

// IRnHeightField
[
	object,
	uuid("D8FFA5B3-BC31-4c54-851B-39CF65B6852E"),
	helpstring("IRnHeightField Interface"),
	pointer_default(unique)
]
__interface IRnHeightField : IUnknown
{
};



// CRnCube2

class ATL_NO_VTABLE CRnHeightField : 
    public CComObjectRootEx<CComGlobalsThreadModel>,
    public IRsUnknownNode,
	public IRnHeightField
{
public:
	CRnHeightField();

    BEGIN_COM_MAP(CRnHeightField)
        COM_INTERFACE_ENTRY(IRnHeightField)
        COM_INTERFACE_ENTRY(IRsUnknownNode)
        COM_INTERFACE_ENTRY2(IUnknown, IRsUnknownNode)
        COM_INTERFACE_ENTRY2(IRsObject, IRsUnknownNode)
        COM_INTERFACE_ENTRY_AGGREGATE(DEF_GUIDNAME(IRsBaseNode), m_pUnkAgg.p)
        COM_INTERFACE_ENTRY_AGGREGATE_BLIND(m_pUnkAgg.p)    // Just for safety, the speed is slower only for unknown interfaces
    END_COM_MAP()

    DECLARE_GET_CONTROLLING_UNKNOWN()
	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct();

	void FinalRelease() 
	{
        m_pUnkAgg.Release();
    }

public:
    // IRsObject
    RS_DECLARE_IRS_OBJECT(IRnHeightField, L"Height field")

    // IRsUnknownNode
    STDMETHOD(OnGetValue)(CONID con_id, IRsDataCache* pDataCache, VARIANT* pVtData, RtBool* pHandled);
    STDMETHOD(OnSetValue)(CONID con_id, IRsDataCache* pDataCache, VARIANT* pvtData, RtBool* pHandled);
    STDMETHOD(OnDefaultValue)(CONID con_id, VARIANT* pVtData, RtBool* pHandled);
    STDMETHOD(OnInvalidate)(CONID con_id, RtBool* pHandled);
    STDMETHOD(OnInitialize)(void);
    STDMETHOD(OnLoadDefault)(void);
    STDMETHOD(OnDestroy)(void);

private:
    CComPtr<IUnknown> m_pUnkAgg;

	//main mesh generate function
	void GetMesh(RtUlong segsx, RtUlong segsy, IRdBitmap *pHeightField, IRdMesh **ppMesh);
};

