PolyCamera.cpp 19 KB

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