首页 > 代码库 > D3D 两个三角形旋转 小例子

D3D 两个三角形旋转 小例子

两个三角形围绕Y轴旋转。

程序

#pragma once

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

#include<d3d9.h>
#include<d3dx9.h>

struct CUSTOMVERTEX
{
	FLOAT x,y,z;
	DWORD color;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
HRESULT InitVariable(HWND hWnd);
void Render();
HRESULT InitD3D(HWND hWnd);
HRESULT InitVertexData();
void CleanUp();
void SetupMatrices();

LPDIRECT3D9 g_pd3d9=NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice9=NULL;
LPDIRECT3DVERTEXBUFFER9 g_pd3dVB=NULL;

INT WINAPI wWinMain(
	HINSTANCE,
	HINSTANCE,
	LPWSTR,
	INT)
{
	//initialize wnd class struct.
	WNDCLASSEX wcex;
	ZeroMemory(&wcex,sizeof(wcex));
	wcex.cbSize=sizeof(wcex);
	wcex.hInstance=GetModuleHandle(NULL);
	wcex.lpfnWndProc=MsgProc;
	wcex.style=CS_CLASSDC;
	wcex.lpszClassName=L"Self002";

	//register wnd class.
	RegisterClassEx(&wcex);

	//create window.
	HWND hWnd=CreateWindowEx(
		WS_EX_OVERLAPPEDWINDOW,
		L"Self002",
		L"Self002 Window",
		WS_OVERLAPPEDWINDOW,
		100,
		100,
		300,
		300,
		NULL,
		NULL,
		wcex.hInstance,
		NULL);

	//show window and enter message loop.
	if(SUCCEEDED(InitVariable(hWnd)))
	{
		//show window.
		ShowWindow(hWnd,SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		//enter message loop.
		MSG msg;
		ZeroMemory(&msg,sizeof(msg));
		while(msg.message != WM_QUIT)
		{
			if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				Render();
			}
		}
	}

	//unregister wnd class.
	UnregisterClass(L"Self002",wcex.hInstance);

	return 0;
}

LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		CleanUp();
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}

HRESULT InitVariable(HWND hWnd)
{
	if(FAILED(InitD3D(hWnd)))
	{
		return E_FAIL;
	}

	if(FAILED(InitVertexData()))
	{
		return E_FAIL;
	}

	return S_OK;
}

void Render()
{
	//TODO: render your sence.
	//clear render target.
	g_pd3dDevice9->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);

	if(SUCCEEDED(g_pd3dDevice9->BeginScene()))
	{
		SetupMatrices();

		g_pd3dDevice9->SetStreamSource(0,g_pd3dVB,0,sizeof(CUSTOMVERTEX));
		g_pd3dDevice9->SetFVF(D3DFVF_CUSTOMVERTEX);
		g_pd3dDevice9->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);

		g_pd3dDevice9->EndScene();
	}

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

HRESULT InitD3D(HWND hWnd)
{
	//TODO: initialize d3d variables.
	//create d3d
	g_pd3d9=Direct3DCreate9(D3D_SDK_VERSION);
	if(g_pd3d9 == NULL)
	{
		return E_FAIL;
	}

	//initialize d3d present parameters.
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.Windowed=TRUE;
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;

	//create d3d device.
	if(FAILED(g_pd3d9->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&g_pd3dDevice9)))
	{
		return E_FAIL;
	}

	//turn off culling.
	g_pd3dDevice9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);

	//turn off lighting.
	g_pd3dDevice9->SetRenderState(D3DRS_LIGHTING,FALSE);

	return S_OK;
}

HRESULT InitVertexData()
{
	//TODO:initialize your vertex data.
	//initialize vertex data.
	CUSTOMVERTEX vertices[6]=
	{
		{-1.0f,-1.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},
		{-1.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,0,255)},
		{0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},
		{0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},
		{1.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,0,255)},
		{1.0f,-1.0f,0.0f,D3DCOLOR_XRGB(255,0,0)}
	};

	//create vertex buffer.
	if(FAILED(g_pd3dDevice9->CreateVertexBuffer(
		sizeof(vertices),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL)))
	{
		return E_FAIL;
	}

	//fill data to vertex buffer.
	void* pVertices=NULL;
	if(FAILED(g_pd3dVB->Lock(0,sizeof(vertices),&pVertices,0)))
	{
		return E_FAIL;
	}
	memcpy(pVertices,vertices,sizeof(vertices));
	g_pd3dVB->Unlock();

	return S_OK;
}

void CleanUp()
{
	//TODO: clean up you variables.
	if(g_pd3dVB != NULL)
	{
		g_pd3dVB->Release();
	}

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

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

void SetupMatrices()
{
	//TODO: setuo transform matrix.
	//set world transform matrix.
	D3DXMATRIXA16 matWorld;
	UINT itimes=timeGetTime()%1000;
	FLOAT fAngle=itimes * ( 2.0f * D3DX_PI ) / 1000.0f;
	D3DXMatrixRotationY(&matWorld,fAngle);
	g_pd3dDevice9->SetTransform(D3DTS_WORLD,&matWorld);

	//set view transform matrix.
	D3DXMATRIXA16 matView;
	D3DXVECTOR3 vEye(0.0f,1.0f,-5.0f);
	D3DXVECTOR3 vLookAt(0.0f,0.0f,0.0f);
	D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
	D3DXMatrixLookAtLH(&matView,&vEye,&vLookAt,&vUp);
	g_pd3dDevice9->SetTransform(D3DTS_VIEW,&matView);

	//set projection transform matrix.
	D3DXMATRIXA16 matProj;
	D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f);
	g_pd3dDevice9->SetTransform(D3DTS_PROJECTION,&matProj);
}

运行结果

1.注意关闭光照和背面剔出。

2.主要练习坐标变换,但是仍然不是很理解。