Scene.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <algorithm>
  2. #include "Scene.h"
  3. namespace gameplay
  4. {
  5. Scene::Scene(void) : _cameraNode(NULL)
  6. {
  7. _ambientColor[0] = 0.0f;
  8. _ambientColor[1] = 0.0f;
  9. _ambientColor[2] = 0.0f;
  10. }
  11. Scene::~Scene(void)
  12. {
  13. }
  14. unsigned int Scene::getTypeId(void) const
  15. {
  16. return SCENE_ID;
  17. }
  18. const char* Scene::getElementName(void) const
  19. {
  20. return "Scene";
  21. }
  22. void Scene::writeBinary(FILE* file)
  23. {
  24. Object::writeBinary(file);
  25. writeBinaryObjects(_nodes, file);
  26. if (_cameraNode)
  27. {
  28. _cameraNode->writeBinaryXref(file);
  29. }
  30. else
  31. {
  32. writeZero(file);
  33. }
  34. write(_ambientColor, Light::COLOR_SIZE, file);
  35. }
  36. void Scene::writeText(FILE* file)
  37. {
  38. fprintElementStart(file);
  39. for (std::list<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); i++)
  40. {
  41. (*i)->writeText(file);
  42. }
  43. if (_cameraNode)
  44. {
  45. fprintfElement(file, "activeCamera", _cameraNode->getId());
  46. }
  47. fprintfElement(file, "ambientColor", _ambientColor, Light::COLOR_SIZE);
  48. fprintElementEnd(file);
  49. }
  50. void Scene::add(Node* node)
  51. {
  52. _nodes.push_back(node);
  53. }
  54. void Scene::setActiveCameraNode(Node* node)
  55. {
  56. _cameraNode = node;
  57. }
  58. Node* Scene::getFirstCameraNode() const
  59. {
  60. for (std::list<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); i++)
  61. {
  62. Node* n = (*i)->getFirstCameraNode();
  63. if (n)
  64. {
  65. return n;
  66. }
  67. }
  68. return NULL;
  69. }
  70. void Scene::calcAmbientColor()
  71. {
  72. float values[3] = {0.0f, 0.0f, 0.0f};
  73. for (std::list<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); i++)
  74. {
  75. calcAmbientColor(*i, values);
  76. }
  77. _ambientColor[0] = std::min(values[0], 1.0f);
  78. _ambientColor[1] = std::min(values[1], 1.0f);
  79. _ambientColor[2] = std::min(values[2], 1.0f);
  80. }
  81. void Scene::calcAmbientColor(const Node* node, float* values) const
  82. {
  83. if (!node)
  84. {
  85. return;
  86. }
  87. if (node->hasLight())
  88. {
  89. Light* light = node->getLight();
  90. if (light->isAmbient())
  91. {
  92. values[0] += light->getRed();
  93. values[1] += light->getGreen();
  94. values[2] += light->getBlue();
  95. }
  96. }
  97. for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
  98. {
  99. calcAmbientColor(child, values);
  100. }
  101. }
  102. }