【遊戲開發】關於Direct X(二)繪製兩條白色線段、三角形、正方形

//Direct3D9頭文件
#include <d3d9.h>

//宏定義窗口類名字
#define WINDOW_CLASS "UGPDX"

//宏定義窗口名字
#define WINDOW_NAME "Drawing Lines"

//設置和創建Direct3D
bool InitializeD3D(HWND hWnd, bool fullscreen);

//創建將要繪製在屏幕上的物體
bool InitializeObjects();

//渲染圖形
void RenderScene();

//在程序退出時清除程序
void Shutdown();

//分別將Direct3D對象和Direct3D設備對象置爲空
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

//將頂點緩存置爲空
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

//定義Direct3D頂點結構
struct stD3DVertex
{
    //xyz座標
	float x, y, z;

    //顏色
	unsigned long color;
};

//宏定義靈活頂點格式
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE);
bool InitializeObject()
{
    //指定顏色爲白色
	unsigned long col = D3DCOLOR_XRGB(255, 255, 255);

    //爲了創建2條線段,需要指定4個點,以及每個點的x,y,z位置座標和顏色值
	stD3DVertex objData[] = {
		{ 420.0f, 150.0f, 0.5f, col, },
		{ 420.0f, 350.0f, 0.5f, col, },
		{ 220.0f, 150.0f, 0.5f, col, },
		{ 220.0f, 350.0f, 0.5f, col, },
	};
    //創建頂點緩存保存數據
	if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
		D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL)))return false;

    //聲明一個指針,Lock()函數可以獲取一個指向頂點緩存中已分配內存的指針,進而操作該指針
	void *ptr;

    //鎖定頂點緩存
	if (FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0)))return false;

    //將數據複製到頂點緩存中
	memcpy(ptr, objData, sizeof(objData));

    //將頂點緩存設置爲準備就緒
	g_VertexBuffer->Unlock();

	return true;
}
void RenderScene()
{
    //清屏
	g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    //開始渲染
	g_D3DDevice->BeginScene();
    
    //將頂點設置爲渲染流
	g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));

    //設置頂點格式FVF
	g_D3DDevice->SetFVF(D3DFVF_VERTEX);

    //繪製出當前設置爲流的頂點緩存內容
	g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2);

    //結束渲染
	g_D3DDevice->EndScene();
    
    //顯示
	g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}
void Shutdown()
{
    //釋放D3D設備對象
	if (g_D3DDevice != NULL)g_D3DDevice->Release();

    //釋放D3D對象
	if (g_D3D != NULL)g_D3D->Release();

    //釋放頂點緩存
	if (g_VertexBuffer != NULL)g_VertexBuffer->Release();

    //分別置爲空
	g_D3DDevice = NULL;
	g_D3D = NULL;
	g_VertexBuffer = NULL;
}
//完整代碼:繪製兩條白色平行線

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

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

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME  "Drawing Lines"

bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();

LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

struct stD3DVertex
{
	float x, y, z, rhw;
	unsigned long color;
};

#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if (wp == VK_ESCAPE) PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wp, lp);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
	LPSTR cmdLine, int show)
{
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
		0, 0, GetModuleHandle(NULL), NULL, NULL,
		NULL, NULL, WINDOW_CLASS, NULL };
	RegisterClassEx(&wc);

	HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
		WS_OVERLAPPEDWINDOW, 100, 100,
		640, 480, GetDesktopWindow(), NULL,
		wc.hInstance, NULL);

	if (InitializeD3D(hWnd, false))
	{
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		MSG msg;
		ZeroMemory(&msg, sizeof(msg));

		while (msg.message != WM_QUIT)
		{
			if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
				RenderScene();
		}
	}

	Shutdown();

	UnregisterClass(WINDOW_CLASS, wc.hInstance);
	return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
	D3DDISPLAYMODE displayMode;

	g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (g_D3D == NULL) return false;


	if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
		&displayMode))) return false;


	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));


	if (fullscreen)
	{
		d3dpp.Windowed = FALSE;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
	}
	else
		d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = displayMode.Format;


	if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&d3dpp, &g_D3DDevice))) return false;

	if (!InitializeObjects()) return false;

	return true;
}


bool InitializeObjects()
{
	unsigned long col = D3DCOLOR_XRGB(255, 255, 255);
    
    //兩條平行線的座標及顏色
	stD3DVertex objData[] =
	{
		{ 420.0f, 150.0f, 0.5f, 1.0f, col, },
		{ 420.0f, 350.0f, 0.5f, 1.0f, col, },
		{ 220.0f, 150.0f, 0.5f, 1.0f, col, },
		{ 220.0f, 350.0f, 0.5f, 1.0f, col, },
	};

	if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
		D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
		NULL))) return false;

	void *ptr;

	if (FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
		(void**)&ptr, 0))) return false;

	memcpy(ptr, objData, sizeof(objData));

	g_VertexBuffer->Unlock();

	return true;
}


void RenderScene()
{
	g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
		D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_D3DDevice->BeginScene();

	g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,
		sizeof(stD3DVertex));

	g_D3DDevice->SetFVF(D3DFVF_VERTEX);

    //繪製兩條線段圖元
	g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2);

	g_D3DDevice->EndScene();

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


void Shutdown()
{
	if (g_D3DDevice != NULL) g_D3DDevice->Release();

	if (g_D3D != NULL) g_D3D->Release();

	if (g_VertexBuffer != NULL) g_VertexBuffer->Release();

	g_D3DDevice = NULL;

	g_D3D = NULL;

	g_VertexBuffer = NULL;
}

