home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
model.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-10-03
|
49KB
|
1,801 lines
#include "main.h"
//------------------------------------------------------------------
// Name: MODEL()
// Desc: konstruktor
//------------------------------------------------------------------
MODEL::MODEL()
{
g_pVB = NULL;
g_pTexture = NULL;
g_pEnviroMap = NULL;
ModelFrame;
Color = GetColor(1.0f,1.0f,1.0f,1.0f);
Lighting = true ;
Specular = false ;
BumpMap = false;
EnviromentMapping = false;
MultiTexture = false;
OnlyShadow = false;
Textured = true;
FrustrumTest = true;
SmoothShading = true;
Normals = true;
Frame = 0.0f;
NumFrames = 0;
NumFaces = 0;
Pos = Get3D(0.0f,0.0f,0.0f);
Rot = Get3D(0.0f,0.0f,0.0f);
Sca = Get3D(1.0f,1.0f,1.0f);
}
//------------------------------------------------------------------
// Name: ~MODEL()
// Desc: destruktor
//------------------------------------------------------------------
MODEL::~MODEL()
{
if (g_pTexture != NULL)
g_pTexture->Release();
g_pTexture = NULL;
if (g_pEnviroMap != NULL)
g_pEnviroMap->Release();
g_pEnviroMap = NULL;
if (g_pVB != NULL)
g_pVB->Release();
g_pVB = NULL;
//znicmodel
DestroyModel(&ModelFrame);
}
//------------------------------------------------------------------
// Name: DestroyModel()
// Desc: Znic model
//------------------------------------------------------------------
void MODEL::DestroyModel(MODELFRAME *Mod)
{
if (Mod->FaceList != NULL)
delete [] Mod->FaceList;
Mod->FaceList = NULL;
}
//------------------------------------------------------------------
// Name: InitializeModel()
// Desc: inicializuj model podla poctu trojholnikov
//------------------------------------------------------------------
void MODEL::InitializeModel(MODELFRAME *Mod,int NFaces)
{
Mod->FaceList = new MODELFACE[NFaces];
Mod->NumFaces = NFaces;
Mod->ActFace = 0;
Mod->Centre = Get3D(0.0f,0.0f,0.0f);
}
//------------------------------------------------------------------
// Name: GetNumFace()
// Desc: Zisti pocet Face z Ase suboru
//------------------------------------------------------------------
int MODEL::GetNumFace(char *FileName)
{
FILE *fp;
char cBuf[80];
int Vys = 0;
int Pom = 0;
fp = fopen(FileName,"r");
if (fp == NULL)
{
sprintf(cBuf," Nenasiel som subor %s",FileName);
LogPrint(cBuf);
}
while(!feof(fp))
{
fscanf(fp,"%s",cBuf);
if (strcmp("*MESH_NUMFACES",cBuf) == 0)
{
fscanf(fp,"%d",&Pom);
Vys = Vys + Pom;
}
}
fclose(fp);
return Vys;
}
//------------------------------------------------------------------
// Name: InitializeAse()
// Desc: zinicializuje polia podla Ase modelu
//------------------------------------------------------------------
void MODEL::InitializeAse(int NFrames,char *FileName)
{
char cBuf[80];
LogPrint("Inicializujem ASE model");
//nacita pocet trojholnikov z Ase suboru
NumFrames = NFrames;
NumFaces = GetNumFace(FileName);
sprintf(cBuf," NumFrames: %d",NumFrames);
LogPrint(cBuf);
sprintf(cBuf," NumFaces: %d",NumFaces);
LogPrint(cBuf);
//inicializuje model
InitializeModel(&ModelFrame,NumFaces);
//vytvaranie vertex buffera
if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumFaces*3*sizeof(CUSTOMVERTEXMODEL),
D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEXMODEL,
D3DPOOL_DEFAULT, &g_pVB, NULL )))
{
LogPrint(" Chyba pri vytvarani VB");
}
else
{
LogPrint(" Vertex Buffer vytvoreny");
}
}
//------------------------------------------------------------------
// Name: LoadAse()
// Desc: loadne ase model
//------------------------------------------------------------------
void MODEL::LoadAse(int IntoFrame,char *FileName)
{
FILE *fp; //subor
char cBuf[130]; //nacitavanie
fp = fopen(FileName,"r");
if (fp == NULL)
{
sprintf(cBuf," Nenasiel som subor %s",FileName);
LogPrint(cBuf);
}
//ak narazis na GEOMOBJECT nacitaj node
while(!feof(fp))
{
fscanf(fp,"%s",cBuf);
//----------------*GEOMOBJECT----------------
if (strcmp(cBuf,"*GEOMOBJECT") == 0)
{
AddNode(&ModelFrame,fp);
continue;
}
}
fclose(fp);
//vypocitajminmax
CalcBoxPoints(&ModelFrame);
//vypocitaj normaly
if (Normals == true)
CalcFaceNormals(&ModelFrame);
//vypocitaj smooth shading
if (SmoothShading == true)
CalcSmoothShading(&ModelFrame);
//------------------
// Naplni VB
//------------------
int ActVertex = 0;
//pomocne
int u,i;
//Vertex do ktoreho sa uklada
CUSTOMVERTEXMODEL *Vertex;
//Otvor VB
g_pVB->Lock(0, 0,(void**)&Vertex,D3DLOCK_NOSYSLOCK) ;
//zobrazi model do pola vertexov
for (i = 0;i<NumFaces;i++)
{
for (u=0;u<3;u++)
{
Vertex[ActVertex].pos.x = ModelFrame.FaceList[i].P[u].X;
Vertex[ActVertex].pos.y = ModelFrame.FaceList[i].P[u].Y;
Vertex[ActVertex].pos.z = ModelFrame.FaceList[i].P[u].Z;
Vertex[ActVertex].normal.x = ModelFrame.FaceList[i].N[u].X;
Vertex[ActVertex].normal.y = ModelFrame.FaceList[i].N[u].Y;
Vertex[ActVertex].normal.z = ModelFrame.FaceList[i].N[u].Z;
Vertex[ActVertex].tu = ModelFrame.FaceList[i].T[u].X;
Vertex[ActVertex].tv = ModelFrame.FaceList[i].T[u].Y;
Vertex[ActVertex].color = D3DCOLOR_COLORVALUE(Color.R,
Color.G,
Color.B,
Color.A);
ActVertex = ActVertex + 1;
}
}
//uzavri VB
g_pVB->Unlock() ;
}
//------------------------------------------------------------------
// Name: AddNode()
// Desc: prida do modelu geom objekt
//------------------------------------------------------------------
void MODEL::AddNode(MODELFRAME *Mod,FILE *fp)
{
//zasobniky vertexov
VECTOR3D *V = NULL;
VECTOR3D *T = NULL;
char cBuf[80]; //nacitavanie
int NumV; //pocet vertexov
int NumT; //pocet texture vertexov
int NumF; //pocet facov
D3DXMATRIX RotationMatrix;
D3DXVECTOR3 RotVector;
float RotAngle;
while(1==1)
{
//nacitaj retazec
fscanf(fp,"%s",cBuf);
//-----------*TM_ROTAXIS----------------
if (strcmp(cBuf,"*TM_ROTAXIS") == 0)
{
fscanf(fp,"%f %f %f",&RotVector.x,&RotVector.z,&RotVector.y);
continue;
}
//-----------*TM_ROTANGLE---------------
if (strcmp(cBuf,"*TM_ROTANGLE") == 0)
{
fscanf(fp,"%f",&RotAngle);
//vypocita maticu rotacie
D3DXMatrixIdentity(&RotationMatrix);
D3DXMatrixRotationAxis(&RotationMatrix,&RotVector,RotAngle);
continue;
}
//-----------*MESH_NUMVERTEX------------
if (strcmp(cBuf,"*MESH_NUMVERTEX") == 0)
{
fscanf(fp,"%d",&NumV);
V = new VECTOR3D[NumV];
continue;
}
//-----------*MESH_NUMTVERTEX------------
if (strcmp(cBuf,"*MESH_NUMTVERTEX") == 0)
{
fscanf(fp,"%d",&NumT);
T = new VECTOR3D[NumT];
continue;
}
//-----------*MESH_NUMFACES-------------
if (strcmp(cBuf,"*MESH_NUMFACES") == 0)
{
fscanf(fp,"%d",&NumF);
continue;
}
//-----------*MESH_VERTEX---------------
if (strcmp(cBuf,"*MESH_VERTEX") == 0)
{
int Id;
float Xs, Ys, Zs;
fscanf(fp,"%d %f %f %f",&Id,&Xs,&Zs,&Ys);
V[Id] = Get3D(Xs,Ys,Zs);
continue;
}
//-----------*MESH_FACE-----------------
if (strcmp(cBuf,"*MESH_FACE") == 0)
{
int Id = Mod->ActFace;
int V1,V2,V3;
fscanf(fp,"%s %s %d %s %d %s %d",cBuf,cBuf,&V1,cBuf,&V2,cBuf,&V3);
Mod->FaceList[Id].P[0] = V[V3];
Mod->FaceList[Id].P[1] = V[V2];
Mod->FaceList[Id].P[2] = V[V1];
Mod->ActFace = Mod->ActFace + 1;
continue;
}
//-----------*MESH_TVERT----------------
if (strcmp(cBuf,"*MESH_TVERT") == 0)
{
int Idf;
float Xs, Ys, Zs;
fscanf(fp,"%d %f %f %f",&Idf,&Xs,&Ys,&Zs);
T[Idf] = Get3D(Xs,-Ys,Zs);
continue;
}
//-----------*MESH_TFACE----------------
if (strcmp(cBuf,"*MESH_TFACE") == 0)
{
int Id = Mod->ActFace;
int Idf;
int V1,V2,V3;
fscanf(fp,"%d %d %d %d",&Idf,&V1,&V2,&V3);
Mod->FaceList[Id - NumF + Idf].T[0].X = T[V3].X;
Mod->FaceList[Id - NumF + Idf].T[0].Y = T[V3].Y;
Mod->FaceList[Id - NumF + Idf].T[1].X = T[V2].X;
Mod->FaceList[Id - NumF + Idf].T[1].Y = T[V2].Y;
Mod->FaceList[Id - NumF + Idf].T[2].X = T[V1].X;
Mod->FaceList[Id - NumF + Idf].T[2].Y = T[V1].Y;
continue;
}
//---------*MESH_FACENORMAL----------------
if (strcmp(cBuf,"*MESH_FACENORMAL") == 0)
{
float X,Y,Z;
int Id ;
int VId;
fscanf(fp,"%d",&Id);
Id = Mod->ActFace-NumF+Id;
fscanf(fp,"%d %f %f %f",&VId,&X,&Z,&Y);
//v0
fscanf(fp,"%s %d %f %f %f",cBuf,&VId,&X,&Z,&Y);
Mod->FaceList[Id].N[2] = Get3D(X,Y,Z);
Mod->FaceList[Id].N[2] = TransformNormal(Mod->FaceList[Id].N[2], RotationMatrix);
//v1
fscanf(fp,"%s %d %f %f %f",cBuf,&VId,&X,&Z,&Y);
Mod->FaceList[Id].N[1] = Get3D(X,Y,Z);
Mod->FaceList[Id].N[1] = TransformNormal(Mod->FaceList[Id].N[1], RotationMatrix);
//v2
fscanf(fp,"%s %d %f %f %f",cBuf,&VId,&X,&Z,&Y);
Mod->FaceList[Id].N[0] = Get3D(X,Y,Z);
Mod->FaceList[Id].N[0] = TransformNormal(Mod->FaceList[Id].N[0], RotationMatrix);
//plane
VECTOR3D N,V1,V2;
Sub(&V1,Mod->FaceList[Id].P[0],Mod->FaceList[Id].P[1]);
Sub(&V2,Mod->FaceList[Id].P[2],Mod->FaceList[Id].P[1]);
Cross(&N,V1,V2);
Normalize(&N);
Mod->FaceList[Id].Plane.Normal = N;
Mod->FaceList[Id].Plane.D = -(N.X * Mod->FaceList[Id].P[1].X)
-(N.Y * Mod->FaceList[Id].P[1].Y)
-(N.Z * Mod->FaceList[Id].P[1].Z);
continue;
}
//-----------END GEOM OBJ--------------
if (strcmp(cBuf,"*PROP_MOTIONBLUR") == 0)
{
break;
}
}
if (V != NULL)
delete [] V;
if (T != NULL)
delete [] T;
}
//------------------------------------------------------------------
// Name: LoadTexture()
// Desc: Loadne texturu
//------------------------------------------------------------------
void MODEL::LoadTexture(char *FileName,COLOR ColorKey)
{
char cBuf[80];
sprintf(cBuf," Textura: %s",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), //ColourKey
NULL,
NULL,
&g_pTexture)))
{
sprintf(cBuf," chyba pri vytvarani textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Textura modelu vytvorena");
}
}
//------------------------------------------------------------------
// Name: LoadTexture()
// Desc: Loadne texturu
//------------------------------------------------------------------
void MODEL::LoadEnviroMap(char *FileName)
{
char cBuf[80];
sprintf(cBuf," Enviroment Textura: %s",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
0, //ColourKey
NULL,
NULL,
&g_pEnviroMap)))
{
sprintf(cBuf," chyba pri vytvarani Enviroment Textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Enviroment Textura modelu vytvorena");
}
}
//------------------------------------------------------------------
// Name: RenderModelNormal()
// Desc: Ulozi model do Vertex pola + vyrenderuje normalne
//------------------------------------------------------------------
void MODEL::RenderModelNormal(MODELFRAME Mod)
{
//
//TEXTURED
//
if (Textured == true)
{
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
}
else
{
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
}
//nastav material
g_pd3dDevice->SetMaterial(&Material);
//zapni vypni svetla
Engine.SetLighting(Lighting);
//zapni specular ak je zapnuty
Engine.SetSpecular(Specular);
//zapni dx back face culling
Engine.SetCullMode(D3DCULL_CCW);
//renderuje z VB
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXMODEL));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXMODEL);
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, NumFaces);
//reset to default
Engine.ResetToDefault();
}
//------------------------------------------------------------------
// Name: RenderModelOnlyShadow()
// Desc: Ulozi model do Vertex pola + iba pre tien
//------------------------------------------------------------------
void MODEL::RenderModelOnlyShadow(MODELFRAME Mod)
{
int ActVertex = 0;
//
//TEXTURED
//
if (Textured == true)
{
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
}
else
{
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
}
//reset to default
Engine.ResetToDefault();
}
//------------------------------------------------------------------
// Name: RenderModelEnvironment()
// Desc: Ulozi model do Vertex pola + vyrenderuje cez enviro mapping
//------------------------------------------------------------------
void MODEL::RenderModelEnvironment(MODELFRAME Mod)
{
int ActVertex = 0;
//pomocne
VECTOR3D P,N;
int u,i;
//Vertex do ktoreho sa uklada
CUSTOMVERTEXMODEL *Vertex;
//Otvor VB
g_pVB->Lock(0, 0, (void**)&Vertex, D3DLOCK_DISCARD|D3DLOCK_NOOVERWRITE ) ;
//zobrazi model do pola vertexov
for (i = 0;i<NumFaces;i++)
{
for (u=0;u<3;u++)
{
Vertex[ActVertex].pos.x = Mod.FaceList[i].P[u].X;
Vertex[ActVertex].pos.y = Mod.FaceList[i].P[u].Y;
Vertex[ActVertex].pos.z = Mod.FaceList[i].P[u].Z;
Vertex[ActVertex].normal.x = Mod.FaceList[i].N[u].X;
Vertex[ActVertex].normal.y = Mod.FaceList[i].N[u].Y;
Vertex[ActVertex].normal.z = Mod.FaceList[i].N[u].Z;
Vertex[ActVertex].tu = Mod.FaceList[i].T[u].X;
Vertex[ActVertex].tv = Mod.FaceList[i].T[u].Y;
Vertex[ActVertex].color = D3DXCOLOR(Color.R,
Color.G,
Color.B,
Color.A);
//////////////////////
//ENVIROMENT MAPPING//
//////////////////////
P = TransformPoint(Mod.FaceList[i].P[u],Matica);
N = TransformNormal(Mod.FaceList[i].N[u],Matica);
Mod.FaceList[i].T[u] = EnvironmentMapping(P,N,400.0f,matView);
Vertex[ActVertex].tu = Mod.FaceList[i].T[u].X;
Vertex[ActVertex].tv = Mod.FaceList[i].T[u].Y;
ActVertex = ActVertex + 1;
}
}
//uzavri VB
g_pVB->Unlock() ;
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
//nastav material
g_pd3dDevice->SetMaterial(&Material);
//zapni vypni svetla
Engine.SetLighting(Lighting);
//zapni specular ak je zapnuty
Engine.SetSpecular(Specular);
//renderuje z VB
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXMODEL));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXMODEL);
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, NumFaces);
//reset to default
Engine.ResetToDefault();
}
//------------------------------------------------------------------
// Name: RenderModelMultiTexture()
// Desc: Ulozi model do Vertex pola + vyrenderuje cez enviro mapping
//------------------------------------------------------------------
void MODEL::RenderModelMultiTexture(MODELFRAME Mod)
{
int ActVertex = 0;
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTexture( 1, g_pEnviroMap );
D3DXMATRIX mat;
mat._11 = 0.65f; mat._12 = 0.00f; mat._13 = 0.00f; mat._14 = 0.00f;
mat._21 = 0.00f; mat._22 = 0.65f; mat._23 = 0.00f; mat._24 = 0.00f;
mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
mat._41 = 0.00f; mat._42 = 0.00f; mat._43 = 0.00f; mat._44 = 1.00f;
g_pd3dDevice->SetTransform( D3DTS_TEXTURE1, &mat );
g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADD );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
//nastav material
g_pd3dDevice->SetMaterial(&Material);
//zapni vypni svetla
Engine.SetLighting(Lighting);
//zapni specular ak je zapnuty
Engine.SetSpecular(Specular);
//renderuje z VB
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXMODEL));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXMODEL);
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, NumFaces);
//reset to default
Engine.ResetToDefault();
D3DXMatrixIdentity(&mat);
g_pd3dDevice->SetTransform( D3DTS_TEXTURE1,&mat );
}
//------------------------------------------------------------------
// Name: RenderModelBumpMap()
// Desc: Ulozi model do Vertex pola + vyrenderuje cez bump mapping
//------------------------------------------------------------------
void MODEL::RenderModelBumpMap(MODELFRAME Mod)
{
int ActVertex = 0;
//pomocne
// VECTOR3D N;
// VECTOR2D T;
int u,i;
//Vertex do ktoreho sa uklada
CUSTOMVERTEXMODEL *Vertex;
//Otvor VB
g_pVB->Lock(0, 0, (void**)&Vertex,D3DLOCK_DISCARD|D3DLOCK_NOOVERWRITE) ;
//zobrazi model do pola vertexov
for (i = 0;i<NumFaces;i++)
{
for (u=0;u<3;u++)
{
Vertex[ActVertex].pos.x = Mod.FaceList[i].P[u].X;
Vertex[ActVertex].pos.y = Mod.FaceList[i].P[u].Y;
Vertex[ActVertex].pos.z = Mod.FaceList[i].P[u].Z;
Vertex[ActVertex].normal.x = Mod.FaceList[i].N[u].X;
Vertex[ActVertex].normal.y = Mod.FaceList[i].N[u].Y;
Vertex[ActVertex].normal.z = Mod.FaceList[i].N[u].Z;
Vertex[ActVertex].tu = Mod.FaceList[i].T[u].X;
Vertex[ActVertex].tv = Mod.FaceList[i].T[u].Y;
Vertex[ActVertex].color = D3DXCOLOR(Color.R,
Color.G,
Color.B,
Color.A);
/*
////////////////
//BUMP MAPPING//
////////////////
N = TransformNormal(Mod.FaceList[i].N[u],Matica);
T = BumpMapping(N,Mod.FaceList[i].T[u],0.05f,matView);
//suradnice pre bump map
Vertex[ActVertex].tu2 = T.X;
Vertex[ActVertex].tv2 = T.Y;
*/
ActVertex = ActVertex + 1;
}
}
//uzavri VB
g_pVB->Unlock() ;
////////////////
//BUMP MAPPING//
////////////////
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTexture( 1, g_pTexture );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_SELECTARG2 );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_ADDSIGNED );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE | D3DTA_COMPLEMENT );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_CURRENT );
Engine.SetBlendCustom(D3DBLEND_SRCALPHA,D3DBLEND_ZERO);
//nastav material
g_pd3dDevice->SetMaterial(&Material);
//zapni vypni svetla
Engine.SetLighting(Lighting);
//zapni specular ak je zapnuty
Engine.SetSpecular(Specular);
//renderuje z VB
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXMODEL));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXMODEL);
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, NumFaces);
//reset to default
Engine.ResetToDefault();
//zresetuj blending
Engine.SetBlendNone();
}
//------------------------------------------------------------------
// Name: Render()
// Desc: Vyrenderuje
//------------------------------------------------------------------
void MODEL::Render()
{
if (Frame >= NumFrames)
Frame = 0.0f;
//vypocita maticu
Matica = GetMatrix(Pos,Rot,Sca);
//ziska polomer a minmax
CalcMinMax(&ModelFrame,Matica);
//ak model vidno renderuj ho
if ((Camera.FrustrumSphere(ModelFrame.Centre,ModelFrame.Polomer) == true) ||
(FrustrumTest == false))
{
//nastavi maticu
g_pd3dDevice->SetTransform(D3DTS_WORLD, &Matica);
//renderuj model
if (EnviromentMapping == true)
RenderModelEnvironment(ModelFrame);
if (MultiTexture == true && SpecularEffects == 1)
RenderModelMultiTexture(ModelFrame);
else if (BumpMap == true)
RenderModelBumpMap(ModelFrame);
else if (OnlyShadow == true)
RenderModelOnlyShadow(ModelFrame);
else
RenderModelNormal(ModelFrame);
}
}
//------------------------------------------------------------------
// Name: RenderFast()
// Desc: Rychle vykreslenie
//------------------------------------------------------------------
void MODEL::RenderFast()
{
/*
if (Frame >= NumFrames)
Frame = 0.0f;
//vypocita maticu
Matica = GetMatrix(Pos,Rot,Sca);
//ziska polomer a minmax
CalcMinMax(&ModelFrame[(int)Frame],Matica);
//ulozi min max do transformacneho modelu
ModelT.Min = ModelFrame[(int)Frame].Min;
ModelT.Max = ModelFrame[(int)Frame].Max;
ModelT.Centre = ModelFrame[(int)Frame].Centre;
ModelT.Polomer = ModelFrame[(int)Frame].Polomer;
//ak model vidno renderuj ho
if ((Camera.FrustrumSphere(ModelT.Centre,ModelT.Polomer) == true) ||
(FrustrumTest == false))
{
//interpoluje model podla animacie
InterpolateModel(&ModelT);
//nastavi maticu
g_pd3dDevice->SetTransform( D3DTS_WORLD, &Matica);
//renderuj model
if (EnviromentMapping == true)
RenderModelEnvironment(ModelT);
else if (BumpMap == true)
RenderModelBumpMap(ModelT);
else
RenderModelNormal(ModelT);
}*/
}
//------------------------------------------------------------------
// Name: CalcFaceNormals()
// Desc: Vypocita normaly
//------------------------------------------------------------------
void MODEL::CalcFaceNormals(MODELFRAME *Mod)
{
VECTOR3D V1;
VECTOR3D V2;
VECTOR3D N;
for (int i=0;i<NumFaces;i++)
{
//normalovy vektor
Sub(&V1,Mod->FaceList[i].P[0],Mod->FaceList[i].P[1]);
Sub(&V2,Mod->FaceList[i].P[2],Mod->FaceList[i].P[1]);
Cross(&N,V1,V2);
Normalize(&N);
//kazdy vertex
Mod->FaceList[i].N[0] = N;
Mod->FaceList[i].N[1] = N;
Mod->FaceList[i].N[2] = N;
//vypocet plane trojholnika
Mod->FaceList[i].Plane.Normal = N;
Mod->FaceList[i].Plane.D = -(N.X * Mod->FaceList[i].P[1].X)
-(N.Y * Mod->FaceList[i].P[1].Y)
-(N.Z * Mod->FaceList[i].P[1].Z);
}
}
//------------------------------------------------------------------
// Name: CalcBoxPoints()
// Desc: Vypocita body krabice
//------------------------------------------------------------------
void MODEL::CalcBoxPoints(MODELFRAME *Mod)
{
VECTOR3D Min = {10000.0f,10000.0f,10000.0f};
VECTOR3D Max = {-10000.0f,-10000.0f,-10000.0f};
for (int i=0;i<NumFaces;i++)
{
//
//min max
//
for (int u=0;u<3;u++)
{
if (Mod->FaceList[i].P[u].X < Min.X)
Min.X = Mod->FaceList[i].P[u].X;
if (Mod->FaceList[i].P[u].Y < Min.Y)
Min.Y = Mod->FaceList[i].P[u].Y;
if (Mod->FaceList[i].P[u].Z < Min.Z)
Min.Z = Mod->FaceList[i].P[u].Z;
if (Mod->FaceList[i].P[u].X > Max.X)
Max.X = Mod->FaceList[i].P[u].X;
if (Mod->FaceList[i].P[u].Y > Max.Y)
Max.Y = Mod->FaceList[i].P[u].Y;
if (Mod->FaceList[i].P[u].Z > Max.Z)
Max.Z = Mod->FaceList[i].P[u].Z;
}
}
//
//body
//
//spodna plocha
Mod->P[0].X = Min.X;
Mod->P[0].Y = Min.Y;
Mod->P[0].Z = Min.Z;
Mod->P[1].X = Max.X;
Mod->P[1].Y = Min.Y;
Mod->P[1].Z = Min.Z;
Mod->P[2].X = Max.X;
Mod->P[2].Y = Min.Y;
Mod->P[2].Z = Max.Z;
Mod->P[3].X = Min.X;
Mod->P[3].Y = Min.Y;
Mod->P[3].Z = Max.Z;
//vrchna plocha
Mod->P[4].X = Min.X;
Mod->P[4].Y = Max.Y;
Mod->P[4].Z = Min.Z;
Mod->P[5].X = Max.X;
Mod->P[5].Y = Max.Y;
Mod->P[5].Z = Min.Z;
Mod->P[6].X = Max.X;
Mod->P[6].Y = Max.Y;
Mod->P[6].Z = Max.Z;
Mod->P[7].X = Min.X;
Mod->P[7].Y = Max.Y;
Mod->P[7].Z = Max.Z;
}
//------------------------------------------------------------------
// Name: CalcBoxPoints()
// Desc: Vypocita body krabice
//------------------------------------------------------------------
void MODEL::CalcMinMax(MODELFRAME *Mod,D3DXMATRIXA16 Matica)
{
VECTOR3D Min = {10000.0f,10000.0f,10000.0f};
VECTOR3D Max = {-10000.0f,-10000.0f,-10000.0f};
VECTOR3D P[8];
P[0] = TransformPoint(Mod->P[0],Matica);
P[1] = TransformPoint(Mod->P[1],Matica);
P[2] = TransformPoint(Mod->P[2],Matica);
P[3] = TransformPoint(Mod->P[3],Matica);
P[4] = TransformPoint(Mod->P[4],Matica);
P[5] = TransformPoint(Mod->P[5],Matica);
P[6] = TransformPoint(Mod->P[6],Matica);
P[7] = TransformPoint(Mod->P[7],Matica);
for (int i=0;i<8;i++)
{
//
//min max
//
if (P[i].X < Min.X)
Min.X = P[i].X;
if (P[i].Y < Min.Y)
Min.Y = P[i].Y;
if (P[i].Z < Min.Z)
Min.Z = P[i].Z;
if (P[i].X > Max.X)
Max.X = P[i].X;
if (P[i].Y > Max.Y)
Max.Y = P[i].Y;
if (P[i].Z > Max.Z)
Max.Z = P[i].Z;
}
//
//uloz hodnoty
//
Mod->Centre.X = (Max.X + Min.X)/2.0f;
Mod->Centre.Y = (Max.Y + Min.Y)/2.0f;
Mod->Centre.Z = (Max.Z + Min.Z)/2.0f;
Mod->Min = Min;
Mod->Max = Max;
Mod->Polomer = CalcDistance(Min,Mod->Centre);
}
//------------------------------------------------------------------
// Name: TransformModel()
// Desc:
//------------------------------------------------------------------
void MODEL::TransformModel(MODELFRAME *Mod,D3DXMATRIXA16 Matica)
{
for (int i=0;i<NumFaces;i++)
{
Mod->FaceList[i].P[0] = TransformPoint(Mod->FaceList[i].P[0],Matica);
Mod->FaceList[i].P[1] = TransformPoint(Mod->FaceList[i].P[1],Matica);
Mod->FaceList[i].P[2] = TransformPoint(Mod->FaceList[i].P[2],Matica);
Mod->FaceList[i].N[0] = TransformNormal(Mod->FaceList[i].N[0],Matica);
Mod->FaceList[i].N[1] = TransformNormal(Mod->FaceList[i].N[1],Matica);
Mod->FaceList[i].N[2] = TransformNormal(Mod->FaceList[i].N[2],Matica);
Mod->FaceList[i].Plane = TransformPlane(Mod->FaceList[i].Plane,Matica);
Mod->ZeroPoint = TransformPoint(Mod->ZeroPoint,Matica);
}
}
//------------------------------------------------------------------
// Name: InterpolateModel()
// Desc: interpoluje model a posle ho do ModelT
//------------------------------------------------------------------
void MODEL::InterpolateModel(MODELFRAME *Mod)
{
/*
//dolny horny snimok
int F1 = (int)Frame;
int F2 = ((int)Frame) + 1;
if (F2 == NumFrames)
F2 = 0;
//Interpolant
float Int = Frame - ((float)F1);
//netreba zbytocne interpolovat ak je interpolant rovny nule
if (Int != 0.0f)
{
for (int i=0;i<NumFaces;i++)
{
for (int u=0;u<3;u++)
{
Mod->FaceList[i].P[u].X = ModelFrame[F1].FaceList[i].P[u].X + (Int *(ModelFrame[F2].FaceList[i].P[u].X - ModelFrame[F1].FaceList[i].P[u].X));
Mod->FaceList[i].P[u].Y = ModelFrame[F1].FaceList[i].P[u].Y + (Int *(ModelFrame[F2].FaceList[i].P[u].Y - ModelFrame[F1].FaceList[i].P[u].Y));
Mod->FaceList[i].P[u].Z = ModelFrame[F1].FaceList[i].P[u].Z + (Int *(ModelFrame[F2].FaceList[i].P[u].Z - ModelFrame[F1].FaceList[i].P[u].Z));
Mod->FaceList[i].N[u].X = ModelFrame[F1].FaceList[i].N[u].X + (Int *(ModelFrame[F2].FaceList[i].N[u].X - ModelFrame[F1].FaceList[i].N[u].X));
Mod->FaceList[i].N[u].Y = ModelFrame[F1].FaceList[i].N[u].Y + (Int *(ModelFrame[F2].FaceList[i].N[u].Y - ModelFrame[F1].FaceList[i].N[u].Y));
Mod->FaceList[i].N[u].Z = ModelFrame[F1].FaceList[i].N[u].Z + (Int *(ModelFrame[F2].FaceList[i].N[u].Z - ModelFrame[F1].FaceList[i].N[u].Z));
Mod->FaceList[i].T[u].X = ModelFrame[F1].FaceList[i].T[u].X;
Mod->FaceList[i].T[u].Y = ModelFrame[F1].FaceList[i].T[u].Y;
}
Mod->FaceList[i].Plane.Normal.X = ModelFrame[F1].FaceList[i].Plane.Normal.X + (Int *(ModelFrame[F2].FaceList[i].Plane.Normal.X - ModelFrame[F1].FaceList[i].Plane.Normal.X));
Mod->FaceList[i].Plane.Normal.Y = ModelFrame[F1].FaceList[i].Plane.Normal.Y + (Int *(ModelFrame[F2].FaceList[i].Plane.Normal.Y - ModelFrame[F1].FaceList[i].Plane.Normal.Y));
Mod->FaceList[i].Plane.Normal.Z = ModelFrame[F1].FaceList[i].Plane.Normal.Z + (Int *(ModelFrame[F2].FaceList[i].Plane.Normal.Z - ModelFrame[F1].FaceList[i].Plane.Normal.Z));
Mod->FaceList[i].Plane.D = ModelFrame[F1].FaceList[i].Plane.D + (Int *(ModelFrame[F2].FaceList[i].Plane.D - ModelFrame[F1].FaceList[i].Plane.D));
for (u=0;u<8;u++)
{
Mod->P[u] = ModelFrame[F1].P[u];
}
}
}
else ////////
{
for (int i=0;i<NumFaces;i++)
{
for (int u=0;u<3;u++)
{
Mod->FaceList[i].P[u] = ModelFrame[F1].FaceList[i].P[u] ;
Mod->FaceList[i].N[u] = ModelFrame[F1].FaceList[i].N[u] ;
Mod->FaceList[i].Plane = ModelFrame[F1].FaceList[i].Plane;
Mod->FaceList[i].T[u] = ModelFrame[F1].FaceList[i].T[u];
Mod->FaceList[i].T[u] = ModelFrame[F1].FaceList[i].T[u];
}
for (u=0;u<8;u++)
{
Mod->P[u] = ModelFrame[F1].P[u];
}
}
}*/
}
//------------------------------------------------------------------
// Name: Smooth Shading
// Desc: vypocita smooth shading
//------------------------------------------------------------------
void MODEL::CalcSmoothShading(MODELFRAME *Mod)
{
VECTOR3D *Normal = NULL;
Normal = new VECTOR3D[NumFaces];
for (int i=0;i<NumFaces;i=i+1)
{
for (int u=0;u<3;u=u+1)
{
//bod
VECTOR3D B = Mod->FaceList[i].P[u];
VECTOR3D NormalVys = Get3D(0.0f,0.0f,0.0f);
int ActNormal = 0;
//ziskaj normaly
for (int j=0;j<NumFaces;j=j+1)
{
for (int k=0;k<3;k=k+1)
{
if((B.X == Mod->FaceList[j].P[k].X) &&
(B.Y == Mod->FaceList[j].P[k].Y) &&
(B.Z == Mod->FaceList[j].P[k].Z) )
{
Normal[ActNormal] = Mod->FaceList[j].Plane.Normal ;
ActNormal++;
}
}
}
//vypocitaj normalu
for (int a=0;a<ActNormal;a++)
{
Add(&NormalVys,NormalVys,Normal[a]);
}
//normalizuj vyslednu normalu
Normalize(&NormalVys);
//zapis nove normaly
for (j=0;j<NumFaces;j++)
{
for (int k=0;k<3;k++)
{
if((B.X == Mod->FaceList[j].P[k].X) &&
(B.Y == Mod->FaceList[j].P[k].Y) &&
(B.Z == Mod->FaceList[j].P[k].Z) )
{
Mod->FaceList[j].N[k] = NormalVys;
}
}
}
}
}
if (Normal != NULL)
delete [] Normal;
Normal = NULL;
}
//------------------------------------------------------------------
// Name: SetMaterial()
// Desc: nastavy material
//------------------------------------------------------------------
void MODEL::SetMaterial(D3DMATERIAL9 Mat)
{
Material = Mat;
}
//------------------------------------------------------------------
// Name: CollisionBox()
// Desc: Bouring box kolizia
//------------------------------------------------------------------
bool MODEL::CollisionBox(VECTOR3D P1, VECTOR3D P2,D3DXMATRIX PomMatrix)
{
//transformuj body
P1 = UnTransformPoint(P1,PomMatrix);
P2 = UnTransformPoint(P2,PomMatrix);
if (CollisionBoxEdge(P1,P2,ModelFrame.P[0],ModelFrame.P[6]))
return true;
else
return false;
}
//------------------------------------------------------------------
// Name: CollisionDetail()
// Desc: detailna kolizia
//------------------------------------------------------------------
bool MODEL::CollisionDetail(VECTOR3D P1,VECTOR3D P2,D3DXMATRIX PomMatrix)
{
//iba ak collision BOX == true
if (CollisionBox(P1,P2,PomMatrix) == false)
return false;
else
{
//uhly
float U,U1,U2,U3;
//
//testuj pre vsetky face
//
//transformuj body
P1 = UnTransformPoint(P1,PomMatrix);
P2 = UnTransformPoint(P2,PomMatrix);
VECTOR3D Inter; //priesecnik
for (int i=0;i<NumFaces;i++)
{
if (CalcPriesEdge(&Inter,ModelFrame.FaceList[i].Plane,P1,P2) == true)
{
U1 = CalcAngleCentre(Inter,ModelFrame.FaceList[i].P[0],
ModelFrame.FaceList[i].P[1]);
U2 = CalcAngleCentre(Inter,ModelFrame.FaceList[i].P[1],
ModelFrame.FaceList[i].P[2]);
U3 = CalcAngleCentre(Inter,ModelFrame.FaceList[i].P[2],
ModelFrame.FaceList[i].P[0]);
U = U1+U2+U3;
if (U > 6.2f && U < 6.35f)
{
ColPosition = TransformPoint(Inter,PomMatrix);
ColNormal = TransformNormal(ModelFrame.FaceList[i].Plane.Normal,PomMatrix);
return true;
}
}
}
return false;
}
}
//------------------------------------------------------------------
// Name: LoadMD2
// Desc: Nacita MD2 model
//------------------------------------------------------------------
void MODEL::LoadMD2(char *FileName,int NFrames)
{
/* //info pre Log
char cBuf[80];
LogPrint("Inicializujem MD2 model");
sprintf(cBuf," Subor: %s",FileName);
FILE *File = NULL; //subor z ktoreho sa bude citat
tMd2Header m_Header;
////////////////
//otvori subor//
////////////////
File = fopen(FileName, "rb");
if (File == NULL)
LogPrint(" Subor sa nenasiel");
////////////////////
//nacitaj hlavicku//
////////////////////
fread(&m_Header, 1, sizeof(tMd2Header), File);
NumFaces = m_Header.numTriangles;
NumFrames = NFrames;
//info pre log
sprintf(cBuf," Pocet snimkov: %d",m_Header.numFrames);
LogPrint(cBuf);
sprintf(cBuf," Pocet vertex: %d",m_Header.numVertices);
LogPrint(cBuf);
sprintf(cBuf," Pocet trojholnikov: %d",m_Header.numTriangles);
LogPrint(cBuf);
/////////////////////////
//inicializacia modelov//
/////////////////////////
//vytvori zasobnik modelov
ModelFrame = new MODELFRAME [NumFrames];
//inicializuje zasobnik modelov
for (int i=0;i<NumFrames;i++)
{
InitializeModel(&ModelFrame[i],NumFaces);
}
//inicializuje transformovany model
InitializeModel(&ModelT,NumFaces);
////////////////////////////
//vytvaranie vertex buffer//
////////////////////////////
if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumFaces*3*sizeof(CUSTOMVERTEXMODEL),
D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC, D3DFVF_CUSTOMVERTEXMODEL,
D3DPOOL_DEFAULT, &g_pVB, NULL )))
{
LogPrint(" Chyba pri vytvarani VB");
}
else
{
LogPrint(" Vertex Buffer vytvoreny");
}
///////////////////////////////////
//veci potrebne pre nacitanie MD2//
///////////////////////////////////
unsigned char buffer[MD2_MAX_FRAMESIZE];
tMd2Skin *m_pSkins = new tMd2Skin [m_Header.numSkins];
tMd2TexCoord *m_pTexCoords = new tMd2TexCoord [m_Header.numTexCoords];
tMd2Face *m_pTriangles = new tMd2Face [m_Header.numTriangles];
tMd2Frame *m_pFrames = new tMd2Frame [m_Header.numFrames];
//////////////////////
//Nacitavanie udajov//
//////////////////////
fseek(File, m_Header.offsetSkins, SEEK_SET);
fread(m_pSkins, sizeof(tMd2Skin), m_Header.numSkins, File);
fseek(File, m_Header.offsetTexCoords, SEEK_SET);
fread(m_pTexCoords, sizeof(tMd2TexCoord), m_Header.numTexCoords, File);
fseek(File, m_Header.offsetTriangles, SEEK_SET);
fread(m_pTriangles, sizeof(tMd2Face), m_Header.numTriangles, File);
fseek(File, m_Header.offsetFrames, SEEK_SET);
for (i=0; i < m_Header.numFrames; i++)
{
// Assign our alias frame to our buffer memory
tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer;
// Allocate the memory for the first frame of animation's vertices
m_pFrames[i].pVertices = new tMd2Triangle [m_Header.numVertices];
// Read in the first frame of animation
fread(pFrame, 1, m_Header.frameSize, File);
// Store off a vertex array pointer to cut down large lines of code
tMd2Triangle *pVertices = m_pFrames[i].pVertices;
// Go through all of the number of vertices and assign the scale and translations.
// Store the vertices in our current frame's vertex list array, while swapping Y and Z.
// Notice we also negate the Z axis as well to make the swap correctly.
for (int j=0; j < m_Header.numVertices; j++)
{
pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0];
pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]);
pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2];
}
}
fclose(File);
/////////////////
//Konvertovanie//
/////////////////
//opakuj pre kazdy frame
for (i=0;i<NFrames;i++)
{
//pre vsetky trojholniky vo frame
for (int u=0;u<m_Header.numTriangles;u++)
{
int V1,V2,V3; //index vertexov
int T1,T2,T3; //index texturovych kordinatov
V1 = m_pTriangles[u].vertexIndices[0];
V2 = m_pTriangles[u].vertexIndices[1];
V3 = m_pTriangles[u].vertexIndices[2];
T1 = m_pTriangles[u].textureIndices[0];
T2 = m_pTriangles[u].textureIndices[1];
T3 = m_pTriangles[u].textureIndices[2];
ModelFrame[i].FaceList[u].P[2].X = m_pFrames[i].pVertices[V1].vertex[0];
ModelFrame[i].FaceList[u].P[2].Y = m_pFrames[i].pVertices[V1].vertex[1];
ModelFrame[i].FaceList[u].P[2].Z = m_pFrames[i].pVertices[V1].vertex[2];
ModelFrame[i].FaceList[u].T[2].X = m_pTexCoords[T1].u/float(m_Header.skinWidth) ;
ModelFrame[i].FaceList[u].T[2].Y = m_pTexCoords[T1].v/float(m_Header.skinHeight);
ModelFrame[i].FaceList[u].P[1].X = m_pFrames[i].pVertices[V2].vertex[0];
ModelFrame[i].FaceList[u].P[1].Y = m_pFrames[i].pVertices[V2].vertex[1];
ModelFrame[i].FaceList[u].P[1].Z = m_pFrames[i].pVertices[V2].vertex[2];
ModelFrame[i].FaceList[u].T[1].X = m_pTexCoords[T2].u/float(m_Header.skinWidth);
ModelFrame[i].FaceList[u].T[1].Y = m_pTexCoords[T2].v/float(m_Header.skinHeight);
ModelFrame[i].FaceList[u].P[0].X = m_pFrames[i].pVertices[V3].vertex[0];
ModelFrame[i].FaceList[u].P[0].Y = m_pFrames[i].pVertices[V3].vertex[1];
ModelFrame[i].FaceList[u].P[0].Z = m_pFrames[i].pVertices[V3].vertex[2];
ModelFrame[i].FaceList[u].T[0].X = m_pTexCoords[T3].u/float(m_Header.skinWidth);
ModelFrame[i].FaceList[u].T[0].Y = m_pTexCoords[T3].v/float(m_Header.skinHeight);
}
//vypocitajminmax
CalcBoxPoints(&ModelFrame[i]);
//vypocitaj normaly
if (Normals == true)
CalcFaceNormals(&ModelFrame[i]);
//vypocitaj smooth shading
if (SmoothShading == true)
CalcSmoothShading(&ModelFrame[i]);
}*/
}
//------------------------------------------------------------------
// Name: SaveSHD
// Desc: Ulozi model ako SHD
//------------------------------------------------------------------
void MODEL::SaveSHD(char *FileName)
{
/* //struktury pre zapis
Shd_Header Header;
Shd_Face Face;
//subor
FILE *File;
File = fopen(FileName,"wb");
//nastavy header
Header.NumFaces = NumFaces;
Header.NumFrames = NumFrames;
Header.Version = 1;
//zapis header
fwrite(&Header,sizeof(Header),1,File);
//zapise pole
for (int i=0;i<NumFrames;i++)
{
for (int u=0;u<NumFaces;u++)
{
Face.Vertex[0].P[0] = ModelFrame[i].FaceList[u].P[0].X;
Face.Vertex[0].P[1] = ModelFrame[i].FaceList[u].P[0].Y;
Face.Vertex[0].P[2] = ModelFrame[i].FaceList[u].P[0].Z;
Face.Vertex[0].N[0] = ModelFrame[i].FaceList[u].N[0].X;
Face.Vertex[0].N[1] = ModelFrame[i].FaceList[u].N[0].Y;
Face.Vertex[0].N[2] = ModelFrame[i].FaceList[u].N[0].Z;
Face.Vertex[0].T[0] = ModelFrame[i].FaceList[u].T[0].X;
Face.Vertex[0].T[1] = ModelFrame[i].FaceList[u].T[0].Y;
Face.Vertex[1].P[0] = ModelFrame[i].FaceList[u].P[1].X;
Face.Vertex[1].P[1] = ModelFrame[i].FaceList[u].P[1].Y;
Face.Vertex[1].P[2] = ModelFrame[i].FaceList[u].P[1].Z;
Face.Vertex[1].N[0] = ModelFrame[i].FaceList[u].N[1].X;
Face.Vertex[1].N[1] = ModelFrame[i].FaceList[u].N[1].Y;
Face.Vertex[1].N[2] = ModelFrame[i].FaceList[u].N[1].Z;
Face.Vertex[1].T[0] = ModelFrame[i].FaceList[u].T[1].X;
Face.Vertex[1].T[1] = ModelFrame[i].FaceList[u].T[1].Y;
Face.Vertex[2].P[0] = ModelFrame[i].FaceList[u].P[2].X;
Face.Vertex[2].P[1] = ModelFrame[i].FaceList[u].P[2].Y;
Face.Vertex[2].P[2] = ModelFrame[i].FaceList[u].P[2].Z;
Face.Vertex[2].N[0] = ModelFrame[i].FaceList[u].N[2].X;
Face.Vertex[2].N[1] = ModelFrame[i].FaceList[u].N[2].Y;
Face.Vertex[2].N[2] = ModelFrame[i].FaceList[u].N[2].Z;
Face.Vertex[2].T[0] = ModelFrame[i].FaceList[u].T[2].X;
Face.Vertex[2].T[1] = ModelFrame[i].FaceList[u].T[2].Y;
Face.Normal[0] = ModelFrame[i].FaceList[u].Plane.Normal.X;
Face.Normal[1] = ModelFrame[i].FaceList[u].Plane.Normal.Y;
Face.Normal[2] = ModelFrame[i].FaceList[u].Plane.Normal.Z;
Face.D = ModelFrame[i].FaceList[u].Plane.D;
fwrite(&Face,sizeof(Face),1,File);
}
}
//zavri subor
fclose(File);*/
}
//------------------------------------------------------------------
// Name: LoadSHD
// Desc: Loadne SHD model
//------------------------------------------------------------------
void MODEL::LoadSHD(char *FileName)
{
/*
//log
char cBuf[80];
LogPrint("Inicializujem SHD subor");
sprintf(cBuf," Subor: %s",FileName);
LogPrint(cBuf);
//struktury pre citanie
Shd_Header Header;
Shd_Face Face;
/////////
//subor//
/////////
FILE *File = NULL;
File = fopen(FileName,"rb");
if (File == NULL)
LogPrint(" Subor sa nenasiel");
////////////////////
//precita hlavicku//
////////////////////
fread(&Header,sizeof(Header),1,File);
////////////////////
//nastavy premenne//
////////////////////
NumFaces = Header.NumFaces;
NumFrames = Header.NumFrames;
sprintf(cBuf," Pocet trojholnikov: %d",NumFaces);
LogPrint(cBuf);
sprintf(cBuf," Pocet snimkov: %d",NumFrames);
LogPrint(cBuf);
/////////////////////////
//inicializacia modelov//
/////////////////////////
//vytvori zasobnik modelov
ModelFrame = new MODELFRAME [NumFrames];
//inicializuje zasobnik modelov
for (int i=0;i<NumFrames;i++)
{
InitializeModel(&ModelFrame[i],NumFaces);
}
//inicializuje transformovany model
InitializeModel(&ModelT,NumFaces);
////////////////////////////
//vytvaranie vertex buffer//
////////////////////////////
if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumFaces*3*sizeof(CUSTOMVERTEXMODEL),
D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC, D3DFVF_CUSTOMVERTEXMODEL,
D3DPOOL_DEFAULT, &g_pVB, NULL )))
{
LogPrint(" Chyba pri vytvarani VB");
}
else
{
LogPrint(" Vertex Buffer vytvoreny");
}
/////////////////
//Konvertovanie//
/////////////////
for (i=0;i<NumFrames;i++)
{
for (int u=0;u<NumFaces;u++)
{
fread(&Face,sizeof(Face),1,File);
ModelFrame[i].FaceList[u].P[0].X = Face.Vertex[0].P[0];
ModelFrame[i].FaceList[u].P[0].Y = Face.Vertex[0].P[1];
ModelFrame[i].FaceList[u].P[0].Z = Face.Vertex[0].P[2];
ModelFrame[i].FaceList[u].N[0].X = Face.Vertex[0].N[0];
ModelFrame[i].FaceList[u].N[0].Y = Face.Vertex[0].N[1];
ModelFrame[i].FaceList[u].N[0].Z = Face.Vertex[0].N[2];
ModelFrame[i].FaceList[u].T[0].X = Face.Vertex[0].T[0] ;
ModelFrame[i].FaceList[u].T[0].Y = Face.Vertex[0].T[1];
ModelFrame[i].FaceList[u].P[1].X = Face.Vertex[1].P[0];
ModelFrame[i].FaceList[u].P[1].Y = Face.Vertex[1].P[1];
ModelFrame[i].FaceList[u].P[1].Z = Face.Vertex[1].P[2];
ModelFrame[i].FaceList[u].N[1].X = Face.Vertex[1].N[0];
ModelFrame[i].FaceList[u].N[1].Y = Face.Vertex[1].N[1];
ModelFrame[i].FaceList[u].N[1].Z = Face.Vertex[1].N[2] ;
ModelFrame[i].FaceList[u].T[1].X = Face.Vertex[1].T[0];
ModelFrame[i].FaceList[u].T[1].Y = Face.Vertex[1].T[1];
ModelFrame[i].FaceList[u].P[2].X = Face.Vertex[2].P[0];
ModelFrame[i].FaceList[u].P[2].Y = Face.Vertex[2].P[1];
ModelFrame[i].FaceList[u].P[2].Z = Face.Vertex[2].P[2];
ModelFrame[i].FaceList[u].N[2].X = Face.Vertex[2].N[0];
ModelFrame[i].FaceList[u].N[2].Y = Face.Vertex[2].N[1];
ModelFrame[i].FaceList[u].N[2].Z = Face.Vertex[2].N[2];
ModelFrame[i].FaceList[u].T[2].X = Face.Vertex[2].T[0];
ModelFrame[i].FaceList[u].T[2].Y = Face.Vertex[2].T[1];
ModelFrame[i].FaceList[u].Plane.Normal.X = Face.Normal[0];
ModelFrame[i].FaceList[u].Plane.Normal.Y = Face.Normal[1];
ModelFrame[i].FaceList[u].Plane.Normal.Z = Face.Normal[2];
ModelFrame[i].FaceList[u].Plane.D = Face.D;
}
//vypocitaj min max pre kazdy snimok
CalcBoxPoints(&ModelFrame[i]);
}
//zavri subor
fclose(File);*/
}
//------------------------------------------------------------------
// Name: BuildShadow()
// Desc: urobi tien do stencil class
//------------------------------------------------------------------
void MODEL::BuildShadow(STENCIL *Stn)
{
//nastavi maticu
g_pd3dDevice->SetTransform( D3DTS_WORLD, &Matica);
//transformuje svetlo do priestoru modelu
VECTOR3D LightPos = UnTransformPoint(Stn->LightPosition,Matica);
for (int i=0;i<ModelFrame.NumFaces;i++)
{
//ak je face otoceny k svetlu vynechaj
if (( ModelFrame.FaceList[i].Plane.Normal.X*LightPos.X +
ModelFrame.FaceList[i].Plane.Normal.Y*LightPos.Y +
ModelFrame.FaceList[i].Plane.Normal.Z*LightPos.Z +
ModelFrame.FaceList[i].Plane.D) <= 0.0f)
continue;
Stn->AddEdge(ModelFrame.FaceList[i].P[0],ModelFrame.FaceList[i].P[1]);
Stn->AddEdge(ModelFrame.FaceList[i].P[1],ModelFrame.FaceList[i].P[2]);
Stn->AddEdge(ModelFrame.FaceList[i].P[2],ModelFrame.FaceList[i].P[0]);
}
//vyberie quady ktore su nakraji
Stn->ProcessEdge(LightPos);
//vyrenderuje hotovy tien
Stn->RenderShadow();
}