Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / BspDemo / BspConverter.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "BspConverter.h"
17 #include "BspLoader.h"
18 #include "LinearMath/btVector3.h"
19 #include "LinearMath/btGeometryUtil.h"
20 #include <stdio.h>
21 #include <string.h>
22
23
24 void BspConverter::convertBsp(BspLoader& bspLoader,float scaling)
25 {
26         {
27                 
28                 float   playstartf[3] = {0,0,100};
29
30                 if (bspLoader.findVectorByName(&playstartf[0],"info_player_start"))
31                 {
32                         printf("found playerstart\n");
33                 } 
34                 else
35                 {
36                         if (bspLoader.findVectorByName(&playstartf[0],"info_player_deathmatch"))
37                         {
38                                 printf("found deatchmatch start\n");
39                         }
40                 }
41
42                 btVector3 playerStart (playstartf[0],playstartf[1],playstartf[2]);
43
44
45                 playerStart[2] += 20.f; //start a bit higher
46
47                 playerStart *= scaling;
48
49                 
50
51                 //progressBegin("Loading bsp");
52
53                 for (int i=0;i<bspLoader.m_numleafs;i++)
54                 {
55                         printf("Reading bspLeaf %i from total %i (%f procent)\n",i, bspLoader.m_numleafs,(100.f*(float)i/float(bspLoader.m_numleafs)) );
56                         
57                         bool isValidBrush = false;
58                         
59                         BSPLeaf&        leaf = bspLoader.m_dleafs[i];
60         
61                         for (int b=0;b<leaf.numLeafBrushes;b++)
62                         {
63                                 btAlignedObjectArray<btVector3> planeEquations;
64                                 
65                                 int brushid = bspLoader.m_dleafbrushes[leaf.firstLeafBrush+b];
66
67                                 BSPBrush& brush = bspLoader.m_dbrushes[brushid];
68                                 if (brush.shaderNum!=-1)
69                                 {
70                                         if (bspLoader.m_dshaders[ brush.shaderNum ].contentFlags & BSPCONTENTS_SOLID)
71                                         {
72                                                 brush.shaderNum = -1;
73
74                                                 for (int p=0;p<brush.numSides;p++)
75                                                 {
76                                                         int sideid = brush.firstSide+p;
77                                                         BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
78                                                         int planeid = brushside.planeNum;
79                                                         BSPPlane& plane = bspLoader.m_dplanes[planeid];
80                                                         btVector3 planeEq;
81                                                         planeEq.setValue(
82                                                                 plane.normal[0],
83                                                                 plane.normal[1],
84                                                                 plane.normal[2]);
85                                                         planeEq[3] = scaling*-plane.dist;
86
87                                                         planeEquations.push_back(planeEq);
88                                                         isValidBrush=true;
89                                                 }
90                                                 if (isValidBrush)
91                                                 {
92
93                                                         btAlignedObjectArray<btVector3> vertices;
94                                                         btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices);
95                                                 
96                                                         bool isEntity = false;
97                                                         btVector3 entityTarget(0.f,0.f,0.f);
98                                                         addConvexVerticesCollider(vertices,isEntity,entityTarget);
99                                                 
100                                                 }
101                                         }
102                                 } 
103                         }
104                 }
105
106 #define USE_ENTITIES
107 #ifdef USE_ENTITIES
108
109                 
110                 {
111                         int i;
112                         for (i=0;i<bspLoader.m_num_entities;i++)
113                                 {
114                                         const BSPEntity& entity = bspLoader.m_entities[i];
115                                         const char* cl = bspLoader.getValueForKey(&entity,"classname");
116                                         if ( !strcmp( cl, "trigger_push" ) ) {
117                                                 btVector3 targetLocation(0.f,0.f,0.f);
118
119                                                 cl = bspLoader.getValueForKey(&entity,"target");
120                                                 if ( strcmp( cl, "" ) ) {
121                                                         //its not empty so ...
122
123                                                         /*
124                                                         //lookup the target position for the jumppad:
125                                                         const BSPEntity* targetentity = bspLoader.getEntityByValue( "targetname" , cl );
126                                                         if (targetentity)
127                                                         {
128                                                                 if (bspLoader.getVectorForKey( targetentity , "origin",&targetLocation[0]))
129                                                                 {
130                                                                                                                                         
131                                                                 }
132                                                         }
133                                                         */
134
135
136                                                         cl = bspLoader.getValueForKey(&entity,"model");
137                                                         if ( strcmp( cl, "" ) ) {
138                                                                 // add the model as a brush
139                                                                 if (cl[0] == '*')
140                                                                 {
141                                                                         int modelnr = atoi(&cl[1]);
142                                                                         if ((modelnr >=0) && (modelnr < bspLoader.m_nummodels))
143                                                                         {
144                                                                                 const BSPModel& model = bspLoader.m_dmodels[modelnr];
145                                                                                 for (int n=0;n<model.numBrushes;n++)
146                                                                                 {
147                                                                                         btAlignedObjectArray<btVector3> planeEquations;
148                                                                                         bool    isValidBrush = false;
149
150                                                                                         //convert brush
151                                                                                         const BSPBrush& brush = bspLoader.m_dbrushes[model.firstBrush+n];
152                                                                                         {
153                                                                                                 for (int p=0;p<brush.numSides;p++)
154                                                                                                 {
155                                                                                                         int sideid = brush.firstSide+p;
156                                                                                                         BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
157                                                                                                         int planeid = brushside.planeNum;
158                                                                                                         BSPPlane& plane = bspLoader.m_dplanes[planeid];
159                                                                                                         btVector3 planeEq;
160                                                                                                         planeEq.setValue(
161                                                                                                                 plane.normal[0],
162                                                                                                                 plane.normal[1],
163                                                                                                                 plane.normal[2]);
164                                                                                                         planeEq[3] = scaling*-plane.dist;
165                                                                                                         planeEquations.push_back(planeEq);
166                                                                                                         isValidBrush=true;
167                                                                                                 }
168                                                                                                 if (isValidBrush)
169                                                                                                 {
170                                                                                                         
171                                                                                                         btAlignedObjectArray<btVector3> vertices;
172                                                                                                         btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices);
173
174                                                                                                         bool isEntity=true;
175                                                                                                         addConvexVerticesCollider(vertices,isEntity,targetLocation);
176                                                                                                         
177                                                                                                 }
178                                                                                         }
179
180                                                                                 }
181                                                                         }
182                                                                 } 
183                                                                 else
184                                                                 {
185                                                                         printf("unsupported trigger_push model, md3 ?\n");
186                                                                 }
187                                                         }
188                                                 
189                                                 }
190                                         }
191                                 }
192                         }
193                                         
194 #endif //USE_ENTITIES
195
196                 
197
198                 //progressEnd();
199                 }
200         
201         }
202
203
204
205
206
207