PolyCamera.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 "PolyCamera.h"
  20. #include "PolyCore.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyMaterial.h"
  23. #include "PolyRenderer.h"
  24. #include "PolyResource.h"
  25. #include "PolyResourceManager.h"
  26. #include "PolyScene.h"
  27. #include "PolyShader.h"
  28. #include "PolyTexture.h"
  29. using namespace Polycode;
  30. Camera::Camera(Scene *parentScene) : Entity() {
  31. projectionMode = PERSPECTIVE_FOV;
  32. renderer = CoreServices::getInstance()->getRenderer();
  33. setParentScene(parentScene);
  34. setFOV(45.0f);
  35. filterShaderMaterial = NULL;
  36. originalSceneTexture = NULL;
  37. zBufferSceneTexture = NULL;
  38. exposureLevel = 1.0f;
  39. _hasFilterShader = false;
  40. frustumCulling = true;
  41. nearClipPlane = 1.0;
  42. farClipPlane = 1000.0;
  43. topLeftOrtho = false;
  44. orthoSizeX = 1.0;
  45. orthoSizeY = 1.0;
  46. }
  47. Camera::~Camera() {
  48. for(int i=0; i < localShaderOptions.size(); i++) {
  49. delete localShaderOptions[i];
  50. }
  51. delete originalSceneTexture;
  52. delete zBufferSceneTexture;
  53. }
  54. void Camera::setClippingPlanes(Number nearClipPlane, Number farClipPlane) {
  55. this->nearClipPlane = nearClipPlane;
  56. this->farClipPlane = farClipPlane;
  57. }
  58. void Camera::setFOV(Number fov) {
  59. setProjectionMode(PERSPECTIVE_FOV);
  60. this->fov = fov;
  61. }
  62. bool Camera::isSphereInFrustum(Vector3 pos, Number fRadius) {
  63. if(!frustumCulling)
  64. return true;
  65. for( int i = 0; i < 6; ++i )
  66. {
  67. if( frustumPlanes[i][0] * pos.x +
  68. frustumPlanes[i][1] * pos.y +
  69. frustumPlanes[i][2] * pos.z +
  70. frustumPlanes[i][3] <= -fRadius )
  71. return false;
  72. }
  73. return true;
  74. }
  75. void Camera::setOrthoSize(Number orthoSizeX, Number orthoSizeY) {
  76. this->orthoSizeX = orthoSizeX;
  77. this->orthoSizeY = orthoSizeY;
  78. }
  79. void Camera::setOrthoMode(bool mode) {
  80. if (mode && !getOrthoMode()) {
  81. setProjectionMode(ORTHO_SIZE_LOCK_HEIGHT);
  82. }
  83. else if (!mode && getOrthoMode()) {
  84. setProjectionMode(PERSPECTIVE_FOV);
  85. }
  86. }
  87. void Camera::setFrustumMode(Number left, Number right, Number bottom, Number top, Number front, Number back) {
  88. setProjectionMode(PERSPECTIVE_FRUSTUM);
  89. leftFrustum = left;
  90. rightFrustum = right;
  91. bottomFrustum = bottom;
  92. topFrustum = top;
  93. nearClipPlane = front;
  94. farClipPlane = back;
  95. }
  96. Number Camera::getOrthoSizeX() {
  97. return orthoSizeX;
  98. }
  99. Number Camera::getOrthoSizeY() {
  100. return orthoSizeY;
  101. }
  102. void Camera::buildFrustumPlanes() {
  103. Matrix4 p;
  104. Matrix4 mv;
  105. Matrix4 mvp;
  106. Number t;
  107. p = renderer->getProjectionMatrix();
  108. mv = renderer->getModelviewMatrix();
  109. //
  110. // Concatenate the projection matrix and the model-view matrix to produce
  111. // a combined model-view-projection matrix.
  112. //
  113. mvp.ml[ 0] = mv.ml[ 0] * p.ml[ 0] + mv.ml[ 1] * p.ml[ 4] + mv.ml[ 2] * p.ml[ 8] + mv.ml[ 3] * p.ml[12];
  114. mvp.ml[ 1] = mv.ml[ 0] * p.ml[ 1] + mv.ml[ 1] * p.ml[ 5] + mv.ml[ 2] * p.ml[ 9] + mv.ml[ 3] * p.ml[13];
  115. mvp.ml[ 2] = mv.ml[ 0] * p.ml[ 2] + mv.ml[ 1] * p.ml[ 6] + mv.ml[ 2] * p.ml[10] + mv.ml[ 3] * p.ml[14];
  116. mvp.ml[ 3] = mv.ml[ 0] * p.ml[ 3] + mv.ml[ 1] * p.ml[ 7] + mv.ml[ 2] * p.ml[11] + mv.ml[ 3] * p.ml[15];
  117. mvp.ml[ 4] = mv.ml[ 4] * p.ml[ 0] + mv.ml[ 5] * p.ml[ 4] + mv.ml[ 6] * p.ml[ 8] + mv.ml[ 7] * p.ml[12];
  118. mvp.ml[ 5] = mv.ml[ 4] * p.ml[ 1] + mv.ml[ 5] * p.ml[ 5] + mv.ml[ 6] * p.ml[ 9] + mv.ml[ 7] * p.ml[13];
  119. mvp.ml[ 6] = mv.ml[ 4] * p.ml[ 2] + mv.ml[ 5] * p.ml[ 6] + mv.ml[ 6] * p.ml[10] + mv.ml[ 7] * p.ml[14];
  120. mvp.ml[ 7] = mv.ml[ 4] * p.ml[ 3] + mv.ml[ 5] * p.ml[ 7] + mv.ml[ 6] * p.ml[11] + mv.ml[ 7] * p.ml[15];
  121. mvp.ml[ 8] = mv.ml[ 8] * p.ml[ 0] + mv.ml[ 9] * p.ml[ 4] + mv.ml[10] * p.ml[ 8] + mv.ml[11] * p.ml[12];
  122. mvp.ml[ 9] = mv.ml[ 8] * p.ml[ 1] + mv.ml[ 9] * p.ml[ 5] + mv.ml[10] * p.ml[ 9] + mv.ml[11] * p.ml[13];
  123. mvp.ml[10] = mv.ml[ 8] * p.ml[ 2] + mv.ml[ 9] * p.ml[ 6] + mv.ml[10] * p.ml[10] + mv.ml[11] * p.ml[14];
  124. mvp.ml[11] = mv.ml[ 8] * p.ml[ 3] + mv.ml[ 9] * p.ml[ 7] + mv.ml[10] * p.ml[11] + mv.ml[11] * p.ml[15];
  125. mvp.ml[12] = mv.ml[12] * p.ml[ 0] + mv.ml[13] * p.ml[ 4] + mv.ml[14] * p.ml[ 8] + mv.ml[15] * p.ml[12];
  126. mvp.ml[13] = mv.ml[12] * p.ml[ 1] + mv.ml[13] * p.ml[ 5] + mv.ml[14] * p.ml[ 9] + mv.ml[15] * p.ml[13];
  127. mvp.ml[14] = mv.ml[12] * p.ml[ 2] + mv.ml[13] * p.ml[ 6] + mv.ml[14] * p.ml[10] + mv.ml[15] * p.ml[14];
  128. mvp.ml[15] = mv.ml[12] * p.ml[ 3] + mv.ml[13] * p.ml[ 7] + mv.ml[14] * p.ml[11] + mv.ml[15] * p.ml[15];
  129. //
  130. // Extract the frustum's right clipping plane and normalize it.
  131. //
  132. frustumPlanes[0][0] = mvp.ml[ 3] - mvp.ml[ 0];
  133. frustumPlanes[0][1] = mvp.ml[ 7] - mvp.ml[ 4];
  134. frustumPlanes[0][2] = mvp.ml[11] - mvp.ml[ 8];
  135. frustumPlanes[0][3] = mvp.ml[15] - mvp.ml[12];
  136. t = (Number) sqrt( frustumPlanes[0][0] * frustumPlanes[0][0] +
  137. frustumPlanes[0][1] * frustumPlanes[0][1] +
  138. frustumPlanes[0][2] * frustumPlanes[0][2] );
  139. frustumPlanes[0][0] /= t;
  140. frustumPlanes[0][1] /= t;
  141. frustumPlanes[0][2] /= t;
  142. frustumPlanes[0][3] /= t;
  143. //
  144. // Extract the frustum's left clipping plane and normalize it.
  145. //
  146. frustumPlanes[1][0] = mvp.ml[ 3] + mvp.ml[ 0];
  147. frustumPlanes[1][1] = mvp.ml[ 7] + mvp.ml[ 4];
  148. frustumPlanes[1][2] = mvp.ml[11] + mvp.ml[ 8];
  149. frustumPlanes[1][3] = mvp.ml[15] + mvp.ml[12];
  150. t = (Number) sqrt( frustumPlanes[1][0] * frustumPlanes[1][0] +
  151. frustumPlanes[1][1] * frustumPlanes[1][1] +
  152. frustumPlanes[1][2] * frustumPlanes[1][2] );
  153. frustumPlanes[1][0] /= t;
  154. frustumPlanes[1][1] /= t;
  155. frustumPlanes[1][2] /= t;
  156. frustumPlanes[1][3] /= t;
  157. //
  158. // Extract the frustum's bottom clipping plane and normalize it.
  159. //
  160. frustumPlanes[2][0] = mvp.ml[ 3] + mvp.ml[ 1];
  161. frustumPlanes[2][1] = mvp.ml[ 7] + mvp.ml[ 5];
  162. frustumPlanes[2][2] = mvp.ml[11] + mvp.ml[ 9];
  163. frustumPlanes[2][3] = mvp.ml[15] + mvp.ml[13];
  164. t = (Number) sqrt( frustumPlanes[2][0] * frustumPlanes[2][0] +
  165. frustumPlanes[2][1] * frustumPlanes[2][1] +
  166. frustumPlanes[2][2] * frustumPlanes[2][2] );
  167. frustumPlanes[2][0] /= t;
  168. frustumPlanes[2][1] /= t;
  169. frustumPlanes[2][2] /= t;
  170. frustumPlanes[2][3] /= t;
  171. //
  172. // Extract the frustum's top clipping plane and normalize it.
  173. //
  174. frustumPlanes[3][0] = mvp.ml[ 3] - mvp.ml[ 1];
  175. frustumPlanes[3][1] = mvp.ml[ 7] - mvp.ml[ 5];
  176. frustumPlanes[3][2] = mvp.ml[11] - mvp.ml[ 9];
  177. frustumPlanes[3][3] = mvp.ml[15] - mvp.ml[13];
  178. t = (Number) sqrt( frustumPlanes[3][0] * frustumPlanes[3][0] +
  179. frustumPlanes[3][1] * frustumPlanes[3][1] +
  180. frustumPlanes[3][2] * frustumPlanes[3][2] );
  181. frustumPlanes[3][0] /= t;
  182. frustumPlanes[3][1] /= t;
  183. frustumPlanes[3][2] /= t;
  184. frustumPlanes[3][3] /= t;
  185. //
  186. // Extract the frustum's far clipping plane and normalize it.
  187. //
  188. frustumPlanes[4][0] = mvp.ml[ 3] - mvp.ml[ 2];
  189. frustumPlanes[4][1] = mvp.ml[ 7] - mvp.ml[ 6];
  190. frustumPlanes[4][2] = mvp.ml[11] - mvp.ml[10];
  191. frustumPlanes[4][3] = mvp.ml[15] - mvp.ml[14];
  192. t = (Number) sqrt( frustumPlanes[4][0] * frustumPlanes[4][0] +
  193. frustumPlanes[4][1] * frustumPlanes[4][1] +
  194. frustumPlanes[4][2] * frustumPlanes[4][2] );
  195. frustumPlanes[4][0] /= t;
  196. frustumPlanes[4][1] /= t;
  197. frustumPlanes[4][2] /= t;
  198. frustumPlanes[4][3] /= t;
  199. //
  200. // Extract the frustum's near clipping plane and normalize it.
  201. //
  202. frustumPlanes[5][0] = mvp.ml[ 3] + mvp.ml[ 2];
  203. frustumPlanes[5][1] = mvp.ml[ 7] + mvp.ml[ 6];
  204. frustumPlanes[5][2] = mvp.ml[11] + mvp.ml[10];
  205. frustumPlanes[5][3] = mvp.ml[15] + mvp.ml[14];
  206. t = (Number) sqrt( frustumPlanes[5][0] * frustumPlanes[5][0] +
  207. frustumPlanes[5][1] * frustumPlanes[5][1] +
  208. frustumPlanes[5][2] * frustumPlanes[5][2] );
  209. frustumPlanes[5][0] /= t;
  210. frustumPlanes[5][1] /= t;
  211. frustumPlanes[5][2] /= t;
  212. frustumPlanes[5][3] /= t;
  213. }
  214. Entity *Camera::Clone(bool deepClone, bool ignoreEditorOnly) const {
  215. Camera *newCamera = new Camera(NULL);
  216. applyClone(newCamera, deepClone, ignoreEditorOnly);
  217. return newCamera;
  218. }
  219. void Camera::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const {
  220. Entity::applyClone(clone, deepClone, ignoreEditorOnly);
  221. Camera *cloneCamera = (Camera*) clone;
  222. cloneCamera->projectionMatrix = Matrix4(projectionMatrix.ml);
  223. cloneCamera->fov = fov;
  224. cloneCamera->viewport = viewport;
  225. cloneCamera->setOrthoSize(orthoSizeX, orthoSizeY);
  226. cloneCamera->projectionMode = projectionMode;
  227. cloneCamera->setClippingPlanes(nearClipPlane, farClipPlane);
  228. cloneCamera->setExposureLevel(exposureLevel);
  229. }
  230. Scene *Camera::getParentScene() const {
  231. return parentScene;
  232. }
  233. bool Camera::canSee(Entity *entity) {
  234. return isSphereInFrustum(entity->getPosition(), entity->getBBoxRadius());
  235. }
  236. void Camera::setParentScene(Scene *parentScene) {
  237. this->parentScene = parentScene;
  238. }
  239. void Camera::setPostFilterByName(const String& materialName) {
  240. Material *shaderMaterial = (Material*) CoreServices::getInstance()->getResourceManager()->getGlobalPool()->getResource(Resource::RESOURCE_MATERIAL, materialName);
  241. if(shaderMaterial)
  242. setPostFilter(shaderMaterial);
  243. }
  244. void Camera::removePostFilter() {
  245. if(_hasFilterShader) {
  246. filterShaderMaterial = NULL;
  247. _hasFilterShader = false;
  248. }
  249. }
  250. void Camera::setPostFilter(Material *shaderMaterial) {
  251. if(!shaderMaterial)
  252. return;
  253. if(shaderMaterial->getNumShaders() == 0)
  254. return;
  255. this->filterShaderMaterial = shaderMaterial;
  256. if(!originalSceneTexture) {
  257. renderer->createRenderTextures(&originalSceneTexture, &zBufferSceneTexture, CoreServices::getInstance()->getCore()->getXRes(), CoreServices::getInstance()->getCore()->getYRes(), shaderMaterial->fp16RenderTargets);
  258. }
  259. for(int i=0; i < shaderMaterial->getNumShaders(); i++) {
  260. ShaderBinding* binding = shaderMaterial->getShader(i)->createBinding();
  261. localShaderOptions.push_back(binding);
  262. binding->addParamPointer(ProgramParam::PARAM_NUMBER, "exposure", (void*)&exposureLevel);
  263. }
  264. _hasFilterShader = true;
  265. }
  266. bool Camera::hasFilterShader() {
  267. return _hasFilterShader;
  268. }
  269. void Camera::setLightDepthTexture(Texture *texture) {
  270. for(int i=0; i < localShaderOptions.size(); i++) {
  271. localShaderOptions[i]->clearTexture("PolyLight0ZBuffer");
  272. localShaderOptions[i]->addTexture("PolyLight0ZBuffer", texture);
  273. }
  274. }
  275. void Camera::drawFilter(Texture *targetTexture, Number targetTextureWidth, Number targetTextureHeight, Texture *targetColorTexture, Texture *targetZTexture) {
  276. if(!filterShaderMaterial)
  277. return;
  278. Texture *finalTargetColorTexture;
  279. Texture *finalTargetZTexture;
  280. if(targetTexture) {
  281. finalTargetColorTexture = targetColorTexture;
  282. finalTargetZTexture = targetZTexture;
  283. renderer->setViewportSize(targetTextureWidth, targetTextureHeight);
  284. } else {
  285. finalTargetColorTexture = originalSceneTexture;
  286. finalTargetZTexture = zBufferSceneTexture;
  287. renderer->setViewportSize(renderer->getXRes(), renderer->getYRes());
  288. }
  289. renderer->bindFrameBufferTexture(finalTargetColorTexture);
  290. renderer->bindFrameBufferTextureDepth(finalTargetZTexture);
  291. parentScene->Render(this);
  292. renderer->unbindFramebuffers();
  293. ShaderBinding* materialBinding;
  294. for(int i=0; i < filterShaderMaterial->getNumShaders(); i++) {
  295. materialBinding = filterShaderMaterial->getShaderBinding(i);
  296. for(int j=0; j < materialBinding->getNumColorTargetBindings(); j++) {
  297. RenderTargetBinding *colorBinding = materialBinding->getColorTargetBinding(j);
  298. materialBinding->clearTexture(colorBinding->name);
  299. materialBinding->addTexture(colorBinding->name, finalTargetColorTexture);
  300. }
  301. for(int j=0; j < materialBinding->getNumDepthTargetBindings(); j++) {
  302. RenderTargetBinding *depthBinding = materialBinding->getDepthTargetBinding(j);
  303. materialBinding->clearTexture(depthBinding->name);
  304. materialBinding->addTexture(depthBinding->name, finalTargetZTexture);
  305. }
  306. renderer->applyMaterial(filterShaderMaterial, localShaderOptions[i], i, true);
  307. if(i==filterShaderMaterial->getNumShaders()-1) {
  308. if(targetTexture) {
  309. renderer->setViewportSize(targetTextureWidth, targetTextureHeight);
  310. renderer->bindFrameBufferTexture(targetTexture);
  311. renderer->clearScreen();
  312. renderer->loadIdentity();
  313. renderer->drawScreenQuad(targetTextureWidth, targetTextureHeight);
  314. renderer->unbindFramebuffers();
  315. } else {
  316. renderer->setViewportSize(renderer->getXRes(), renderer->getYRes());
  317. renderer->clearScreen();
  318. renderer->loadIdentity();
  319. renderer->drawScreenQuad(renderer->getXRes(), renderer->getYRes());
  320. }
  321. } else {
  322. for(int j=0; j < materialBinding->getNumOutTargetBindings(); j++) {
  323. Texture *bindingTexture = materialBinding->getOutTargetBinding(j)->texture;
  324. if(bindingTexture) {
  325. renderer->setViewportSize(bindingTexture->getWidth(), bindingTexture->getHeight());
  326. renderer->bindFrameBufferTexture(bindingTexture);
  327. renderer->drawScreenQuad(bindingTexture->getWidth(), bindingTexture->getHeight());
  328. renderer->unbindFramebuffers();
  329. }
  330. }
  331. }
  332. renderer->clearShader();
  333. renderer->loadIdentity();
  334. }
  335. }
  336. Matrix4 Camera::getProjectionMatrix() {
  337. return projectionMatrix;
  338. }
  339. Polycode::Rectangle Camera::getViewport() {
  340. return viewport;
  341. }
  342. Number Camera::getNearClippingPlane() {
  343. return nearClipPlane;
  344. }
  345. Number Camera::getFarClippingPlane() {
  346. return farClipPlane;
  347. }
  348. void Camera::setProjectionMode(int mode) {
  349. projectionMode = mode;
  350. }
  351. void Camera::doCameraTransform() {
  352. viewport = renderer->getViewport();
  353. switch (projectionMode) {
  354. case PERSPECTIVE_FOV:
  355. renderer->setViewportShift(cameraShift.x, cameraShift.y);
  356. renderer->setProjectionFromFoV(fov, nearClipPlane, farClipPlane);
  357. renderer->setPerspectiveDefaults();
  358. break;
  359. case PERSPECTIVE_FRUSTUM:
  360. renderer->setProjectionFromFrustum(leftFrustum, rightFrustum, bottomFrustum, topFrustum, nearClipPlane, farClipPlane);
  361. renderer->setPerspectiveDefaults();
  362. break;
  363. case ORTHO_SIZE_MANUAL:
  364. renderer->setProjectionOrtho(orthoSizeX, orthoSizeY, nearClipPlane, farClipPlane, !topLeftOrtho);
  365. break;
  366. case ORTHO_SIZE_LOCK_HEIGHT:
  367. renderer->setProjectionOrtho(orthoSizeY * (viewport.w/viewport.h), orthoSizeY, nearClipPlane, farClipPlane, !topLeftOrtho);
  368. break;
  369. case ORTHO_SIZE_LOCK_WIDTH:
  370. renderer->setProjectionOrtho(orthoSizeX, orthoSizeX * (viewport.h/viewport.w), nearClipPlane, farClipPlane, !topLeftOrtho);
  371. break;
  372. case ORTHO_SIZE_VIEWPORT:
  373. renderer->setProjectionOrtho(viewport.w / renderer->getBackingResolutionScaleX(), viewport.h / renderer->getBackingResolutionScaleY(), !topLeftOrtho);
  374. break;
  375. }
  376. renderer->setExposureLevel(exposureLevel);
  377. projectionMatrix = renderer->getProjectionMatrix();
  378. if(matrixDirty) {
  379. rebuildTransformMatrix();
  380. }
  381. Matrix4 camMatrix = getConcatenatedMatrix();
  382. renderer->setCameraMatrix(camMatrix);
  383. camMatrix = camMatrix.Inverse();
  384. renderer->multModelviewMatrix(camMatrix);
  385. }