summaryrefslogtreecommitdiff
path: root/src/engine/material.cpp
blob: 6fee9d3f7067881abc6cad5e666c77f2e0fbdd26 (plain) (blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "engine.h"

struct QuadNode {
	int x, y, size;
	uint filled;
	QuadNode *child[4];

	QuadNode(int x, int y, int size) : x(x), y(y), size(size), filled(0) { loopi(4) child[i] = 0; }

	void clear() { loopi(4) DELETEP(child[i]); }

	~QuadNode() { clear(); }

	void insert(int mx, int my, int msize) {
		if(size == msize) {
			filled = 0xF;
			return;
		}
		int csize = size>>1, i = 0;
		if(mx >= x+csize) i |= 1;
		if(my >= y+csize) i |= 2;
		if(csize == msize) {
			filled |= (1 << i);
			return;
		}
		if(!child[i]) child[i] = new QuadNode(i&1 ? x+csize : x, i&2 ? y+csize : y, csize);
		child[i]->insert(mx, my, msize);
		loopj(4) if(child[j])
		{
			if(child[j]->filled == 0xF) {
				DELETEP(child[j]);
				filled |= (1 << j);
			}
		}
	}

	void genmatsurf(ushort mat, uchar orient, uchar visible, int x, int y, int z, int size, materialsurface *&matbuf) {
		materialsurface &m = *matbuf++;
		m.material = mat;
		m.orient = orient;
		m.visible = visible;
		m.csize = size;
		m.rsize = size;
		int dim = dimension(orient);
		m.o[C[dim]] = x;
		m.o[R[dim]] = y;
		m.o[dim] = z;
	}

	void genmatsurfs(ushort mat, uchar orient, uchar flags, int z, materialsurface *&matbuf) {
		if(filled == 0xF) genmatsurf(mat, orient, flags, x, y, z, size, matbuf);
		else if(filled)
		{
			int csize = size>>1;
			loopi(4) if(filled & (1 << i))
				genmatsurf(mat, orient, flags, i&1 ? x+csize : x, i&2 ? y+csize : y, z, csize, matbuf);
		}
		loopi(4) if(child[i]) child[i]->genmatsurfs(mat, orient, flags, z, matbuf);
	}
};

static float wfwave;

static const bvec4 matnormals[6] = {
	bvec4(0x80, 0, 0),
	bvec4(0x7F, 0, 0),
	bvec4(0, 0x80, 0),
	bvec4(0, 0x7F, 0),
	bvec4(0, 0, 0x80),
	bvec4(0, 0, 0x7F)
};

static void drawmaterial(const materialsurface &m, float offset, const bvec4 &color)
{
	if(gle::attribbuf.empty())
	{
		gle::defvertex();
		gle::defcolor(4, GL_UNSIGNED_BYTE);
		gle::begin(GL_QUADS);
	}
	float x = m.o.x, y = m.o.y, z = m.o.z, csize = m.csize, rsize = m.rsize;
	switch(m.orient)
	{
	#define GENFACEORIENT(orient, v0, v1, v2, v3) \
		case orient: v0 v1 v2 v3 break;
	#define GENFACEVERT(orient, vert, mx,my,mz, sx,sy,sz) \
		{ \
			gle::attribf(mx sx, my sy, mz sz); \
			gle::attrib(color); \
		}
		GENFACEVERTS(x, x, y, y, z, z, /**/, + csize, /**/, + rsize, + offset, - offset)
	#undef GENFACEORIENT
	#undef GENFACEVERT
	}
}

const struct material
{
	const char *name;
	ushort id;
} materials[] =
{
	{"air", MAT_AIR},
	{"clip", MAT_CLIP},
	{"noclip", MAT_NOCLIP},
	{"gameclip", MAT_GAMECLIP},
	{"death", MAT_DEATH},
	{"alpha", MAT_ALPHA}
};

int findmaterial(const char *name)
{
	loopi(sizeof(materials)/sizeof(material))
	{
		if(!strcmp(materials[i].name, name)) return materials[i].id;
	}
	return -1;
}

const char *findmaterialname(int mat)
{
	loopi(sizeof(materials)/sizeof(materials[0])) if(materials[i].id == mat) return materials[i].name;
	return NULL;
}

const char *getmaterialdesc(int mat, const char *prefix)
{
	static const ushort matmasks[] = { MATF_VOLUME|MATF_INDEX, MATF_CLIP, MAT_DEATH, MAT_ALPHA };
	static string desc;
	desc[0] = '\0';
	loopi(sizeof(matmasks)/sizeof(matmasks[0])) if(mat&matmasks[i])
	{
		const char *matname = findmaterialname(mat&matmasks[i]);
		if(matname)
		{
			concatstring(desc, desc[0] ? ", " : prefix);
			concatstring(desc, matname);
		}
	}
	return desc;
}

int visiblematerial(const cube &c, int orient, const ivec &co, int size, ushort matmask)
{
	ushort mat = c.material&matmask;
	switch(mat)
	{
	case MAT_AIR:
		 break;

	default:
		if(visibleface(c, orient, co, size, mat, MAT_AIR, matmask))
			return MATSURF_EDIT_ONLY;
		break;
	}
	return MATSURF_NOT_VISIBLE;
}

void genmatsurfs(const cube &c, const ivec &co, int size, vector<materialsurface> &matsurfs)
{
	loopi(6)
	{
		static const ushort matmasks[] = { MATF_VOLUME|MATF_INDEX, MATF_CLIP, MAT_DEATH, MAT_ALPHA };
		loopj(sizeof(matmasks)/sizeof(matmasks[0]))
		{
			int matmask = matmasks[j];
			int vis = visiblematerial(c, i, co, size, matmask&~MATF_INDEX);
			if(vis != MATSURF_NOT_VISIBLE)
			{
				materialsurface m;
				m.material = c.material&matmask;
				m.orient = i;
				m.visible = vis;
				m.o = co;
				m.csize = m.rsize = size;
				if(dimcoord(i)) m.o[dimension(i)] += size;
				matsurfs.add(m);
				break;
			}
		}
	}
}

static inline bool mergematcmp(const materialsurface &x, const materialsurface &y)
{
	int dim = dimension(x.orient), c = C[dim], r = R[dim];
	if(x.o[r] + x.rsize < y.o[r] + y.rsize) return true;
	if(x.o[r] + x.rsize > y.o[r] + y.rsize) return false;
	return x.o[c] < y.o[c];
}

static int mergematr(materialsurface *m, int sz, materialsurface &n)
{
	int dim = dimension(n.orient), c = C[dim], r = R[dim];
	for(int i = sz-1; i >= 0; --i)
	{
		if(m[i].o[r] + m[i].rsize < n.o[r]) break;
		if(m[i].o[r] + m[i].rsize == n.o[r] && m[i].o[c] == n.o[c] && m[i].csize == n.csize)
		{
			n.o[r] = m[i].o[r];
			n.rsize += m[i].rsize;
			memmove(&m[i], &m[i+1], (sz - (i+1)) * sizeof(materialsurface));
			return 1;
		}
	}
	return 0;
}

static int mergematc(materialsurface &m, materialsurface &n)
{
	int dim = dimension(n.orient), c = C[dim], r = R[dim];
	if(m.o[r] == n.o[r] && m.rsize == n.rsize && m.o[c] + m.csize == n.o[c])
	{
		n.o[c] = m.o[c];
		n.csize += m.csize;
		return 1;
	}
	return 0;
}

static int mergemat(materialsurface *m, int sz, materialsurface &n)
{
	for(bool merged = false; sz; merged = true)
	{
		int rmerged = mergematr(m, sz, n);
		sz -= rmerged;
		if(!rmerged && merged) break;
		if(!sz) break;
		int cmerged = mergematc(m[sz-1], n);
		sz -= cmerged;
		if(!cmerged) break;
	}
	m[sz++] = n;
	return sz;
}

static int mergemats(materialsurface *m, int sz)
{
	quicksort(m, sz, mergematcmp);

	int nsz = 0;
	loopi(sz) nsz = mergemat(m, nsz, m[i]);
	return nsz;
}

static inline bool optmatcmp(const materialsurface &x, const materialsurface &y)
{
	if(x.material < y.material) return true;
	if(x.material > y.material) return false;
	if(x.orient > y.orient) return true;
	if(x.orient < y.orient) return false;
	int dim = dimension(x.orient);
	return x.o[dim] < y.o[dim];
}

VARF(optmats, 0, 1, 1, allchanged());

int optimizematsurfs(materialsurface *matbuf, int matsurfs)
{
	quicksort(matbuf, matsurfs, optmatcmp);
	if(!optmats) return matsurfs;
	materialsurface *cur = matbuf, *end = matbuf+matsurfs;
	while(cur < end)
	{
		 materialsurface *start = cur++;
		 int dim = dimension(start->orient);
		 while(cur < end &&
			   cur->material == start->material &&
			   cur->orient == start->orient &&
			   cur->visible == start->visible &&
			   cur->o[dim] == start->o[dim])
			++cur;
		 if(start->orient != O_TOP)
		 {
			if(start!=matbuf) memmove(matbuf, start, (cur-start)*sizeof(materialsurface));
			matbuf += mergemats(matbuf, cur-start);
		 }
		 else if(cur-start>=4)
		 {
			QuadNode vmats(0, 0, worldsize);
			loopi(cur-start) vmats.insert(start[i].o[C[dim]], start[i].o[R[dim]], start[i].csize);
			vmats.genmatsurfs(start->material, start->orient, start->visible, start->o[dim], matbuf);
		 }
		 else
		 {
			if(start!=matbuf) memmove(matbuf, start, (cur-start)*sizeof(materialsurface));
			matbuf += cur-start;
		 }
	}
	return matsurfs - (end-matbuf);
}

void setupmaterials(int start, int len)
{
	int hasmat = 0;
	unionfind uf;
	if(!len) len = valist.length();
	for(int i = start; i < len; i++)
	{
		vtxarray *va = valist[i];
		materialsurface *skip = NULL;
		loopj(va->matsurfs)
		{
			materialsurface &m = va->matbuf[j];
			int matvol = m.material&MATF_VOLUME;
			if(matvol) hasmat |= 1<<m.material;
			m.skip = 0;
			if(skip && m.material == skip->material && m.orient == skip->orient && skip->skip < 0xFFFF)
				skip->skip++;
			else
				skip = &m;
		}
	}
}

VARP(showmat, 0, 1, 1);

static int sortdim[3];
static ivec sortorigin;
static bool sortedit;

static inline bool vismatcmp(const materialsurface *xm, const materialsurface *ym)
{
	const materialsurface &x = *xm, &y = *ym;
	int xdim = dimension(x.orient), ydim = dimension(y.orient);
	loopi(3)
	{
		int dim = sortdim[i], xmin, xmax, ymin, ymax;
		xmin = xmax = x.o[dim];
		if(dim==C[xdim]) xmax += x.csize;
		else if(dim==R[xdim]) xmax += x.rsize;
		ymin = ymax = y.o[dim];
		if(dim==C[ydim]) ymax += y.csize;
		else if(dim==R[ydim]) ymax += y.rsize;
		if(xmax > ymin && ymax > xmin) continue;
		int c = sortorigin[dim];
		if(c > xmin && c < xmax) return sortedit;
		if(c > ymin && c < ymax) return !sortedit;
		xmin = abs(xmin - c);
		xmax = abs(xmax - c);
		ymin = abs(ymin - c);
		ymax = abs(ymax - c);
		if(max(xmin, xmax) <= min(ymin, ymax)) return sortedit;
		else if(max(ymin, ymax) <= min(xmin, xmax)) return !sortedit;
	}
	if(x.material < y.material) return sortedit;
	if(x.material > y.material) return !sortedit;
	return false;
}

void sortmaterials(vector<materialsurface *> &vismats)
{
	sortorigin = ivec(camera1->o);
	vec dir;
	vecfromyawpitch(camera1->yaw, camera1->pitch, 1, 0, dir);
	loopi(3) { dir[i] = fabs(dir[i]); sortdim[i] = i; }
	if(dir[sortdim[2]] > dir[sortdim[1]]) swap(sortdim[2], sortdim[1]);
	if(dir[sortdim[1]] > dir[sortdim[0]]) swap(sortdim[1], sortdim[0]);
	if(dir[sortdim[2]] > dir[sortdim[1]]) swap(sortdim[2], sortdim[1]);

	for(vtxarray *va = visibleva; va; va = va->next)
	{
		if(!va->matsurfs || va->occluded >= OCCLUDE_BB) continue;
		loopi(va->matsurfs)
		{
			materialsurface &m = va->matbuf[i];
			if(!editmode || !showmat || drawtex)
			{
				int matvol = m.material&MATF_VOLUME;
				if(m.visible == MATSURF_EDIT_ONLY) { i += m.skip; continue; }
			}
			else if(glaring) continue;
			vismats.add(&m);
		}
	}
	sortedit = editmode && showmat && !drawtex;
	vismats.sort(vismatcmp);
}

void rendermatgrid(vector<materialsurface *> &vismats)
{
	enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	int lastmat = -1;
	bvec4 color(0, 0, 0, 0);
	loopvrev(vismats)
	{
		materialsurface &m = *vismats[i];
		if(m.material != lastmat)
		{
			switch(m.material&~MATF_INDEX)
			{
				case MAT_CLIP:	 color = bvec4(85,  0,  0, 255); break; // red
				case MAT_NOCLIP:   color = bvec4( 0, 85,  0, 255); break; // green
				case MAT_GAMECLIP: color = bvec4(85, 85,  0, 255); break; // yellow
				case MAT_DEATH:	color = bvec4(40, 40, 40, 255); break; // black
				case MAT_ALPHA:	color = bvec4(85,  0, 85, 255); break; // pink
				default: continue;
			}
			lastmat = m.material;
		}
		drawmaterial(m, -0.1f, color);
	}
	xtraverts += gle::end();
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
}

void rendermaterials()
{
	vector<materialsurface *> vismats;
	sortmaterials(vismats);
	if(vismats.empty()) return;

	glDisable(GL_CULL_FACE);

	MSlot *mslot = NULL;
	int lastorient = -1, lastmat = -1;
	bool depth = true, blended = false;
	ushort envmapped = EMID_NONE;

	GLOBALPARAM(camera, camera1->o);

	if(editmode && showmat && !drawtex)
	{
		glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
		glEnable(GL_BLEND); blended = true;
		bvec4 color(0, 0, 0, 0);
		loopv(vismats)
		{
			const materialsurface &m = *vismats[i];
			if(lastmat!=m.material)
			{
				switch(m.material&~MATF_INDEX)
				{
					case MAT_CLIP:	 color = bvec4(  0, 255, 255, 255); break; // red
					case MAT_NOCLIP:   color = bvec4(255,   0, 255, 255); break; // green
					case MAT_GAMECLIP: color = bvec4(  0,   0, 255, 255); break; // yellow
					case MAT_DEATH:	color = bvec4(192, 192, 192, 255); break; // black
					case MAT_ALPHA:	color = bvec4(  0, 255,   0, 255); break; // pink
					default: continue;
				}
				lastmat = m.material;
			}
			drawmaterial(m, -0.1f, color);
		}
		xtraverts += gle::end();
	}

	if(lastorient >= 0)
		if(lastmat&~MATF_INDEX)
			xtraverts += gle::end();

	if(!depth) glDepthMask(GL_TRUE);
	if(blended) glDisable(GL_BLEND);
	extern int wireframe;
	if(editmode && showmat && !drawtex && !wireframe)
		rendermatgrid(vismats);

	glEnable(GL_CULL_FACE);
}