summaryrefslogtreecommitdiff
path: root/Demos/BspDemo/BspConverter.cpp
blob: 2c9475a562dc860cf50e73134d55d40be4fae9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include "BspConverter.h"
#include "BspLoader.h"
#include "CcdPhysicsEnvironment.h"
#include "LinearMath/btVector3.h"

void BspConverter::convertBsp(BspLoader& bspLoader,float scaling)
{
	{
		btVector3 playerStart (0.f, 0.f, 100.f);

		if (bspLoader.findVectorByName(&playerStart[0],"info_player_start"))
		{
			printf("found playerstart\n");
		} 
		else
		{
			if (bspLoader.findVectorByName(&playerStart[0],"info_player_deathmatch"))
			{
				printf("found deatchmatch start\n");
			}
		}

		playerStart[2] += 20.f; //start a bit higher

		playerStart *= scaling;

		

		//progressBegin("Loading bsp");

		for (int i=0;i<bspLoader.m_numleafs;i++)
		{
			printf("Reading bspLeaf %i from total %i (%f procent)\n",i, bspLoader.m_numleafs,(100.f*(float)i/float(bspLoader.m_numleafs)) );
			
			bool isValidBrush = false;
			
			BSPLeaf&	leaf = bspLoader.m_dleafs[i];
	
			for (int b=0;b<leaf.numLeafBrushes;b++)
			{
				std::vector<btVector3> planeEquations;
				
				int brushid = bspLoader.m_dleafbrushes[leaf.firstLeafBrush+b];

				BSPBrush& brush = bspLoader.m_dbrushes[brushid];
				if (brush.shaderNum!=-1)
				{
					if (bspLoader.m_dshaders[ brush.shaderNum ].contentFlags & BSPCONTENTS_SOLID)
					{
						brush.shaderNum = -1;

						for (int p=0;p<brush.numSides;p++)
						{
							int sideid = brush.firstSide+p;
							BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
							int planeid = brushside.planeNum;
							BSPPlane& plane = bspLoader.m_dplanes[planeid];
							btVector3 planeEq;
							planeEq.setValue(
								plane.normal[0],
								plane.normal[1],
								plane.normal[2],
								scaling*-plane.dist);

							planeEquations.push_back(planeEq);
							isValidBrush=true;
						}
						if (isValidBrush)
						{

							std::vector<btVector3>	vertices;
							
							getVerticesFromPlaneEquations(planeEquations,vertices);
							printf("getVerticesFromPlaneEquations returned %i\n",(int)vertices.size());

							bool isEntity = false;
							btVector3 entityTarget(0.f,0.f,0.f);
							AddConvexVerticesCollider(vertices,isEntity,entityTarget);
						
						}
					}
				} 
			}
		}

#define USE_ENTITIES
#ifdef USE_ENTITIES

		
		{
			int i;
			for (i=0;i<bspLoader.m_num_entities;i++)
				{
					const BSPEntity& entity = bspLoader.m_entities[i];
					const char* cl = bspLoader.ValueForKey(&entity,"classname");
					if ( !strcmp( cl, "trigger_push" ) ) {
						btVector3 targetLocation(0.f,0.f,0.f);

						cl = bspLoader.ValueForKey(&entity,"target");
						if ( strcmp( cl, "" ) ) {
							//its not empty so ...

							//lookup the target position for the jumppad:
							const BSPEntity* targetentity = bspLoader.getEntityByValue( "targetname" , cl );
							if (targetentity)
							{
								if (bspLoader.GetVectorForKey( targetentity , "origin",&targetLocation[0]))
								{
																	
								}
							}

							cl = bspLoader.ValueForKey(&entity,"model");
							if ( strcmp( cl, "" ) ) {
								// add the model as a brush
								if (cl[0] == '*')
								{
									int modelnr = atoi(&cl[1]);
									if ((modelnr >=0) && (modelnr < bspLoader.m_nummodels))
									{
										const BSPModel& model = bspLoader.m_dmodels[modelnr];
										for (int n=0;n<model.numBrushes;n++)
										{
											std::vector<btVector3> planeEquations;
											bool	isValidBrush = false;

											//convert brush
											const BSPBrush& brush = bspLoader.m_dbrushes[model.firstBrush+n];
											{
												for (int p=0;p<brush.numSides;p++)
												{
													int sideid = brush.firstSide+p;
													BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
													int planeid = brushside.planeNum;
													BSPPlane& plane = bspLoader.m_dplanes[planeid];
													btVector3 planeEq;
													planeEq.setValue(
														plane.normal[0],
														plane.normal[1],
														plane.normal[2],
														scaling*-plane.dist);
													planeEquations.push_back(planeEq);
													isValidBrush=true;
												}
												if (isValidBrush)
												{
													
													std::vector<btVector3>	vertices;
													getVerticesFromPlaneEquations(planeEquations,vertices);

													bool isEntity=true;
													AddConvexVerticesCollider(vertices,isEntity,targetLocation);
													
												}
											}

										}
									}
								} 
								else
								{
									printf("unsupported trigger_push model, md3 ?\n");
								}
							}
						
						}
					}
				}
			}
					
#endif //USE_ENTITIES

		

		//progressEnd();
		}
	
	}






void	BspConverter::getVerticesFromPlaneEquations(const std::vector<btVector3>& planeEquations , std::vector<btVector3>& verticesOut )
{
	const int numbrushes = planeEquations.size();
	// brute force:
	for (int i=0;i<numbrushes;i++)
	{
		const btVector3& N1 = planeEquations[i];
		

		for (int j=i+1;j<numbrushes;j++)
		{
			const btVector3& N2 = planeEquations[j];
				
			for (int k=j+1;k<numbrushes;k++)
			{

				const btVector3& N3 = planeEquations[k];

				btVector3 n2n3; n2n3 = N2.cross(N3);
				btVector3 n3n1; n3n1 = N3.cross(N1);
				btVector3 n1n2; n1n2 = N1.cross(N2);
				
				if ( ( n2n3.length2() > 0.0001f ) &&
					 ( n3n1.length2() > 0.0001f ) &&
					 ( n1n2.length2() > 0.0001f ) )
				{
					//point P out of 3 plane equations:

					//	d1 ( N2 * N3 ) + d2 ( N3 * N1 ) + d3 ( N1 * N2 )  
					//P =  -------------------------------------------------------------------------  
					//   N1 . ( N2 * N3 )  


					float quotient = (N1.dot(n2n3));
					if (btFabs(quotient) > 0.000001f)
					{
						quotient = -1.f / quotient;
						n2n3 *= N1[3];
						n3n1 *= N2[3];
						n1n2 *= N3[3];
						btVector3 potentialVertex = n2n3;
						potentialVertex += n3n1;
						potentialVertex += n1n2;
						potentialVertex *= quotient;

						//check if inside, and replace supportingVertexOut if needed
						if (isInside(planeEquations,potentialVertex,0.1f))
						{
							verticesOut.push_back(potentialVertex);
						}
					}
				}
			}
		}
	}
}




bool	BspConverter::isInside(const std::vector<btVector3>& planeEquations, const btVector3& point, float	margin)
{
	int numbrushes = planeEquations.size();
	for (int i=0;i<numbrushes;i++)
	{
		const btVector3& N1 = planeEquations[i];
		float dist = float(N1.dot(point))+float(N1[3])-margin;
		if (dist>0.f)
		{
			return false;
		}
	}
	return true;
		
}