forked from zdd/RubikCube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3D9.cpp
466 lines (372 loc) · 10.2 KB
/
D3D9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "D3D9.h"
D3D9::D3D9(void)
: d3d_(NULL),
d3ddevice_(NULL),
is_fullscreen_(false)
{
camera = new Camera();
}
D3D9::~D3D9(void)
{
delete camera;
camera = NULL;
// Release Direct3D Device
if(d3ddevice_ != NULL)
{
d3ddevice_->Release();
d3ddevice_ = NULL;
}
// Release Direct3D object
if(d3d_ != NULL)
{
d3d_->Release();
d3d_ = NULL;
}
}
void D3D9::InitD3D9(HWND hWnd)
{
this->hWnd = hWnd;
// Create the D3D object.
if( NULL == ( d3d_ = Direct3DCreate9( D3D_SDK_VERSION ) ) )
MessageBox(hWnd, L"Create Direct3D9 failed!", L"error!", 0) ;
ZeroMemory( &d3dpp_, sizeof(d3dpp_) );
// Get max display resolution and set it as back buffer size
D3DDISPLAYMODE displayMode ;
d3d_->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode) ;
// Initialize the max resolution width and height
screen_width_ = displayMode.Width;
screen_height_ = displayMode.Height;
if(is_fullscreen_)
{
d3dpp_.Windowed = FALSE;
d3dpp_.BackBufferWidth = displayMode.Width;
d3dpp_.BackBufferHeight = displayMode.Height;
}
else
{
d3dpp_.Windowed = TRUE;
// Get current window size and set it as back buffer size
RECT rect;
GetClientRect(hWnd, &rect);
d3dpp_.BackBufferWidth = rect.right - rect.left;
d3dpp_.BackBufferHeight = rect.bottom - rect.top;
}
// We didn't specify the back-buffer width and height, D3D will initialize it to the window width and height
d3dpp_.hDeviceWindow = hWnd;
d3dpp_.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp_.BackBufferCount = 1;
d3dpp_.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp_.EnableAutoDepthStencil = TRUE ;
d3dpp_.AutoDepthStencilFormat = D3DFMT_D16 ;
// Create the D3DDevice
HRESULT hr = d3d_->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp_, &d3ddevice_ );
if(FAILED(hr))
{
MessageBox(hWnd, L"Create Direct3D9 device failed!", L"error!", 0) ;
}
// Setup view matrix
D3DXVECTOR3 vecEye(0.0f, 0.0f, -10.0f);
D3DXVECTOR3 vecAt (0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vecUp (0.0f, 1.0f, 0.0f) ;
camera->SetViewParams(vecEye, vecAt, vecUp);
// Setup projection matrix
float aspectRatio = (float)d3dpp_.BackBufferWidth / (float)d3dpp_.BackBufferHeight ;
camera->SetProjParams(D3DX_PI / 4, aspectRatio, 1.0f, 1000.0f) ;
ResetDevice();
}
LPDIRECT3DTEXTURE9 D3D9::CreateTexture(int texWidth, int texHeight, D3DCOLOR color)
{
LPDIRECT3DTEXTURE9 pTexture;
HRESULT hr = D3DXCreateTexture(d3ddevice_,
texWidth,
texHeight,
0,
0,
D3DFMT_A8R8G8B8, // 4 bytes for a pixel
D3DPOOL_MANAGED,
&pTexture);
if (FAILED(hr))
{
MessageBox(NULL, L"Create texture failed", L"Error", 0);
}
// Lock the texture and fill in color
D3DLOCKED_RECT lockedRect;
hr = pTexture->LockRect(0, &lockedRect, NULL, 0);
if (FAILED(hr))
{
MessageBox(NULL, L"Lock texture failed!", L"Error", 0);
}
DWORD sideColor = 0xff000000; // the side line color
int side_width = 10;
// Calculate number of rows in the locked Rect
int rowCount = (texWidth * texHeight * 4 ) / lockedRect.Pitch;
for (int i = 0; i < texWidth; ++i)
{
for (int j = 0; j < texHeight; ++j)
{
int index = i * rowCount + j;
int* pData = (int*)lockedRect.pBits;
if (i <= side_width || i >= texWidth - side_width
|| j <= side_width || j >= texHeight - side_width)
{
memcpy(&pData[index], &sideColor, 4);
}
else
{
memcpy(&pData[index], &color, 4);
}
}
}
pTexture->UnlockRect(0);
return pTexture;
}
LPDIRECT3DTEXTURE9 D3D9::CreateInnerTexture(int texWidth, int texHeight, D3DCOLOR color)
{
LPDIRECT3DTEXTURE9 pTexture;
HRESULT hr = D3DXCreateTexture(d3ddevice_,
texWidth,
texHeight,
0,
0,
D3DFMT_A8R8G8B8, // 4 bytes for a pixel
D3DPOOL_MANAGED,
&pTexture);
if(FAILED(hr))
{
MessageBox(NULL, L"Create texture failed", L"Error", 0);
}
// Lock the texture and fill in color
D3DLOCKED_RECT lockedRect;
hr = pTexture->LockRect(0, &lockedRect, NULL, 0);
if(FAILED(hr))
{
MessageBox(NULL, L"Lock texture failed!", L"Error", 0);
}
DWORD sideColor = 0xff121212; // the side line color
// Calculate number of rows in the locked Rect
int rowCount = (texWidth * texHeight * 4 ) / lockedRect.Pitch;
for (int i = 0; i < texWidth; ++i)
{
for (int j = 0; j < texHeight; ++j)
{
int index = i * rowCount + j;
int* pData = (int*)lockedRect.pBits;
memcpy(&pData[index], &color, 4);
}
}
pTexture->UnlockRect(0);
return pTexture;
}
void D3D9::SetupMatrix()
{
// View matrix
D3DXMATRIX matView = camera->GetViewMatrix() ;
d3ddevice_->SetTransform(D3DTS_VIEW, &matView) ;
// Projection matrix
D3DXMATRIX matProj = camera->GetProjMatrix() ;
d3ddevice_->SetTransform(D3DTS_PROJECTION, &matProj) ;
}
void D3D9::SetupLight()
{
// Create light
D3DLIGHT9 pointLight ;
// Light color
D3DXCOLOR color = D3DCOLOR_XRGB(255, 255, 255) ;
// The light position is always same as the camera eye point
// so no matter how you rotate the camera, the cube will keep the same brightness
D3DXVECTOR3 position = camera->GetEyePoint() ;
// Light type, we use point light here
pointLight.Type = D3DLIGHT_POINT ;
// Light attributes
pointLight.Ambient = color * 0.6f;
pointLight.Diffuse = color;
pointLight.Specular = color * 0.6f;
pointLight.Position = position;
pointLight.Range = 320.0f;
pointLight.Falloff = 1.0f;
pointLight.Attenuation0 = 1.0f;
pointLight.Attenuation1 = 0.0f;
pointLight.Attenuation2 = 0.0f;
// Set material
D3DMATERIAL9 material ;
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0) ;
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0);
material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0);
material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
material.Power = 2.0f ;
d3ddevice_->SetMaterial(&material) ;
// Enable light
d3ddevice_->SetLight(0, &pointLight) ;
d3ddevice_->LightEnable(0, true) ;
}
void D3D9::ResizeD3DScene(int width, int height)
{
if (height == 0) // Prevent A Divide By Zero By
height = 1; // Making Height Equal One
// Compute aspect ratio
float fAspectRatio = width / (FLOAT)height;
// Setup Projection matrix
camera->SetProjParams(D3DX_PI / 4, fAspectRatio, 1.0f, 1000.0f);
camera->SetWindow(width, height);
}
// We can remove this parameter here, and use device to get the
// back buffer size dynamically.
HRESULT D3D9::ResetDevice()
{
// Check device state
HRESULT hr = d3ddevice_->TestCooperativeLevel() ;
// Device can be reset now
if (SUCCEEDED(hr) || hr == D3DERR_DEVICENOTRESET)
{
// Reset device
HRESULT hr = d3ddevice_->Reset(&d3dpp_);
if (SUCCEEDED(hr))
{
ResizeD3DScene(d3dpp_.BackBufferWidth, d3dpp_.BackBufferHeight) ;
}
else // Reset device failed, show error box
{
const WCHAR* errorString = DXGetErrorString(hr) ;
DXTRACE_ERR_MSGBOX(errorString, hr) ;
}
}
// Device is still in lost state, wait
else if (hr == D3DERR_DEVICELOST)
{
Sleep(25) ;
}
else // Other error, Show error box
{
const WCHAR* errorString = DXGetErrorString(hr) ;
DXTRACE_ERR_MSGBOX(errorString, hr) ;
}
return hr ;
}
void D3D9::FrameMove()
{
camera->OnFrameMove() ;
}
// Calculate the picking ray and transform it to model space
// x and y are the screen coordinates when left button down
Ray D3D9::CalculatePickingRay(int x, int y)
{
float px = 0.0f;
float py = 0.0f;
// Get viewport
D3DVIEWPORT9 vp;
d3ddevice_->GetViewport(&vp);
// Get Projection matrix
D3DXMATRIX proj;
d3ddevice_->GetTransform(D3DTS_PROJECTION, &proj);
px = ((( 2.0f*x) / vp.Width) - 1.0f) / proj(0, 0);
py = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1, 1);
Ray ray;
ray.origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
ray.direction = D3DXVECTOR3(px, py, 1.0f);
// Get view matrix
D3DXMATRIX view;
d3ddevice_->GetTransform(D3DTS_VIEW, &view);
// Get world matrix
D3DXMATRIX world;
d3ddevice_->GetTransform(D3DTS_WORLD, &world);
// Concatinate them in to single matrix
D3DXMATRIX WorldView = world * view;
// inverse it
D3DXMATRIX worldviewInverse;
D3DXMatrixInverse(&worldviewInverse, 0, &WorldView);
// Transform the ray to model space
D3DXVec3TransformCoord(&ray.origin, &ray.origin, &worldviewInverse) ;
D3DXVec3TransformNormal(&ray.direction, &ray.direction, &worldviewInverse) ;
// Normalize the direction
D3DXVec3Normalize(&ray.direction, &ray.direction) ;
return ray;
}
// Transform the screen point to vector in model space
D3DXVECTOR3 D3D9::ScreenToVector3(int x, int y)
{
D3DXVECTOR3 vector3 ;
// Get viewport
D3DVIEWPORT9 vp;
d3ddevice_->GetViewport(&vp);
// Get Projection matrix
D3DXMATRIX proj;
d3ddevice_->GetTransform(D3DTS_PROJECTION, &proj);
vector3.x = ((( 2.0f*x) / vp.Width) - 1.0f) / proj(0, 0);
vector3.y = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1, 1);
vector3.z = 1.0f ;
// Get view matrix
D3DXMATRIX view;
d3ddevice_->GetTransform(D3DTS_VIEW, &view);
// Get world matrix
D3DXMATRIX world;
d3ddevice_->GetTransform(D3DTS_WORLD, &world);
// Concatinate them in to single matrix
D3DXMATRIX WorldView = world * view;
// inverse it
D3DXMATRIX worldviewInverse;
D3DXMatrixInverse(&worldviewInverse, 0, &WorldView);
D3DXVec3TransformCoord(&vector3, &vector3, &worldviewInverse) ;
D3DXVec3Normalize(&vector3, &vector3) ;
return vector3 ;
}
LRESULT D3D9::HandleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return camera->HandleMessages(hWnd, uMsg, wParam, lParam);
}
LPDIRECT3D9 D3D9::GetD3D9() const
{
return d3d_;
}
LPDIRECT3DDEVICE9 D3D9::GetD3DDevice() const
{
return d3ddevice_;
}
D3DPRESENT_PARAMETERS D3D9::GetD3Dpp() const
{
return d3dpp_;
}
void D3D9::SetBackBufferWidth(int width)
{
d3dpp_.BackBufferWidth = width;
}
void D3D9::SetBackBufferHeight(int height)
{
d3dpp_.BackBufferHeight = height;
}
void D3D9::SetIsFullScreen(bool is_fullscreen)
{
is_fullscreen_ = is_fullscreen;
d3dpp_.Windowed = !is_fullscreen;
}
bool D3D9::GetIsFullScreen() const
{
return is_fullscreen_;
}
int D3D9::GetLastWindowWidth() const
{
return last_window_width_;
}
void D3D9::SetLastWindowWidth(int windowWidth)
{
last_window_width_ = windowWidth;
}
int D3D9::GetLastWindowHeight() const
{
return last_window_height_;
}
void D3D9::SetLastWindowHeight(int windowHeight)
{
last_window_height_ = windowHeight;
}
int D3D9::GetScreenWidth() const
{
return screen_width_;
}
int D3D9::GetScreenHeight() const
{
return screen_height_;
}