//----------------------------------------------------------------------------- // File: Text3D.cpp // // Desc: Example code showing how to do text in a Direct3D scene. // // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #define STRICT #include #include #include #include #include #include #include #include "D3DApp.h" #include "D3DFont.h" #include "D3DUtil.h" #include "DXUtil.h" #include #include #include "D3DRes.h" #include "main.h" #include "defines.h" #include "CCamera.h" #include "CText.h" #include "CLight.h" #include "SkinnedModel.h" #include "CModel.h" #include "CObject.h" #include "CBillBoard.h" #include "CTexture.h" #pragma comment(lib,"d3d8.lib") #pragma comment(lib,"d3dx8dt.lib") #pragma comment(lib,"d3dxof.lib") #pragma comment(lib,"dxguid.lib") #pragma comment(lib,"winmm.lib") //----------------------------------------------------------------------------- // Name: WinMain() // Desc: Entry point to the program. Initializes everything, and goes into a // message-processing loop. Idle time is used to render the scene. //----------------------------------------------------------------------------- CMyD3DApplication *d3dApp=NULL; CCameraFactory CameraFactory; CTextFactory TextFactory; CLightFactory LightFactory; CObjectFactory ObjectFactory; CBillBoardFactory BillBoardFactory; CTextureFactory TextureFactory; //CModel testModel; INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { d3dApp = new CMyD3DApplication(); if( FAILED( d3dApp->Create( hInst ) ) ) return 0; return d3dApp->Run(); delete d3dApp; } //----------------------------------------------------------------------------- // Name: ConfirmDevice() // Desc: Called during device intialization, this code checks the device // for some minimum set of capabilities //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format ) { // This sample wants mixed vertex processing rather than hardware // vertex processing so it can fallback to sw processing if the // device supports fewer than three matrices when skinning. if( dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ==0) return E_FAIL; return S_OK; } //----------------------------------------------------------------------------- // Name: CMyD3DApplication() // Desc: Application constructor. Sets attributes for the app. //----------------------------------------------------------------------------- CMyD3DApplication::CMyD3DApplication() { m_strWindowTitle = _T("ZRender"); m_bUseDepthBuffer = TRUE; ZeroMemory( m_bKey, 256 ); } //----------------------------------------------------------------------------- // Name: OneTimeSceneInit() // Desc: Called during initial app startup, this function performs all the // permanent initialization. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::OneTimeSceneInit() { srand(time(0)); CameraFactory.CreateCamera(); TextFactory.CreateText(); LightFactory.CreateLight(); return S_OK; } //----------------------------------------------------------------------------- // Name: FrameMove() // Desc: Called once per frame, the call is the entry point for animating // the scene. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::FrameMove() { D3DXMATRIX matWorld; D3DXMatrixIdentity(&matWorld); m_pd3dDevice->SetTransform( D3DTS_WORLD,&matWorld ); return S_OK; } //----------------------------------------------------------------------------- // Name: Render() // Desc: Called once per frame, the call is the entry point for 3d // rendering. This function sets up render states, clears the // viewport, and renders the scene. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::Render() { // Clear the viewport m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(89,135,179), 1.0f, 0L ); // Begin the scene if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) { static double zpos=0,xpos=0; if(m_bKey[VK_DOWN]) zpos+=.1; else if(m_bKey[VK_UP]) zpos-=.1; if(m_bKey[VK_RIGHT]) xpos+=.1; else if(m_bKey[VK_LEFT]) xpos-=.1; CLight *cl=LightFactory.GetCurrentLight(); cl->SetPosition(CameraFactory.GetCurrentCamera()->GetLocation()); cl->SetDirection(D3DXVECTOR3(0,0,0)-cl->GetPosition()); cl->SetType(D3DLIGHT_SPOT); cl->SpotDefault(); cl->Update(); cl->Enable(); CCamera *pCamera = CameraFactory.GetCurrentCamera(); pCamera->SetLocation(D3DXVECTOR3(10+xpos,25+zpos,10)); pCamera->SetDestination(D3DXVECTOR3(0,0,0)); pCamera->SetUpVector(D3DXVECTOR3(0,1,0)); pCamera->Enable(); pCamera->UpdatePerspective(); //TextureFactory.GetCurrentTexture()->ActivateTexture(); ObjectFactory.GetCurrentObject()->Render(); //display fps CText *pText = TextFactory.GetCurrentText(); static long curVal = 0; curVal++; pText->SetText("fps: %2.2f",m_fFPS); pText->SetPosition(D3DXVECTOR3(0,0,0)); pText->SetColor(D3DCOLOR_XRGB(0,0,0)); pText->Render2d(); pText->SetPosition(D3DXVECTOR3(1,1,0)); pText->SetColor(D3DCOLOR_XRGB(255,0,0)); pText->Render2d(); // End the scene. m_pd3dDevice->EndScene(); } return S_OK; } //----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { // Restore the fonts TextFactory.GetCurrentText()->InitDevice(); //testModel.LoadMeshHierarchy("C:\\DXSDK\\samples\\Multimedia\\Media\\tiny.x"); //testModel.LoadMeshHierarchy("c:\\myd3d\\test1\\models\\box.x"); TextureFactory.CreateTexture()->Load("c:\\myd3d\\test1\\models\\ball.dds"); CObject *pobj=ObjectFactory.CreateObject(); pobj->CreateGrid(100,100); pobj->SetColor(0,0,1); pobj->SetPosition(D3DXVECTOR3(0,0,0)); return S_OK; } //----------------------------------------------------------------------------- // Name: RestoreDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::RestoreDeviceObjects() { // Restore the fonts TextFactory.GetCurrentText()->RestoreDevice(); m_pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); m_pd3dDevice->SetRenderState( D3DRS_COLORVERTEX, FALSE ); // Restore the textures m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE,TRUE); m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x80808080); CLight *cl=LightFactory.GetCurrentLight(); if(cl){ cl->SetType(D3DLIGHT_DIRECTIONAL); cl->SetPosition(D3DXVECTOR3(0,0,0)); cl->Update(); cl->Enable(TRUE); } D3DXMATRIX matWorld; D3DXMatrixIdentity( &matWorld ); m_pd3dDevice->SetTransform( D3DTS_WORLD,&matWorld ); CameraFactory.GetCurrentCamera()->RestoreAspectRatio(); return S_OK; } //----------------------------------------------------------------------------- // Name: InvalidateDeviceObjects() // Desc: Called when the app is exiting, or the device is being changed, // this function deletes any device dependent objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InvalidateDeviceObjects() { TextFactory.GetCurrentText()->InvalidateDevice(); //testModel.InvalidateDevice(); return S_OK; } //----------------------------------------------------------------------------- // Name: DeleteDeviceObjects() // Desc: Called when the app is exiting, or the device is being changed, // this function deletes any device dependent objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::DeleteDeviceObjects() { TextFactory.GetCurrentText()->DeleteDevice(); //testModel.DeleteDevice(); return S_OK; } //----------------------------------------------------------------------------- // Name: FinalCleanup() // Desc: Called before the app exits, this function gives the app the chance // to cleanup after itself. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::FinalCleanup() { return S_OK; } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: Message proc function to handle key and menu input //----------------------------------------------------------------------------- LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { if( WM_KEYDOWN == uMsg ) { m_bKey[wParam] = 1; } // Perform commands when keys are released if( WM_KEYUP == uMsg ) { m_bKey[wParam] = 0; if(wParam=='W'){ uMsg = WM_COMMAND; wParam = IDM_TOGGLEFULLSCREEN; } } if( WM_COMMAND == uMsg ) { } return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam ); }