PolyScene.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. #include "PolyColor.h"
  23. #include "PolyVector3.h"
  24. #include "PolyEntity.h"
  25. #include "PolyCore.h"
  26. #include "PolyEventDispatcher.h"
  27. #include <vector>
  28. class OSFILE;
  29. namespace Polycode {
  30. class Camera;
  31. class Entity;
  32. class SceneLight;
  33. class Mesh;
  34. /**
  35. * Rendering container. The Scene class is the main container for all rendering in Polycode. Scenes are automatically rendered and need only be instantiated to immediately add themselves to the rendering pipeline. A Scene is created with a camera automatically.
  36. */
  37. class _PolyExport Scene : public EventDispatcher {
  38. public:
  39. /**
  40. * Default constructor with options.
  41. * @param sceneType Type of scene to create. Can be Scene::SCENE_2D, Scene::SCENE_3D or Scene::SCENE_2D_TOPLEFT
  42. * @param virtualScene If this flag is set to true, the scene is not rendered to the screen. Use this if you want to render the scene only to a texture.
  43. */
  44. Scene(int sceneType, bool virtualScene = false);
  45. /**
  46. * Default constructor. Defaults to type Scene::SCENE_3D
  47. */
  48. Scene();
  49. virtual ~Scene();
  50. /**
  51. * Adds a new Entity to the scene
  52. * @param entity New entity to add.
  53. */
  54. void addEntity(Entity *entity);
  55. /**
  56. * Adds a new Entity to the scene
  57. * @param entity New entity to add.
  58. */
  59. void addChild(Entity *entity);
  60. /**
  61. * Removes a Entity from the scene
  62. * @param entity New entity to remove.
  63. */
  64. virtual void removeEntity(Entity *entity);
  65. /**
  66. * Returns the scene's default camera.
  67. * @return The scene's default camera.
  68. */
  69. Camera *getDefaultCamera();
  70. /**
  71. * Returns the scene's active camera.
  72. * @return The scene's active camera.
  73. */
  74. Camera *getActiveCamera();
  75. /**
  76. * Sets the scene's active camera.
  77. * @param camera New camera to set as the active camera.
  78. */
  79. void setActiveCamera(Camera *camera);
  80. /**
  81. * Enables and disables lighting in the scene.
  82. * @param enable If false, disables lighting in the scene, if true, enables it.
  83. */
  84. void enableLighting(bool enable);
  85. /**
  86. * Enables and disables fog in the scene.
  87. * @param enable If false, disables lighting in the scene, if true, enables it.
  88. */
  89. void enableFog(bool enable);
  90. /**
  91. * Sets the fog properties for the scene.
  92. * @param fogMode Fog falloff mode. (Renderer::FOG_LINEAR, Renderer::FOG_EXP, Renderer::FOG_EXP2).
  93. * @param color Fog color.
  94. * @param density Fog density.
  95. * @param startDepth Starting depth of the fog.
  96. * @param endDepth Ending depth of the fog.
  97. */
  98. void setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth);
  99. void setSceneType(int newType);
  100. virtual void Update();
  101. void setVirtual(bool val);
  102. bool isVirtual();
  103. bool isEnabled();
  104. void setEnabled(bool enabled);
  105. void Render(Camera *targetCamera = NULL);
  106. void RenderDepthOnly(Camera *targetCamera);
  107. void handleEvent(Event *event);
  108. Ray projectRayFromCameraAndViewportCoordinate(Camera *camera, Vector2 coordinate);
  109. /**
  110. * Adds a light to the scene.
  111. * @param light Light to add to the scene.
  112. */
  113. void addLight(SceneLight *light);
  114. /**
  115. * Removes a light from the scene.
  116. * @param light Light to remove from the scene.
  117. */
  118. void removeLight(SceneLight *light);
  119. SceneLight *getNearestLight(Vector3 pos);
  120. int getNumLights();
  121. SceneLight *getLight(int index);
  122. /**
  123. * Scene clear color
  124. */
  125. Color clearColor;
  126. /**
  127. * If set to true, the renderer will use the scene's clear color when rendering the scene.
  128. */
  129. bool useClearColor;
  130. /**
  131. * Ambient color, passed to lighting shaders
  132. */
  133. Color ambientColor;
  134. /**
  135. * Fog color, passed to lighting shaders.
  136. */
  137. Color fogColor;
  138. /**
  139. * If this is set to false, the scene is not rendered or updated during the render loop.
  140. */
  141. bool enabled;
  142. /**
  143. * If ownsChildren is set to true, the scene will delete its children upon destruction (defaults to false).
  144. */
  145. bool ownsChildren;
  146. static const int SCENE_3D = 0;
  147. static const int SCENE_2D = 1;
  148. static const int SCENE_2D_TOPLEFT = 2;
  149. Entity rootEntity;
  150. Polycode::Rectangle sceneMouseRect;
  151. bool remapMouse;
  152. bool constrainPickingToViewport;
  153. protected:
  154. void initScene(int sceneType, bool virtualScene);
  155. bool hasLightmaps;
  156. Renderer *renderer;
  157. std::vector <SceneLight*> lights;
  158. bool isSceneVirtual;
  159. Camera *defaultCamera;
  160. Camera *activeCamera;
  161. Core *core;
  162. bool lightingEnabled;
  163. bool fogEnabled;
  164. int fogMode;
  165. Number fogDensity;
  166. Number fogStartDepth;
  167. Number fogEndDepth;
  168. int sceneType;
  169. };
  170. }