实时3D地形引擎——Chapter6_01——初读

来源:互联网 发布:会计帮软件下载 编辑:程序博客网 时间:2024/06/16 00:37

        坑已经开几天了,就从今天开始吧。书的第六章只有一个例子,而且不长,但实际上这个例子用到的框架也需要阅读,所以估计也要看很久。

class cMyHost:public cGameHost{public:cMyHost(){};~cMyHost(){};HRESULT InitDeviceObjects();HRESULT RestoreDeviceObjects();HRESULT InvalidateDeviceObjects();HRESULT DeleteDeviceObjects();HRESULT updateScene();HRESULT renderScene();HRESULT OneTimeSceneInit();     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );cTerrain m_terrainSystem;cTexture* m_pHeightMap;cRenderMethod* m_pRenderMethod;cSurfaceMaterial* m_pSurfaceMaterial;};

以上就是这章用到的类,可以看到该类继承了cGameHost,所以接下来看看cGameHost类。
class cGameHost : public CD3DApplication, public cSingleton<cGameHost>{public:// Creators...    cGameHost();    ~cGameHost();// Mutators...const cSystemInfo& systemInfo()const;cResourcePoolManager& resourceManager();cDisplayManager& displayManager();cLightManager& lightManager();void setupWorldQuadTree(const cRect3d& worldExtents);    const D3DPRESENT_PARAMETERS& presentParams()const;  // Parameters for CreateDevice/Reset    HWND  appWindow()const;            // The main app window    HWND  focusWindow()const;          // The D3D focus window (usually same as m_hWnd)    LPDIRECT3D9 d3dInterface()const;    LPDIRECT3DDEVICE9 d3dDevice()const;   // The D3D rendering device    const D3DCAPS9& d3dCaps()const;    // Caps for the device    const D3DSURFACE_DESC& d3dsdBackBuffer()const; // Surface desc of the backbuffer    const RECT& rcWindowClient()const; // Saved client area size for mode switches    const TCHAR* windowTitle()const;    bool hardwareVertexShadersAllowed()const;    void setActiveCamera(cCamera* pCamera);    void beginRenderStage(uint8 stage);    void endRenderStage();const tchar* rootPath()const {return m_rootPath;}cCamera& defaultCamera() {return m_defaultCamera;}cCamera* activeCamera() {return m_pActiveCamera;}cSceneNode& rootNode() {return m_rootNode;}cQuadTree& quadTree() {return m_quadTree;}cRect3d& worldExtents() {return m_worldExtents;}uint32 currentRenderStage()const {return m_activeRenderStage;}protected:virtual HRESULT OneTimeSceneInit();virtual HRESULT InitDeviceObjects();virtual HRESULT RestoreDeviceObjects();virtual HRESULT InvalidateDeviceObjects();virtual HRESULT DeleteDeviceObjects();virtual HRESULT Render();virtual HRESULT FrameMove();virtual HRESULT FinalCleanup();    virtual HRESULT ConfirmDevice(D3DCAPS9* pCaps,DWORD behavior,D3DFORMAT display,D3DFORMAT backbuffer);virtual HRESULT updateScene();virtual HRESULT renderScene();// helper function to provide basic user inputvirtual void updateCamera(float lateralSpeed, float rotationSpeed, cTerrain* pTerrain = 0,float groundOffset = 0,bool forceUpdate = 0);private:// Private Data...cSystemInfo m_systemInfo;CD3DFont* m_pFont;                    floatm_updateTimeCount;uint32 m_activeRenderStage;cResourcePoolManagerm_resourceManager;cDisplayManagerm_displayManager;cLightManagerm_lightManager;cSceneNodem_rootNode;cCameram_defaultCamera;cCamera*m_pActiveCamera;cQuadTreem_quadTree;cRect3dm_worldExtents;tcharm_rootPath[MAX_PATH];boolm_frameReady;// Invalid Functions...    cGameHost(const cGameHost& Src);    cGameHost& operator=(const cGameHost& Src);};
这一次的代码的确非常多。然后就是再次的继承,CD3DApplication和cSingleton。Singleton就不用说了,再次看看CD3DApplication。(看来这一章要分好几篇了,涉及到两个框架的阅读。。。)

//-----------------------------------------------------------------------------// Name: class CD3DApplication// Desc: A base class for creating sample D3D9 applications. To create a simple//       Direct3D application, simply derive this class into a class (such as//       class CMyD3DApplication) and override the following functions, as //       needed://          OneTimeSceneInit()    - To initialize app data (alloc mem, etc.)//          InitDeviceObjects()   - To initialize the 3D scene objects//          FrameMove()           - To animate the scene//          Render()              - To render the scene//          DeleteDeviceObjects() - To cleanup the 3D scene objects//          FinalCleanup()        - To cleanup app data (for exitting the app)//          MsgProc()             - To handle Windows messages//-----------------------------------------------------------------------------class CD3DApplication{protected:    CD3DEnumeration   m_d3dEnumeration;    CD3DSettings      m_d3dSettings;    // Internal variables for the state of the app    bool              m_bWindowed;    bool              m_bActive;    bool              m_bDeviceLost;    bool              m_bMinimized;    bool              m_bMaximized;    bool              m_bIgnoreSizeChange;    bool              m_bDeviceObjectsInited;    bool              m_bDeviceObjectsRestored;    // Internal variables used for timing    bool              m_bFrameMoving;    bool              m_bSingleStep;    // Internal error handling function    HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );    // Internal functions to manage and render the 3D scene    static bool ConfirmDeviceHelper( D3DCAPS9* pCaps, VertexProcessingType vertexProcessingType, D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat );    void    BuildPresentParamsFromSettings();    bool    FindBestWindowedMode( bool bRequireHAL, bool bRequireREF );    bool    FindBestFullscreenMode( bool bRequireHAL, bool bRequireREF );    HRESULT ChooseInitialD3DSettings();    HRESULT Initialize3DEnvironment();    HRESULT HandlePossibleSizeChange();    HRESULT Reset3DEnvironment();    HRESULT ToggleFullscreen();    HRESULT ForceWindowed();    HRESULT UserSelectNewDevice();    void    Cleanup3DEnvironment();    HRESULT Render3DEnvironment();    virtual HRESULT AdjustWindowForChange();    virtual void UpdateStats();protected:    // Main objects used for creating and rendering the 3D scene    D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset    HWND              m_hWnd;              // The main app window    HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)    HMENU             m_hMenu;             // App menu bar (stored here when fullscreen)    LPDIRECT3D9       m_pD3D;              // The main D3D object    LPDIRECT3DDEVICE9 m_pd3dDevice;        // The D3D rendering device    D3DCAPS9          m_d3dCaps;           // Caps for the device    D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer    DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing    DWORD             m_dwWindowStyle;     // Saved window style for mode switches    RECT              m_rcWindowBounds;    // Saved window bounds for mode switches    RECT              m_rcWindowClient;    // Saved client area size for mode switches    // Variables for timing    FLOAT             m_fTime;             // Current time in seconds    FLOAT             m_fElapsedTime;      // Time elapsed since last frame    FLOAT             m_fFPS;              // Instanteous frame rate    TCHAR             m_strDeviceStats[90];// String to hold D3D device stats    TCHAR             m_strFrameStats[90]; // String to hold frame stats    // Overridable variables for the app    TCHAR*            m_strWindowTitle;    // Title for the app's window    DWORD             m_dwCreationWidth;   // Width used to create window    DWORD             m_dwCreationHeight;  // Height used to create window    bool              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen    bool              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen    bool              m_bStartFullscreen;  // Whether to start up the app in fullscreen mode    // Overridable functions for the 3D scene created by the app    virtual HRESULT ConfirmDevice(D3DCAPS9*,DWORD,D3DFORMAT,D3DFORMAT) { return S_OK; }    virtual HRESULT OneTimeSceneInit()                         { return S_OK; }    virtual HRESULT InitDeviceObjects()                        { return S_OK; }    virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }    virtual HRESULT FrameMove()                                { return S_OK; }    virtual HRESULT Render()                                   { return S_OK; }    virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }    virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }    virtual HRESULT FinalCleanup()                             { return S_OK; }public:    // Functions to create, run, pause, and clean up the application    virtual HRESULT Create( HINSTANCE hInstance );    virtual INT     Run();    virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );    virtual void    Pause( bool bPause );    virtual         ~CD3DApplication()                         { }    // Internal constructor    CD3DApplication();};

这次我把注释也贴上来了,因为这个注释很重要。从这个注释中我们也可以知道这个类的作用。限于篇幅,接下来的内容将新开一篇。下一篇是D3DAPP框架的阅读。