简单修改一下也能变成玻璃效果,天上掉玻璃了
主文件
- #include"D3DFrame.h"
- #include"Box.h"
- #include"Ground.h"
- #include"Drawtext.h"
- #include"Sprite.h"
- #include<d3d9.h>
- #include<d3dx9.h>
- voidSetFogEffects(D3DCAPS9caps,boolisFogEnable);
- IDirect3DDevice9*Device=0;
- IDirect3DTexture9*Texture1;
- IDirect3DTexture9*Texture2;
- IDirect3DTexture9*Texture3;
- LPD3DXFONTFont;
- CD3DFrameFrame;
- CGround*Ground=0;
- CBox*Box1=0;
- CBox*Box2=0;
- CDrawtext*Text=0;
- CSprite*Point;
- D3DMATERIAL9material;
- D3DXMATRIXMap;
- D3DXMATRIXV;
- D3DXVECTOR3Txyz1(-3,2,-3);
- D3DXVECTOR3Rxyz1(0,0);
- constintWidth=640;
- constintHeight=480;
- staticRECTFontRect={0,Width,Height};
- staticD3DXVECTOR3position(0.0f,3.0f,3.0f);
- staticD3DXVECTOR3target(0.0f,0.0f,0.0f);
- staticD3DXVECTOR3up(0.0f,1.0f,0.0f);
- D3DCAPS9caps;
- boolisFogEnable=true;
- staticfloattimeConversion=0.1f;
- boolSetup()
- {
- Box1=newCBox(Device);
- Box2=newCBox(Device);
- Ground=newCGround(Device);
- Text=newCDrawtext(Device,Font);
- Point=newCSprite(Device);
- Device->GetDeviceCaps(&caps);
- ::ZeroMemory(&material,sizeof(material));
- material.Ambient=D3DXCOLOR(1.0f,1.0f);
- material.Diffuse=D3DXCOLOR(1.0f,0.1f,1.0f);
- material.Emissive=D3DXCOLOR(1.0f,1.0f);
- material.Power=0.1f;
- material.Specular=D3DXCOLOR(0.0f,1.0f);
- D3DLIGHT9light;
- ::ZeroMemory(&light,sizeof(light));
- light.Type=D3DLIGHT_DIRECTIONAL;
- light.Ambient=D3DXCOLOR(1.0f,1.0f);
- light.Diffuse=D3DXCOLOR(1.0f,1.0f);
- light.Specular=D3DXCOLOR(1.0f,1.0f);
- light.Direction=D3DXVECTOR3(1.0f,-1.0f,0.0f);
- Device->SetLight(0,&light);
- Device->LightEnable(0,true);
- D3DLIGHT9light2;
- ::ZeroMemory(&light2,sizeof(light2));
- light2.Type=D3DLIGHT_DIRECTIONAL;
- light2.Ambient=D3DXCOLOR(1.0f,1.0f);
- light2.Diffuse=D3DXCOLOR(0.1f,0.5f,0.4f,1.0f);
- light2.Specular=D3DXCOLOR(1.0f,1.0f);
- light2.Direction=D3DXVECTOR3(0.0f,0.0f);
- Device->SetLight(1,&light2);
- Device->LightEnable(1,true);
- Device->SetRenderState(D3DRS_NORMALIZENORMALS,true);
- Device->SetRenderState(D3DRS_SPECULARENABLE,true);
- D3DXMatrixLookAtLH(&V,&position,&target,&up);
- Device->SetTransform(D3DTS_VIEW,&V);
- D3DXCreateTextureFromFile(
- Device,
- "crate.jpg",
- &Texture1);
- D3DXCreateTextureFromFile(
- Device,
- "河流.jpg",
- &Texture2);
- D3DXCreateTextureFromFile(
- Device,
- "雪球.tga",
- &Texture3);
- Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
- Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
- Device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
- Device->SetSamplerState(1,D3DTEXF_LINEAR);
- Device->SetSamplerState(1,D3DTEXF_LINEAR);
- Device->SetSamplerState(1,D3DTEXF_LINEAR);
- Device->SetSamplerState(3,D3DTEXF_LINEAR);
- Device->SetSamplerState(3,D3DTEXF_LINEAR);
- Device->SetSamplerState(3,D3DTEXF_LINEAR);
- D3DXMATRIXproj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI*0.5f,
- (float)Width/(float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION,&proj);
- SetFogEffects(caps,true);
- Device->SetRenderState(D3DRS_LIGHTING,true);
- Device->SetRenderState(D3DRS_AMBIENT,D3DXCOLOR(0.3f,1.0f));
- Device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
- returntrue;
- }
- voidCleanup()
- {
- Frame.Delete<CSprite*>(Point);
- Frame.Delete<CBox*>(Box2);
- Frame.Delete<CBox*>(Box1);
- Frame.Delete<CGround*>(Ground);
- Frame.Delete<CDrawtext*>(Text);
- Frame.Release<LPD3DXFONT>(Font);
- Frame.Release<IDirect3DTexture9*>(Texture3);
- Frame.Release<IDirect3DTexture9*>(Texture2);
- Frame.Release<IDirect3DTexture9*>(Texture1);
- }
- voidSetMatrixLookAt(D3DXMATRIX*V,D3DXVECTOR3*position,D3DXVECTOR3*target,D3DXVECTOR3*up)
- {
- D3DXMatrixLookAtLH(V,position,target,up);
- Device->SetTransform(D3DTS_VIEW,V);
- }
- voidSetFogEffects(D3DCAPS9caps,boolisFogEnable)
- {
- floatstart=12,end=15;
- Device->SetRenderState(D3DRS_FOGENABLE,isFogEnable);
- Device->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(128,128,128));
- Device->SetRenderState(D3DRS_FOGVERTEXMODE,D3DFOG_LINEAR);
- Device->SetRenderState(D3DRS_FOGSTART,*(DWORD*)(&start));
- Device->SetRenderState(D3DRS_FOGEND,*(DWORD*)(&end));
- if(caps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
- Device->SetRenderState(D3DRS_RANGEFOGENABLE,true);
- }
- boolCalculation(void)
- {
- Rxyz1.x+=0.005f;
- Rxyz1.y+=0.005f;
- Rxyz1.z+=0.005f;
- if(Rxyz1.x>=2*D3DX_PI)
- Rxyz1.x=0.0f;
- if(Rxyz1.y>=2*D3DX_PI)
- Rxyz1.y=0.0f;
- if(Rxyz1.z>=2*D3DX_PI)
- Rxyz1.z=0.0f;
- timeConversion+=0.1f;
- if(timeConversion>=2.0f)
- {
- timeConversion=0.1f;
- FontRect.left+=1;
- FontRect.top+=1;
- FontRect.right-=1;
- FontRect.bottom-=1;
- if(FontRect.left>=Width/2)
- FontRect.left=0;
- if(FontRect.top>=Height/2)
- FontRect.top=0;
- if(FontRect.right<=Width/2)
- FontRect.right=Width;
- if(FontRect.bottom<=Height/2)
- FontRect.bottom=Height;
- }
- returntrue;
- }
- boolDisplay(floattimeDelta)
- {
- if(Device)
- {
- Calculation();
- Device->Clear(0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(00,00,00),0);
- Device->BeginScene();
- Device->SetTransform(D3DTS_VIEW,&V);
- Box1->SetCalculation(Txyz1,Rxyz1);
- Box1->Draw(&material,Texture1);
- Box2->SetCalculation(D3DXVECTOR3(3,-3),D3DXVECTOR3(0,0));
- Box2->Draw(&material,Texture1);
- Ground->DrawGround(&Map,&material,Texture2);
- Text->DrawTextA(NULL,"雾特效,河流底面,雪花飘!~简单演示~",
- -1,&FontRect,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
- Point->Draw(Texture3);
- Device->EndScene();
- Device->Present(0,0);
- }
- returntrue;
- }
- LRESULTCALLBACKWndProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam)
- {
- switch(msg)
- {
- caseWM_DESTROY:
- ::PostQuitMessage(0);
- break;
- caseWM_KEYDOWN:
- switch(wParam)
- {
- caseVK_ESCAPE:
- ::DestroyWindow(hwnd);
- break;
- caseVK_LEFT:
- target.x+=0.2f;
- position.x+=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_RIGHT:
- target.x-=0.2f;
- position.x-=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_UP:
- target.z-=0.2f;
- position.z-=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_DOWN:
- target.z+=0.2f;
- position.z+=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_SHIFT:
- position.y+=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_CONTROL:
- position.y-=0.2f;
- SetMatrixLookAt(&V,&up);
- break;
- caseVK_SPACE:
- isFogEnable=(isFogEnable==1?0:1);
- SetFogEffects(caps,isFogEnable);
- break;
- }
- }
- return::DefWindowProc(hwnd,msg,wParam,lParam);
- }
- intWINAPIWinMain(HINSTANCEhinstance,
- HINSTANCEprevInstance,
- PSTRcmdLine,
- intshowCmd)
- {
- WNDCLASSwc;
- 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);
- returnfalse;
- }
- if(!Frame.InitD3D(hinstance,
- 640,480,true,D3DDEVTYPE_HAL,&Device))
- {
- ::MessageBox(0,"InitD3D()-FAILED",0);
- return0;
- }
- if(!Setup())
- {
- ::MessageBox(0,"Setup()-FAILED",0);
- return0;
- }
- Frame.EnterMsgLoop(Display);
- Cleanup();
- Device->Release();
- return0;
- }
两个粒子系统文件
- #pragmaonce
- #include<d3d9.h>
- #include<d3dx9.h>
- #include"Vertex.h"
- #definecount1000
- structSPRITE
- {
- D3DXVECTOR3pos;
- floatspeed;
- };
- classCSprite
- {
- public:
- CSprite(IDirect3DDevice9*device);
- ~CSprite(void);
- public:
- voidDraw(IDirect3DTexture9*texture);
- protected:
- voidUpdate(void);
- inlineunsignedlongFtoDW(floatv);
- public:
- IDirect3DDevice9*_device;
- IDirect3DVertexBuffer9*_vb;
- IDirect3DTexture9*_texture;
- protected:
- SPRITE_sprite[count];
- unsignedlong_col;
- };
- #include"Sprite.h"
- CSprite::CSprite(IDirect3DDevice9*device)
- {
- _device=device;
- _col=D3DCOLOR_XRGB(255,00);
- for(inti=0;i<=count;i++)
- {
- _sprite[i].pos=D3DXVECTOR3(0.0f,0.0f);
- _sprite[i].speed=0.0f;
- }
- _device->CreateVertexBuffer(
- count*sizeof(Vertex3),
- D3DUSAGE_WRITEONLY,
- FVF_VERTEX3,
- D3DPOOL_MANAGED,
- &_vb,
- 0);
- }
- CSprite::~CSprite(void)
- {
- if(_vb){_vb->Release();_vb=0;}
- }
- unsignedlongCSprite::FtoDW(floatv)
- {
- return*((unsignedlong*)&v);
- }
- voidCSprite::Draw(IDirect3DTexture9*texture)
- {
- _texture=texture;
- _device->SetTexture(0,_texture);
- Update();
- _device->SetRenderState(D3DRS_POINTSPRITEENABLE,TRUE);
- _device->SetRenderState(D3DRS_POINTSCALEENABLE,TRUE);
- _device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
- _device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
- _device->SetRenderState(D3DRS_POINTSIZE,FtoDW(0.1f));
- _device->SetRenderState(D3DRS_POINTSIZE_MIN,FtoDW(0.0f));
- _device->SetRenderState(D3DRS_POINTSCALE_A,FtoDW(0.0f));
- _device->SetRenderState(D3DRS_POINTSCALE_B,FtoDW(0.0f));
- _device->SetRenderState(D3DRS_POINTSCALE_C,FtoDW(1.0f));
- _device->SetFVF(FVF_VERTEX3);
- _device->SetStreamSource(0,_vb,sizeof(Vertex3));
- _device->DrawPrimitive(D3DPT_TRIANGLESTRIP,count);
- _device->SetRenderState(D3DRS_POINTSPRITEENABLE,FALSE);
- _device->SetRenderState(D3DRS_POINTSCALEENABLE,FALSE);
- _device->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
- }
- voidCSprite::Update()
- {
- Vertex3*v;
- _vb->Lock(0,(void**)&v,0);
- for(inti=0;i<count/4;i++)
- {
- _sprite[i].pos.y-=0.01f*_sprite[i].speed;
- if(_sprite[i].pos.y<0.0f)
- {
- ::ZeroMemory(&v[i],sizeof(v[i]));
- ::ZeroMemory(&_sprite[i],sizeof(_sprite[i]));
- ::ZeroMemory(&_sprite[i].speed,sizeof(_sprite[i].speed));
- _sprite[i].speed=((rand()%20)*0.1f);
- _sprite[i].pos.x=((rand()%100)*0.1f);
- _sprite[i].pos.y=5;
- _sprite[i].pos.z=((rand()%100)*-0.1f);
- }
- v[i]=Vertex3(_sprite[i].pos.x,_sprite[i].pos.y,_sprite[i].pos.z,255));
- }
- for(inti=count/4;i<count/2;i++)
- {
- _sprite[i].pos.y-=0.01f*_sprite[i].speed;
- if(_sprite[i].pos.y<0.0f)
- {
- ::ZeroMemory(&v[i],sizeof(_sprite[i].speed));
- _sprite[i].speed=((rand()%20)*0.1f);
- _sprite[i].pos.x=((rand()%100)*-0.1f);
- _sprite[i].pos.y=5.0f;
- _sprite[i].pos.z=((rand()%100)*-0.1f);
- }
- v[i]=Vertex3(_sprite[i].pos.x,255));
- }
- for(inti=count/2;i<count*3/4;i++)
- {
- _sprite[i].pos.y-=0.01f*_sprite[i].speed;
- if(_sprite[i].pos.y<0.0f)
- {
- ::ZeroMemory(&v[i],sizeof(v[i]));
- ::ZeroMemory(&_sprite[i],sizeof(_sprite[i]));
- ::ZeroMemory(&_sprite[i].speed,sizeof(_sprite[i].speed));
- _sprite[i].speed=((rand()%20)*0.1f);
- _sprite[i].pos.x=((rand()%100)*0.1f);
- _sprite[i].pos.y=5.0f;
- _sprite[i].pos.z=((rand()%100)*0.1f);
- }
- v[i]=Vertex3(_sprite[i].pos.x,Z正半轴粒子顶点缓存数据
- for(inti=count*3/4;i<=count;i++)
- {
- _sprite[i].pos.y-=0.01f*_sprite[i].speed;
- if(_sprite[i].pos.y<0.0f)
- {
- ::ZeroMemory(&v[i],sizeof(_sprite[i].speed));
- _sprite[i].speed=((rand()%20)*0.1f);
- _sprite[i].pos.x=((rand()%100)*-0.1f);
- _sprite[i].pos.y=5.0f;
- _sprite[i].pos.z=((rand()%100)*0.1f);
- }
- v[i]=Vertex3(_sprite[i].pos.x,255));
- }
- _vb->Unlock();
- }
这个代码有点重复,但也不修改了,只是测试一下,还是有漏洞 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|