運行結果:

 

//完整代碼:繪製一個白色三角形

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

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

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME  "Drawing Triangle"

bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();

LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

struct stD3DVertex
{
	float x, y, z, rhw;
	unsigned long color;
};

#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if (wp == VK_ESCAPE) PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wp, lp);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
	LPSTR cmdLine, int show)
{
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
		0, 0, GetModuleHandle(NULL), NULL, NULL,
		NULL, NULL, WINDOW_CLASS, NULL };
	RegisterClassEx(&wc);

	HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
		WS_OVERLAPPEDWINDOW, 100, 100,
		640, 480, GetDesktopWindow(), NULL,
		wc.hInstance, NULL);

	if (InitializeD3D(hWnd, false))
	{
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		MSG msg;
		ZeroMemory(&msg, sizeof(msg));

		while (msg.message != WM_QUIT)
		{
			if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
				RenderScene();
		}
	}

	Shutdown();

	UnregisterClass(WINDOW_CLASS, wc.hInstance);
	return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
	D3DDISPLAYMODE displayMode;

	g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (g_D3D == NULL) return false;


	if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
		&displayMode))) return false;


	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));


	if (fullscreen)
	{
		d3dpp.Windowed = FALSE;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
	}
	else
		d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = displayMode.Format;


	if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&d3dpp, &g_D3DDevice))) return false;

	if (!InitializeObjects()) return false;

	return true;
}


bool InitializeObjects()
{
	unsigned long col = D3DCOLOR_XRGB(255, 255, 255);
    
    //此處有變動,三點相連
	stD3DVertex objData[] =
	{
		{ 320.0f, 150.0f, 0.5f, 1.0f, col, },
		{ 420.0f, 350.0f, 0.5f, 1.0f, col, },
		{ 220.0f, 350.0f, 0.5f, 1.0f, col, },
	};

	if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
		D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
		NULL))) return false;

	void *ptr;

	if (FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
		(void**)&ptr, 0))) return false;

	memcpy(ptr, objData, sizeof(objData));

	g_VertexBuffer->Unlock();

	return true;
}


void RenderScene()
{
	g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
		D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_D3DDevice->BeginScene();

	g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,
		sizeof(stD3DVertex));

	g_D3DDevice->SetFVF(D3DFVF_VERTEX);
    
    //此處有變動,1個三角形圖元
	g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	g_D3DDevice->EndScene();

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


void Shutdown()
{
	if (g_D3DDevice != NULL) g_D3DDevice->Release();

	if (g_D3D != NULL) g_D3D->Release();

	if (g_VertexBuffer != NULL) g_VertexBuffer->Release();

	g_D3DDevice = NULL;

	g_D3D = NULL;

	g_VertexBuffer = NULL;
}

運行結果:

 

//完整代碼:繪製一個白色正方形

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

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

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME  "Drawing Lines"

bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();

LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

struct stD3DVertex
{
	float x, y, z, rhw;
	unsigned long color;
};

#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if (wp == VK_ESCAPE) PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wp, lp);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
	LPSTR cmdLine, int show)
{
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
		0, 0, GetModuleHandle(NULL), NULL, NULL,
		NULL, NULL, WINDOW_CLASS, NULL };
	RegisterClassEx(&wc);

	HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
		WS_OVERLAPPEDWINDOW, 100, 100,
		640, 480, GetDesktopWindow(), NULL,
		wc.hInstance, NULL);

	if (InitializeD3D(hWnd, false))
	{
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		MSG msg;
		ZeroMemory(&msg, sizeof(msg));

		while (msg.message != WM_QUIT)
		{
			if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
				RenderScene();
		}
	}

	Shutdown();

	UnregisterClass(WINDOW_CLASS, wc.hInstance);
	return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
	D3DDISPLAYMODE displayMode;

	g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (g_D3D == NULL) return false;


	if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
		&displayMode))) return false;


	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));


	if (fullscreen)
	{
		d3dpp.Windowed = FALSE;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
	}
	else
		d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = displayMode.Format;


	if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&d3dpp, &g_D3DDevice))) return false;

	if (!InitializeObjects()) return false;

	return true;
}


bool InitializeObjects()
{
	unsigned long col = D3DCOLOR_XRGB(255, 255, 255);

	//此處有變動,四點依次相連
	stD3DVertex objData[] =
	{
		{ 420.0f, 150.0f, 0.5f, 1.0f, col, },
		{ 420.0f, 350.0f, 0.5f, 1.0f, col, },
		{ 220.0f, 150.0f, 0.5f, 1.0f, col, },
		{ 220.0f, 350.0f, 0.5f, 1.0f, col, },
	};

	if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
		D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
		NULL))) return false;

	void *ptr;

	if (FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
		(void**)&ptr, 0))) return false;

	memcpy(ptr, objData, sizeof(objData));

	g_VertexBuffer->Unlock();

	return true;
}


void RenderScene()
{
	g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
		D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_D3DDevice->BeginScene();

	g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,
		sizeof(stD3DVertex));

	g_D3DDevice->SetFVF(D3DFVF_VERTEX);

	//此處有變動,2個三角形圖元(每3個頂點一組繪製2個三角形,由座標可知爲正方形)
	g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

	g_D3DDevice->EndScene();

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


void Shutdown()
{
	if (g_D3DDevice != NULL) g_D3DDevice->Release();

	if (g_D3D != NULL) g_D3D->Release();

	if (g_VertexBuffer != NULL) g_VertexBuffer->Release();

	g_D3DDevice = NULL;

	g_D3D = NULL;

	g_VertexBuffer = NULL;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章