//----------------------------------------------------------------- // // DirectX 3D // //----------------------------------------------------------------- //#define sqrtf sqrt //#define sinf sin //#define cosf cos //#define tanf tan #include #include #include #include #pragma comment (lib,"d3d9.lib") #pragma comment (lib,"d3dx9.lib") #pragma comment (lib,"dxguid.lib") //----------------------------------------------------------------- // Grobal Variables. //----------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; LPDIRECT3DTEXTURE9 g_pTexture=nullptr; //テクスチャ-インターフェ-ス用 /* struct CUSTOMVERTEX { FLOAT x , y , z; DWORD color; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE) */ struct CUSTOMVERTEX { FLOAT x , y , z; DWORD color; float u ,v; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 ) #define RADIAN(arc) (((arc) * D3DX_PI ) / 180) // ラジアンの計算に必要 //----------------------------------------------------------------- // Prototypes. //----------------------------------------------------------------- HWND InitApp( HINSTANCE , int ); BOOL InitDirect3D( HWND ); BOOL CleanupDirect3D(); BOOL InitGeometry(); BOOL SetupMatrices(); BOOL RenderDirect3D(); LRESULT CALLBACK WndProc( HWND , UINT , WPARAM , LPARAM ); MSG msg ; //----------------------------------------------------------------- // Main. //----------------------------------------------------------------- int WINAPI WinMain( HINSTANCE hInst , HINSTANCE hPrevinst , LPSTR nCmdLine , int nCmdShow ) { HWND hWnd ; hWnd = InitApp( hInst , nCmdShow ); if ( !hWnd ) return FALSE; if ( !InitDirect3D( hWnd ) ) return FALSE; if ( !InitGeometry() ) return FALSE; while( msg.message != WM_QUIT ){ if ( PeekMessage( &msg , NULL , 0 , 0 , PM_REMOVE )!= 0 ){ TranslateMessage( &msg ); DispatchMessage( &msg ); }else{ RenderDirect3D(); } Sleep( 1 ); } return msg.wParam; } //----------------------------------------------------------------- // Initialize Application. //----------------------------------------------------------------- HWND InitApp( HINSTANCE hInst , int nCmdShow ) { WNDCLASS wc; HWND hWnd; char szClassName[] = "Direct3D Test"; wc.style = CS_HREDRAW | CS_VREDRAW; wc.hInstance = hInst; wc.hCursor = LoadCursor( NULL , IDC_ARROW ); wc.hIcon = LoadIcon( NULL , IDI_APPLICATION ); wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH ); wc.lpszClassName = szClassName; wc.lpszMenuName = NULL; wc.lpfnWndProc = WndProc; wc.cbWndExtra = 0; wc.cbClsExtra = 0; if ( !RegisterClass( &wc ) ) return FALSE; hWnd = CreateWindow( szClassName , "Direct3D Test" , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT , 640 , 480, NULL , NULL , hInst , NULL ); if ( !hWnd ) return FALSE; ShowWindow( hWnd , nCmdShow ); UpdateWindow( hWnd ); return hWnd; } //----------------------------------------------------------------- // Initialize Direct3D. //----------------------------------------------------------------- BOOL InitDirect3D( HWND hWnd ) { D3DPRESENT_PARAMETERS d3dpp; HRESULT hr; if ( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ){ MessageBox( hWnd , "Can't create Direct3D." , "Error" , MB_OK ); return FALSE; } ZeroMemory( &d3dpp , sizeof( d3dpp ) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &d3dpp , &g_pd3dDevice ); if ( FAILED( hr ) ){ MessageBox( hWnd , "Can't create device." , "Error" , MB_OK ); return FALSE; } g_pd3dDevice->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice,"data\\TEXTURE\\Logo.png",&g_pTexture))) { MessageBox(NULL,"テクスチャのファイルの読み込みに失敗しました","失敗", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2); } return TRUE; } //----------------------------------------------------------------- // Cleanup Direct3D. //----------------------------------------------------------------- BOOL CleanupDirect3D() { if ( g_pVB != NULL ) g_pVB->Release(); if ( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if ( g_pD3D != NULL ) g_pD3D->Release(); return TRUE; } //----------------------------------------------------------------- // Window Proc. //----------------------------------------------------------------- LRESULT CALLBACK WndProc( HWND hWnd , UINT msg , WPARAM wp , LPARAM lp ) { switch( msg ){ case WM_DESTROY: CleanupDirect3D(); PostQuitMessage( 0 ); break; default: return DefWindowProc( hWnd , msg , wp , lp ); } return 0L; } //----------------------------------------------------------------- // Initialize Geometry. //----------------------------------------------------------------- BOOL InitGeometry() { HRESULT hr; VOID *pVertices; CUSTOMVERTEX Vertices[] = { { -1.0f , -1.0f , 5.0f , 0xFFFF00FF , 0.5f, 0.0f } , { 0.0f , 1.0f , 5.0f , 0xFFFFFF00 , 0.0f, 1.0f } , { 1.0f , -1.0f , 5.0f , 0xFF00FFFF , 1.0f, 1.0f } , }; hr = g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ) , 0 , D3DFVF_CUSTOMVERTEX , D3DPOOL_DEFAULT , &g_pVB , NULL ); if ( FAILED( hr ) ){ MessageBox( NULL , "Couldn't create vertex buffer" , "Error" , MB_OK ); } g_pVB->Lock( 0 , sizeof( Vertices ) , ( void** )&pVertices , 0 ); memcpy( pVertices , Vertices , sizeof( Vertices ) ); g_pVB->Unlock(); return TRUE; } //----------------------------------------------------------------- // Setup Matrices. //----------------------------------------------------------------- BOOL SetupMatrices() { D3DXMATRIXA16 matWorld , matView , matProj; D3DXVECTOR3 vEyePt , vLookatPt , vUpVec; // World Matrix. D3DXMatrixIdentity( &matWorld ); //D3DXMatrixRotationY( &matWorld, RADIAN(20) ); g_pd3dDevice->SetTransform( D3DTS_WORLD , &matWorld ); // Camera. vEyePt.x = 0.0f; vEyePt.y = 0.0f; vEyePt.z = 0.0f-5.0f; vLookatPt.x = 0.0f; vLookatPt.y = 0.0f; vLookatPt.z = 0.0f; vUpVec.x = 0.0f; vUpVec.y = 1.0f; vUpVec.z = 0.0f; D3DXMatrixLookAtLH( &matView , &vEyePt , &vLookatPt , &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW , &matView ); // Projection Matrix. D3DXMatrixPerspectiveFovLH( &matProj , 3.0f / 4.0f , 1.0f , 1.0f , 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION , &matProj ); return TRUE; } //----------------------------------------------------------------- // Render Direct3D. //----------------------------------------------------------------- BOOL RenderDirect3D() { RECT rc; D3DXVECTOR3 center , position; g_pd3dDevice->Clear( 0 , NULL , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , 0x00000000 , 1.0f , 0 ); g_pd3dDevice->BeginScene(); SetupMatrices(); g_pd3dDevice->SetStreamSource( 0 , g_pVB , 0 , sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->SetTexture(0,g_pTexture); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP , 0 , 1 ); g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL , NULL , NULL , NULL ); return TRUE; }