light.cpp
上传用户:qccn516
上传日期:2013-05-02
资源大小:3382k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /* Light
  2.  *
  3.  * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include "engine.h"
  20. #include "flare.h"
  21. #include "light.h"
  22. Light::Light(const vec3 &pos,float radius,const vec4 &color,int shadows) : radius(radius), color(color), shadows(shadows), material(NULL), flare(NULL), time(0.0) {
  23. set(pos);
  24. }
  25. Light::~Light() {
  26. if(flare) delete flare;
  27. }
  28. /*
  29.  */
  30. void Light::update(float ifps) {
  31. time += ifps;
  32. pos.update(time,transform);
  33. }
  34. /*
  35.  */
  36. int Light::bindMaterial(const char *name,Material *material) {
  37. this->material = material;
  38. return 1;
  39. }
  40. /*
  41.  */
  42. void Light::setFlare(Flare *flare) {
  43. this->flare = flare;
  44. }
  45. /*
  46.  */
  47. void Light::renderFlare() {
  48. if(flare) flare->render(pos,color);
  49. }
  50. /*
  51.  */
  52. void Light::set(const vec3 &p) {
  53. transform.translate(p);
  54. pos.radius = radius;
  55. pos = p;
  56. }
  57. /*
  58.  */
  59. void Light::set(const mat4 &m) {
  60. transform = m;
  61. pos.radius = radius;
  62. pos = m * vec3(0,0,0);
  63. }
  64. /*
  65.  */
  66. void Light::setColor(const vec4 &c) {
  67. color = c;
  68. }
  69. /*
  70.  */
  71. void Light::getScissor(int *scissor) {
  72. if((pos - Engine::camera).length() < radius * 1.5) {
  73. scissor[0] = Engine::viewport[0];
  74. scissor[1] = Engine::viewport[1];
  75. scissor[2] = Engine::viewport[2];
  76. scissor[3] = Engine::viewport[3];
  77. return;
  78. }
  79. mat4 tmodelview = Engine::modelview.transpose();
  80. mat4 mvp = Engine::projection * Engine::modelview;
  81. vec3 x = tmodelview * vec3(radius,0,0);
  82. vec3 y = tmodelview * vec3(0,radius,0);
  83. vec4 p[4];
  84. p[0] = mvp * vec4(pos - x,1);
  85. p[1] = mvp * vec4(pos + x,1);
  86. p[2] = mvp * vec4(pos - y,1);
  87. p[3] = mvp * vec4(pos + y,1);
  88. p[0] /= p[0].w;
  89. p[1] /= p[1].w;
  90. p[2] /= p[2].w;
  91. p[3] /= p[3].w;
  92. if(p[0].x < p[2].x) {
  93. scissor[0] = Engine::viewport[0] + (int)((float)Engine::viewport[2] * (p[0].x + 1.0) / 2.0);
  94. scissor[2] = Engine::viewport[0] + (int)((float)Engine::viewport[2] * (p[1].x + 1.0) / 2.0);
  95. } else {
  96. scissor[0] = Engine::viewport[0] + (int)((float)Engine::viewport[2] * (p[1].x + 1.0) / 2.0);
  97. scissor[2] = Engine::viewport[0] + (int)((float)Engine::viewport[2] * (p[0].x + 1.0) / 2.0);
  98. }
  99. if(p[1].y < p[3].y) {
  100. scissor[1] = Engine::viewport[1] + (int)((float)Engine::viewport[3] * (p[2].y + 1.0) / 2.0);
  101. scissor[3] = Engine::viewport[1] + (int)((float)Engine::viewport[3] * (p[3].y + 1.0) / 2.0);
  102. } else {
  103. scissor[1] = Engine::viewport[1] + (int)((float)Engine::viewport[3] * (p[3].y + 1.0) / 2.0);
  104. scissor[3] = Engine::viewport[1] + (int)((float)Engine::viewport[3] * (p[2].y + 1.0) / 2.0);
  105. }
  106. if(scissor[0] < Engine::viewport[0]) scissor[0] = Engine::viewport[0];
  107. else if(scissor[0] > Engine::viewport[0] + Engine::viewport[2]) scissor[0] = Engine::viewport[0] + Engine::viewport[2];
  108. if(scissor[1] < Engine::viewport[1]) scissor[1] = Engine::viewport[1];
  109. else if(scissor[1] > Engine::viewport[1] + Engine::viewport[3]) scissor[1] = Engine::viewport[1] + Engine::viewport[3];
  110. if(scissor[2] < Engine::viewport[0]) scissor[2] = Engine::viewport[0];
  111. else if(scissor[2] > Engine::viewport[2] + Engine::viewport[3]) scissor[2] = Engine::viewport[0] + Engine::viewport[2];
  112. if(scissor[3] < Engine::viewport[1]) scissor[3] = Engine::viewport[1];
  113. else if(scissor[3] > Engine::viewport[1] + Engine::viewport[3]) scissor[3] = Engine::viewport[1] + Engine::viewport[3];
  114. scissor[2] -= scissor[0];
  115. scissor[3] -= scissor[1];
  116. }