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

游戏引擎

开发平台:

Visual C++

  1. /* Object
  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 "frustum.h"
  21. #include "bsp.h"
  22. #include "position.h"
  23. #include "shader.h"
  24. #include "material.h"
  25. #include "rigidbody.h"
  26. #include "object.h"
  27. Object::Object(int type) : type(type), rigidbody(NULL), is_identity(1),
  28. num_opacities(0), opacities(NULL), num_transparents(0), transparents(NULL),
  29. shadows(1), time(0), frame(0) {
  30. }
  31. Object::~Object() {
  32. for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].removeObject(this);
  33. if(rigidbody) delete rigidbody;
  34. }
  35. /*
  36.  */
  37. void Object::update(float ifps) {
  38. time += ifps;
  39. if(rigidbody) {
  40. if(Engine::physic_toggle) rigidbody->simulate();
  41. } else {
  42. pos.update(time,this);
  43. }
  44. }
  45. /*
  46.  */
  47. void Object::updatePos(const vec3 &p) {
  48. for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].removeObject(this);
  49. pos.radius = getRadius();
  50. pos = p;
  51. for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].addObject(this);
  52. }
  53. /*
  54.  */
  55. int Object::bindMaterial(const char *name,Material *material) {
  56. int bind = 0;
  57. for(int i = 0; i < getNumSurfaces(); i++) {
  58. if(Engine::match(name,getSurfaceName(i))) {
  59. int j;
  60. materials[i] = material;
  61. if(materials[i]->blend) {
  62. for(j = 0; j < num_opacities; j++) if(opacities[j] == i) break;
  63. if(j != num_opacities) {
  64. for(; j < num_opacities - 1; j++) opacities[j] = opacities[j + 1];
  65. num_opacities--;
  66. }
  67. for(j = 0; j < num_transparents; j++) if(transparents[j] == i) break;
  68. if(j == num_transparents) transparents[num_transparents++] = i;
  69. else transparents[j] = i;
  70. } else {
  71. for(j = 0; j < num_transparents; j++) if(transparents[j] == i) break;
  72. if(j != num_transparents) {
  73. for(; j < num_transparents - 1; j++) transparents[j] = transparents[j + 1];
  74. num_transparents--;
  75. }
  76. for(j = 0; j < num_opacities; j++) if(opacities[j] == i) break;
  77. if(j == num_opacities) opacities[num_opacities++] = i;
  78. else opacities[j] = i;
  79. }
  80. bind = 1;
  81. }
  82. }
  83. return bind;
  84. }
  85. /*
  86.  */
  87. void Object::setRigidBody(RigidBody *rigidbody) {
  88. is_identity = 0;
  89. this->rigidbody = rigidbody;
  90. }
  91. /*
  92.  */
  93. void Object::setShadows(int shadows) {
  94. this->shadows = shadows;
  95. }
  96. /*
  97.  */
  98. void Object::set(const vec3 &p) {
  99. is_identity = 0;
  100. transform.translate(p);
  101. itransform = transform.inverse();
  102. updatePos(p);
  103. if(rigidbody) rigidbody->set(p);
  104. }
  105. void Object::set(const mat4 &m) {
  106. is_identity = 0;
  107. transform = m;
  108. itransform = transform.inverse();
  109. updatePos(m * vec3(0,0,0));
  110. if(rigidbody) rigidbody->set(m);
  111. }
  112. /*
  113.  */
  114. void Object::enable() {
  115. if(is_identity) return;
  116. old_modelview = Engine::modelview;
  117. old_imodelview = Engine::imodelview;
  118. old_transform = Engine::transform;
  119. old_itransform = Engine::itransform;
  120. Engine::modelview = Engine::modelview * transform;
  121. Engine::imodelview = itransform * Engine::imodelview;
  122. Engine::transform = Engine::transform * transform;
  123. Engine::itransform = itransform * Engine::itransform;
  124. glLoadMatrixf(Engine::modelview);
  125. // new transformation
  126. if(Shader::old_shader) Shader::old_shader->bind();
  127. }
  128. void Object::disable() {
  129. if(is_identity) return;
  130. Engine::modelview = old_modelview;
  131. Engine::imodelview = old_imodelview;
  132. Engine::transform = old_transform;
  133. Engine::itransform = old_itransform;
  134. glLoadMatrixf(Engine::modelview);
  135. if(Shader::old_shader) Shader::old_shader->bind();
  136. }