00001
00002
00003 #include "pvs.h"
00004 #include "listnode.h"
00005 #include "plane.h"
00006 #include "portal.h"
00007 #include "vertex.h"
00008 #include "vector.h"
00009 #include "camera.h"
00010 #include "locmath.h"
00011 #include "mmgr.h"
00012
00013 extern int numleaves;
00014 extern LinkedList<BSP_node> LeafList;
00015 extern LinkedList<BSP_node> PartitionList;
00016 extern BSP_node* listnode;
00017 extern CAMERA* camera;
00018 extern int currentCamera;
00019 extern PLANE frustum[6];
00020
00021 int CountVisibleLeaves()
00022 {
00023 int counter = 0;
00024 BSP_node* tempnode;
00025 for (int loop = 1; loop <= numleaves; loop++)
00026 {
00027 tempnode = LeafList.Get(loop);
00028 if (tempnode->visible)
00029 {
00030 counter++;
00031 }
00032 }
00033 return counter;
00034 }
00035
00036 void FindVisibleLeaves(int Parent, PORTAL* CurrentPortal, BSP_node* ParentNode, int portalnumber)
00037 {
00038 int loop, numPlanes, Nminus1, counter, flag, result, parentid, planenumber;
00039 VECTOR edge1, edge2, pointOnPlane, planesNormal;
00040 BSP_node* CurrentNode;
00041 PORTAL* front = new PORTAL;
00042 PORTAL* back = new PORTAL;
00043 PORTAL* inputPortal;
00044
00045 inputPortal = ParentNode->portallist.Get(portalnumber);
00046 parentid = inputPortal->backleaf->nodeid;
00047 CurrentNode = LeafList.Get(parentid);
00048 CurrentNode->visible = true;
00049
00050
00051 pointOnPlane = camera[currentCamera].Position;
00052
00053 numPlanes = CurrentPortal->numVertices;
00054 PLANE* Planes = new PLANE[numPlanes];
00055 for (loop = 0; loop < numPlanes; loop++)
00056 {
00057 if (loop == 0)
00058 Nminus1 = numPlanes - 1;
00059 else
00060 Nminus1 = loop - 1;
00061
00062 edge1 = CurrentPortal->Vertex[loop].coords - camera[currentCamera].Position;
00063 edge2 = CurrentPortal->Vertex[Nminus1].coords - camera[currentCamera].Position;
00064 planesNormal = CrossVector(edge1, edge2);
00065 Planes[loop].normal = planesNormal;
00066 }
00067
00068
00069 for (portalnumber = 1; portalnumber <= CurrentNode->numportals; portalnumber++)
00070 {
00071 counter = 0;
00072 CurrentPortal = CurrentNode->portallist.Get(portalnumber);
00073 flag = 1;
00074
00075 if (CurrentPortal->backleaf->nodeid != Parent)
00076 {
00077
00078 for (planenumber = 0; planenumber < numPlanes; planenumber++)
00079 {
00080 result = SplitPortal(CurrentPortal, Planes[planenumber], pointOnPlane, front, back);
00081 if (result == PortalWasSplit)
00082 {
00083 delete[] back->Vertex;
00084 if (flag == 0)
00085 {
00086 delete[] CurrentPortal->Vertex;
00087 delete CurrentPortal;
00088 }
00089 flag = 0;
00090 CurrentPortal = front;
00091 front = new PORTAL;
00092 }
00093 if (result != Back)
00094 counter++;
00095 }
00096
00097 if (counter == numPlanes)
00098 {
00099 FindVisibleLeaves(parentid, CurrentPortal, CurrentNode, portalnumber);
00100 }
00101 if (flag == 0)
00102 {
00103 delete[] CurrentPortal->Vertex;
00104 delete CurrentPortal;
00105 }
00106 }
00107 }
00108 delete[] Planes;
00109 delete front;
00110 delete back;
00111 }
00112
00113 void CalculatePVS(int currentleaf)
00114 {
00115 int flag, result, counter, parentid, portalnumber, planenumber;
00116 VECTOR pointOnPlane;
00117 VECTOR ZUnit = camera[currentCamera].GetZUnit();
00118 BSP_node* CurrentNode;
00119 PORTAL* CurrentPortal;
00120 PORTAL* front = new PORTAL;
00121 PORTAL* back = new PORTAL;
00122
00123 parentid = currentleaf;
00124 CurrentNode = LeafList.Get(currentleaf);
00125 CurrentNode->visible = true;
00126
00127 for (portalnumber = 1; portalnumber <= CurrentNode->numportals; portalnumber++)
00128 {
00129 counter = 0;
00130 CurrentPortal = CurrentNode->portallist.Get(portalnumber);
00131 flag = 1;
00132
00133 for (planenumber = 0; planenumber < 6; planenumber++)
00134 {
00135
00136 if (planenumber == 4)
00137 {
00138 pointOnPlane.x = camera[currentCamera].Position.x;
00139 pointOnPlane.x -= ZUnit.x * 500.0;
00140 pointOnPlane.y = camera[currentCamera].Position.y;
00141 pointOnPlane.y -= ZUnit.y * 500.0;
00142 pointOnPlane.z = camera[currentCamera].Position.z;
00143 pointOnPlane.z -= ZUnit.z * 500.0;
00144 }
00145 else if (planenumber == 5)
00146 continue;
00147 else
00148
00149 pointOnPlane = camera[currentCamera].Position;
00150
00151 result = SplitPortal(CurrentPortal, frustum[planenumber], pointOnPlane, front, back);
00152 if (result == PortalWasSplit)
00153 {
00154 delete[] back->Vertex;
00155 if (flag == 0)
00156 {
00157 delete[] CurrentPortal->Vertex;
00158 delete CurrentPortal;
00159 }
00160 flag = 0;
00161 CurrentPortal = front;
00162 front = new PORTAL;
00163 }
00164 if (result != Back)
00165 counter++;
00166 }
00167
00168 if (counter == 5)
00169 {
00170 FindVisibleLeaves(parentid, CurrentPortal, CurrentNode, portalnumber);
00171 }
00172 if (flag == 0)
00173 {
00174 delete[] CurrentPortal->Vertex;
00175 delete CurrentPortal;
00176 }
00177 }
00178 delete front;
00179 delete back;
00180 }