void HelpText_Render(HWND hwnd) { //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS()); g_pTextFPS->DrawText(NULL, g_strFPS, charCount, &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(255, 39, 136)); g_pTextAdaperName->DrawText(NULL, g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); formatRect.top = 30; static wchar_t strInfo[256] = { 0 }; swprintf_s(strInfo, -1, L"模型坐标:(%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); D3DXVECTOR3 targetPosition; g_pCamera->GetTargetPosition(&targetPosition); formatRect.top += 25; swprintf_s(strInfo, -1, L"targetPosition:(%.2f, %.2f, %.2f)", targetPosition.x, targetPosition.y, targetPosition.z); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); D3DXVECTOR3 cameraPosition; g_pCamera->GetCameraPosition(&cameraPosition); formatRect.top += 25; swprintf_s(strInfo, -1, L"cameraPosition:(%.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); formatRect.left = 0, formatRect.top = 380; g_pTextInfo->DrawText(NULL, L"控制说明:", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(235, 123, 230, 255)); formatRect.top += 35; g_pTextHelper->DrawText(NULL, L"W:向前飞翔 S:向后飞翔", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"A:向左飞翔 D:向右飞翔", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"R:垂直向上飞翔 F:垂直向下飞翔", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"Q:向左倾斜 E:向右倾斜", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"上、下、左、右方向键:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"鼠标滚轮:人物模型Z轴方向移动", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"ESC键:退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之三】:正式绘制,在这里我们写了四段文字 //-------------------------------------------------------------------------------------- //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS() ); g_pFont->DrawText(NULL, g_strFPS, charCount , &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(255,39,136)); //在纵坐标100处,写第一段文字 formatRect.top = 100;//指定文字的纵坐标 g_pFont->DrawText(0, _T("【致我们永不熄灭的游戏开发梦想】"), -1, &formatRect, DT_CENTER, D3DCOLOR_XRGB(68,139,256)); //在纵坐标250处,写第二段文字 formatRect.top = 250; g_pFont->DrawText(0, _T("游戏开发的世界,我们来降服你了~!"), -1, &formatRect, DT_CENTER, D3DCOLOR_XRGB(255,255,255)); //在纵坐标400处,写第三段文字 formatRect.top = 400; g_pFont->DrawText(0, _T("闪闪惹人爱"), -1, &formatRect, DT_CENTER, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256));//采用随机RGB值,做出“闪闪惹人爱”的特效 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(150, 150, 100), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之三】:正式绘制,在这里我们写了四段文字 //-------------------------------------------------------------------------------------- for (DWORD i = 0; i < g_dwNumMtrls; i++) { 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("FPS:%0.3f"), Get_FPS() ); g_pTextFPS->DrawText(NULL, g_strFPS, charCount , &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(255,39,136)); g_pTextAdaperName->DrawText(NULL, g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); formatRect.top = 30; static wchar_t strInfo[256] = { 0 }; swprintf_s(strInfo, -1, L"模型坐标:(%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); formatRect.left = 0, formatRect.top = 380; g_pTextInfo->DrawText(NULL, L"控制说明:", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(235, 123, 230, 255)); formatRect.top += 35; g_pTextHelper->DrawText(NULL, L"按住鼠标左键并拖动:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"按住鼠标右键并拖动:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"滑动鼠标滚轮:拉伸模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"W,S,A,D键:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"上、下、左、右方向键:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L"ESC键:退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_ARGB(255, 200, 0, 255)); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之三】:正式绘制,利用顶点缓存绘制图形 //-------------------------------------------------------------------------------------- g_pd3dDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(CUSTOMVERTEX));//把包含的几何体信息的顶点缓存和渲染流水线相关联 g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);//指定我们使用的灵活顶点格式的宏名称 g_pd3dDevice->SetIndices(g_pIndexBuffer);//设置索引缓存 //-------------------------------------------------------------------------------------- // 【纹理绘制四步曲之四】:纹理的启用 //-------------------------------------------------------------------------------------- g_pd3dDevice->SetTexture(0, g_pTexture); //启用纹理 g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);//利用索引缓存配合顶点缓存绘制图形 //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS()); g_pTextFPS->DrawText(NULL, g_strFPS, charCount, &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_RGBA(0, 239, 136, 255)); //显示显卡类型名 g_pTextAdaperName->DrawText(NULL, g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); // 输出绘制信息 formatRect.top = 30; static wchar_t strInfo[256] = { 0 }; swprintf_s(strInfo, -1, L"模型坐标: (%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); // 输出帮助信息 formatRect.left = 0, formatRect.top = 380; g_pTextInfor->DrawText(NULL, L"控制说明:", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235, 123, 230, 255)); formatRect.top += 35; g_pTextHelper->DrawText(NULL, L" 按住鼠标左键并拖动:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 按住鼠标右键并拖动:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 滑动鼠标滚轮:拉伸模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" W、S、A、D键:平移模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 上、下、左、右方向键:旋转模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 键盘上1,2数字键:在两种填充模式之间切换 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" ESC键 : 退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 Matrix_Set();//调用封装了四大变换的函数,对Direct3D世界变换,取景变换,投影变换,视口变换进行设置 // 获取键盘消息并给予设置相应的填充模式 if (::GetAsyncKeyState(0x31) & 0x8000f) // 若数字键1被按下,进行实体填充 g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID); if (::GetAsyncKeyState(0x32) & 0x8000f) // 若数字键2被按下,进行线框填充 g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之三】:正式绘制,利用顶点缓存绘制图形 //-------------------------------------------------------------------------------------- D3DXMatrixRotationY(&R, ::timeGetTime() / 1440.0f); //设置公转的矩阵 // 进行立方体的绘制 D3DXMatrixTranslation(&g_WorldMatrix[0], 3.0f, -3.0f, 0.0f); g_WorldMatrix[0] = g_WorldMatrix[0]*R; g_pd3dDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix[0]); g_cube->DrawSubset(0); //进行茶壶的绘制 D3DXMatrixTranslation(&g_WorldMatrix[1], -3.0f, -3.0f, 0.0f); g_WorldMatrix[1] = g_WorldMatrix[1]*R; g_pd3dDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix[1]); g_teapot->DrawSubset(0); // 进行圆环的绘制 D3DXMatrixTranslation(&g_WorldMatrix[2], 3.0f, 3.0f, 0.0f); g_WorldMatrix[2] = g_WorldMatrix[2]*R; g_pd3dDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix[2]); g_torus->DrawSubset(0); // 进行球面体的绘制 D3DXMatrixTranslation(&g_WorldMatrix[3], -3.0f, 3.0f, 0.0f); g_WorldMatrix[3] = g_WorldMatrix[3]*R; g_pd3dDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix[3]); g_sphere->DrawSubset(0); //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS() ); g_pFont->DrawText(NULL, g_strFPS, charCount , &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(255,39,136)); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }