Line data Source code
1 : #ifndef OCTAWORLD_H_
2 : #define OCTAWORLD_H_
3 :
4 : #define EDGE_GET(edge, coord) ((coord) ? (edge)>>4 : (edge)&0xF)
5 : #define EDGE_SET(edge, coord, val) ((edge) = ((coord) ? ((edge)&0xF)|((val)<<4) : ((edge)&0xF0)|(val)))
6 :
7 : #define CUBE_EDGE(c, d, x, y) ((c).edges[(((d)<<2)+((y)<<1)+(x))])
8 :
9 : extern int oppositeorient(int orient);
10 :
11 : enum BlendMapLayers
12 : {
13 : BlendLayer_Top = (1<<5),
14 : BlendLayer_Bottom = (1<<6),
15 : BlendLayer_Blend = BlendLayer_Top|BlendLayer_Bottom,
16 : };
17 :
18 : struct prefab;
19 :
20 : struct vertinfo final
21 : {
22 : ushort x, y, z, norm;
23 :
24 0 : void setxyz(ushort a, ushort b, ushort c)
25 : {
26 0 : x = a;
27 0 : y = b;
28 0 : z = c;
29 0 : }
30 :
31 0 : void setxyz(const ivec &v)
32 : {
33 0 : setxyz(v.x, v.y, v.z);
34 0 : }
35 :
36 0 : void set(ushort a, ushort b, ushort c, ushort n = 0)
37 : {
38 0 : setxyz(a, b, c);
39 0 : norm = n;
40 0 : }
41 :
42 0 : void set(const ivec &v, ushort n = 0)
43 : {
44 0 : set(v.x, v.y, v.z, n);
45 0 : }
46 :
47 0 : ivec getxyz() const
48 : {
49 0 : return ivec(x, y, z);
50 : }
51 : };
52 :
53 : struct octaentities final
54 : {
55 : std::vector<int> mapmodels, //set of indices refering to a position in the getentities() vector corresponding to a mapmodel
56 : decals, //set of indices refering to a position in the getentities() vector corresponding to a decal
57 : other; //set of indices refering to a position in the getentities() vector corresponding to a non-mapmodel non-decal entity (sound etc.)
58 : occludequery *query;
59 : octaentities *next, *rnext;
60 : int distance;
61 : ivec o;
62 : int size;
63 : ivec bbmin, bbmax;
64 :
65 0 : octaentities(const ivec &o, int size) : query(0), o(o), size(size), bbmin(o), bbmax(o)
66 : {
67 0 : bbmin.add(size);
68 0 : }
69 : };
70 :
71 : enum CubeMerges
72 : {
73 : Merge_Origin = 1<<0,
74 : Merge_Part = 1<<1,
75 : Merge_Use = 1<<2
76 : };
77 :
78 : class cube;
79 :
80 : struct clipplanes final
81 : {
82 : vec o, r;
83 : std::array<vec, 8> v;
84 : std::array<plane, 12> p;
85 : std::array<uchar, 12> side;
86 : uchar size, //should always be between 0..11 (a valid index to p/side arrays above)
87 : visible;
88 : const cube *owner;
89 : int version;
90 :
91 0 : void clear()
92 : {
93 0 : o = vec(0,0,0);
94 0 : r = vec(0,0,0);
95 0 : for(int i = 0; i < 8; ++i)
96 : {
97 0 : v[i] = vec(0,0,0);
98 : }
99 0 : for(int i = 0; i < 12; ++i)
100 : {
101 0 : p[i] = plane();
102 0 : side[i] = 0;
103 : }
104 0 : size = 0;
105 0 : visible = 0;
106 0 : owner = nullptr;
107 0 : version = 0;
108 0 : }
109 : };
110 :
111 : struct surfaceinfo final
112 : {
113 : uchar verts, numverts;
114 :
115 0 : surfaceinfo() : verts(0), numverts(BlendLayer_Top)
116 : {
117 0 : }
118 0 : surfaceinfo(uchar verts, uchar num) : verts(verts), numverts(num)
119 : {
120 0 : }
121 :
122 0 : int totalverts() const
123 : {
124 0 : return numverts&Face_MaxVerts;
125 : }
126 :
127 0 : bool used() const
128 : {
129 0 : return (numverts&~BlendLayer_Top) != 0;
130 : }
131 :
132 0 : void clear()
133 : {
134 0 : numverts = (numverts&Face_MaxVerts) | BlendLayer_Top;
135 0 : }
136 :
137 : void brighten()
138 : {
139 : clear();
140 : }
141 : };
142 :
143 : struct vtxarray;
144 :
145 : struct cubeext final
146 : {
147 : vtxarray *va; /**< Vertex array for children, or nullptr. */
148 : octaentities *ents; /**< Map entities inside cube. */
149 : std::array<surfaceinfo, 6> surfaces; // render info for each surface
150 : int tjoints; // linked list of t-joints
151 : uchar maxverts; // allocated space for verts
152 :
153 0 : vertinfo *verts()
154 : {
155 0 : return reinterpret_cast<vertinfo *>(this+1);
156 : }
157 : };
158 :
159 : enum CubeFaceOrientation
160 : {
161 : Orient_Left = 0,
162 : Orient_Right,
163 : Orient_Back,
164 : Orient_Front,
165 : Orient_Bottom,
166 : Orient_Top,
167 : Orient_Any
168 : };
169 :
170 : extern uchar octaboxoverlap(const ivec &o, int size, const ivec &bbmin, const ivec &bbmax);
171 :
172 : #define LOOP_OCTA_BOX(o, size, bbmin, bbmax) uchar possible = octaboxoverlap(o, size, bbmin, bbmax); for(int i = 0; i < 8; ++i) if(possible&(1<<i))
173 :
174 : #define OCTA_COORD(d, i) (((i)&octadim(d))>>(d))
175 : #define OCTA_STEP(x, y, z, scale) (((((z)>>(scale))&1)<<2) | ((((y)>>(scale))&1)<<1) | (((x)>>(scale))&1))
176 :
177 : extern int wtris, wverts,
178 : vtris, vverts,
179 : glde, gbatches,
180 : rplanes;
181 :
182 : extern int allocnodes;
183 :
184 : enum
185 : {
186 : ViewFrustumCull_FullyVisible = 0,
187 : ViewFrustumCull_PartlyVisible,
188 : ViewFrustumCull_Fogged,
189 : ViewFrustumCull_NotVisible,
190 : };
191 :
192 : /**
193 : * @brief Returns an octet of cubes allocated on the heap.
194 : *
195 : * These cubes should be freed with freeocta() to prevent a leak.
196 : *
197 : * `allocnodes` is incremented by one for each call of this function.
198 : *
199 : * @param face The face values to set the eight cubes
200 : * @param mat The material mask to assign to the cubes
201 : *
202 : * @return a std::array<cube, 8> pointer pointing to an array containing the created cubes
203 : */
204 : extern std::array<cube, 8> *newcubes(uint face = faceempty, int mat = Mat_Air);
205 : extern cubeext *growcubeext(cubeext *ext, int maxverts);
206 : extern void setcubeext(cube &c, cubeext *ext);
207 : extern cubeext *newcubeext(cube &c, int maxverts = 0, bool init = true);
208 : extern void getcubevector(const cube &c, int d, int x, int y, int z, ivec &p);
209 : extern void setcubevector(cube &c, int d, int x, int y, int z, const ivec &p);
210 : extern int familysize(const cube &c);
211 : extern void freeocta(std::array<cube, 8> *&c);
212 : extern void validatec(std::array<cube, 8> *&c, int size = 0);
213 :
214 : extern std::array<const cube *, 32> neighborstack;
215 : extern int neighbordepth;
216 : extern int getmippedtexture(const cube &p, int orient);
217 : extern void forcemip(cube &c, bool fixtex = true);
218 : extern bool subdividecube(cube &c, bool fullcheck=true, bool brighten=true);
219 : extern int faceconvexity(const std::array<ivec, 4> &v);
220 : extern int faceconvexity(const std::array<ivec, 4> &v, int &vis);
221 : extern int faceconvexity(const vertinfo *verts, int numverts, int size);
222 : extern int faceconvexity(const cube &c, int orient);
223 : extern uint faceedges(const cube &c, int orient);
224 : extern bool flataxisface(const cube &c, int orient);
225 : extern void genclipbounds(const cube &c, const ivec &co, int size, clipplanes &p);
226 : extern void genclipplanes(const cube &c, const ivec &co, int size, clipplanes &p, bool collide = true, bool noclip = false);
227 : extern bool visibleface(const cube &c, int orient, const ivec &co, int size, ushort mat = Mat_Air, ushort nmat = Mat_Air, ushort matmask = MatFlag_Volume);
228 : extern int classifyface(const cube &c, int orient, const ivec &co, int size);
229 : extern int visibletris(const cube &c, int orient, const ivec &co, int size, ushort vmat = Mat_Air, ushort nmat = Mat_Alpha, ushort matmask = Mat_Alpha);
230 : extern int visibleorient(const cube &c, int orient);
231 : extern void genfaceverts(const cube &c, int orient, std::array<ivec, 4> &v);
232 : extern int calcmergedsize(const ivec &co, int size, const vertinfo *verts, int numverts);
233 : extern void invalidatemerges(cube &c);
234 : extern void remip();
235 : extern cubeext &ext(cube &c);
236 :
237 : #define GENFACEVERTX(o,n, x,y,z, xv,yv,zv) GENFACEVERT(o,n, x,y,z, xv,yv,zv)
238 :
239 : #define GENFACEVERTSX(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
240 : GENFACEORIENT(0, GENFACEVERTX(0,0, x0,y1,z1, d0,r1,c1), GENFACEVERTX(0,1, x0,y1,z0, d0,r1,c0), GENFACEVERTX(0,2, x0,y0,z0, d0,r0,c0), GENFACEVERTX(0,3, x0,y0,z1, d0,r0,c1)) \
241 : GENFACEORIENT(1, GENFACEVERTX(1,0, x1,y1,z1, d1,r1,c1), GENFACEVERTX(1,1, x1,y0,z1, d1,r0,c1), GENFACEVERTX(1,2, x1,y0,z0, d1,r0,c0), GENFACEVERTX(1,3, x1,y1,z0, d1,r1,c0))
242 :
243 : #define GENFACEVERTY(o,n, x,y,z, xv,yv,zv) GENFACEVERT(o,n, x,y,z, xv,yv,zv)
244 :
245 : #define GENFACEVERTSY(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
246 : GENFACEORIENT(2, GENFACEVERTY(2,0, x1,y0,z1, c1,d0,r1), GENFACEVERTY(2,1, x0,y0,z1, c0,d0,r1), GENFACEVERTY(2,2, x0,y0,z0, c0,d0,r0), GENFACEVERTY(2,3, x1,y0,z0, c1,d0,r0)) \
247 : GENFACEORIENT(3, GENFACEVERTY(3,0, x0,y1,z0, c0,d1,r0), GENFACEVERTY(3,1, x0,y1,z1, c0,d1,r1), GENFACEVERTY(3,2, x1,y1,z1, c1,d1,r1), GENFACEVERTY(3,3, x1,y1,z0, c1,d1,r0))
248 : #define GENFACEVERTZ(o,n, x,y,z, xv,yv,zv) GENFACEVERT(o,n, x,y,z, xv,yv,zv)
249 :
250 : #define GENFACEVERTSZ(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
251 : GENFACEORIENT(4, GENFACEVERTZ(4,0, x0,y0,z0, r0,c0,d0), GENFACEVERTZ(4,1, x0,y1,z0, r0,c1,d0), GENFACEVERTZ(4,2, x1,y1,z0, r1,c1,d0), GENFACEVERTZ(4,3, x1,y0,z0, r1,c0,d0)) \
252 : GENFACEORIENT(5, GENFACEVERTZ(5,0, x0,y0,z1, r0,c0,d1), GENFACEVERTZ(5,1, x1,y0,z1, r1,c0,d1), GENFACEVERTZ(5,2, x1,y1,z1, r1,c1,d1), GENFACEVERTZ(5,3, x0,y1,z1, r0,c1,d1))
253 :
254 : #define GENFACEVERTSXY(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
255 : GENFACEVERTSX(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
256 : GENFACEVERTSY(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1)
257 :
258 : #define GENFACEVERTS(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
259 : GENFACEVERTSXY(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1) \
260 : GENFACEVERTSZ(x0,x1, y0,y1, z0,z1, c0,c1, r0,r1, d0,d1)
261 :
262 : #endif
|