home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
particles.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-10-08
|
11KB
|
410 lines
#include "main.h"
//------------------------------------------------------------------
// Name:
// Desc: konstruktor
//------------------------------------------------------------------
PARTICLE::PARTICLE()
{
MaxParticles = 0;
MaxFrames = 0;
ActParticle = 0;
ActVertex = 0;
Vertex = NULL;
g_pTexture = NULL;
g_pVB = NULL;
TextureID = NULL;
}
//------------------------------------------------------------------
// Name:
// Desc: destruktor
//------------------------------------------------------------------
PARTICLE::~PARTICLE()
{
//zmaz vertexi
if (Vertex != NULL)
delete [] Vertex;
Vertex = NULL;
//zmaz textury
if (g_pTexture)
{
for (int i=0;i<MaxFrames;i++)
{
if(g_pTexture[i])
{
g_pTexture[i]->Release();
}
}
delete[] g_pTexture ;
}
//odstran VB
if( g_pVB != NULL )
g_pVB->Release();
//default vlastnosti
MaxParticles = 0;
MaxFrames = 0;
ActParticle = 0;
ActVertex = 0;
}
//------------------------------------------------------------------
// Name: Initialize()
// Desc: zinicializuje premenne podla poctu snimkov alebo particlov
//------------------------------------------------------------------
void PARTICLE::Initialize(int MParticles,int MFrames)
{
//log info
char cBuf[80];
LogPrint("Vytvaram Particle");
sprintf(cBuf," MaxParticles: %d",MParticles);
sprintf(cBuf," MaxFrames: %d",MFrames);
MaxFrames = MFrames;
MaxParticles = MParticles;
//vytvori vertex
Vertex = new CUSTOMVERTEXPARTICLE[MaxParticles*6];
//inicializuje textruru
g_pTexture = new LPDIRECT3DTEXTURE9[MaxFrames];
for(int i=0;i<MaxFrames;i++)
{
g_pTexture[i] = NULL;
}
//texture id
TextureID = new int[MaxParticles];
//vytvori VB
if( FAILED( g_pd3dDevice->CreateVertexBuffer( MaxParticles*6*sizeof(CUSTOMVERTEXPARTICLE),
D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC, D3DFVF_CUSTOMVERTEXPARTICLE,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
LogPrint(" chyba pri vytvarani Vertex Bufferu");
}
else
{
LogPrint(" Vertex Buffer vytvoreny");
}
}
//------------------------------------------------------------------
// Name: RenderParticle()
// Desc: prida particle, particle natoceny ku kamere
//------------------------------------------------------------------
void PARTICLE::RenderParticle(VECTOR3D Pos,COLOR Color,float Size,float Rotation,float Frame)
{
VECTOR3D P[4]; //pomocne body
D3DXMATRIX Matica; //transformacna matica
D3DXMATRIX MatLook;
D3DXMATRIX MatRotZ;
//zada poziciu
P[0] = Get3D(-Size,Size,0.0f);
P[1] = Get3D(Size,Size,0.0f);
P[2] = Get3D(Size,-Size,0.0f);
P[3] = Get3D(-Size,-Size,0.0f);
//textura
TextureID[ActParticle] = (int)Frame;
//vypocita maticu
D3DXMatrixIdentity(&Matica);
D3DXVECTOR3 vEyePt( Pos.X, Pos.Y, Pos.Z );
D3DXVECTOR3 vLookatPt( Camera.Pos.X, Camera.Pos.Y,Camera.Pos.Z );
D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &MatLook, &vEyePt, &vLookatPt, &vUpVec );
D3DXMatrixInverse(&MatLook,NULL,&MatLook);
D3DXMatrixRotationZ(&MatRotZ,Rotation);
D3DXMatrixMultiply(&Matica,&Matica,&MatRotZ);
D3DXMatrixMultiply(&Matica,&Matica,&MatLook);
//transformuje jednotlive body
P[0] = TransformPoint(P[0],Matica);
P[1] = TransformPoint(P[1],Matica);
P[2] = TransformPoint(P[2],Matica);
P[3] = TransformPoint(P[3],Matica);
//vytvori triangle 1
//0
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[0].X,P[0].Y,P[0].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
//1
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[1].X,P[1].Y,P[1].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
//2
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[2].X,P[2].Y,P[2].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//vytvori triangle2
//2
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[2].X,P[2].Y,P[2].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//3
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[3].X,P[3].Y,P[3].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//0
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[0].X,P[0].Y,P[0].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
ActParticle++;
}
//------------------------------------------------------------------
// Name: RenderParticleSprite()
// Desc: prida particle, particle nezavysli od kamery
//------------------------------------------------------------------
void PARTICLE::RenderParticleSprite(VECTOR3D Pos,VECTOR3D Normal,COLOR Color,float Size,float Frame)
{
VECTOR3D P[4]; //pomocne body
D3DXMATRIXA16 Matica; //transformacna matica
VECTOR3D Rot; //rotacia
//zada poziciu
P[0] = Get3D(-Size,Size,0.0f);
P[1] = Get3D(Size,Size,0.0f);
P[2] = Get3D(Size,-Size,0.0f);
P[3] = Get3D(-Size,-Size,0.0f);
//vypocita rotaciu
Rot = GetRotationSme(Normal);
//textura
TextureID[ActParticle] = (int)Frame;
//tranformuje maticu
Matica = GetMatrix(Pos,Rot,Get3D(1.0f,1.0f,1.0f));
//transformuje jednotlive body
P[0] = TransformPoint(P[0],Matica);
P[1] = TransformPoint(P[1],Matica);
P[2] = TransformPoint(P[2],Matica);
P[3] = TransformPoint(P[3],Matica);
//vytvori triangle 1
//0
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[0].X,P[0].Y,P[0].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
//1
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[1].X,P[1].Y,P[1].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
//2
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[2].X,P[2].Y,P[2].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//vytvori triangle2
//2
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[2].X,P[2].Y,P[2].Z);
Vertex[ActVertex].tu = 1.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//3
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[3].X,P[3].Y,P[3].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 1.0f;
ActVertex++;
//0
Vertex[ActVertex].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
Vertex[ActVertex].pos = D3DXVECTOR3(P[0].X,P[0].Y,P[0].Z);
Vertex[ActVertex].tu = 0.0f;
Vertex[ActVertex].tv = 0.0f;
ActVertex++;
ActParticle++;
}
//------------------------------------------------------------------
// Name: AddFrame()
// Desc: prida texturu framu
//------------------------------------------------------------------
void PARTICLE::AddFrame(int FrameID,char *FileName,COLOR ColorKey)
{
char cBuf[80];
sprintf(cBuf," Textura: %d %s ",FrameID,FileName);
LogPrint(cBuf);
if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,
FileName,
D3DX_DEFAULT,
D3DX_DEFAULT,
Engine.MipMapLevels, //MipLevels
0,
Engine.TextureFormat,
Engine.TextureCreatingFlag,
D3DX_DEFAULT, //Filter
D3DX_DEFAULT, //MipFilter
D3DXCOLOR(ColorKey.R,
ColorKey.G,
ColorKey.B,
ColorKey.A),
NULL,
NULL,
&g_pTexture[FrameID])))
{
sprintf(cBuf," chyba pri vytvarani textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Textura particlu vytvorena");
}
}
//------------------------------------------------------------------
// Name: Render()
// Desc: Vyrenderuje vsetky pridane particli
//------------------------------------------------------------------
void PARTICLE::Render()
{
if (ActParticle == 0)
return;
//napln VB podla ID textury
CUSTOMVERTEXPARTICLE* pVert;
g_pVB->Lock( 0, 0, (void**)&pVert, D3DLOCK_NOOVERWRITE|D3DLOCK_DISCARD ) ;
int ActVer = 0;
int *Zaciatok = new int[MaxFrames*6]; //zaciatocny vertex
int *NumParticles =new int[MaxFrames]; //aktualny particle
for (int i=0;i<MaxFrames;i++)
{
Zaciatok[i] = ActVer; //zaciatok pola
NumParticles[i] = 0; //pocet particlov
for (int u=0;u<ActParticle;u++)
{
//ak je dany particle z materialu rpidaj
if (TextureID[u] == i)
{
pVert[ActVer+0] = Vertex[u*6+0];
pVert[ActVer+1] = Vertex[u*6+1];
pVert[ActVer+2] = Vertex[u*6+2];
pVert[ActVer+3] = Vertex[u*6+3];
pVert[ActVer+4] = Vertex[u*6+4];
pVert[ActVer+5] = Vertex[u*6+5];
ActVer = ActVer + 6 ;
NumParticles[i]++;
}
}
}
g_pVB->Unlock();
//vynuluje world maticu
D3DXMATRIXA16 NullMatrix;
D3DXMatrixIdentity(&NullMatrix);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &NullMatrix);
//renderuj podla textury
for (i=0;i<MaxFrames;i++)
{
if (g_pTexture[i] != NULL)
g_pd3dDevice->SetTexture( 0, g_pTexture[i]);
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXPARTICLE) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEXPARTICLE );
if (NumParticles[i] > 0)
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, Zaciatok[i], NumParticles[i]*2);
}
//vymaz pomocne polia
if (Zaciatok != NULL)
delete [] Zaciatok;
if (NumParticles != NULL)
delete [] NumParticles;
ActParticle = 0;
ActVertex = 0;
}