00001
00002
00003
00004 #include "main.h"
00005 #include "DrawMain.h"
00006
00007
00008 #ifdef __OUTPUT_DOXYGEN
00009 #undef __NOT_USE_DIRECTX
00010 #endif
00011
00013 #ifndef __NOT_USE_DIRECTX
00014
00015 #include "FPS.h"
00016
00018 using namespace std;
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define MOUSE_Z_WEIGHT 120.0f
00028
00029
00030
00031
00032 D3DVIEWPORT9 viDefView;
00033 D3DXVECTOR3 vecCameraRot;
00034 D3DXVECTOR3 vecCameraPos;
00035 D3DXVECTOR3 vecCameraEye;
00036 D3DXVECTOR3 vecCameraWeight;
00037 D3DXVECTOR3 vecCameraMove;
00038 CDirectInput *DiKey;
00039 CDirectInput *DiMouse;
00040
00041
00042 LPD3DXMESH lpTeapot;
00043 LPD3DXMESH lpBox;
00044 LPD3DXMESH lpSphere;
00045 D3DXVECTOR3 vecTeapotBSCenter;
00046 FLOAT fTeapotBSRadius;
00047 D3DXVECTOR3 vecTeapotBBLeftBottom;
00048 D3DXVECTOR3 vecTeapotBBRightTop;
00049 D3DMATERIAL9 matBox;
00050 D3DMATERIAL9 matMaterial;
00051 D3DXVECTOR3 vecBoxSize;
00052
00053
00054 DWORD FpsValue;
00055 DWORD waitTime;
00056
00057
00058
00059
00060 D3DXMATRIX mView;
00061 D3DXMATRIX mProj;
00062
00063
00064
00065
00066 void FrameInit();
00067 void FrameRelease();
00068 void DrawFrame();
00069 LRESULT MyWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00070
00071
00072
00073
00074 static void Proc();
00075 static void Render();
00076 static void Sprite();
00077 static void SetCamera(const D3DXVECTOR3 &vecPos,const D3DXVECTOR3 &vecEye,float fPerspect);
00078
00079
00089
00090 LRESULT MyWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00091 {
00092 switch(uMsg) {
00093 case WM_ACTIVATE:
00094 if(LOWORD(wParam) != WA_INACTIVE)
00095 {
00096 if(DiKey)
00097 DiKey->AcquireDevice(TRUE);
00098 }
00099 return 1;
00100 case WM_MOUSEMOVE:
00101 if(DiMouse)
00102 DiMouse->AcquireDevice(TRUE);
00103 return 1;
00104 }
00105
00106 return 1;
00107 }
00108
00109
00114
00115 void InitDiInput()
00116 {
00117 list<DIDEVICEINSTANCE> lstKey;
00118 list<DIDEVICEINSTANCE> lstMouse;
00119
00120
00121
00122
00123 if(FAILED(CDirectInput::DIObjectInit(hInstance)))
00124 {
00125 SendMessage(hWindow,WM_CLOSE,0,0);
00126 return;
00127 }
00128
00129
00130
00131
00132
00133 CDirectInputBase::GetDeviceInstance(&lstMouse,DI8DEVCLASS_POINTER,DIEDFL_ATTACHEDONLY);
00134 CDirectInputBase::GetDeviceInstance(&lstKey,DI8DEVCLASS_KEYBOARD,DIEDFL_ATTACHEDONLY);
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 CDirectInput::DIObjectInit(hInstance);
00154 DiKey = new CDirectInput();
00155 DiKey->InitDevice(*(lstKey.begin()),0,hWindow,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
00156 DiKey->AcquireDevice(TRUE);
00157
00158
00159
00160 DiMouse = new CDirectInput();
00161 DiMouse->InitDevice(*(lstMouse.begin()),0,hWindow,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
00162 DiMouse->AcquireDevice(TRUE);
00163 }
00164
00165
00166
00171
00172 void FrameInit()
00173 {
00174
00175
00176
00177
00178 pD3DDevice->GetViewport(&viDefView);
00179 vecCameraRot = D3DXVECTOR3(0,0,10.0f);
00180 vecCameraWeight = D3DXVECTOR3(2.0f,2.0f,10.0f);
00181 vecCameraPos = D3DXVECTOR3(0,0,-10);
00182 vecCameraEye = D3DXVECTOR3(0,0,0);
00183
00184
00185
00186
00187 InitDiInput();
00188
00189
00190
00191
00192 D3DXCreateTeapot(pD3DDevice,&lpTeapot,NULL);
00193
00194
00195
00196
00197 LPVOID pBuffer;
00198 lpTeapot->LockVertexBuffer(D3DLOCK_READONLY,&pBuffer);
00199
00200
00201 D3DXComputeBoundingSphere(
00202 (D3DXVECTOR3 *)pBuffer,
00203 lpTeapot->GetNumVertices(),
00204 lpTeapot->GetNumBytesPerVertex(),
00205 &vecTeapotBSCenter,&fTeapotBSRadius);
00206
00207 D3DXComputeBoundingBox(
00208 (D3DXVECTOR3 *)pBuffer,
00209 lpTeapot->GetNumVertices(),
00210 lpTeapot->GetNumBytesPerVertex(),
00211 &vecTeapotBBLeftBottom,&vecTeapotBBRightTop);
00212
00213 lpTeapot->UnlockVertexBuffer();
00214
00215 vecBoxSize.x = fabs(vecTeapotBBRightTop.x - vecTeapotBBLeftBottom.x);
00216 vecBoxSize.y = fabs(vecTeapotBBRightTop.y - vecTeapotBBLeftBottom.y);
00217 vecBoxSize.z = fabs(vecTeapotBBRightTop.z - vecTeapotBBLeftBottom.z);
00218
00219
00220
00221 D3DXCreateBox(pD3DDevice,
00222 vecBoxSize.x,
00223 vecBoxSize.y,
00224 vecBoxSize.z,
00225 &lpBox,NULL);
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 ZeroMemory(&matMaterial,sizeof(matMaterial));
00239 matMaterial.Diffuse.a = 1.0f;
00240 matMaterial.Diffuse.r = 1.0f;
00241 matMaterial.Ambient = matMaterial.Diffuse;
00242
00243 matMaterial.Power = 10;
00244 matMaterial.Specular.a = 1.0f;
00245 matMaterial.Specular.r = 0.5f;
00246 matMaterial.Specular.g = 0.5f;
00247 matMaterial.Specular.b = 0.5f;
00248
00249
00250
00251 matBox = matMaterial;
00252 matBox.Diffuse.a = 0.4f;
00253 matBox.Diffuse.r = 1.0f;
00254 matBox.Diffuse.g = 1.0f;
00255 matBox.Diffuse.b = 1.0f;
00256 matBox.Ambient = matBox.Diffuse;
00257
00258 SetFpsValue(60);
00259
00260 return;
00261 }
00262
00263
00268
00269 void FrameRelease()
00270 {
00271
00272 SAFE_RELEASE(lpSphere);
00273 SAFE_RELEASE(lpBox);
00274 SAFE_RELEASE(lpTeapot);
00275
00276
00277 SAFE_RELEASE(DiKey);
00278 SAFE_RELEASE(DiMouse);
00279 CDirectInput::DIObjectRelease();
00280 }
00281
00282
00287
00288 void Proc()
00289 {
00290 DIMOUSESTATE2 dims;
00291 BYTE byKeyState[256];
00292
00293
00294
00295
00296 DiMouse->GetState(&dims);
00297 DiKey->GetState(byKeyState);
00298
00299
00300
00301
00302 if(dims.rgbButtons[1] & 0x80)
00303 {
00304 vecCameraRot.x += (signed int)dims.lY / vecCameraWeight.y;
00305 vecCameraRot.y += (signed int)dims.lX / vecCameraWeight.x;
00306 vecCameraRot.z += ((signed int)dims.lZ / MOUSE_Z_WEIGHT) * (vecCameraRot.z / vecCameraWeight.z);
00307 }
00308
00309 if(byKeyState[DIK_LEFT] & 0x80)
00310 {
00311 }
00312 if(byKeyState[DIK_RIGHT] & 0x80)
00313 {
00314 }
00315 if(byKeyState[DIK_UP] & 0x80)
00316 {
00317 }
00318 if(byKeyState[DIK_DOWN] & 0x80)
00319 {
00320 }
00321 if(byKeyState[DIK_Z] & 0x80)
00322 {
00323 }
00324 if(byKeyState[DIK_X] & 0x80)
00325 {
00326 }
00327
00328 }
00329
00330
00335
00336 void Render()
00337 {
00338 }
00339
00340
00344
00345 void Sprite()
00346 {
00347
00348 TCHAR msg[256];
00349 RECT rect = {0,0,100,20};
00350
00351
00352
00353
00354 pSprite->Begin(D3DXSPRITE_ALPHABLEND);
00355
00356
00357 sprintf(msg,"FPS : %d",FpsValue);
00358 lpFont->DrawTextA(pSprite,msg,-1,&rect,DT_LEFT | DT_NOCLIP,D3DCOLOR_XRGB(0xff,0xff,0xff));
00359
00360
00361 rect.top += 20;
00362 sprintf(msg,"Wait : %d",waitTime);
00363 lpFont->DrawTextA(pSprite,msg,-1,&rect,DT_LEFT | DT_NOCLIP,D3DCOLOR_XRGB(0xff,0xff,0xff));
00364
00365 pSprite->End();
00366 }
00367
00368
00374
00375 void DrawFrame()
00376 {
00377
00378 SetBeforTime();
00379
00380 Proc();
00381
00382 if(SUCCEEDED(pD3DDevice->BeginScene()))
00383 {
00384 D3DVIEWPORT9 viViewPort;
00385
00386
00387
00388
00389
00390 D3DXMATRIX matCameraRot;
00391 D3DXVECTOR3 vecCameraDir(0,0,1.0f),vecCameraMove2(0,0,0);
00392 D3DXMatrixRotationYawPitchRoll(&matCameraRot,D3DXToRadian(vecCameraRot.y),D3DXToRadian(vecCameraRot.x),0);
00393 D3DXVec3TransformCoord(&vecCameraDir,&vecCameraDir,&matCameraRot);
00394 D3DXVec3TransformCoord(&vecCameraMove2,&vecCameraMove,&matCameraRot);
00395 vecCameraDir *= vecCameraRot.z;
00396 vecCameraEye = vecCameraMove2;
00397 vecCameraPos = vecCameraDir + vecCameraEye;
00398
00399
00400
00401 pD3DDevice->SetViewport(&viDefView);
00402 pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
00403 SetCamera(vecCameraPos,vecCameraEye,120.0f);
00404
00405
00406
00407 Render();
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 Sprite();
00431
00432 pD3DDevice->EndScene();
00433 }
00434 if(FAILED(pD3DDevice->Present(NULL, NULL, NULL, NULL)))
00435 pD3DDevice->Reset(&d3dpp);
00436
00437
00438
00439 SetNowTime();
00440
00441
00442 if(GetElapsedTime(&waitTime))
00443 Sleep(waitTime);
00444
00445
00446 GetNowFPSValue(&FpsValue);
00447 }
00448
00449
00456
00457 void SetCamera(const D3DXVECTOR3 &vecPos,const D3DXVECTOR3 &vecEye,float fPerspect)
00458 {
00459
00460 D3DXMatrixLookAtLH(&mView,
00461 &vecPos,
00462 &vecEye,
00463 &D3DXVECTOR3(0.0f, 1.0f, 0.0f)
00464 );
00465
00466 D3DXMatrixPerspectiveFovLH(&mProj,
00467 D3DXToRadian(fPerspect),
00468 4.0f/3.0f,
00469 1.0f,
00470 1000.0f
00471 );
00472
00473
00474 pD3DDevice->SetTransform(D3DTS_VIEW, &mView);
00475 pD3DDevice->SetTransform(D3DTS_PROJECTION, &mProj);
00476
00477 }
00478
00479 #endif
00480