2012年5月23日水曜日

【C++】DirectXがうまくいった!円柱の表示成功!【やっと三歩目】

とりあえずコードから

#include <windows.h>
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"winmm.lib")

#include<TCHAR.h>
#include<d3dx9.h>
#include<math.h>
#include<strsafe.h>


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

void Render();
void Cleanup();
void SetupLights();
void SetupMatrices();

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

HRESULT InitD3D( HWND );
HRESULT InitGeometry();
HRESULT SetStreamSource(UINT StreamNumber, IDirect3DVertexBuffer9 *pStreamData, UINT OffsetInBytes, UINT Stride );
HRESULT DrawPrimitive( D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount );



struct CUSTOMVERTEX
{
    D3DXVECTOR3 position; // The 3D position for the vertex
    D3DXVECTOR3 normal;   // The surface normal for the vertex
};


int WINAPI _tWinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpszCmdLine, int nShowCmd)
{
 HWND hWnd;
 MSG msg;
 WNDCLASS wc;
 
 
 
 //①ウィンドウクラスの定義・登録
 wc.style = CS_HREDRAW | CS_VREDRAW; //スタイル
 wc.lpfnWndProc = WndProc; //メッセージ処理関数
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hInstance = hInst; //プログラムのハンドル
 wc.hIcon = NULL; //アイコン
 wc.hCursor = LoadCursor( NULL, IDC_ARROW ); //カーソル
 wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH ); //背景色
 wc.lpszMenuName = NULL; //メニュー
 wc.lpszClassName = _T("test"); //クラス名 
 if( !RegisterClass( &wc ) ) 
 {
  return 0;
 }

 //②ウィンドウの作成
 hWnd = CreateWindow
 (
  _T("test"), // 登録されているクラス名
  _T("Test by aoisensi."), // ウィンドウ名
  WS_OVERLAPPEDWINDOW, // ウィンドウスタイル
  CW_USEDEFAULT, // ウィンドウの横方向の位置
  CW_USEDEFAULT, // ウィンドウの縦方向の位置
  640, // ウィンドウの幅
  480, // ウィンドウの高さ
  NULL, // 親ウィンドウまたはオーナーウィンドウのハンドル
  NULL, // メニューハンドルまたは子ウィンドウ ID
  hInst, // アプリケーションインスタンスのハンドル
  NULL // ウィンドウ作成データ
 );
 
 //③ウィンドウを表示

 if( SUCCEEDED( InitD3D( hWnd ) ) )
 {
  if( SUCCEEDED( InitGeometry() ) )
  {
   ShowWindow( hWnd, nShowCmd );
   UpdateWindow( hWnd );

   //④メインループ
   while( GetMessage( &msg, NULL, 0, 0 ) )
   {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
   }
  }
  else
  {
   Render();
  }
 }

 UnregisterClass( _T("D3D Tutorial"), wc.hInstance );

 return 0;
}


void Render()
{
 if( NULL == g_pd3dDevice )
 {
  return;
 }
 g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0x00, 0x00, 0xFF), 1.0f, 0 );

 if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
 {
  SetupLights();

  SetupMatrices();

  g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
  g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2*50-2 );

  g_pd3dDevice->EndScene();
 }

 g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

void Cleanup()
{
 if( g_pd3dDevice != NULL)
 {
  g_pd3dDevice->Release();
 }

 if ( g_pD3D != NULL)
 { 
  g_pD3D->Release();
 }
}

void SetupLights()
{
 D3DMATERIAL9 mtrl;
 ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
 mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
 mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
 mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;
 mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
 g_pd3dDevice->SetMaterial( &mtrl );

 D3DXVECTOR3 vecDir;
 D3DLIGHT9 light;
 ZeroMemory( &light, sizeof(D3DLIGHT9) );
 light.Type     = D3DLIGHT_DIRECTIONAL;
 light.Diffuse.r = 1.0f;
 light.Diffuse.g = 1.0f;
 light.Diffuse.b = 1.0f;
 vecDir = D3DXVECTOR3(cosf(timeGetTime()/350.0f),1.0f,sinf(timeGetTime()/350.0f) );
 D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
 light.Range = 1000.0f;
 g_pd3dDevice->SetLight( 0, &light );
 g_pd3dDevice->LightEnable( 0, true );
 g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, true );
 g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
}

void SetupMatrices()
{
 D3DXMATRIXA16 matWorld;
 D3DXMatrixIdentity( &matWorld );
 D3DXMatrixRotationX( &matWorld, timeGetTime()/500.0f );
 g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

 D3DXVECTOR3 vEyePt( 0.0f, 3.0f, -5.0f );
 D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
 D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
 
 D3DXMATRIXA16 matView;
 D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
 g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

 D3DXMATRIXA16 matProj;
 D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );

 g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}


LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
 switch( msg )
 {
  case WM_DESTROY:
   Cleanup();
   PostQuitMessage( 0 );
   return 0;
  case WM_PAINT:
   Render();
   ValidateRect( hWnd, NULL );
   return 0;
  default:
   return( DefWindowProc( hWnd, msg, wParam, lParam) );
 }
}


HRESULT InitD3D( HWND hWnd )
{
 if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
 {
  return E_FAIL;
 }

 D3DPRESENT_PARAMETERS d3dpp;
 ZeroMemory( &d3dpp, sizeof(d3dpp) );
 d3dpp.Windowed = true;
 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
 d3dpp.EnableAutoDepthStencil = true;
 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

 if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )
 {
  return E_FAIL;
 }

 g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

 g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, true );

 return S_OK;
}

HRESULT InitGeometry()
{
 if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
    0, D3DFVF_CUSTOMVERTEX,
    D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
 {
  return E_FAIL;
 }

 CUSTOMVERTEX* pVertices;
 if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
 {
  return E_FAIL;
 }
 for( DWORD i=0; i<50; i++ )
 {
  FLOAT theta = (2*D3DX_PI*i)/(50-1);
  pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) );
  pVertices[2*i+0].normal   = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
  pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) );
  pVertices[2*i+1].normal   = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
 }
 g_pVB->Unlock();

 return S_OK;
}

うまく行かなかった原因はDirectXSDK/Lib/x86をちゃんと入れてませんでした
http://www.se-land.com/chapter.php?cha_id=cha0000000366

よっしゃあああああああああああああああああああああああああああああああ

0 件のコメント:

コメントを投稿