dx雾特效,旋转的立方体,文字动画显示,底面简单演示

 代码太多,添加了三个类,把很多都封装起来了

按空格键开启或关闭雾效果

给出主文件:

Code:
  1. //  
  2. // 文件: main.cpp  
  3. // 作者:CYM  
  4. //  *雾特效,旋转的立方体,底面!~简单演示  
  5. //  
  6.   
  7. #include "D3DFrame.h"  
  8. #include "Box.h"  
  9. #include "Ground.h"  
  10. #include "Drawtext.h"  
  11. #include "d3dx9.h"  
  12.   
  13. void SetFogEffects(D3DCAPS9 caps,bool isFogEnable);     //设置雾效果函数声明  
  14.   
  15. //  
  16. // 全局设置  
  17. //  
  18.   
  19. IDirect3DDevice9* Device = 0;   //定义设备对象   
  20. IDirect3DTexture9*  Texture1;   //立方体纹理  
  21. IDirect3DTexture9*  Texture2;   //地面地图纹理  
  22. LPD3DXFONT  Font;               //定义字体  
  23.   
  24. CD3DFrame Frame;    //定义框架类  
  25. CGround*    Ground=0;   //定义场地类  
  26. CBox*   Box = 0;    //定义Box对象  
  27. CDrawtext*  Text=0; //定义文字绘制对象  
  28.   
  29. D3DMATERIAL9 material;  
  30. D3DXMATRIX  Map;     //地图矩阵  
  31. D3DXMATRIX  Tmatrix; //平移世界矩阵  
  32. D3DXMATRIX  Rmatrix; //旋转矩阵  
  33. D3DXMATRIX  MATRIX;  //结合矩阵  
  34. D3DXMATRIX V;        //视图矩阵  
  35.   
  36. const int Width  = 640;     //窗口的宽高  
  37. const int Height = 480;  
  38.   
  39. static D3DXVECTOR3  Txyz(0.0f,0.0f,0.0f);       //矩阵平移位置坐标  
  40. static D3DXVECTOR3  Rxyz(0.0f,0.0f,0.0f);       //矩阵旋转角度坐标  
  41. static RECT FontRect={0,0,Width,Height};        //绘制文字的矩形  
  42.   
  43. D3DCAPS9 caps;  //设备功能支持的结构体  
  44.   
  45. bool isFogEnable=true;  //判断雾效果是否开启  
  46. static float timeConversion=0.1f;   //时间转换(速度同步)数值  
  47.   
  48. //  
  49. // 框架函数  
  50. //  
  51. bool Setup()  
  52. {  
  53.     //创造对象  
  54.     Box = new CBox(Device);  
  55.     Ground = new CGround(Device);  
  56.     Text = new CDrawtext(Device,Font);  
  57.   
  58.     Device->GetDeviceCaps(&caps);   //获取设备功能支持信息的结构体  
  59.   
  60.     //设置纹理  
  61.     //D3DMATERIAL9 material;  
  62.     ::ZeroMemory(&material, sizeof(material));  
  63.     material.Ambient=D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  64.     material.Diffuse=D3DXCOLOR(1.0f, 1.0f, 0.1f, 1.0f);  
  65.     material.Emissive=D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  66.     material.Power=0.1f;  
  67.     material.Specular=D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);  
  68.   
  69.     //设置平行光  
  70.     D3DLIGHT9 light;  
  71.     ::ZeroMemory(&light, sizeof(light));  
  72.     light.Type      = D3DLIGHT_DIRECTIONAL;  
  73.     light.Ambient   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  74.     light.Diffuse   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  75.     light.Specular  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  76.     light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  
  77.     Device->SetLight(0, &light);  
  78.     Device->LightEnable(0, true);  
  79.   
  80.     //设置平行光  
  81.     D3DLIGHT9 light2;  
  82.     ::ZeroMemory(&light2, sizeof(light2));  
  83.     light2.Type      = D3DLIGHT_DIRECTIONAL;  
  84.     light2.Ambient   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  85.     light2.Diffuse   = D3DXCOLOR(0.1f, 0.5f, 0.4f, 1.0f);  
  86.     light2.Specular  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
  87.     light2.Direction = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  
  88.     Device->SetLight(1, &light2);  
  89.     Device->LightEnable(1, true);  
  90.   
  91.     Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);   //正常的渲染等级  
  92.     Device->SetRenderState(D3DRS_SPECULARENABLE, true);     //镜面高光  
  93.       
  94.   
  95.     D3DXVECTOR3 position( 0.0f, 0.0f, 4.0f );  
  96.     D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);  
  97.     D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);  
  98.     D3DXMatrixLookAtLH(&V, &position, &target, &up);  
  99.       
  100.     Device->SetTransform(D3DTS_VIEW, &V);  
  101.   
  102.     //  
  103.     // Create texture.  
  104.     //  
  105.     //载入立方体纹理  
  106.     D3DXCreateTextureFromFile(  
  107.         Device,  
  108.         "crate.jpg",  
  109.         &Texture1);  
  110.   
  111.     //载入地面纹理  
  112.     D3DXCreateTextureFromFile(  
  113.         Device,  
  114.         "河流.jpg",  
  115.         &Texture2);  
  116.   
  117.     //   
  118.     // 设置纹理过滤方式.  
  119.     //  
  120.   
  121.     Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);  
  122.     Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);  
  123.     Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);  
  124.   
  125.     Device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);  
  126.     Device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);  
  127.     Device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);  
  128.   
  129.     //  
  130.     // 设置投影矩阵.  
  131.     //  
  132.   
  133.     D3DXMATRIX proj;  
  134.     D3DXMatrixPerspectiveFovLH(  
  135.             &proj,  
  136.             D3DX_PI * 0.5f, // 90 - degree  
  137.             (float)Width / (float)Height,  
  138.             1.0f,  
  139.             1000.0f);  
  140.     Device->SetTransform(D3DTS_PROJECTION, &proj);  
  141.   
  142.     //设置雾效果  
  143.     SetFogEffects(caps,true);  
  144.   
  145.     Device->SetRenderState(D3DRS_AMBIENT,D3DXCOLOR(0.3f,0.4f,0.5f,1.0f));  
  146.     Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);  // 不剔除任何面  
  147.   
  148.     return true;  
  149. }  
  150.   
  151. void Cleanup()  
  152. {  
  153.     Frame.Delete<CBox*>(Box);  
  154.     Frame.Delete<CGround*>(Ground);  
  155.     Frame.Delete<CDrawtext*>(Text);  
  156.     Frame.Release<LPD3DXFONT>(Font);  
  157.     Frame.Release<IDirect3DTexture9*>(Texture2);  
  158.     Frame.Release<IDirect3DTexture9*>(Texture1);  
  159. }  
  160.   
  161. //设置雾特效函数  
  162. void SetFogEffects(D3DCAPS9 caps,bool isFogEnable)  
  163. {  
  164.     // 雾特效的开始和结束距离数值  
  165.     float start = 0, end = 8;  
  166.     // 设置雾的相关属性  
  167.     Device->SetRenderState(D3DRS_FOGENABLE, isFogEnable);  // 启用雾效果  
  168.     // 设置要渲染的雾的期望颜色(这里为灰色)  
  169.     Device->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(100, 128, 128));  
  170.     // 使用顶点雾  
  171.     Device->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);  
  172.     // 指定雾的开始距离和终点距离  
  173.     Device->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));  
  174.     Device->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));  
  175.     // Pixel Fog  像素雾  
  176.     //Device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);  
  177.     // 如果支持,则启用基于距离的雾  
  178.     if(caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE)  
  179.         Device->SetRenderState(D3DRS_RANGEFOGENABLE, true);  
  180. }  
  181.   
  182. //数值计算函数  
  183. bool Calculation(void)  
  184. {  
  185.     //*start:立方体(箱子)的矩阵变化数值计算  
  186.     Rxyz.x+=0.001f;  
  187.     Rxyz.y+=0.001f;  
  188.     Rxyz.z+=0.001f;  
  189.           
  190.     if(Rxyz.x>=2*D3DX_PI)       //当角度达到360时,赋值为零  
  191.         Rxyz.x=0.0f;  
  192.     if(Rxyz.y>=2*D3DX_PI)       //当角度达到360时,赋值为零  
  193.         Rxyz.y=0.0f;  
  194.     if(Rxyz.z>=2*D3DX_PI)       //当角度达到360时,赋值为零  
  195.         Rxyz.z=0.0f;      
  196.     //end:立方体(箱子)数值计算;  
  197.   
  198.   
  199.     //*start:文字绘制矩形区域数值变化  
  200.     timeConversion+=0.1f;       //速度同步数值累加  
  201.     if(timeConversion>=2.0f)    //timeConversion累加到2.0f是,执行文字矩形框的数值计算  
  202.     {  
  203.         timeConversion=0.1f;    //重置初始值  
  204.   
  205.         FontRect.left+=1;  
  206.         FontRect.top+=1;  
  207.         FontRect.right-=1;  
  208.         FontRect.bottom-=1;  
  209.   
  210.         if(FontRect.left>=Width/2)      //当矩形left大于100是重置为零  
  211.             FontRect.left=0;  
  212.         if(FontRect.top>=Height/2)      //当矩形top大于100是重置为零  
  213.             FontRect.top=0;  
  214.         if(FontRect.right<=Width/2)     //当矩形right小于400是重置为Width(窗口宽度)  
  215.             FontRect.right=Width;  
  216.         if(FontRect.bottom<=Height/2)   //当矩形bottom小于200是重置为Height(窗口高度)  
  217.             FontRect.bottom=Height;  
  218.     }  
  219.     //end:文字绘制矩形区域数值计算结束  
  220.   
  221.     return true;  
  222. }  
  223.   
  224. bool Display(float timeDelta)  
  225. {  
  226.     if( Device ) //如果设备对象不为空  
  227.     {  
  228.         //数值计算;  
  229.         Calculation();  
  230.           
  231.         Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(128,128,128),  1.0f, 0);  
  232.   
  233.         //开始渲染  
  234.         Device->BeginScene();  
  235.   
  236.         Device->SetTransform(D3DTS_VIEW, &V);  
  237.         //绘制立方体  
  238.         Tmatrix=Box->MatrixTranslation(Tmatrix,Txyz);  
  239.         Rmatrix=Box->MatrixRotation(Rmatrix,Rxyz);  
  240.         MATRIX=Tmatrix*Rmatrix;                 //平移矩阵和旋转矩阵相乘  
  241.         Box->Draw(&MATRIX,&material,Texture1);  //绘制最终立方体  
  242.   
  243.         Ground->DrawGround(&Map,&material,Texture2);    //绘制地形(地图)  
  244.   
  245.         //显示文字  
  246.         Text->DrawTextA(NULL,"雾特效,旋转的立方体,河流底面!~简单演示O(∩_∩)O哈哈~",  
  247.                         -1,&FontRect,DT_CENTER,D3DCOLOR_XRGB(255,255,255));   
  248.   
  249.         //结束渲染  
  250.         Device->EndScene();  
  251.           
  252.         Device->Present(0, 0, 0, 0);  
  253.     }  
  254.     return true;  
  255. }  
  256.   
  257. //  
  258. // 消息处理  
  259. //  
  260. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)  
  261. {  
  262.     switch( msg )  
  263.     {  
  264.     case WM_DESTROY:  
  265.         ::PostQuitMessage(0);  
  266.         break;  
  267.     case WM_KEYDOWN:  
  268.         switch(wParam)  
  269.         {  
  270.         case VK_ESCAPE:  
  271.             ::DestroyWindow(hwnd);  
  272.             break;  
  273.         case VK_LEFT:  
  274.             break;  
  275.         case VK_RIGHT:  
  276.             break;  
  277.         case VK_UP:  
  278.             break;  
  279.         case VK_DOWN:  
  280.             break;  
  281.         case VK_SPACE:  
  282.             isFogEnable=(isFogEnable==1?0:1);   //启动或关闭雾效果  
  283.             SetFogEffects(caps,isFogEnable);  
  284.             break;  
  285.         }  
  286.     }  
  287.     return ::DefWindowProc(hwnd, msg, wParam, lParam);  
  288. }  
  289.   
  290. //  
  291. // 入口函数  
  292. //  
  293. int WINAPI WinMain(HINSTANCE hinstance,  
  294.                    HINSTANCE prevInstance,   
  295.                    PSTR cmdLine,  
  296.                    int showCmd)  
  297. {  
  298.     WNDCLASS wc;  
  299.   
  300.     wc.style         = CS_HREDRAW | CS_VREDRAW;  
  301.     wc.lpfnWndProc   = (WNDPROC)WndProc;   
  302.     wc.cbClsExtra    = 0;  
  303.     wc.cbWndExtra    = 0;  
  304.     wc.hInstance     = hinstance;  
  305.     wc.hIcon         = LoadIcon(0, IDI_APPLICATION);  
  306.     wc.hCursor       = LoadCursor(0, IDC_ARROW);  
  307.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
  308.     wc.lpszMenuName  = 0;  
  309.     wc.lpszClassName = "Direct3D9App";  
  310.   
  311.     if( !RegisterClass(&wc) )   
  312.     {  
  313.         ::MessageBox(0, "RegisterClass() - FAILED", 0, 0);  
  314.         return false;  
  315.     }  
  316.   
  317.     //初始化D3D  
  318.     if(!Frame.InitD3D(hinstance,  
  319.         640, 480, true, D3DDEVTYPE_HAL, &Device))  
  320.     {  
  321.         ::MessageBox(0, "InitD3D() - FAILED", 0, 0);  
  322.         return 0;  
  323.     }  
  324.           
  325.     if(!Setup())  
  326.     {  
  327.         ::MessageBox(0, "Setup() - FAILED", 0, 0);  
  328.         return 0;  
  329.     }  
  330.   
  331.     //进入游戏循环  
  332.     Frame.EnterMsgLoop( Display );  
  333.   
  334.     Cleanup();  
  335.   
  336.     Device->Release();  
  337.   
  338.     return 0;  
  339. }  

关闭雾:

开启雾:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值