Scene.cpp 2.6 KB

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