หลงทางในโลกของ DirectX

posted on 17 Aug 2008 22:15 by xvista  in ComputerVista

*** จิ้ม F5 พลีสสสสส

 

ตอนนี้หันมาศึกษา DirectX อย่างเป็นล่ำเป็นสันล่ะ

ไปอ่านและศึกษาจากเว็บต่างประเทศ เว็บไรไม่รู้ บันทึกไว้ใน Bookmark

ทำไปทำมา ได้โค้ดแบบนี้...

 

ขนาดพิมพ์เองนะนี่

งง ได้ขนาดนี้...

 

+++++++++++++++++++++++++++++++


1// include the basic window header files and the Direct3D header file
2#include <windows.h>
3#include <windowsx.h>
4#include <d3d9.h>
5#include <d3dx9.h>
6
7// define the screen resolution and keyboard macros
8#define SCREEN_WIDTH 640
9#define SCREEN_HEIGHT 480
10#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
11#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
12
13// include the Direct3D Library file
14#pragma comment (lib, "d3d9.lib")
15#pragma comment (lib, "d3dx9.lib")
16
17// global declarations
18LPDIRECT3D9 d3d;	// the pointer to our Direct3D interface
19LPDIRECT3DDEVICE9 d3ddev;	// the pointer to the device class
20LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;	// the pointer to the vertex buffer
21
22// texture declarations
23LPDIRECT3DTEXTURE9 texture_1;	// our first texture
24
25// function prototypes
26void initD3D(HWND hWnd);	// sets up and initializes Direct3D
27void render_frame(void);	// renders a single frame
28void cleanD3D(void);	// closes Direct3D and releases memory
29void init_graphics(void);	// 3D declarations
30
31struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR; FLOAT U, V;};
32#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
33
34// the WindowProc function prototype
35LRESULT CALLBACK WindowProc(HWND hWnd,
36							UINT message,
37							WPARAM wParam,
38							LPARAM lParam);
39
40// the entry point for any Windows program
41int WINAPI WinMain(HINSTANCE hInstance,
42				   HINSTANCE hPrevInstance,
43				   LPSTR lpCmdLine,
44				   int nCmdShow)
45{
46	HWND hWnd;
47	WNDCLASSEX wc;
48
49	ZeroMemory(&wc, sizeof(WNDCLASSEX));
50
51	wc.cbSize = sizeof(WNDCLASSEX);
52	wc.style = CS_HREDRAW | CS_VREDRAW;
53	wc.lpfnWndProc = (WNDPROC)WindowProc;
54	wc.hInstance = hInstance;
55	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
56	// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;	// not needed any more
57	wc.lpszClassName = "WindowClass1";
58
59	RegisterClassEx(&wc);
60
61	hWnd = CreateWindowEx(NULL,
62						  "WindowClass1",
63						  "Our Direct3D Program",
64						  WS_EX_TOPMOST | WS_POPUP,	// fullscreen values
65						  0, 0,	// the starting x and y position should be 0
66						  SCREEN_WIDTH, SCREEN_HEIGHT,	// set the window to 640 x 480
67						  NULL,
68						  NULL,
69						  hInstance,
70						  NULL);
71
72	ShowWindow(hWnd, nCmdShow);
73	
74	// set up and initialize Direct3D
75	initD3D(hWnd);
76
77	// enter the main loop:
78
79	MSG msg;
80
81	while(TRUE)
82	{
83		DWORD starting_point = GetTickCount();
84
85		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
86		{
87			if (msg.message == WM_QUIT)
88				break;
89				TranslateMessage(&msg);
90				DispatchMessage(&msg);
91		}
92
93		render_frame();
94
95		// check the 'escape' key
96		if(KEY_DOWN(VK_ESCAPE))
97			PostMessage(hWnd, WM_DESTROY, 0, 0);
98
99		while((GetTickCount() - starting_point) < 25);
100	}
101
102	// clean up DirectX and COM
103	cleanD3D();
104
105	return msg.wParam;
106}
107
108// this is the main message handler for the program
109LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
110{
111	switch(message)
112	{
113		case WM_DESTROY:
114			{
115				PostQuitMessage(0);
116				return 0;
117			} break;
118	}
119
120	return DefWindowProc (hWnd, message, wParam, lParam);
121}
122
123// this function initializes and prepares Direct3D for use
124void initD3D(HWND hWnd)
125{
126	d3d = Direct3DCreate9(D3D_SDK_VERSION);	// create the Direct3D interface
127
128	D3DPRESENT_PARAMETERS d3dpp;	// create a struct to hold various device information
129
130	ZeroMemory(&d3dpp, sizeof(d3dpp));	// clear out the struct for use
131	d3dpp.Windowed = FALSE;	// program fullscreen, not windowed
132	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	// discard old frames
133	d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
134	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;	// set the back buffer format to 32-bit
135	d3dpp.BackBufferWidth = SCREEN_WIDTH;	// set the width of the buffer
136	d3dpp.BackBufferHeight = SCREEN_HEIGHT;	// set the height of the buffer
137	d3dpp.EnableAutoDepthStencil = TRUE;	// automatically run the z-buffer for us
138	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;	// 16-bit pixel format for the z-buffer
139
140	// create a device class using this information and the info from the d3dpp struct
141	d3d->CreateDevice(D3DADAPTER_DEFAULT,
142					  D3DDEVTYPE_HAL,
143					  hWnd,
144					  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
145					  &d3dpp,
146					  &d3ddev);
147
148	init_graphics();	// call the function to initialize the triangle
149
150	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);	// turn off the 3D lighting
151	d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);	// turn on the z-buffer
152
153	return;
154}
155
156// this is the function used to render a single frame
157void render_frame(void)
158{
159	// clear the window to a deep blue
160	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
161	d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
162
163	d3ddev->BeginScene();
164
165	// select which vertex format we are using
166	d3ddev->SetFVF(CUSTOMFVF);
167
168	D3DXMATRIX matView;	// the view transform matrix
169
170	D3DXMatrixLookAtLH(&matView,
171					   &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),	// the camera position
172					   &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),	// the look-at position
173					   &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));	// the up direction
174
175	d3ddev->SetTransform(D3DTS_VIEW, &matView);	// set the view transform to matView
176
177	D3DXMATRIX matProjection;	// the projection transform matrix
178
179	D3DXMatrixPerspectiveFovLH(&matProjection,
180							   D3DXToRadian(45),	// the horizontal field of view
181							   (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,	// aspect ratio
182							   1.0f,	// the near view-plane
183							   100.0f);	// the far view-plane
184
185	d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);	// set the projection
186
187	// select the vertex buffer to display
188	d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));
189
190	D3DXMATRIX matTranslateA;	// a matrix to store the translation for triangle A
191	D3DXMATRIX matTranslateB;	// a matrix to store the translation for triangle B
192	D3DXMATRIX matRotateYA;	// a matrix to store the rotation for each triangle
193	D3DXMATRIX matRotateYB;	// a matrix to store the rotation for the reverse sides
194	static float index = 0.0f; index+=0.03f;	// an ever-increasing float value
195
196	D3DXMATRIX matRotateY;	// a matrix to store the rotation for each triangle
197	D3DXMatrixRotationY(&matRotateY, index);	// the rotation matrix
198	d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY));	// set the world transform
199
200	// aelect the vertex buffer to display
201	d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));
202
203	// set the texture
204	d3ddev->SetTexture(0, texture_1);
205
206	// draw the textured square
207	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
208
209	d3ddev->EndScene();	// ends the 3D scene
210
211	d3ddev->Present(NULL, NULL, NULL, NULL); // display the created frame on the screen
212
213	return;
214}
215
216// this is the function that cleans up Direct3D and COM
217void cleanD3D(void)
218{
219	t_buffer->Release();	// close and release the vertex buffer
220	texture_1->Release();	// close and release the texture
221	d3ddev->Release();	// close and release the 3D device
222	d3d->Release();	// close and release Direct3D
223
224	return;
225}
226
227// this is the function that puts the 3D models into video RAM
228void init_graphics(void)
229{
230	// load the texture we will use
231	D3DXCreateTextureFromFile(d3ddev,
232							  "metal.png",
233							  &texture_1);
234
235	// create the vertices using the CUSTOMVERTEX struct
236	CUSTOMVERTEX t_vert[] = 
237	{
238		{5, 0, -5, 0xffffffff, 1, 0,},
239		{-5, 0, -5, 0xffffffff, 0, 0,},
240		{5, 0, 5, 0xffffffff, 1, 1,},
241		{-5, 0, 5, 0xffffffff, 0, 1,},
242	};
243
244	// create a vertex buffer interface called t_buffer
245	d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
246							   0,
247							   CUSTOMFVF,
248							   D3DPOOL_MANAGED,
249							   &t_buffer,
250							   NULL);
251
252	VOID* pVoid;	// a void pointer
253
254	// lock t_buffer and load the vertices into it
255	t_buffer->Lock(0, 0, (void**)&pVoid, 0);
256	memcpy(pVoid, t_vert, sizeof(t_vert));
257	t_buffer->Unlock();
258
259	return;
260}

