forked from NikitaNikson/X-Ray_Renewal_Engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xrCDB.h
286 lines (251 loc) · 6.62 KB
/
xrCDB.h
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
275
276
277
278
279
280
281
282
283
284
285
286
#pragma once
#ifdef XRCDB_STATIC
# define XRCDB_API
#elif defined XRCDB_EXPORTS
# define XRCDB_API __declspec(dllexport)
#else
# define XRCDB_API __declspec(dllimport)
#endif
// forward declarations
class CFrustum;
namespace Opcode {
class OPCODE_Model;
class AABBNoLeafNode;
};
#pragma pack(push, 8)
namespace CDB {
#ifdef _M_X64
#pragma pack(push, 1)
// Triangle for x86
class XRCDB_API TRI_DEPRECATED //*** 16 bytes total (was 32 :)
{
public:
u32 verts[3]; // 3*4 = 12b
union {
u32 dummy; // 4b
struct {
u32 material : 14;
u32 suppress_shadows : 1;
u32 suppress_wm : 1;
u32 sector : 16;
};
};
public:
IC u32 IDvert(u32 ID) { return verts[ID]; }
};
#pragma pack (pop)
#endif
// Triangle
class XRCDB_API TRI //*** 24 bytes total
{
public:
u32 verts[3]; // 3*4 = 12b
union
{
size_t dummy; // 4b
struct
{
size_t material : 14;
size_t suppress_shadows : 1;
size_t suppress_wm : 1;
size_t sector : 16;
#ifdef _M_X64
size_t dumb : 32;
#endif
};
#ifdef _M_X64
struct
{
u32 dummy_low;
u32 dummy_high;
};
#endif
};
#ifdef _M_X64
TRI(TRI_DEPRECATED& oldTri)
{
verts[0] = oldTri.verts[0];
verts[1] = oldTri.verts[1];
verts[2] = oldTri.verts[2];
dummy = oldTri.dummy;
dumb = 0;
}
TRI()
{
verts[0] = 0;
verts[1] = 0;
verts[2] = 0;
dummy = 0;
}
TRI& operator= (const TRI_DEPRECATED& oldTri)
{
verts[0] = oldTri.verts[0];
verts[1] = oldTri.verts[1];
verts[2] = oldTri.verts[2];
dummy = oldTri.dummy;
dumb = 0;
return *this;
}
#endif
public:
IC u32 IDvert(u32 ID) { return verts[ID]; }
};
// Build callback
typedef void __stdcall build_callback(Fvector* V, int Vcnt, TRI* T, int Tcnt, void* params);
// Model definition
class XRCDB_API MODEL
{
friend class COLLIDER;
enum
{
S_READY = 0,
S_INIT = 1,
S_BUILD = 2,
S_forcedword = u32(-1)
};
private:
xrCriticalSection cs;
Opcode::OPCODE_Model* tree;
u32 status; // 0=ready, 1=init, 2=building
// tris
TRI* tris;
int tris_count;
Fvector* verts;
int verts_count;
public:
MODEL();
~MODEL();
IC Fvector* get_verts () { return verts; }
IC int get_verts_count () const { return verts_count;}
IC TRI* get_tris () { return tris; }
IC int get_tris_count () const { return tris_count;}
IC void syncronize () const
{
if (S_READY!=status)
{
Log ("! WARNING: syncronized CDB::query");
xrCriticalSection* C = (xrCriticalSection*) &cs;
C->Enter ();
C->Leave ();
}
}
void build_internal(Fvector* V, int Vcnt, TRI* T, int Tcnt, build_callback* bc = nullptr, void* bcp = nullptr, const bool rebuildTrisRequired = true);
void build(Fvector* V, int Vcnt, TRI* T, int Tcnt, build_callback* bc = nullptr, void* bcp = nullptr, const bool rebuildTrisRequired = true);
u32 memory();
};
// Collider result
struct XRCDB_API RESULT
{
Fvector verts[3];
union {
size_t dummy; // 4b
struct {
size_t material : 14;
size_t suppress_shadows : 1;
size_t suppress_wm : 1;
size_t sector : 16;
#ifdef _M_X64
u64 stub : 32;
#endif
};
#ifdef _M_X64
struct
{
u32 dummy_h;
u32 dummy_l;
};
#endif
};
int id;
float range;
float u,v;
};
// Collider Options
enum {
OPT_CULL = (1<<0),
OPT_ONLYFIRST = (1<<1),
OPT_ONLYNEAREST = (1<<2),
OPT_FULL_TEST = (1<<3) // for box & frustum queries - enable class III test(s)
};
// Collider itself
class XRCDB_API COLLIDER
{
// Ray data and methods
u32 ray_mode;
u32 box_mode;
u32 frustum_mode;
// Result management
xr_vector<RESULT> rd;
public:
COLLIDER ();
~COLLIDER ();
ICF void ray_options (u32 f) { ray_mode = f; }
void ray_query (const MODEL *m_def, const Fvector& r_start, const Fvector& r_dir, float r_range = 10000.f);
ICF void box_options (u32 f) { box_mode = f; }
void box_query (const MODEL *m_def, const Fvector& b_center, const Fvector& b_dim);
ICF void frustum_options (u32 f) { frustum_mode = f; }
void frustum_query (const MODEL *m_def, const CFrustum& F);
ICF RESULT* r_begin() { return std::data(rd); }
ICF RESULT* r_end() { return std::data(rd) + std::size(rd); }
RESULT& r_add () ;
void r_free () ;
ICF size_t r_count () { return rd.size(); };
ICF void r_clear () { rd.clear_not_free(); };
ICF void r_clear_compact () { rd.clear_and_free(); };
};
//
class XRCDB_API Collector
{
xr_vector<Fvector> verts;
xr_vector<TRI> faces;
u32 VPack ( const Fvector& V, float eps);
public:
void add_face ( const Fvector& v0, const Fvector& v1, const Fvector& v2, u16 material, u16 sector );
void add_face_D ( const Fvector& v0, const Fvector& v1, const Fvector& v2, size_t dummy );
void add_face_packed_D ( const Fvector& v0, const Fvector& v1, const Fvector& v2, size_t dummy, float eps = EPS );
void add_face_packed ( const Fvector& v0, const Fvector& v1, const Fvector& v2, u16 material, u16 sector, float eps = EPS );
void remove_duplicate_T ( );
void calc_adjacency ( xr_vector<u32>& dest );
Fvector* getV () { return &*verts.begin(); }
size_t getVS () { return verts.size(); }
TRI* getT () { return &*faces.begin(); }
size_t getTS () { return faces.size(); }
void clear () { verts.clear(); faces.clear(); }
};
class Noncopyable
{
public:
Noncopyable() = default;
Noncopyable(const Noncopyable&) = delete;
Noncopyable &operator=(const Noncopyable&) = delete;
};
#pragma warning(push)
#pragma warning(disable:4275)
const u32 clpMX = 24, clpMY=16, clpMZ=24;
class XRCDB_API CollectorPacked : private Noncopyable {
typedef xr_vector<u32> DWORDList;
typedef DWORDList::iterator DWORDIt;
xr_vector<Fvector> verts;
xr_vector<TRI> faces;
Fvector VMmin, VMscale;
DWORDList VM [clpMX+1][clpMY+1][clpMZ+1];
Fvector VMeps;
u32 VPack ( const Fvector& V);
public:
CollectorPacked (const Fbox &bb, int apx_vertices=5000, int apx_faces=5000);
// __declspec(noinline) CollectorPacked &operator= (const CollectorPacked &object)
// {
// verts
// }
void add_face ( const Fvector& v0, const Fvector& v1, const Fvector& v2, u16 material, u16 sector );
void add_face_D ( const Fvector& v0, const Fvector& v1, const Fvector& v2, size_t dummy );
xr_vector<Fvector>& getV_Vec() { return verts; }
Fvector* getV() { return &*verts.begin(); }
size_t getVS() { return verts.size(); }
TRI* getT() { return &*faces.begin(); }
size_t getTS() { return faces.size(); }
void clear();
};
#pragma warning(pop)
};
#pragma pack(pop)