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

游戏引擎

开发平台:

Visual C++

  1. /* Bsp
  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. #ifndef __BSP_H__
  20. #define __BSP_H__
  21. #include <stdio.h>
  22. #include "mathlib.h"
  23. #define BSP_MAGIC ('B' | ('S' << 8) | ('P' << 16) | ('M' << 24))
  24. class Mesh;
  25. class Material;
  26. class Object;
  27. class ObjectMesh;
  28. /*
  29.  */
  30. class Node {
  31. public:
  32. Node();
  33. ~Node();
  34. void create(Mesh *mesh);
  35. void load(FILE *file);
  36. void save(FILE *file);
  37. void bindMaterial(const char *name,Material *material);
  38. void render();
  39. enum {
  40. TRIANGLES_PER_NODE = 1024,
  41. };
  42. vec3 min; // bound box
  43. vec3 max;
  44. vec3 center; // bound sphere
  45. float radius;
  46. Node *left,*right; // childrens
  47. ObjectMesh *object; // object
  48. };
  49. /*
  50.  */
  51. class Portal {
  52. public:
  53. Portal();
  54. ~Portal();
  55. void create(Mesh *mesh,int s);
  56. void getScissor(int *scissor);
  57. void render();
  58. vec3 center; // bond sphere
  59. float radius;
  60. int num_sectors;
  61. int *sectors;
  62. vec3 points[4];
  63. int frame;
  64. };
  65. /*
  66.  */
  67. class Sector {
  68. public:
  69. Sector();
  70. ~Sector();
  71. void create(Mesh *mesh,int s);
  72. void getNodeObjects(Node *node);
  73. void create();
  74. int inside(const vec3 &point);
  75. int inside(Portal *portal);
  76. int inside(const vec3 &center,float radius);
  77. int inside(Mesh *mesh,int s);
  78. void addObject(Object *object);
  79. void removeObject(Object *object);
  80. void bindMaterial(const char *name,Material *material);
  81. void render(Portal *portal = NULL);
  82. void saveState();
  83. void restoreState(int frame);
  84. enum {
  85. NUM_OBJECTS = 256,
  86. };
  87. vec3 center; // bound sphere
  88. float radius;
  89. int num_planes; // bound
  90. vec4 *planes;
  91. Node *root; // binary tree
  92. int num_portals; // portals
  93. int *portals;
  94. int num_objects; // dynamic objects
  95. Object **objects;
  96. int num_node_objects; // static object from the node
  97. Object **node_objects;
  98. int num_visible_objects; // only visible objects
  99. Object **visible_objects;
  100. Portal *portal; // sector is visible through this portal
  101. int frame;
  102. int old_num_visible_objects; // save/restore state
  103. Object **old_visible_objects;
  104. Portal *old_portal;
  105. int old_frame;
  106. };
  107. /*
  108.  */
  109. class Bsp {
  110. public:
  111. Bsp();
  112. ~Bsp();
  113. void load(const char *name);
  114. void save(const char *name);
  115. void bindMaterial(const char *name,Material *material);
  116. void render();
  117. void saveState();
  118. void restoreState(int frame);
  119. static int num_portals;
  120. static Portal *portals;
  121. static int num_sectors;
  122. static Sector *sectors;
  123. static int num_visible_sectors;
  124. static Sector **visible_sectors;
  125. static int old_num_visible_sectors;
  126. static Sector **old_visible_sectors;
  127. };
  128. #endif /* __BSP_H__ */