+++++++++++++++++++++++++++++++

จบแล้ว งง มั้ยยยย?

Comment



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

งง จบ question

#1 By chenlee on 2008-08-17 22:19

อยู่แล้วววว sad smile

#2 By xViStA on 2008-08-17 22:23

อารายเนี่ย -*-
มันใช้ภาษาไรเป็นพื้นเนี่ย พวกC อ่ะป่าว
หรือ JAVA แต่ไม่น่าใช่เพราะไมโครซอฟคงจะพัฒนาลง C หรือ VB.net มากก่า รึเปล่า??

งงอ่ะ -*-
ภาษา C ครับ
แต่ผมว่า C++ มากกว่านะครับ sad smile

#4 By xViStA on 2008-08-17 22:55

ถ้าไม่อยากสนับสนุนไมโครซอฟท์ (DirectX) แล้วอยากลอง OpenGL ลองเว็บนี้ครัีบ http://nehe.gamedev.net/lesson.asp?index=01

สอนดี และสอนง่ายครับ ถ้าใช้ Windows ก็ใช้ C++ ทำได้ครับ

#5 By manop on 2008-08-18 01:14

มัน c ชนิดไหนเนี่ย - -

#6 By Milkyway.sk on 2008-08-18 06:25

อืม หาอะไรมาศึกษามั้งดีกว่า

