本来はこんなにカクカクじゃないからね!
ヌルヌルだからね!
そして追加でエラー終了する前にそれぞれのエラーを表示するようにしました スパゲッティソースはこちら
#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;
}

0 件のコメント:
コメントを投稿