PolySceneLight.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "PolySceneLight.h"
  20. #include "PolyCamera.h"
  21. #include "PolyCore.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyMesh.h"
  24. #include "PolyRenderer.h"
  25. #include "PolyScene.h"
  26. using namespace Polycode;
  27. SceneLight::SceneLight(int type, Scene *parentScene, Number intensity, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation) : Entity() {
  28. this->type = type;
  29. this->intensity = intensity;
  30. this->constantAttenuation = constantAttenuation;
  31. this->linearAttenuation = linearAttenuation;
  32. this->quadraticAttenuation = quadraticAttenuation;
  33. spotlightCutoff = 40;
  34. spotlightExponent = 10;
  35. this->depthWrite = false;
  36. lightMesh = new Mesh(Mesh::QUAD_MESH);
  37. lightMesh->createBox(0.1,0.1,0.1);
  38. bBoxRadius = lightMesh->getRadius();
  39. bBox = lightMesh->calculateBBox();
  40. shadowMapFOV = 60.0f;
  41. zBufferTexture = NULL;
  42. spotCamera = NULL;
  43. this->parentScene = parentScene;
  44. shadowsEnabled = false;
  45. lightColor.setColor(1.0f,1.0f,1.0f,1.0f);
  46. setSpotlightProperties(40,0.1);
  47. /*
  48. if(type == SceneLight::SPOT_LIGHT) {
  49. lightShape = new ScenePrimitive(ScenePrimitive::TYPE_CONE, 3, 1.0, 8);
  50. lightShape->Translate(0,0,-1.5);
  51. lightShape->setPitch(90.0);
  52. lightShape->setColor(1.0,1.0,0.0, 0.75);
  53. lightShape->renderWireframe = true;
  54. addChild(lightShape);
  55. } else {
  56. lightShape = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 0.5, 0.5, 0.5);
  57. lightShape->setColor(1.0,1.0,0.0, 0.75);
  58. lightShape->renderWireframe = true;
  59. addChild(lightShape);
  60. }
  61. lightShape->castShadows = false;
  62. lightShape->visible = false;
  63. */
  64. lightShape = NULL;
  65. lightImportance = 0;
  66. }
  67. void SceneLight::setLightImportance(int newImportance) {
  68. lightImportance = newImportance;
  69. }
  70. int SceneLight::getLightImportance() const {
  71. return lightImportance;
  72. }
  73. void SceneLight::enableDebugDraw(bool val) {
  74. if(lightShape) {
  75. lightShape->visible = val;
  76. }
  77. }
  78. void SceneLight::enableShadows(bool val, Number resolution) {
  79. if(val) {
  80. if(!zBufferTexture) {
  81. CoreServices::getInstance()->getRenderer()->createRenderTextures(NULL, &zBufferTexture, resolution, resolution, false);
  82. }
  83. if(!spotCamera) {
  84. spotCamera = new Camera(parentScene);
  85. // spotCamera->setPitch(-45.0f);
  86. addChild(spotCamera);
  87. }
  88. shadowMapRes = resolution;
  89. shadowsEnabled = true;
  90. } else {
  91. shadowsEnabled = false;
  92. }
  93. }
  94. bool SceneLight::areShadowsEnabled() const {
  95. return shadowsEnabled;
  96. }
  97. void SceneLight::setAttenuation(Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation) {
  98. this->constantAttenuation = constantAttenuation;
  99. this->linearAttenuation = linearAttenuation;
  100. this->quadraticAttenuation = quadraticAttenuation;
  101. }
  102. void SceneLight::setIntensity(Number newIntensity) {
  103. intensity = newIntensity;
  104. }
  105. void SceneLight::setShadowMapFOV(Number fov) {
  106. shadowMapFOV = fov;
  107. }
  108. SceneLight::~SceneLight() {
  109. printf("Destroying scene light...\n");
  110. }
  111. void SceneLight::renderDepthMap(Scene *scene) {
  112. CoreServices::getInstance()->getRenderer()->clearScreen();
  113. CoreServices::getInstance()->getRenderer()->pushMatrix();
  114. CoreServices::getInstance()->getRenderer()->loadIdentity();
  115. CoreServices::getInstance()->getRenderer()->setViewportSizeAndFOV(shadowMapRes, shadowMapRes, shadowMapFOV);
  116. CoreServices::getInstance()->getRenderer()->bindFrameBufferTexture(zBufferTexture);
  117. scene->RenderDepthOnly(spotCamera);
  118. lightViewMatrix = CoreServices::getInstance()->getRenderer()->getModelviewMatrix() * CoreServices::getInstance()->getRenderer()->getProjectionMatrix();
  119. CoreServices::getInstance()->getRenderer()->unbindFramebuffers();
  120. CoreServices::getInstance()->getRenderer()->popMatrix();
  121. CoreServices::getInstance()->getRenderer()->setViewportSizeAndFOV(CoreServices::getInstance()->getCore()->getXRes(), CoreServices::getInstance()->getCore()->getYRes(), 45.0f);
  122. }
  123. const Matrix4& SceneLight::getLightViewMatrix() const {
  124. return lightViewMatrix;
  125. }
  126. Texture *SceneLight::getZBufferTexture() const {
  127. return zBufferTexture;
  128. }
  129. Number SceneLight::getIntensity() const {
  130. return intensity;
  131. }
  132. void SceneLight::Render() {
  133. /*
  134. CoreServices::getInstance()->getRenderer()->setTexture(NULL);
  135. CoreServices::getInstance()->getRenderer()->beginRenderOperation(lightMesh->getMeshType());
  136. for(int i=0; i < lightMesh->getPolygonCount(); i++) {
  137. CoreServices::getInstance()->getRenderer()->draw3DPolygon(lightMesh->getPolygon(i));
  138. }
  139. CoreServices::getInstance()->getRenderer()->endRenderOperation();
  140. */
  141. }
  142. int SceneLight::getType() const {
  143. return type;
  144. }