OPC_BaseModel.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. BaseModel::BaseModel() : mIMesh(null), mModelCode(0), mSource(null), mTree(null)
  36. {
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. /**
  40. * Destructor.
  41. */
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. BaseModel::~BaseModel()
  44. {
  45. ReleaseBase();
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. /**
  49. * Releases everything.
  50. */
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. void BaseModel::ReleaseBase()
  53. {
  54. DELETESINGLE(mSource);
  55. DELETESINGLE(mTree);
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. /**
  59. * Creates an optimized tree according to user-settings, and setups mModelCode.
  60. * \param no_leaf [in] true for "no leaf" tree
  61. * \param quantized [in] true for quantized tree
  62. * \return true if success
  63. */
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. bool BaseModel::CreateTree(bool no_leaf, bool quantized)
  66. {
  67. DELETESINGLE(mTree);
  68. // Setup model code
  69. if(no_leaf) mModelCode |= OPC_NO_LEAF;
  70. else mModelCode &= ~OPC_NO_LEAF;
  71. if(quantized) mModelCode |= OPC_QUANTIZED;
  72. else mModelCode &= ~OPC_QUANTIZED;
  73. // Create the correct class
  74. if(mModelCode & OPC_NO_LEAF)
  75. {
  76. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedNoLeafTree;
  77. else mTree = new AABBNoLeafTree;
  78. }
  79. else
  80. {
  81. if(mModelCode & OPC_QUANTIZED) mTree = new AABBQuantizedTree;
  82. else mTree = new AABBCollisionTree;
  83. }
  84. CHECKALLOC(mTree);
  85. return true;
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. /**
  89. * Refits the collision model. This can be used to handle dynamic meshes. Usage is:
  90. * 1. modify your mesh vertices (keep the topology constant!)
  91. * 2. refit the tree (call this method)
  92. * \return true if success
  93. */
  94. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. bool BaseModel::Refit()
  96. {
  97. // Refit the optimized tree
  98. return mTree->Refit(mIMesh);
  99. // Old code kept for reference : refit the source tree then rebuild !
  100. // if(!mSource) return false;
  101. // // Ouch...
  102. // mSource->Refit(&mTB);
  103. // // Ouch...
  104. // return mTree->Build(mSource);
  105. }