ラベル DirectX の投稿を表示しています。 すべての投稿を表示
ラベル DirectX の投稿を表示しています。 すべての投稿を表示

2012年5月23日水曜日

【C++】DirectXで円柱x3を回転+α【そして四歩目】

こんな感じでぐーるぐる



本来はこんなにカクカクじゃないからね!
ヌルヌルだからね!

そして追加でエラー終了する前にそれぞれのエラーを表示するようにしました スパゲッティソースはこちら
#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)
#define WS_GAMEWINDOW (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU)

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

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

LRESULT CALLBACK MsgProc( 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;
 WNDCLASSEX wcex = {};
 
 
 
 //①ウィンドウクラスの定義・登録
 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style = CS_CLASSDC; //スタイル
 wcex.lpfnWndProc = MsgProc; //メッセージ処理関数
 wcex.cbClsExtra = 0L;
 wcex.cbWndExtra = 0L;
 wcex.hInstance = GetModuleHandle(NULL); //プログラムのハンドル
 wcex.hIcon = NULL; //アイコン
 wcex.hCursor = NULL; //カーソル
 wcex.hbrBackground = NULL; //背景色
 wcex.lpszMenuName = NULL; //メニュー
 wcex.lpszClassName = _T("D3D Tutorial"); //クラス名
 if( !RegisterClassEx( &wcex ) )
 {
  MessageBox(hWnd, _T("_TWinMain Error!"), _T("Failed to RegisterClassEx( &wcex )."), MB_OK|MB_ICONSTOP );
  return 0;
 }

 //②ウィンドウの作成
 if( !(hWnd = CreateWindow
 (
  _T("D3D Tutorial"),      // 登録されているクラス名
  _T("Test by aoisensi."), // ウィンドウ名
  WS_GAMEWINDOW,           // ウィンドウスタイル
  CW_USEDEFAULT,           // ウィンドウの横方向の位置
  CW_USEDEFAULT,           // ウィンドウの縦方向の位置
  640,                     // ウィンドウの幅
  480,                     // ウィンドウの高さ
  NULL,                    // 親ウィンドウまたはオーナーウィンドウのハンドル
  NULL,                    // メニューハンドルまたは子ウィンドウ ID
  wcex.hInstance,          // アプリケーションインスタンスのハンドル
  NULL                     // ウィンドウ作成データ
 ) ) )
 {
  MessageBox(hWnd, _T("_TWinMain Error!"), _T("Failed to CreateWindow."), MB_OK|MB_ICONSTOP );
  return 0;
 }
 
 //③ウィンドウを表示
 if( SUCCEEDED( InitD3D( hWnd ) ) )
 {
  if( SUCCEEDED( InitGeometry() ) )
  {
   ShowWindow( hWnd, SW_SHOWDEFAULT );
   UpdateWindow( hWnd );

   ZeroMemory( &msg, sizeof(msg) );

   //④メインループ
   while( msg.message!=WM_QUIT )
   {
    if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    {
     TranslateMessage( &msg );
     DispatchMessage( &msg );
    }
    else
    {
     Render();
    }
   }
  }
  else
  {
   MessageBox(hWnd, _T("_tWinMain Error!"), _T("Failed to InitGeometry."), MB_OK|MB_ICONSTOP );
  }
 }

 UnregisterClass( _T("D3D Tutorial"), wcex.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 ); //一つ目のトイレットペーパーの芯

  D3DXMATRIXA16 matWorld, curWorld;
  g_pd3dDevice->GetTransform( D3DTS_WORLD, &curWorld);
  D3DXMatrixTranslation( &matWorld, -3,0,2 );
  curWorld = curWorld * matWorld;
  g_pd3dDevice->SetTransform( D3DTS_WORLD, &curWorld );
  g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2*50-2 ); //二つ目のトイレットペーパーの芯

  D3DXMatrixTranslation( &matWorld,  3,0,2 );
  curWorld = curWorld * matWorld;
  g_pd3dDevice->SetTransform( D3DTS_WORLD, &curWorld );
  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 WINAPI MsgProc( 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 ) ) )
 {
  MessageBox(hWnd, _T("InitD3D Error!"), _T("Null to Direct3DCreate9."), MB_OK|MB_ICONSTOP );
  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;
 d3dpp.BackBufferWidth = 640;
 d3dpp.BackBufferHeight = 480;

 

 if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )
 {
  MessageBox(hWnd, _T("InitD3D Error!"), _T("Failed to CreateDevice."), MB_OK|MB_ICONSTOP );
  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;
}

【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

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

【C++】DirectXがうまくいかない…

こんな感じでスパゲッティコード作ったんですがうまくコンパイルできない…
エロイ人教えて!

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "wininet.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"), // ウィンドウ名
  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;
}

エラー内容
1>------ ビルド開始: プロジェクト: p1, 構成: Debug Win32 ------
1>main.obj : error LNK2019: 未解決の外部シンボル _D3DXVec3Normalize@8 が関数 "void __cdecl SetupLights(void)" (?SetupLights@@YAXXZ) で参照されました。
1>main.obj : error LNK2019: 未解決の外部シンボル __imp__timeGetTime@0 が関数 "void __cdecl SetupLights(void)" (?SetupLights@@YAXXZ) で参照されました。
1>main.obj : error LNK2019: 未解決の外部シンボル _D3DXMatrixPerspectiveFovLH@20 が関数 "void __cdecl SetupMatrices(void)" (?SetupMatrices@@YAXXZ) で参照されました。
1>main.obj : error LNK2019: 未解決の外部シンボル _D3DXMatrixLookAtLH@16 が関数 "void __cdecl SetupMatrices(void)" (?SetupMatrices@@YAXXZ) で参照されました。
1>main.obj : error LNK2019: 未解決の外部シンボル _D3DXMatrixRotationX@8 が関数 "void __cdecl SetupMatrices(void)" (?SetupMatrices@@YAXXZ) で参照されました。
1>C:\Users\aoisensi\Documents\Visual Studio 2010\DirectX\p1\Debug\p1.exe : fatal error LNK1120: 外部参照 5 が未解決です。
========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========
わかる方いればよろしくお願いします

2012年5月22日火曜日

【C++】DirectX初期化まで【次の二歩目】

前回の記事と見比べると、どの部分がDirectX初期化についてかよく分かると思います
http://aoisensi.blogspot.jp/2012/05/cdirectx_22.html


#pragma comment(lib, "d3d9.lib")

#include<windows.h>
#include<TCHAR.h>
#include<d3d9.h>

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


LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
void Render();
void Cleanup();
HRESULT InitD3D( HWND );

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"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );
 
 //③ウィンドウを表示

 if( SUCCEEDED( InitD3D( hWnd ) ) )
 {

  ShowWindow( hWnd, nShowCmd );
  UpdateWindow( hWnd );

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

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

 return 0;
}

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) );
 }
}

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

 if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
 {
  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();
 }
}

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;

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

メモ程度なので、コメントは残していません

【C++】DirectX用のウィンドウの作成【はじめの一歩目】

とりあえず、ソースコードから

#include<windows.h>
#include<TCHAR.h>

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

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"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );
 
 //③ウィンドウを表示
 ShowWindow( hWnd, nShowCmd );
 UpdateWindow( hWnd );

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

 return 0;
}

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

http://com.nicovideo.jp/community/co248498

こちらの生放送で教えて頂きました
とりあえず、メモ程度に

2012年5月20日日曜日

【C++】初めて参考書買った【DirectX】

こちらの商品をジュンク堂で買って来ました













ですが、
ぶっちゃけ、全然わからない…
まず、フォームから作れない
なんでなんだろ
難しいね