55#define SHATTER_DEBUG_LOG_ENABLED 0
56#if (SHATTER_DEBUG_LOG_ENABLED)
57#define SHATTER_DEBUG_SAY(x) WWDEBUG_SAY(x)
59#define SHATTER_DEBUG_SAY(x)
64#define SHATTER_PATTERN_FORMAT "ShatterPlanes%d"
70#define BPT_EPSILON 0.0001f
71#define BPT_COINCIDENCE_EPSILON 0.000001f
228static Vector3 * _get_temp_vertex_position_array(
int count)
230 if (TmpVertPositions.Length() < count) {
231 TmpVertPositions.Resize(count);
233 return &(TmpVertPositions[0]);
236static Vector3 * _get_temp_vertex_normal_array(
int count)
238 if (TmpVertNormals.Length() < count) {
239 TmpVertNormals.Resize(count);
241 return &(TmpVertNormals[0]);
280 DCG[ipass]=0xffffffff;
281 DIG[ipass]=0xffffffff;
294 for (
int ipass=0; ipass<
PassCount; ipass++) {
295 DCG[ipass] = that.
DCG[ipass];
296 DIG[ipass] = that.
DIG[ipass];
309 for (
int ipass=0; ipass<
PassCount; ipass++) {
310 DCG[ipass] = that.
DCG[ipass];
311 DIG[ipass] = that.
DIG[ipass];
341 for (
int ipass=0; ipass<v0.
PassCount; ipass++) {
409 Verts[i] = points[i];
440 nx += (double)(
Verts[i].Position.Y -
Verts[j].Position.Y) * (double)(
Verts[i].Position.Z +
Verts[j].Position.Z);
441 ny += (double)(
Verts[i].Position.Z -
Verts[j].Position.Z) * (double)(
Verts[i].Position.X +
Verts[j].Position.X);
442 nz += (double)(
Verts[i].Position.X -
Verts[j].Position.X) * (double)(
Verts[i].Position.Y +
Verts[j].Position.Y);
444 ax += (double)
Verts[i].Position.X;
445 ay += (double)
Verts[i].Position.Y;
446 az += (double)
Verts[i].Position.Z;
465 side_mask |=
Verts[i].Which_Side(plane);
469 if (side_mask ==
BPT_ON) {
506 int sidelastdefinite = 0;
512 side =
Verts[i].Which_Side(plane);
522 }
else if (side ==
BPT_ON) {
553 }
else if (side ==
BPT_ON) {
569 }
else if (sideprev ==
BPT_ON) {
579 }
else if (side ==
BPT_ON) {
623 WWDEBUG_SAY((
"Degenerate Poly - fewer than 3 vertices\r\n"));
630 float delta = (
Verts[i].Position -
Verts[j].Position).Length();
632 WWDEBUG_SAY((
"Degenerate Poly - coincident vertices!\r\n"));
646 WWDEBUG_SAY((
"Degenerate Poly - invalid plane!\r\n"));
663 float delta = (
Verts[i].Position -
Verts[i+1].Position).Length();
666 for (
int j=i+1; j<
NumVerts-1; j++) {
701 for (
int ibone=0; ibone < tree->
Num_Pivots(); ibone++) {
706 if (
Plane.In_Front(point)) {
754 front_poly = polygon;
774 Front->Clip_Polygon(front_poly);
785 Back->Clip_Polygon(back_poly);
823 while (htree !=
NULL) {
825 int leaf_counter = 0;
848 for (
int i=0; i<ShatterPatterns.Count(); i++) {
849 delete ShatterPatterns[i];
850 ShatterPatterns[i] =
NULL;
852 ShatterPatterns.Delete_All();
858 if (ShatterPatterns.Count() == 0) {
887 WWDEBUG_SAY((
"Failed to shatter model: %s. It has shader or material arrays\n",model->
Get_Name()));
894 WWDEBUG_SAY((
"Failed to shatter model: %s. Texture array in pass: %d stage: %d\n",model->
Get_Name(),ipass,istage));
904 BSPClass * clipper = ShatterPatterns[rand() % ShatterPatterns.Count()];
935 Mshatter_to_world.
Look_At(point,point+direction,0.0f);
953 float scale_factor = 5.0f / sphere.
Radius;
956 Mscale_to_shatter.
Scale(scale_factor);
957 Mscale_from_shatter.
Scale(1.0f / scale_factor);
993 for (ivert=0; ivert<3; ivert++) {
995 int vert_index = polys[ipoly][ivert];
998 polygon.
Verts[ivert].
Normal = src_vnorms[vert_index];
999 SHATTER_DEBUG_SAY((
"position: %f %f %f\n",verts[vert_index].X,verts[vert_index].Y,verts[vert_index].Z));
1000 SHATTER_DEBUG_SAY((
"normal: %f %f %f\n",src_vnorms[vert_index].X,src_vnorms[vert_index].Y,src_vnorms[vert_index].Z));
1003 if (mtl_params.
DCG[ipass] !=
NULL) {
1004 polygon.
Verts[ivert].
DCG[ipass] = mtl_params.
DCG[ipass][vert_index];
1005 SHATTER_DEBUG_SAY((
"DCG: pass: %d : %f %f %f\n",ipass,mtl_params.
DCG[ipass][vert_index].X,mtl_params.
DCG[ipass][vert_index].Y,mtl_params.
DCG[ipass][vert_index].Z));
1008 if (mtl_params.
DIG[ipass] !=
NULL) {
1009 polygon.
Verts[ivert].
DIG[ipass] = mtl_params.
DIG[ipass][vert_index];
1010 SHATTER_DEBUG_SAY((
"DIG: pass: %d : %f %f %f\n",ipass,mtl_params.
DIG[ipass][vert_index].X,mtl_params.
DIG[ipass][vert_index].Y,mtl_params.
DIG[ipass][vert_index].Z));
1014 if (mtl_params.
UV[ipass][istage] !=
NULL) {
1015 polygon.
Verts[ivert].
TexCoord[ipass][istage] = mtl_params.
UV[ipass][istage][vert_index];
1016 SHATTER_DEBUG_SAY((
"UV pass: %d stage: %d: %f %f\n",ipass,istage,mtl_params.
UV[ipass][istage][vert_index].
X,mtl_params.
UV[ipass][istage][vert_index].
Y));
1049 return MeshFragments.Count();
1054 if (MeshFragments[fragment_index] !=
NULL) {
1055 MeshFragments[fragment_index]->Add_Ref();
1057 return MeshFragments[fragment_index];
1062 return MeshFragments[fragment_index];
1068 for (
int i=0; i<MeshFragments.Count(); i++) {
1073 MeshFragments.Delete_All(
false);
1080 ClipPools[i].Delete_All(
false);
1107 if (ClipPools[ipool].Count() > 0) {
1109 int ivert,ipoly,ipass,istage;
1116 for (ipoly=0;ipoly<ClipPools[ipool].Count();ipoly++) {
1117 int poly_vert_count = ClipPools[ipool][ipoly].Get_Vertex_Count();
1118 vcount += poly_vert_count;
1119 pcount += poly_vert_count-2;
1138 bool has_textures =
false;
1146 has_textures =
true;
1171 for (ipoly=0; ipoly<ClipPools[ipool].Count(); ipoly++) {
1192 for (ipass=0; ipass<mtl_params.
PassCount; ipass++) {
1200 if (mtl_params.
DCG[ipass] !=
NULL) {
1206 mycolor=vert.
DCG[ipass];
1210 if (mtl_params.
DIG[ipass] !=
NULL) {
1218 new_mesh->
Color(mycolor);
1226 if (mtl_params.
UV[ipass][istage] !=
NULL) {
1228 new_mesh->
UV(vert.
TexCoord[ipass][istage],istage);
1264 MeshFragments.Add(new_mesh);
BSPClass * Get_Front_Child(void)
void Set_Plane_From_Transform(const Matrix3D &tm)
void Clip_Polygon(const PolygonClass &poly)
const PlaneClass & Get_Plane(void)
BSPClass(HTreeClass *tree, int bone_index, int &leaf_index)
BSPClass * Get_Back_Child(void)
static Vector4 Convert_Color(unsigned color)
void UV(float u, float v, int uv_array_index=0)
void Normal(float x, float y, float z)
void Color(float r, float g, float b, float a, int color_array_index=0)
void Set_Dirty_Bounds(void)
virtual void Set_Material_Info(MaterialInfoClass *mat_info)
void Clear_Dirty_Vertex_Normals(void)
void Translate_Vertices(const Vector3 &offset)
void Set_Pass_Count(int passes)
void Location_Inline(float x, float y, float z)
DynamicMeshModel * Peek_Model(void)
int Set_Shader(const ShaderClass &shader, int pass=0)
void Set_Dirty_Planes(void)
int Set_Vertex_Material(int idx, int pass=0)
void Set_Single_Texture(TextureClass *tex, int pass=0, int stage=0)
int Get_Parent_Index(int bone_indx) const
void Base_Update(const Matrix3D &root)
WWINLINE int Num_Pivots(void) const
WWINLINE const Matrix3D & Get_Transform(int pivot) const
int Add_Vertex_Material(VertexMaterialClass *vmat)
int Add_Texture(TextureClass *tex)
static void Multiply(const Matrix3D &A, const Matrix3D &B, Matrix3D *set_result)
WWINLINE Vector3 Get_Z_Vector() const
void Translate(float x, float y, float z)
void Get_Orthogonal_Inverse(Matrix3D &set_inverse) const
static WWINLINE void Transform_Vector(const Matrix3D &tm, const Vector3 &in, Vector3 *out)
WWINLINE Vector3 Get_Translation(void) const
WWINLINE void Scale(float scale)
void Look_At(const Vector3 &p, const Vector3 &t, float roll)
MeshModelClass * Get_Model(void)
void Get_Bounding_Sphere(SphereClass *set_sphere)
const Vector3 * Get_Vertex_Normal_Array(void)
int Get_Vertex_Count(void) const
const char * Get_Name(void) const
const TriIndex * Get_Polygon_Array(void)
Vector3 * Get_Vertex_Array(void)
int Get_Polygon_Count(void) const
int Get_Flag(FlagsType flag)
bool Has_Shader_Array(int pass) const
unsigned * Get_DCG_Array(int pass)
bool Has_Material_Array(int pass) const
int Get_Pass_Count(void) const
bool Has_Texture_Array(int pass, int stage) const
const Vector2 * Get_UV_Array(int pass=0, int stage=0)
VertexMaterialClass * Peek_Single_Material(int pass=0) const
unsigned * Get_DIG_Array(int pass)
ShaderClass Get_Single_Shader(int pass=0) const
TextureClass * Peek_Single_Texture(int pass=0, int stage=0) const
unsigned * DCG[MeshMatDescClass::MAX_PASSES]
Vector2 * UV[MeshMatDescClass::MAX_PASSES][MeshMatDescClass::MAX_TEX_STAGES]
unsigned * DIG[MeshMatDescClass::MAX_PASSES]
~MeshMtlParamsClass(void)
MeshMtlParamsClass(MeshModelClass *model)
bool Compute_Intersection(const Vector3 &p0, const Vector3 &p1, float *set_t) const
void Split(const PlaneClass &plane, PolygonClass &front, PolygonClass &back) const
const PlaneClass & Get_Plane(void) const
const VertexClass & operator[](int i) const
void Set_Plane(const PlaneClass &plane)
int Get_Material_ID(void) const
VertexClass Verts[BPT_POLY_MAX_VERTS]
PolygonClass & operator=(const PolygonClass &that)
int Get_Vertex_Count(void) const
void Set_Vertex_Count(int count)
bool Salvage_Degenerate(void)
void Set_Material_Id(int id)
int Which_Side(const PlaneClass &plane) const
virtual void Set_Transform(const Matrix3D &m)
virtual const AABoxClass & Get_Bounding_Box(void) const
const Matrix3D & Get_Transform(void) const
static void Reset_Clip_Pools(void)
static void Shutdown(void)
static RenderObjClass * Get_Fragment(int fragment_index)
static RenderObjClass * Peek_Fragment(int fragment_index)
static void Shatter_Mesh(MeshClass *mesh, const Vector3 &point, const Vector3 &velocity)
static void Release_Fragments(void)
static void Process_Clip_Pools(const Matrix3D &Mshatter_to_mesh, MeshClass *mesh, MeshMtlParamsClass &mtl_params)
static int Get_Fragment_Count(void)
int _cdecl Format(const TCHAR *format,...)
static void Lerp(const Vector2 &a, const Vector2 &b, float t, Vector2 *set_result)
static WWINLINE float Dot_Product(const Vector3 &a, const Vector3 &b)
static void Lerp(const Vector3 &a, const Vector3 &b, float alpha, Vector3 *set_result)
static Vector4 Lerp(const Vector4 &a, const Vector4 &b, float alpha)
static void Transform(Vector3 *dst, const Vector3 *src, const Matrix3D &matrix, const int count)
int Which_Side(const PlaneClass &plane) const
static void Lerp(const VertexClass &v0, const VertexClass &v1, float lerp, VertexClass *res)
VertexClass & operator=(const VertexClass &that)
unsigned DCG[MeshMatDescClass::MAX_PASSES]
static void Intersect_Plane(const VertexClass &v0, const VertexClass &v1, const PlaneClass &plane, VertexClass *res)
unsigned DIG[MeshMatDescClass::MAX_PASSES]
Vector2 TexCoord[MeshMatDescClass::MAX_PASSES][MeshMatDescClass::MAX_TEX_STAGES]
static WW3DAssetManager * Get_Instance(void)
virtual HTreeClass * Get_HTree(const char *name)
static float Sqrt(float val)
#define REF_PTR_RELEASE(x)
#define BPT_COINCIDENCE_EPSILON
#define SHATTER_PATTERN_FORMAT
#define SHATTER_DEBUG_SAY(x)
#define WWASSERT_PRINT(expr, string)