447. 448. 449. 450. 451. 452. 453. 454. 455. 456. 457. 458. 459. 460.
// Name: Direct3D_Render() // Desc: 进行图形的渲染操作 // Point:【Direct3D渲染五步曲】 // 1.渲染五步曲之一,清屏操作 // 2.渲染五步曲之二,开始绘制 // 3.渲染五步曲之三,正式绘制 // 4.渲染五步曲之四,结束绘制 // 5.渲染五步曲之五,翻转显示
//***********************************************************
void Direct3D_Render(HWND hwnd) {
//-------------------------------------------------------
******************************
------------------------------- 461. 462. 463. 464. 465. 466. 467. 468. 469.
// 【Direct3D渲染五步曲之一】:清屏操作
//------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBU
//定义一个矩形,用于获取主窗口矩形 RECT formatRect;
GetClientRect(hwnd, &formatRect);
//-------------------------------------------------------
-------------------------------
FFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
------------------------------- 470. 471.
// 【Direct3D渲染五步曲之二】:开始绘制
//-------------------------------------------------------
------------------------------- 472. 473. 474.
g_pd3dDevice->BeginScene(); // 开始绘
//-----------------------------------------------
制
--------------------------------------- 475. 476. 477. 478. 479. 480.
// 【Direct3D渲染五步曲之三】:正式绘制,利用顶点缓存绘制 //-----------------------------------------------
// 绘制网格
for (DWORD i = 0; i < g_dwNumMtrls; i++) {
图形
---------------------------------------
481.
482.
483. 484. 485. 486. 487. 488. 489. 490. 491. 492. 493. 494. 495. 496. 497.
g_pd3dDevice->SetMaterial(&g_pMaterials[i]); g_pd3dDevice->SetTexture(0, g_pTextures[i]); g_pMesh->DrawSubset(i); }
//在窗口右上角处,显示每秒帧数
int charCount = swprintf_s(g_strFPS, 20, _T(\ g_pTextFPS->DrawText(NULL, g_strFPS, charCount ,
//显示显卡类型名
g_pTextAdaperName->DrawText(NULL,g_strAdapterName DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f,
// 输出绘制信息
formatRect.top = 30;
static wchar_t strInfo[256] = {0}; swprintf_s(strInfo,-1, L\
模
型
坐
%0.3f\), Get_FPS() );
&formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_RGBA(0,239,136,255));
, -1, &formatRect, 1.0f));
标: (%.2f, %.2f, %.2f)\, g_matWorld._41, g_matWorld._42, g_matWorld._43); 498.
g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135,239,136,255)); 499. 500. 501. 502. 503. 504. 505. 506. 507. 508. 509.
// 输出帮助信息
formatRect.left = 0,formatRect.top = 380; g_pTextInfor->DrawText(NULL, L\
控
制
说
明:\, -1, &formatRect,
DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 35;
g_pTextHelper->DrawText(NULL, L\按住鼠标左键并拖 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\按住鼠标右键并拖 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235,123,230,255));
动:平移模型\, -1, &formatRect, _RGBA(255,200,0,255));
动:旋转模型\, -1, &formatRect,
_RGBA(255,200,0,255)); 510. 511. 512. 513. 514. 515. 516. 517. 518. 519. 520. 521. 522. 523. 524. 525. 526. 527.
formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\滑动鼠标滚轮:拉 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\、S、A、D键: DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\上、下、左、右方 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\数字键1和2:在 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR formatRect.top += 25;
g_pTextHelper->DrawText(NULL, L\键 : 退出程 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR
//-------------------------------------------------------
伸模型\, -1, &formatRect, _RGBA(255,200,0,255));
平移模型 \, -1, &formatRect, _RGBA(255,200,0,255));
向键:旋转模型 \, -1, &formatRect, _RGBA(255,200,0,255));
实体填充与线框填充之间切换\, -1, &formatRect, _RGBA(255,200,0,255));
序\, -1, &formatRect, _RGBA(255,200,0,255));
------------------------------- 528. 529.
// 【Direct3D渲染五步曲之四】:结束绘制
//-------------------------------------------------------
------------------------------- 530. 531.
g_pd3dDevice->EndScene(); // 结束绘 //-------------------------------------------------------
制
------------------------------- 532. 533.
// 【Direct3D渲染五步曲之五】:显示翻转
//-------------------------------------------------------
------------------------------- 534. 535. 536.
g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与 }
显示
537. 538. 539. 540. 541.
//***********************************************************
****************************** 542. 543. 544. 545. 546. 547. 548. 549. 550. 551. 552. 553. 554. 555.
// Name:Get_FPS()函数 // Desc: 用于计算帧速率
//***********************************************************float Get_FPS() {
//定义四个静态变量
static float fps = 0; //我们需要计算的FPS值 static int frameCount = 0;//帧数 static float currentTime =0.0f;//当前时间 static float lastTime = 0.0f;//持续时间
frameCount++;//每调用一次Get_FPS()函数,帧数自增1
currentTime = timeGetTime()*0.001f;//获取系统时间,其中
******************************
timeGetTime函数返回的是以毫秒为单位的系统时间,所以需要乘以0.001,得到单位为秒的时间 556. 557. 558. 559. 560.
//如果当前时间减去持续时间大于了1秒钟,就进行一次FPS的计算和持续时 if(currentTime - lastTime > 1.0f) //将时间控制在1秒钟 {
fps = (float)frameCount /(currentTime - lastTime);//
间的更新,并将帧数值清零
计算这1秒钟的FPS值 561. 562. 563. 564. 565. 566. 567. 568. 569. 570. 571. 572.
lastTime = currentTime; //将当前时间currentTime赋给持续 frameCount = 0;//将本次帧数frameCount值清零 }
return fps; }
//***********************************************************// Name: Device_Read(); // Desc: 智能读取设备的输入数据 时间lastTime,作为下一秒的基准时间
******************************
573. 574. 575. 576. 577. 578. 579. 580. 581. 582. 583. 584. 585. 586. 587. 588. 589. 590. 591. 592. 593. 594. 595. 596. 597. 598. 599. 600. 601. 602. 603. 604. 605. 606.
//***********************************************************BOOL Device_Read(IDirectInputDevice8 *pDIDevice, void* pBuffe{
HRESULT hr; while (true) {
pDIDevice->Poll(); // 轮询设备
pDIDevice->Acquire(); // 获取设备的控制权 if (SUCCEEDED(hr = pDIDevice->GetDeviceState(lSize, p if (hr != DIERR_INPUTLOST || hr != DIERR_NOTACQUIRED) if (FAILED(pDIDevice->Acquire())) return FALSE; }
return TRUE; }
//***********************************************************// Name: Direct3D_CleanUp()
// Desc: 对Direct3D的资源进行清理,释放COM接口对象
//***********************************************************void Direct3D_CleanUp() {
//释放COM接口对象
g_pMouseDevice->Unacquire(); g_pKeyboardDevice->Unacquire();
for (DWORD i = 0; i ****************************** r, long lSize) Buffer))) break; return FALSE; ****************************** ******************************