TrailFrag.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
- #include "TrailFrag.h"
- TrailFrag::TrailFrag(LPDIRECT3DDEVICE9 device, int life, int texnum)
- {
- this->device = device;
- this->life = life;
- this->texnum = texnum;
- this->textures = new LPDIRECT3DTEXTURE9[texnum];
- this->head = new Particle;
- this->head->next = NULL;
- this->trail = head;
- }
- TrailFrag::~TrailFrag()
- {
- }
- bool TrailFrag::Init()
- {
- SNOWVERTEX vertices[] =
- {
- {-1 , -1, 0, 0, 0},
- {-1, 1, 0, 0, 1},
- {1, 1, 0, 1, 1},
- {1, -1, 0, 1, 0}
- };
- if(FAILED(device->CreateVertexBuffer(4 * sizeof(SNOWVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &(this->v_frag), NULL)))
- {
- return false;
- }
- void *pVertices;
- if(FAILED(v_frag->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
- return false;
- memcpy(pVertices, vertices, sizeof(vertices));
- v_frag->Unlock();
- this->AddTexture("textures\particle.tga", 0);
- return true;
- }
- bool TrailFrag::AddTexture(char * path, int t)
- {
- if(FAILED(D3DXCreateTextureFromFile(device, path, &(textures[t]))))
- {
- MessageBox(NULL, "ERROR WHILE LOADING frag TEXTURES", "OceanFlame", MB_OK);
- return false;
- }
- return true;
- }
- void TrailFrag::Update()
- {
- Particle *p = this->head;
- while(p->next)
- {
- p = p->next;
- p->frames -= 1;
- if(p->frames < 0)
- {
- head->next = p->next;
- delete p;
- p = head->next;
- }
- p->pos.x+=0.5;
- p->pos.y+= 0.5;
- p->pos.z+=0.5;
-
- }
- }
- void TrailFrag::Render()
- {
- D3DXMATRIX matYaw, matPitch, matTrans;
- D3DXMATRIXA16 matWorld, tmp;
- Particle *p = this->head;
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
- device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
- device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- device->GetTransform(D3DTS_WORLD, &matWorld);
- tmp = matWorld;
- device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
- device->SetRenderState( D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- device->SetRenderState( D3DRS_LIGHTING, false);
- device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
-
- while(p->next)
- {
- p = p->next;
- D3DXMatrixRotationY(&matTrans, p->roty);
- D3DXMatrixTranslation(&matTrans, p->pos.x, p->pos.y, p->pos.z);
- // D3DXMatrixRotationZ(&matTrans, p->rotz * p->frames);
- device->SetTransform( D3DTS_WORLD, &matTrans);
- device->SetTexture( 0, this->textures[0]);//int(float(texnum) * float(p->frames) / float(this->life))] );
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
- this->renderFrag();
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
- }
-
- device->SetTransform(D3DTS_WORLD, &tmp);
- }
- void TrailFrag::renderFrag()
- {
- device->SetStreamSource(0, v_frag, 0, sizeof(SNOWVERTEX));
- device->SetFVF(D3DFVF_SNOWVERTEX);
- device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
- }
- void TrailFrag::newFrag(D3DXVECTOR3 pos, D3DXVECTOR3 vel, float roty, float rotz)
- {
- trail->next = new Particle;
- trail = trail->next;
- trail->pos = pos;
- trail->vel = vel;
- trail->frames = this->life;
- trail->roty = roty;
- trail->rotz = rotz;
- trail->next = NULL;
- }
- void TrailFrag::newFrag(float px, float py, float pz, float vx, float vy, float vz, float roty, float rotz)
- {
- this->newFrag(D3DXVECTOR3(px, py, pz), D3DXVECTOR3(vx, vy, vz), roty, rotz);
- }