PolyScene.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "PolyInputEvent.h"
  31. #include "PolySceneMesh.h"
  32. #include "PolyRay.h"
  33. #include "PolySceneManager.h"
  34. using std::vector;
  35. using namespace Polycode;
  36. Scene::Scene() : EventDispatcher() {
  37. initScene(SCENE_3D, false);
  38. }
  39. Scene::Scene(int sceneType, bool virtualScene) : EventDispatcher() {
  40. initScene(sceneType, virtualScene);
  41. }
  42. void Scene::initScene(int sceneType, bool virtualScene) {
  43. core = CoreServices::getInstance()->getCore();
  44. this->sceneType = sceneType;
  45. defaultCamera = new Camera(this);
  46. activeCamera = defaultCamera;
  47. fogEnabled = false;
  48. lightingEnabled = false;
  49. enabled = true;
  50. isSceneVirtual = virtualScene;
  51. hasLightmaps = false;
  52. clearColor.setColor(0.13f,0.13f,0.13f,1.0f);
  53. ambientColor.setColor(0.0,0.0,0.0,1.0);
  54. useClearColor = false;
  55. ownsChildren = false;
  56. remapMouse = false;
  57. renderer = CoreServices::getInstance()->getRenderer();
  58. rootEntity.setRenderer(renderer);
  59. CoreServices::getInstance()->getSceneManager()->addScene(this);
  60. setSceneType(sceneType);
  61. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  62. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  63. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  64. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEWHEEL_UP);
  65. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEWHEEL_DOWN);
  66. }
  67. void Scene::setSceneType(int newType) {
  68. sceneType = newType;
  69. switch(sceneType) {
  70. case SCENE_2D:
  71. defaultCamera->setClippingPlanes(-100.0, 100.0);
  72. defaultCamera->setOrthoMode(true);
  73. defaultCamera->setOrthoSize(CoreServices::getInstance()->getCore()->getXRes(),CoreServices::getInstance()->getCore()->getYRes());
  74. break;
  75. case SCENE_2D_TOPLEFT:
  76. defaultCamera->setClippingPlanes(-100.0, 100.0);
  77. defaultCamera->setOrthoMode(true);
  78. defaultCamera->setOrthoSizeMode(Camera::ORTHO_SIZE_VIEWPORT);
  79. defaultCamera->topLeftOrtho = true;
  80. rootEntity.setInverseY(true);
  81. break;
  82. case SCENE_3D:
  83. defaultCamera->setClippingPlanes(1.0, 1000.0);
  84. break;
  85. }
  86. }
  87. void Scene::setActiveCamera(Camera *camera) {
  88. activeCamera = camera;
  89. }
  90. Camera *Scene::getActiveCamera() {
  91. return activeCamera;
  92. }
  93. void Scene::setVirtual(bool val) {
  94. isSceneVirtual = val;
  95. }
  96. bool Scene::isVirtual() {
  97. return isSceneVirtual;
  98. }
  99. void Scene::setEnabled(bool enabled) {
  100. this->enabled = enabled;
  101. }
  102. bool Scene::isEnabled() {
  103. return enabled;
  104. }
  105. void Scene::Update() {
  106. if(sceneType == SCENE_2D_TOPLEFT) {
  107. rootEntity.setPositionY(-CoreServices::getInstance()->getCore()->getYRes());
  108. }
  109. rootEntity.doUpdates();
  110. }
  111. Scene::~Scene() {
  112. core->getInput()->removeAllHandlersForListener(this);
  113. CoreServices::getInstance()->getSceneManager()->removeScene(this);
  114. delete defaultCamera;
  115. }
  116. void Scene::enableLighting(bool enable) {
  117. lightingEnabled = enable;
  118. CoreServices::getInstance()->getRenderer()->enableLighting(enable);
  119. }
  120. void Scene::enableFog(bool enable) {
  121. fogEnabled = enable;
  122. }
  123. void Scene::setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth) {
  124. this->fogMode = fogMode;
  125. fogColor = color;
  126. fogDensity = density;
  127. fogStartDepth = startDepth;
  128. fogEndDepth = endDepth;
  129. }
  130. void Scene::addEntity(Entity *entity) {
  131. rootEntity.addChild(entity);
  132. }
  133. void Scene::addChild(Entity *entity) {
  134. addEntity(entity);
  135. }
  136. void Scene::removeEntity(Entity *entity) {
  137. rootEntity.removeChild(entity);
  138. }
  139. Camera *Scene::getDefaultCamera() {
  140. return defaultCamera;
  141. }
  142. void Scene::Render(Camera *targetCamera) {
  143. if(!targetCamera && !activeCamera)
  144. return;
  145. if(!targetCamera)
  146. targetCamera = activeCamera;
  147. //make these the closest
  148. Matrix4 textureMatrix;
  149. Matrix4 *matrixPtr;
  150. targetCamera->rebuildTransformMatrix();
  151. if(useClearColor) {
  152. CoreServices::getInstance()->getRenderer()->setClearColor(clearColor.r,clearColor.g,clearColor.b, clearColor.a);
  153. CoreServices::getInstance()->getRenderer()->clearScreen();
  154. }
  155. CoreServices::getInstance()->getRenderer()->setAmbientColor(ambientColor.r,ambientColor.g,ambientColor.b);
  156. CoreServices::getInstance()->getRenderer()->clearLights();
  157. for(int i=0; i < lights.size(); i++) {
  158. SceneLight *light = lights[i];
  159. if(!light->enabled)
  160. continue;
  161. Vector3 direction;
  162. Vector3 position;
  163. matrixPtr = NULL;
  164. direction.x = 0;
  165. direction.y = 0;
  166. direction.z = -1;
  167. direction = light->getConcatenatedMatrix().rotateVector(direction);
  168. direction.Normalize();
  169. Texture *shadowMapTexture = NULL;
  170. if(light->areShadowsEnabled()) {
  171. if(light->getType() == SceneLight::SPOT_LIGHT) {
  172. // textureMatrix.identity();
  173. Matrix4 matTexAdj(0.5f, 0.0f, 0.0f, 0.0f,
  174. 0.0f, 0.5f, 0.0f, 0.0f,
  175. 0.0f, 0.0f, 0.5f, 0.0f,
  176. 0.5f, 0.5f, 0.5f, 1.0f );
  177. light->renderDepthMap(this);
  178. textureMatrix = light->getLightViewMatrix() * matTexAdj;
  179. matrixPtr = &textureMatrix;
  180. // CoreServices::getInstance()->getRenderer()->addShadowMap(light->getZBufferTexture());
  181. shadowMapTexture = light->getZBufferTexture();
  182. }
  183. }
  184. position = light->getPosition();
  185. if(light->getParentEntity() != NULL) {
  186. position = light->getParentEntity()->getConcatenatedMatrix() * position;
  187. }
  188. 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);
  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. rootEntity.updateEntityMatrix();
  199. rootEntity.transformAndRender();
  200. }
  201. void Scene::RenderDepthOnly(Camera *targetCamera) {
  202. CoreServices::getInstance()->getRenderer()->cullFrontFaces(true);
  203. targetCamera->rebuildTransformMatrix();
  204. targetCamera->doCameraTransform();
  205. targetCamera->buildFrustumPlanes();
  206. CoreServices::getInstance()->getRenderer()->setTexture(NULL);
  207. CoreServices::getInstance()->getRenderer()->enableShaders(false);
  208. rootEntity.updateEntityMatrix();
  209. rootEntity.transformAndRender();
  210. CoreServices::getInstance()->getRenderer()->enableShaders(true);
  211. CoreServices::getInstance()->getRenderer()->cullFrontFaces(false);
  212. }
  213. Ray Scene::projectRayFromCameraAndViewportCoordinate(Camera *camera, Vector2 coordinate) {
  214. Polycode::Rectangle viewport = camera->getViewport();
  215. if(remapMouse) {
  216. viewport.x = sceneMouseRect.x * renderer->getBackingResolutionScaleX();
  217. viewport.y = (core->getYRes() - (sceneMouseRect.y + sceneMouseRect.h)) * renderer->getBackingResolutionScaleY();
  218. }
  219. Vector3 dir = renderer->projectRayFrom2DCoordinate(coordinate.x * renderer->getBackingResolutionScaleX(), coordinate.y * renderer->getBackingResolutionScaleY(), camera->getConcatenatedMatrix(), camera->getProjectionMatrix(), viewport);
  220. Vector3 pos;
  221. switch(sceneType) {
  222. case SCENE_2D:
  223. {
  224. Number orthoSizeX = camera->getOrthoSizeX();
  225. Number orthoSizeY = camera->getOrthoSizeY();
  226. Vector2 remappedMouse = Vector2(coordinate.x, coordinate.y);
  227. Vector2 screenSize = Vector2(core->getXRes(), core->getYRes());
  228. if(remapMouse) {
  229. remappedMouse.x = coordinate.x - sceneMouseRect.x;
  230. remappedMouse.y = coordinate.y - sceneMouseRect.y;
  231. screenSize = Vector2(sceneMouseRect.w, sceneMouseRect.h);
  232. }
  233. pos = Vector3(((remappedMouse.x/screenSize.x)*orthoSizeX) - (orthoSizeX*0.5), (((screenSize.y-remappedMouse.y)/screenSize.y)*orthoSizeY) - (orthoSizeY*0.5), 0.0);
  234. pos = camera->getConcatenatedMatrix() * pos;
  235. }
  236. break;
  237. case SCENE_2D_TOPLEFT:
  238. pos = Vector3(coordinate.x, core->getYRes()-coordinate.y, 0.0);
  239. pos = camera->getConcatenatedMatrix() * pos;
  240. break;
  241. case SCENE_3D:
  242. pos = camera->getConcatenatedMatrix().getPosition();
  243. break;
  244. }
  245. return Ray(pos, dir);
  246. }
  247. void Scene::handleEvent(Event *event) {
  248. if(event->getDispatcher() == core->getInput() && rootEntity.processInputEvents) {
  249. InputEvent *inputEvent = (InputEvent*) event;
  250. Ray ray = projectRayFromCameraAndViewportCoordinate(activeCamera, inputEvent->mousePosition);
  251. switch(inputEvent->getEventCode()) {
  252. case InputEvent::EVENT_MOUSEDOWN:
  253. rootEntity.onMouseDown(ray, inputEvent->mouseButton, inputEvent->timestamp);
  254. break;
  255. case InputEvent::EVENT_MOUSEMOVE:
  256. rootEntity.onMouseMove(ray, inputEvent->timestamp);
  257. break;
  258. case InputEvent::EVENT_MOUSEUP:
  259. rootEntity.onMouseUp(ray, inputEvent->mouseButton, inputEvent->timestamp);
  260. break;
  261. case InputEvent::EVENT_MOUSEWHEEL_UP:
  262. rootEntity.onMouseWheelUp(ray, inputEvent->timestamp);
  263. break;
  264. case InputEvent::EVENT_MOUSEWHEEL_DOWN:
  265. rootEntity.onMouseWheelDown(ray,inputEvent->timestamp);
  266. break;
  267. }
  268. }
  269. }
  270. void Scene::addLight(SceneLight *light) {
  271. lights.push_back(light);
  272. }
  273. void Scene::removeLight(SceneLight *light) {
  274. removeEntity(light);
  275. for(int i=0; i < lights.size(); i++) {
  276. if(lights[i] == light) {
  277. lights.erase(lights.begin()+i);
  278. return;
  279. }
  280. }
  281. }
  282. SceneLight *Scene::getNearestLight(Vector3 pos) {
  283. if(lights.size() > 0)
  284. return lights[0];
  285. else
  286. return NULL;
  287. }
  288. int Scene::getNumLights() {
  289. return lights.size();
  290. }
  291. SceneLight *Scene::getLight(int index) {
  292. return lights[index];
  293. }