#7 By ตุ้มเป๊ะ on 2008-08-18 08:47

จ๊ากกก งงงวยwink

#8 By (^_^)/nana on 2008-08-18 09:18

น่าจะพวก C นะ แต่ว่างงจังembarrassed

#9 By ~ M@shiiro ~ on 2008-08-18 11:10

แม่ปันปราย โคตรงง เลยง่ะ..
big smile

#10 By MomMom on 2008-08-18 11:29

งงนิดหน่อยอ่ะครับ
-'w'- งงอย่างแรง

เป็นภาษา C++ ใช่ไหมอ่ะครับ

#12 By Kefron Kerina on 2008-08-18 19:06

เยี่ยมเลยครับ
ได้เห็นของแปลกอีกแล้ว...แจ่ม...
น่าศึกษาจริงๆconfused smile

#13 By robocon on 2008-08-18 20:54

ไม่เข้าใจเลยด้วยซ้ำ..sad smile sad smile

#14 By VVITch on 2008-08-18 20:55

C++ แน่นอน คับconfused smile

#15 By amino-x on 2008-08-20 19:17

ถ้าเขียนด้วย DirectX จะยาก
ตอนนี้ Microsoft ออก XNA Framework
เป็นเฟรมเวิกสำหรับเขียนเกมส์ ทำให้เขียนง่ายขึ้น และใช้ภาษา C# (ง่ายกว่า C++) {ลองดูนะครับ}

#16 By Jo (202.57.179.218) on 2008-09-18 16:44

เป็นการเขียน game ด้วย C++ เป็นแบบ Win32API ใช้Libraryของ directX9 คับ พ้มว่าคนที่เริ่มเขียนโปรแกรมน่าจะลองศึกษานะคับ มันเป็นพื้นๆๆของการเขียนโปรแกรมเลย เขียนด้วยwin32APIมันจะทำให้เกมคุณเร็วมากๆๆคับ

พอดีกำลังเรียน gameProgramming อยู่พอดี หาบอร์ดคุยยากจิง

#17 By เยี่ยมชม (203.209.53.85) on 2009-02-20 15:10

xViStA Search ... powered by Google

คนออนไลน์ขณะนี้

Recommend

Favourites

Blogrolls

ตรวจสอบเลขประจำตัวประชาชนเล่นๆ ดู
กรอกไปเลย ไม่มีเครื่องหมายขีดขั้น
เลขบัตรประชาชน :
Script Credit