OPC_BaseModel.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "Opcode.h"
  28. using namespace Opcode;
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * Constructor.
  32. */
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. OPCODECREATE::OPCODECREATE()
  35. {
  36. mIMesh = null;
  37. mSettings.mRules = SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER;
  38. mSettings.mLimit = 1; // Mandatory for complete trees
  39. mNoLeaf = true;
  40. mQuantized = true;
  41. #ifdef __MESHMERIZER_H__
  42. mCollisionHull = false;
  43. #endif // __MESHMERIZER_H__
  44. mKeepOriginal = false;
  45. mCanRemap = false;
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. /**
  49. * Constructor.
  50. */
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. BaseModel::BaseModel() : mIMesh(null), mModelCode(0), mSource(null), mTree(null)
  53. {
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * Destructor.
  58. */
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. BaseModel::~BaseModel()
  61. {
  62. ReleaseBase();
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. /**
  66. * Releases everything.
  67. */
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. void BaseModel::ReleaseBase()
  70. {
  71. DELETESINGLE(mSource);
  72. DELETESINGLE(mTree);
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. /**
  76. * Creates an optimized tree according to user-settings, and setups mModelCode.
  77. * \param no_leaf [in] true for "no leaf" tree
  78. * \param quantized [in] true for quantized tree
  79. * \return true if success
  80. */
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. bool BaseModel::CreateTree(bool no_leaf, bool quantized)
  83. {
  84. DELETESINGLE(mTree);
  85. // Setup model code
  86. if(no_leaf) mModelCode |= OPC_NO_LEAF;
  87. else mModelCode &= ~OPC_NO_LEAF;
  88. if(quantized) mModelCode |= OPC_QUANTIZED;
  89. else mModelCode &= ~OPC_QUANTIZED;
  90. // Create the correct class
  91. if(mModelCode & OPC_NO_LEAF)
  92. {
  93. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedNoLeafTree;
  94. else mTree = new AABBNoLeafTree;
  95. }
  96. else
  97. {
  98. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedTree;
  99. else mTree = new AABBCollisionTree;
  100. }
  101. CHECKALLOC(mTree);
  102. return true;
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. /**
  106. * Refits the collision model. This can be used to handle dynamic meshes. Usage is:
  107. * 1. modify your mesh vertices (keep the topology constant!)
  108. * 2. refit the tree (call this method)
  109. * \return true if success
  110. */
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. bool BaseModel::Refit()
  113. {
  114. // Refit the optimized tree
  115. return mTree->Refit(mIMesh);
  116. // Old code kept for reference : refit the source tree then rebuild !
  117. // if(!mSource) return false;
  118. // // Ouch...
  119. // mSource->Refit(&mTB);
  120. // // Ouch...
  121. // return mTree->Build(mSource);
  122. }