TransformVisitor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /***************************************************************************
  2. TransformVisitor.cpp - description
  3. -------------------
  4. begin : Wed Jan 26 2000
  5. copyright : (C) 2000 by Henrik Enqvist IB (GPL)
  6. email : henqvist@excite.com
  7. ***************************************************************************/
  8. #include "Private.h"
  9. #include "TransformVisitor.h"
  10. #include "Group.h"
  11. #include "Shape3D.h"
  12. #include "Sound.h"
  13. #include "Camera.h"
  14. #include "Light.h"
  15. #include "BillBoard.h"
  16. #include "CollisionBounds.h"
  17. #include "Polygon.h"
  18. #include "EMath.h"
  19. TransformVisitor * TransformVisitor::p_TransformVisitor = NULL;
  20. TransformVisitor::TransformVisitor() {
  21. p_GroupCamera = NULL;
  22. }
  23. TransformVisitor::~TransformVisitor() {
  24. p_TransformVisitor = NULL;
  25. }
  26. TransformVisitor * TransformVisitor::getInstance() {
  27. if (p_TransformVisitor == NULL) {
  28. p_TransformVisitor = new TransformVisitor();
  29. }
  30. return p_TransformVisitor;
  31. }
  32. void TransformVisitor::setCamera(Group * g) {
  33. p_GroupCamera = g;
  34. }
  35. void TransformVisitor::empty() {
  36. }
  37. void TransformVisitor::visit(Group* g) {
  38. // Check properties before applying transform
  39. if (g->m_iProperties & EM_GROUP_NO_TRANSFORM) return;
  40. if (g->m_iProperties & EM_GROUP_TRANSFORM_ONCE) {
  41. g->unsetProperty(EM_GROUP_TRANSFORM_ONCE);
  42. g->setProperty(EM_GROUP_NO_TRANSFORM);
  43. }
  44. // Apply the transform to the Group.
  45. if (g->p_Parent == NULL) {
  46. // The group with no parent must be the engine!
  47. EMath::matrixMulti(g->m_mtxSrc, EMath::identityMatrix, g->m_mtxTrans);
  48. } else {
  49. EMath::matrixMulti(g->m_mtxSrc, g->p_Parent->m_mtxTrans, g->m_mtxTrans);
  50. }
  51. // Apple transform to all shapes.
  52. vector<Shape3D*>::iterator shapeIter = g->m_vShape3D.begin();
  53. vector<Shape3D*>::iterator shapeEnd = g->m_vShape3D.end();
  54. for ( ; shapeIter != shapeEnd; ++shapeIter) {
  55. vector<Polygon3D*>::iterator polyIter = (*shapeIter)->m_vPolygon.begin();
  56. vector<Polygon3D*>::iterator polyEnd = (*shapeIter)->m_vPolygon.end();
  57. // Apply transform to all polygon normals
  58. for ( ; polyIter != polyEnd; ++polyIter) {
  59. // Rotate the normal.
  60. //EMath::applyMatrixRot(g->m_mtxTrans, (*polyIter)->m_nmlSrc, (*polyIter)->m_nmlTrans);
  61. EMathApplyMatrixRot(g->m_mtxTrans, (*polyIter)->m_nmlSrc, (*polyIter)->m_nmlTrans);
  62. //EMath::normalizeVector((*polyIter)->m_nmlTrans);
  63. // TODO: optimze - remove normalize
  64. }
  65. // Apply transform to all vertices and normals
  66. vector<Vertex3D>::iterator srcIter = (*shapeIter)->m_vVtxSrc.begin();
  67. vector<Vertex3D>::iterator srcEnd = (*shapeIter)->m_vVtxSrc.end();
  68. vector<Vertex3D>::iterator transIter = (*shapeIter)->m_vVtxTrans.begin();
  69. vector<Vertex3D>::iterator nmlSrcIter = (*shapeIter)->m_vNmlSrc.begin();
  70. vector<Vertex3D>::iterator nmlTransIter = (*shapeIter)->m_vNmlTrans.begin();
  71. // EmAssert(((*shapeIter)->m_vVtxSrc.size() ==
  72. // (*shapeIter)->m_vVtxTrans.size()) &&
  73. // ((*shapeIter)->m_vNmlSrc.size() ==
  74. // (*shapeIter)->m_vNmlTrans.size()),
  75. // "size miss match " <<
  76. // (*shapeIter)->m_vVtxSrc.size() <<" "<<
  77. // (*shapeIter)->m_vVtxTrans.size() <<" "<<
  78. // (*shapeIter)->m_vNmlSrc.size() <<" "<<
  79. // (*shapeIter)->m_vNmlTrans.size() );
  80. for ( ; srcIter != srcEnd;
  81. ++srcIter, ++transIter, ++nmlSrcIter, ++nmlTransIter) {
  82. //EMath::applyMatrix(g->m_mtxTrans, (*srcIter), (*transIter));
  83. //EMath::applyMatrixRot(g->m_mtxTrans, (*nmlSrcIter), (*nmlTransIter));
  84. EMathApplyMatrix(g->m_mtxTrans, (*srcIter), (*transIter));
  85. EMathApplyMatrixRot(g->m_mtxTrans, (*nmlSrcIter), (*nmlTransIter));
  86. //EMath::normalizeVector(*nmlTransIter);
  87. // TODO: optimize - macro instead of apply-fct calls, remove normalize
  88. EM_COUT_D("TransformVisitor::visit() " << srcIter <<" "<<
  89. (*srcIter).x <<" "<< (*srcIter).y <<" "<< (*srcIter).z <<" -> "<<
  90. (*transIter).x <<" "<< (*transIter).y <<" "<< (*transIter).z, 0);
  91. }
  92. }
  93. // Apply transform to Sound.
  94. /*
  95. if (g->oSound != NULL) {
  96. Vertex3D* vtx = &(g->oSound->vtxPos);
  97. Vertex3D* trVtx = &(g->oSound->vtxTrPos);
  98. EMath::applyMatrix(g->trMatrix, vtx, trVtx);
  99. }
  100. */
  101. // Apply transform to BillBoard.
  102. if (g->p_BillBoard != NULL) {
  103. EMath::applyMatrix(g->m_mtxTrans, g->p_BillBoard->m_vtxSrc, g->p_BillBoard->m_vtxTrans);
  104. }
  105. // Apply transform to Light.
  106. if (g->p_Light != NULL) {
  107. EMath::applyMatrix(g->m_mtxTrans, g->p_Light->m_vtxSrc, g->p_Light->m_vtxTrans);
  108. }
  109. // Apply transform to CollisionBounds
  110. if (g->p_CollisionBounds != NULL) {
  111. g->p_CollisionBounds->transform(g->m_mtxTrans);
  112. }
  113. }