代码太多,添加了三个类,把很多都封装起来了
按空格键开启或关闭雾效果
给出主文件:
- //
- // 文件: main.cpp
- // 作者:CYM
- // *雾特效,旋转的立方体,底面!~简单演示
- //
- #include "D3DFrame.h"
- #include "Box.h"
- #include "Ground.h"
- #include "Drawtext.h"
- #include "d3dx9.h"
- void SetFogEffects(D3DCAPS9 caps,bool isFogEnable); //设置雾效果函数声明
- //
- // 全局设置
- //
- IDirect3DDevice9* Device = 0; //定义设备对象
- IDirect3DTexture9* Texture1; //立方体纹理
- IDirect3DTexture9* Texture2; //地面地图纹理
- LPD3DXFONT Font; //定义字体
- CD3DFrame Frame; //定义框架类
- CGround* Ground=0; //定义场地类
- CBox* Box = 0; //定义Box对象
- CDrawtext* Text=0; //定义文字绘制对象
- D3DMATERIAL9 material;
- D3DXMATRIX Map; //地图矩阵
- D3DXMATRIX Tmatrix; //平移世界矩阵
- D3DXMATRIX Rmatrix; //旋转矩阵
- D3DXMATRIX MATRIX; //结合矩阵
- D3DXMATRIX V; //视图矩阵
- const int Width = 640; //窗口的宽高
- const int Height = 480;
- static D3DXVECTOR3 Txyz(0.0f,0.0f,0.0f); //矩阵平移位置坐标
- static D3DXVECTOR3 Rxyz(0.0f,0.0f,0.0f); //矩阵旋转角度坐标
- static RECT FontRect={0,0,Width,Height}; //绘制文字的矩形
- D3DCAPS9 caps; //设备功能支持的结构体
- bool isFogEnable=true; //判断雾效果是否开启
- static float timeConversion=0.1f; //时间转换(速度同步)数值
- //
- // 框架函数
- //
- bool Setup()
- {
- //创造对象
- Box = new CBox(Device);
- Ground = new CGround(Device);
- Text = new CDrawtext(Device,Font);
- Device->GetDeviceCaps(&caps); //获取设备功能支持信息的结构体
- //设置纹理
- //D3DMATERIAL9 material;
- ::ZeroMemory(&material, sizeof(material));
- material.Ambient=D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- material.Diffuse=D3DXCOLOR(1.0f, 1.0f, 0.1f, 1.0f);
- material.Emissive=D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- material.Power=0.1f;
- material.Specular=D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
- //设置平行光
- D3DLIGHT9 light;
- ::ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_DIRECTIONAL;
- light.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- light.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
- Device->SetLight(0, &light);
- Device->LightEnable(0, true);
- //设置平行光
- D3DLIGHT9 light2;
- ::ZeroMemory(&light2, sizeof(light2));
- light2.Type = D3DLIGHT_DIRECTIONAL;
- light2.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- light2.Diffuse = D3DXCOLOR(0.1f, 0.5f, 0.4f, 1.0f);
- light2.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- light2.Direction = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
- Device->SetLight(1, &light2);
- Device->LightEnable(1, true);
- Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); //正常的渲染等级
- Device->SetRenderState(D3DRS_SPECULARENABLE, true); //镜面高光
- D3DXVECTOR3 position( 0.0f, 0.0f, 4.0f );
- D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
- D3DXMatrixLookAtLH(&V, &position, &target, &up);
- Device->SetTransform(D3DTS_VIEW, &V);
- //
- // Create texture.
- //
- //载入立方体纹理
- D3DXCreateTextureFromFile(
- Device,
- "crate.jpg",
- &Texture1);
- //载入地面纹理
- D3DXCreateTextureFromFile(
- Device,
- "河流.jpg",
- &Texture2);
- //
- // 设置纹理过滤方式.
- //
- Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
- //
- // 设置投影矩阵.
- //
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.5f, // 90 - degree
- (float)Width / (float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- //设置雾效果
- SetFogEffects(caps,true);
- Device->SetRenderState(D3DRS_AMBIENT,D3DXCOLOR(0.3f,0.4f,0.5f,1.0f));
- Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // 不剔除任何面
- return true;
- }
- void Cleanup()
- {
- Frame.Delete<CBox*>(Box);
- Frame.Delete<CGround*>(Ground);
- Frame.Delete<CDrawtext*>(Text);
- Frame.Release<LPD3DXFONT>(Font);
- Frame.Release<IDirect3DTexture9*>(Texture2);
- Frame.Release<IDirect3DTexture9*>(Texture1);
- }
- //设置雾特效函数
- void SetFogEffects(D3DCAPS9 caps,bool isFogEnable)
- {
- // 雾特效的开始和结束距离数值
- float start = 0, end = 8;
- // 设置雾的相关属性
- Device->SetRenderState(D3DRS_FOGENABLE, isFogEnable); // 启用雾效果
- // 设置要渲染的雾的期望颜色(这里为灰色)
- Device->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(100, 128, 128));
- // 使用顶点雾
- Device->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
- // 指定雾的开始距离和终点距离
- Device->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
- Device->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));
- // Pixel Fog 像素雾
- //Device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
- // 如果支持,则启用基于距离的雾
- if(caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE)
- Device->SetRenderState(D3DRS_RANGEFOGENABLE, true);
- }
- //数值计算函数
- bool Calculation(void)
- {
- //*start:立方体(箱子)的矩阵变化数值计算
- Rxyz.x+=0.001f;
- Rxyz.y+=0.001f;
- Rxyz.z+=0.001f;
- if(Rxyz.x>=2*D3DX_PI) //当角度达到360时,赋值为零
- Rxyz.x=0.0f;
- if(Rxyz.y>=2*D3DX_PI) //当角度达到360时,赋值为零
- Rxyz.y=0.0f;
- if(Rxyz.z>=2*D3DX_PI) //当角度达到360时,赋值为零
- Rxyz.z=0.0f;
- //end:立方体(箱子)数值计算;
- //*start:文字绘制矩形区域数值变化
- timeConversion+=0.1f; //速度同步数值累加
- if(timeConversion>=2.0f) //timeConversion累加到2.0f是,执行文字矩形框的数值计算
- {
- timeConversion=0.1f; //重置初始值
- FontRect.left+=1;
- FontRect.top+=1;
- FontRect.right-=1;
- FontRect.bottom-=1;
- if(FontRect.left>=Width/2) //当矩形left大于100是重置为零
- FontRect.left=0;
- if(FontRect.top>=Height/2) //当矩形top大于100是重置为零
- FontRect.top=0;
- if(FontRect.right<=Width/2) //当矩形right小于400是重置为Width(窗口宽度)
- FontRect.right=Width;
- if(FontRect.bottom<=Height/2) //当矩形bottom小于200是重置为Height(窗口高度)
- FontRect.bottom=Height;
- }
- //end:文字绘制矩形区域数值计算结束
- return true;
- }
- bool Display(float timeDelta)
- {
- if( Device ) //如果设备对象不为空
- {
- //数值计算;
- Calculation();
- Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);
- //开始渲染
- Device->BeginScene();
- Device->SetTransform(D3DTS_VIEW, &V);
- //绘制立方体
- Tmatrix=Box->MatrixTranslation(Tmatrix,Txyz);
- Rmatrix=Box->MatrixRotation(Rmatrix,Rxyz);
- MATRIX=Tmatrix*Rmatrix; //平移矩阵和旋转矩阵相乘
- Box->Draw(&MATRIX,&material,Texture1); //绘制最终立方体
- Ground->DrawGround(&Map,&material,Texture2); //绘制地形(地图)
- //显示文字
- Text->DrawTextA(NULL,"雾特效,旋转的立方体,河流底面!~简单演示O(∩_∩)O哈哈~",
- -1,&FontRect,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
- //结束渲染
- Device->EndScene();
- Device->Present(0, 0, 0, 0);
- }
- return true;
- }
- //
- // 消息处理
- //
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch( msg )
- {
- case WM_DESTROY:
- ::PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- switch(wParam)
- {
- case VK_ESCAPE:
- ::DestroyWindow(hwnd);
- break;
- case VK_LEFT:
- break;
- case VK_RIGHT:
- break;
- case VK_UP:
- break;
- case VK_DOWN:
- break;
- case VK_SPACE:
- isFogEnable=(isFogEnable==1?0:1); //启动或关闭雾效果
- SetFogEffects(caps,isFogEnable);
- break;
- }
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //
- // 入口函数
- //
- int WINAPI WinMain(HINSTANCE hinstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- WNDCLASS wc;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hinstance;
- wc.hIcon = LoadIcon(0, IDI_APPLICATION);
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = 0;
- wc.lpszClassName = "Direct3D9App";
- if( !RegisterClass(&wc) )
- {
- ::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
- return false;
- }
- //初始化D3D
- if(!Frame.InitD3D(hinstance,
- 640, 480, true, D3DDEVTYPE_HAL, &Device))
- {
- ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
- return 0;
- }
- if(!Setup())
- {
- ::MessageBox(0, "Setup() - FAILED", 0, 0);
- return 0;
- }
- //进入游戏循环
- Frame.EnterMsgLoop( Display );
- Cleanup();
- Device->Release();
- return 0;
- }
关闭雾:
开启雾: