OPC_BaseModel.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains base model interface.
  11. * \file OPC_BaseModel.cpp
  12. * \author Pierre Terdiman
  13. * \date May, 18, 2003
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * The base class for collision models.
  19. *
  20. * \class BaseModel
  21. * \author Pierre Terdiman
  22. * \version 1.3
  23. * \date May, 18, 2003
  24. */
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. // Precompiled Header
  28. #include "Stdafx.h"
  29. using namespace Opcode;
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. /**
  32. * Constructor.
  33. */
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. OPCODECREATE::OPCODECREATE()
  36. {
  37. mIMesh = null;
  38. mSettings.mRules = SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER;
  39. mSettings.mLimit = 1; // Mandatory for complete trees
  40. mNoLeaf = true;
  41. mQuantized = true;
  42. #ifdef __MESHMERIZER_H__
  43. mCollisionHull = false;
  44. #endif // __MESHMERIZER_H__
  45. mKeepOriginal = false;
  46. mCanRemap = false;
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. /**
  50. * Constructor.
  51. */
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. BaseModel::BaseModel() : mIMesh(null), mModelCode(0), mSource(null), mTree(null)
  54. {
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. /**
  58. * Destructor.
  59. */
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. BaseModel::~BaseModel()
  62. {
  63. ReleaseBase();
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. /**
  67. * Releases everything.
  68. */
  69. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. void BaseModel::ReleaseBase()
  71. {
  72. DELETESINGLE(mSource);
  73. DELETESINGLE(mTree);
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. /**
  77. * Creates an optimized tree according to user-settings, and setups mModelCode.
  78. * \param no_leaf [in] true for "no leaf" tree
  79. * \param quantized [in] true for quantized tree
  80. * \return true if success
  81. */
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. bool BaseModel::CreateTree(bool no_leaf, bool quantized)
  84. {
  85. DELETESINGLE(mTree);
  86. // Setup model code
  87. if(no_leaf) mModelCode |= OPC_NO_LEAF;
  88. else mModelCode &= ~OPC_NO_LEAF;
  89. if(quantized) mModelCode |= OPC_QUANTIZED;
  90. else mModelCode &= ~OPC_QUANTIZED;
  91. // Create the correct class
  92. if(mModelCode & OPC_NO_LEAF)
  93. {
  94. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedNoLeafTree;
  95. else mTree = new AABBNoLeafTree;
  96. }
  97. else
  98. {
  99. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedTree;
  100. else mTree = new AABBCollisionTree;
  101. }
  102. CHECKALLOC(mTree);
  103. return true;
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. /**
  107. * Refits the collision model. This can be used to handle dynamic meshes. Usage is:
  108. * 1. modify your mesh vertices (keep the topology constant!)
  109. * 2. refit the tree (call this method)
  110. * \return true if success
  111. */
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. bool BaseModel::Refit()
  114. {
  115. // Refit the optimized tree
  116. return mTree->Refit(mIMesh);
  117. // Old code kept for reference : refit the source tree then rebuild !
  118. // if(!mSource) return false;
  119. // // Ouch...
  120. // mSource->Refit(&mTB);
  121. // // Ouch...
  122. // return mTree->Build(mSource);
  123. }