PolyScene.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyScene.h"
  20. #include "OSBasics.h"
  21. #include "PolyCamera.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyLogger.h"
  24. #include "PolyMaterial.h"
  25. #include "PolyMesh.h"
  26. #include "PolyRenderer.h"
  27. #include "PolyResource.h"
  28. #include "PolyResourceManager.h"
  29. #include "PolySceneLight.h"
  30. #include "PolySceneMesh.h"
  31. #include "PolySceneManager.h"
  32. using std::vector;
  33. using namespace Polycode;
  34. Scene::Scene() : EventDispatcher() {
  35. defaultCamera = new Camera(this);
  36. activeCamera = defaultCamera;
  37. fogEnabled = false;
  38. lightingEnabled = false;
  39. enabled = true;
  40. isSceneVirtual = false;
  41. hasLightmaps = false;
  42. clearColor.setColor(0.13f,0.13f,0.13f,1.0f);
  43. ambientColor.setColor(0.0,0.0,0.0,1.0);
  44. useClearColor = false;
  45. ownsChildren = false;
  46. CoreServices::getInstance()->getSceneManager()->addScene(this);
  47. }
  48. Scene::Scene(bool virtualScene) : EventDispatcher() {
  49. defaultCamera = new Camera(this);
  50. activeCamera = defaultCamera;
  51. fogEnabled = false;
  52. lightingEnabled = false;
  53. enabled = true;
  54. isSceneVirtual = virtualScene;
  55. hasLightmaps = false;
  56. clearColor.setColor(0.13f,0.13f,0.13f,1.0f);
  57. ambientColor.setColor(0.0,0.0,0.0,1.0);
  58. useClearColor = false;
  59. ownsChildren = false;
  60. if (!isSceneVirtual) {
  61. CoreServices::getInstance()->getSceneManager()->addScene(this);
  62. }
  63. }
  64. void Scene::setActiveCamera(Camera *camera) {
  65. activeCamera = camera;
  66. }
  67. Camera *Scene::getActiveCamera() {
  68. return activeCamera;
  69. }
  70. void Scene::setVirtual(bool val) {
  71. isSceneVirtual = val;
  72. }
  73. bool Scene::isVirtual() {
  74. return isSceneVirtual;
  75. }
  76. void Scene::setEnabled(bool enabled) {
  77. this->enabled = enabled;
  78. }
  79. bool Scene::isEnabled() {
  80. return enabled;
  81. }
  82. void Scene::Update() {
  83. for(int i=0; i<entities.size();i++) {
  84. entities[i]->doUpdates();
  85. entities[i]->updateEntityMatrix();
  86. }
  87. }
  88. Scene::~Scene() {
  89. if(ownsChildren) {
  90. for(int i=0; i < entities.size(); i++) {
  91. delete entities[i];
  92. }
  93. }
  94. CoreServices::getInstance()->getSceneManager()->removeScene(this);
  95. delete defaultCamera;
  96. }
  97. void Scene::enableLighting(bool enable) {
  98. lightingEnabled = enable;
  99. CoreServices::getInstance()->getRenderer()->enableLighting(enable);
  100. }
  101. void Scene::enableFog(bool enable) {
  102. fogEnabled = enable;
  103. }
  104. void Scene::setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth) {
  105. this->fogMode = fogMode;
  106. fogColor = color;
  107. fogDensity = density;
  108. fogStartDepth = startDepth;
  109. fogEndDepth = endDepth;
  110. }
  111. SceneEntity *Scene::getEntityAtScreenPosition(Number x, Number y) {
  112. for(int i =0; i< entities.size(); i++) {
  113. if(entities[i]->testMouseCollision(x,y)) {
  114. return entities[i];
  115. }
  116. }
  117. return NULL;
  118. }
  119. void Scene::addEntity(SceneEntity *entity) {
  120. entity->setRenderer(CoreServices::getInstance()->getRenderer());
  121. entities.push_back(entity);
  122. }
  123. void Scene::addChild(SceneEntity *entity) {
  124. addEntity(entity);
  125. }
  126. void Scene::removeEntity(SceneEntity *entity) {
  127. for(int i=0; i < entities.size(); i++) {
  128. if(entities[i] == entity) {
  129. entities.erase(entities.begin()+i);
  130. return;
  131. }
  132. }
  133. }
  134. Camera *Scene::getDefaultCamera() {
  135. return defaultCamera;
  136. }
  137. void Scene::Render(Camera *targetCamera) {
  138. if(!targetCamera && !activeCamera)
  139. return;
  140. if(!targetCamera)
  141. targetCamera = activeCamera;
  142. // prepare lights...
  143. for(int i=0; i<entities.size();i++) {
  144. entities[i]->updateEntityMatrix();
  145. }
  146. //make these the closest
  147. Matrix4 textureMatrix;
  148. Matrix4 *matrixPtr;
  149. targetCamera->rebuildTransformMatrix();
  150. if(useClearColor)
  151. CoreServices::getInstance()->getRenderer()->setClearColor(clearColor.r,clearColor.g,clearColor.b);
  152. CoreServices::getInstance()->getRenderer()->setAmbientColor(ambientColor.r,ambientColor.g,ambientColor.b);
  153. CoreServices::getInstance()->getRenderer()->clearLights();
  154. for(int i=0; i < lights.size(); i++) {
  155. SceneLight *light = lights[i];
  156. if(!light->enabled)
  157. continue;
  158. Vector3 direction;
  159. Vector3 position;
  160. matrixPtr = NULL;
  161. direction.x = 0;
  162. direction.y = 0;
  163. direction.z = -1;
  164. direction = light->getConcatenatedMatrix().rotateVector(direction);
  165. direction.Normalize();
  166. Texture *shadowMapTexture = NULL;
  167. if(light->areShadowsEnabled()) {
  168. if(light->getType() == SceneLight::SPOT_LIGHT) {
  169. // textureMatrix.identity();
  170. Matrix4 matTexAdj(0.5f, 0.0f, 0.0f, 0.0f,
  171. 0.0f, 0.5f, 0.0f, 0.0f,
  172. 0.0f, 0.0f, 0.5f, 0.0f,
  173. 0.5f, 0.5f, 0.5f, 1.0f );
  174. light->renderDepthMap(this);
  175. textureMatrix = light->getLightViewMatrix() * matTexAdj;
  176. matrixPtr = &textureMatrix;
  177. // CoreServices::getInstance()->getRenderer()->addShadowMap(light->getZBufferTexture());
  178. shadowMapTexture = light->getZBufferTexture();
  179. }
  180. }
  181. position = light->getPosition();
  182. if(light->getParentEntity() != NULL) {
  183. position = light->getParentEntity()->getConcatenatedMatrix() * position;
  184. }
  185. CoreServices::getInstance()->getRenderer()->addLight(light->getLightImportance(), position, direction, light->getLightType(), light->lightColor, light->specularLightColor, light->getConstantAttenuation(), light->getLinearAttenuation(), light->getQuadraticAttenuation(), light->getIntensity(), light->getSpotlightCutoff(), light->getSpotlightExponent(), light->areShadowsEnabled(), matrixPtr, shadowMapTexture);
  186. }
  187. if(targetCamera->getOrthoMode()) {
  188. CoreServices::getInstance()->getRenderer()->_setOrthoMode(targetCamera->getOrthoSizeX(), targetCamera->getOrthoSizeY());
  189. }
  190. targetCamera->doCameraTransform();
  191. targetCamera->buildFrustumPlanes();
  192. CoreServices::getInstance()->getRenderer()->enableFog(fogEnabled);
  193. if(fogEnabled) {
  194. CoreServices::getInstance()->getRenderer()->setFogProperties(fogMode, fogColor, fogDensity, fogStartDepth, fogEndDepth);
  195. } else {
  196. CoreServices::getInstance()->getRenderer()->setFogProperties(fogMode, fogColor, 0.0, fogStartDepth, fogEndDepth);
  197. }
  198. for(int i=0; i<entities.size();i++) {
  199. if(entities[i]->getBBoxRadius() > 0) {
  200. if(targetCamera->isSphereInFrustum((entities[i]->getPosition()), entities[i]->getBBoxRadius()))
  201. entities[i]->transformAndRender();
  202. } else {
  203. entities[i]->transformAndRender();
  204. }
  205. }
  206. if(targetCamera->getOrthoMode()) {
  207. CoreServices::getInstance()->getRenderer()->setPerspectiveMode();
  208. }
  209. }
  210. void Scene::RenderDepthOnly(Camera *targetCamera) {
  211. CoreServices::getInstance()->getRenderer()->cullFrontFaces(true);
  212. /*
  213. for(int i=0; i<entities.size();i++) {
  214. entities[i]->doUpdates();
  215. entities[i]->updateEntityMatrix();
  216. }
  217. */
  218. targetCamera->rebuildTransformMatrix();
  219. targetCamera->doCameraTransform();
  220. targetCamera->buildFrustumPlanes();
  221. CoreServices::getInstance()->getRenderer()->setTexture(NULL);
  222. CoreServices::getInstance()->getRenderer()->enableShaders(false);
  223. for(int i=0; i<entities.size();i++) {
  224. if(entities[i]->castShadows) {
  225. if(entities[i]->getBBoxRadius() > 0) {
  226. if(targetCamera->isSphereInFrustum((entities[i]->getPosition()), entities[i]->getBBoxRadius()))
  227. entities[i]->transformAndRender();
  228. } else {
  229. entities[i]->transformAndRender();
  230. }
  231. }
  232. }
  233. CoreServices::getInstance()->getRenderer()->enableShaders(true);
  234. CoreServices::getInstance()->getRenderer()->cullFrontFaces(false);
  235. }
  236. void Scene::addLight(SceneLight *light) {
  237. lights.push_back(light);
  238. addEntity(light);
  239. }
  240. void Scene::removeLight(SceneLight *light) {
  241. removeEntity(light);
  242. for(int i=0; i < lights.size(); i++) {
  243. if(lights[i] == light) {
  244. lights.erase(lights.begin()+i);
  245. return;
  246. }
  247. }
  248. }
  249. SceneLight *Scene::getNearestLight(Vector3 pos) {
  250. if(lights.size() > 0)
  251. return lights[0];
  252. else
  253. return NULL;
  254. }
  255. int Scene::getNumLights() {
  256. return lights.size();
  257. }
  258. SceneLight *Scene::getLight(int index) {
  259. return lights[index];
  260. }