CSceneNode.h
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #ifndef _CSCENENODE_H_
  2. #define _CSCENENODE_H_
  3. #include "main.h"
  4. /*
  5. my hierarchy nodes need the following:
  6. vectorq<CSceneNode*> vKids; // children nodes vector (to which nodes is this node parent)
  7. CSceneNode *pParent; // parent node (whos child this is)
  8. CMatrix matrix; // matrix (position, rotation)
  9. and other: (not everything exist yet, this is just what will probably ever be in there)
  10. CModel *pModel; // pointer to model (this will be rendered)
  11. NewtonBody *pBody // pointer to newton body object, so we can set physic properties
  12. CVector colour; // colour of object (eg. somebody gets stoned, so his colour changes =])
  13. */
  14. enum SN_RTTI
  15. {
  16. SN_PHYSIC_BASE,
  17. SN_PARTICLES,
  18. SN_ROOT,
  19. SN_MAIN_ROOT,
  20. SN_BLENDING_ROOT
  21. };
  22. class CSceneNode;
  23. typedef vectorq<CSceneNode*> vqCSN;
  24. typedef vqCSN::iterator vqCSNi;
  25. class CSceneNode
  26. {
  27. void SetParent(CSceneNode *p)
  28. {
  29. pParent = p;
  30. }
  31. public:
  32. vqCSN vKids;
  33. CSceneNode *pParent;
  34. // warning!!
  35. // leaveXXX can NOT call another leaveXXX, because it would end as infinite loop
  36. // so every function does all the work itself, and can use only SetParent(..) and vectorq functions
  37. void AddChild(CSceneNode *ch)
  38. {
  39. if (!ch) return;
  40. vKids.add(ch);
  41. ch->SetParent(this);
  42. }
  43. void LeaveChild(CSceneNode *ch)
  44. {
  45. if (!ch) return;
  46. vKids.rm(ch);
  47. ch->SetParent(NULL);
  48. }
  49. void DeleteChild(CSceneNode *ch)
  50. {
  51. if (!ch) return;
  52. // child will be dettached, its in destructor
  53. vqCSNi i = vKids.Find(ch);
  54. if (!i) return;
  55. vKids.rm(i);
  56. delete ch;
  57. }
  58. void DeleteAllChildren()
  59. {
  60. // vectorq<CSceneNode*>::iterator i;
  61. vqCSNi i;
  62. for(i = vKids.begin() ; i != vKids.end() ; i++)
  63. {
  64. if (!*i) continue;
  65. delete *i;
  66. // this is in destructor, but just to be sure
  67. *i = 0;
  68. }
  69. vKids.clear();
  70. }
  71. void LeaveAllChildren()
  72. {
  73. // vectorq<CSceneNode*>::iterator i;
  74. vqCSNi i;
  75. for(i = vKids.begin() ; i != vKids.end() ; i++)
  76. {
  77. if (!*i) continue;
  78. (*i)->SetParent(0);
  79. }
  80. vKids.clear();
  81. }
  82. void LeaveParent()
  83. {
  84. if (!pParent) return;
  85. pParent->vKids.rm(this);
  86. SetParent(0);
  87. }
  88. CSceneNode *GetParent()
  89. { return pParent; }
  90. vqCSN *GetChildrenList()
  91. { return &vKids; }
  92. ~CSceneNode()
  93. {
  94. DeleteAllChildren();
  95. LeaveParent();
  96. }
  97. CMatrix mMatrix; // position and rotation
  98. CVector vColour; // colour of object
  99. // functions
  100. CSceneNode()
  101. {
  102. mMatrix.Reset();
  103. vColour.Set(0, 0, 0);
  104. }
  105. void SetMatrix(CMatrix m)
  106. { mMatrix = m; }
  107. CMatrix GetMatrix()
  108. { return mMatrix; }
  109. void SetPosition(CVector v)
  110. { mMatrix.m_posit = v; }
  111. CVector GetPosition()
  112. { return mMatrix.m_posit; }
  113. virtual void Update(float fTime) = 0;
  114. virtual void Render() = 0;
  115. virtual SN_RTTI GetType() = 0;
  116. /*
  117. void UpdateChildren(float fTime)
  118. {
  119. for (vqCSNi i = vKids.begin() ; i != vKids.end() ; i++)
  120. {
  121. if (!*i) continue;
  122. CSceneNode *pSN;
  123. pSN = *i;
  124. pSN->Update(fTime);
  125. }
  126. }
  127. void RenderChildren()
  128. {
  129. glPushMatrix();
  130. glMultMatrixf(&mMatrix[0][0]);
  131. for (vqCSNi i = vKids.begin() ; i != vKids.end() ; i++)
  132. {
  133. if (!*i) continue;
  134. CSceneNode *pSN;
  135. pSN = *i;
  136. pSN->Render();
  137. }
  138. glPopMatrix();
  139. }
  140. */
  141. };
  142. #endif