summaryrefslogtreecommitdiff
path: root/layoutgl/widget.cpp
blob: 32789f5e622e726e64292a89f5e3ca7ef7e5b8ed (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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "widget.h"

LGLWidget::LGLWidget(QWidget *parent) :
	QGLWidget(parent), m_layout(0) {
}


void LGLWidget::setLayout(LYTLayout *layout) {
	// TODO: cleanup stuff for previous layout

	m_layout = layout;
	resize(layout->width + 64, layout->height + 64);
}


void LGLWidget::initializeGL() {
	qDebug() << "initialising GL";
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_TEXTURE_2D);

	// this makes texture transparency work
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// makes glColor value blend with textures
	glEnable(GL_COLOR_MATERIAL);

	m_texMgr.setup(this, m_layout);
}


void LGLWidget::resizeGL(int w, int h) {
	glViewport(0, 0, (GLint)w, (GLint)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	float halfW = w / 2.0f, halfH = h / 2.0f;

	glOrtho(-halfW, halfW, -halfH, halfH, -100, 100);
	glMatrixMode(GL_MODELVIEW);
}


void LGLWidget::paintGL() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if (m_layout == 0)
		return;

	glLoadIdentity();

	renderPane(m_layout->rootPane, 255);

	// now, debugging/editing aids
	// first off reset the textures
	for (int i = 0; i < 8; i++) {
		glActiveTexture(GL_TEXTURE0+i);
		glDisable(GL_TEXTURE_2D);
	}
	glActiveTexture(GL_TEXTURE0);

	glColor3ub(255, 0, 0);
	float halfW = m_layout->width / 2.0f;
	float halfH = m_layout->height / 2.0f;

	const float lineExtension = 12.0f;
	glBegin(GL_QUADS);

	// top line
	glVertex2f(-halfW - 1.0f - lineExtension, halfH + 1.0f);
	glVertex2f(halfW + 1.0f + lineExtension, halfH + 1.0f);
	glVertex2f(halfW + 1.0f + lineExtension, halfH);
	glVertex2f(-halfW - 1.0f - lineExtension, halfH);
	// bottom line
	glVertex2f(-halfW - 1.0f - lineExtension, -halfH);
	glVertex2f(halfW + 1.0f + lineExtension, -halfH);
	glVertex2f(halfW + 1.0f + lineExtension, -halfH - 1.0f);
	glVertex2f(-halfW - 1.0f - lineExtension, -halfH - 1.0f);
	// left line
	glVertex2f(-halfW - 1.0f, halfH + 1.0f + lineExtension);
	glVertex2f(-halfW, halfH + 1.0f + lineExtension);
	glVertex2f(-halfW, -halfH - 1.0f - lineExtension);
	glVertex2f(-halfW - 1.0f, -halfH - 1.0f - lineExtension);
	// right line
	glVertex2f(halfW, halfH + 1.0f + lineExtension);
	glVertex2f(halfW + 1.0f, halfH + 1.0f + lineExtension);
	glVertex2f(halfW + 1.0f, -halfH - 1.0f - lineExtension);
	glVertex2f(halfW, -halfH - 1.0f - lineExtension);

	// centre: horizontal
	glVertex2f(-lineExtension, 0.5f);
	glVertex2f(lineExtension, 0.5f);
	glVertex2f(lineExtension, -0.5f);
	glVertex2f(-lineExtension, -0.5f);
	// centre: vertical
	glVertex2f(-0.5f, lineExtension);
	glVertex2f(0.5f, lineExtension);
	glVertex2f(0.5f, -lineExtension);
	glVertex2f(-0.5f, -lineExtension);

	glEnd();
}

void LGLWidget::renderPane(const LYTPane *pane, quint8 parentAlpha) {
	if (!pane->visible)
		return;

	glPushMatrix();

	glScalef(pane->xScale, pane->yScale, 1.0f);
	glRotatef(pane->xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(pane->yRot, 0.0f, 1.0f, 0.0f);
	glRotatef(pane->zRot, 0.0f, 0.0f, 1.0f);
	glTranslatef(pane->xTrans, pane->yTrans, pane->zTrans);
	//qDebug() << "Translating by" << pane->xTrans << pane->yTrans << pane->zTrans;

	quint8 effectiveAlpha = (parentAlpha == 255) ? pane->alpha : ((pane->alpha * parentAlpha) / 255);

	switch (pane->type()) {
	case LYTPane::PictureType:
		drawPicture((LYTPicture*)pane, effectiveAlpha);
		break;
	case LYTPane::WindowType:
		drawWindow((LYTWindow*)pane, effectiveAlpha);
		break;
	}

	quint8 childrenAlpha = pane->influencedAlpha ? effectiveAlpha : 255;
	foreach (const LYTPane *childPane, pane->children)
		renderPane(childPane, childrenAlpha);

	//qDebug() << "Popping";
	glPopMatrix();
}

void LGLWidget::drawPicture(const LYTPicture *pic, quint8 effectiveAlpha) {
	float dX = pic->drawnVertexX();
	float dY = pic->drawnVertexY();
	//qDebug() << "Drawing" << dX << dY << pic->width << pic->height;

	useMaterial(pic->materialName);

	drawQuad(dX, dY, pic->width, pic->height, pic->texCoords, pic->vtxColours, effectiveAlpha);

	//glColor3ub(255, 255, 255);
	//renderText(dX, (dY-pic->height)+10, 0, pic->name);
}

struct TexFlipBit {
	quint8 bits[8];
	quint8 one, two;
};

static const TexFlipBit TextureFlipInfo[6] = {
	{{0,0,1,0,0,1,1,1},0,1},
	{{1,0,0,0,1,1,0,1},0,1},
	{{0,1,1,1,0,0,1,0},0,1},
	{{0,1,0,0,1,1,1,0},1,0},
	{{1,1,0,1,1,0,0,0},0,1},
	{{1,0,1,1,0,0,0,1},1,0}
};

void LGLWidget::dealWithWindowFrame(LYTTexCoords &coords, const QString &materialName,
							   int flipType, float pieceWidth, float pieceHeight,
							   int rep1, int rep2, int rep3, int rep4) {
	// What are you DOING Nintendo.. the code for this is a mess!
	// I seriously don't get it.
	// I am also using a terrible hack here.
	const TexFlipBit &info = TextureFlipInfo[flipType];

	qreal *hack = (qreal*)(&coords.coord[0]);

	float assign1 = info.bits[rep1 + info.one];
	hack[rep1 + info.one] = assign1;
	hack[rep3 + info.one] = assign1;

	float assign2 = info.bits[rep1 + info.two];
	hack[rep1 + info.two] = assign2;
	hack[rep2 + info.two] = assign2;

	float texSize[2];
	getTextureSize(materialName, &texSize[0], &texSize[1]);

	float assign3 = info.bits[rep1 + info.one] + (pieceWidth / ((info.bits[rep2 + info.one] - info.bits[rep1 + info.one]) * texSize[info.one]));
	hack[rep2 + info.one] = assign3;
	hack[rep4 + info.one] = assign3;

	float assign4 = info.bits[rep1 + info.two] + (pieceHeight / ((info.bits[rep3 + info.two] - info.bits[rep1 + info.two]) * texSize[info.two]));
	hack[rep3 + info.two] = assign4;
	hack[rep4 + info.two] = assign4;
}

void LGLWidget::drawWindow(const LYTWindow *wnd, quint8 effectiveAlpha) {
	float dX = wnd->drawnVertexX();
	float dY = wnd->drawnVertexY();

	// get the frame size
	float frameLeft, frameTop, frameRight, frameBottom;

	switch (wnd->frames.count()) {
	case 1:
		float oneW, oneH;
		getTextureSize(wnd->frames.at(0)->materialName, &oneW, &oneH);
		frameLeft = frameRight = oneW;
		frameTop = frameBottom = oneH;
		break;
	case 4: case 8:
		getTextureSize(wnd->frames.at(0)->materialName, &frameLeft, &frameTop);
		getTextureSize(wnd->frames.at(3)->materialName, &frameRight, &frameBottom);
		break;
	}

	// draw the content
	useMaterial(wnd->contentMaterialName);
	//qDebug() << "content material:" << wnd->contentMaterialName;
	drawQuad(
				dX + frameLeft - wnd->contentOverflowLeft,
				dY - frameTop + wnd->contentOverflowTop,
				((wnd->contentOverflowLeft + (wnd->width - frameLeft)) - frameRight) + wnd->contentOverflowRight,
				((wnd->contentOverflowTop + (wnd->height - frameTop)) - frameBottom) + wnd->contentOverflowBottom,
				wnd->contentTexCoords, wnd->contentVtxColours, effectiveAlpha);

	// deal with the frame
	LYTTexCoords texCoords;

	switch (wnd->frames.count()) {
	case 1:
	{
		const LYTWindowFrame &frame = *wnd->frames.first();
		const LYTMaterial &mat = getMaterial(frame.materialName);

		if (!mat.texMaps.empty()) {
			useMaterial(mat);

			// top left
			float pieceWidth = wnd->width - frameRight;
			float pieceHeight = frameTop;

			dealWithWindowFrame(texCoords, frame.materialName, 0, pieceWidth, pieceHeight, 0, 2, 4, 6);
			drawQuad(dX, dY, pieceWidth, pieceHeight, 1, &texCoords, 0, effectiveAlpha);

			// top right
			pieceWidth = frameRight;
			pieceHeight = wnd->height - frameBottom;

			dealWithWindowFrame(texCoords, frame.materialName, 1, pieceWidth, pieceHeight, 2, 0, 6, 4);

			drawQuad(dX + wnd->width - frameRight, dY, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);

			// bottom left
			pieceWidth = frameLeft;
			pieceHeight = wnd->height - frameTop;

			dealWithWindowFrame(texCoords, frame.materialName, 2, pieceWidth, pieceHeight, 4, 6, 0, 2);

			drawQuad(dX, dY - frameTop, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);

			// bottom right
			pieceWidth = wnd->width - frameLeft;
			pieceHeight = frameBottom;

			dealWithWindowFrame(texCoords, frame.materialName, 4, pieceWidth, pieceHeight, 6, 4, 2, 0);

			drawQuad(dX + frameLeft, dY - wnd->height + frameBottom, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);
		}
	}
		break;
	case 4:
	{
		// top left
		const LYTWindowFrame &fTL = *wnd->frames.at(0);
		const LYTMaterial &mTL = getMaterial(fTL.materialName);

		if (!mTL.texMaps.empty()) {
			useMaterial(mTL);

			float pieceWidth = wnd->width - frameRight;
			float pieceHeight = frameTop;

			dealWithWindowFrame(texCoords, fTL.materialName, fTL.type, pieceWidth, pieceHeight, 0, 2, 4, 6);
			drawQuad(dX, dY, pieceWidth, pieceHeight, 1, &texCoords, 0, effectiveAlpha);
		}

		// top right
		const LYTWindowFrame &fTR = *wnd->frames.at(1);
		const LYTMaterial &mTR = getMaterial(fTR.materialName);

		if (!mTR.texMaps.empty()) {
			useMaterial(mTR);

			float pieceWidth = frameRight;
			float pieceHeight = wnd->height - frameBottom;

			dealWithWindowFrame(texCoords, fTR.materialName, fTR.type, pieceWidth, pieceHeight, 2, 0, 6, 4);

			drawQuad(dX + wnd->width - frameRight, dY, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);
		}

		// bottom left
		const LYTWindowFrame &fBL = *wnd->frames.at(2);
		const LYTMaterial &mBL = getMaterial(fBL.materialName);

		if (!mBL.texMaps.empty()) {
			useMaterial(mBL);

			float pieceWidth = frameLeft;
			float pieceHeight = wnd->height - frameTop;

			dealWithWindowFrame(texCoords, fBL.materialName, fBL.type, pieceWidth, pieceHeight, 4, 6, 0, 2);

			drawQuad(dX, dY - frameTop, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);
		}

		const LYTWindowFrame &fBR = *wnd->frames.at(3);
		const LYTMaterial &mBR = getMaterial(fBR.materialName);

		// bottom right
		if (!mBR.texMaps.empty()) {
			useMaterial(mBR);

			float pieceWidth = wnd->width - frameLeft;
			float pieceHeight = frameBottom;

			dealWithWindowFrame(texCoords, fBR.materialName, fBR.type, pieceWidth, pieceHeight, 6, 4, 2, 0);

			drawQuad(dX + frameLeft, dY - wnd->height + frameBottom, pieceWidth, pieceHeight,
					 1, &texCoords, 0, effectiveAlpha);
		}
	}
		break;
	case 8:
	{
		qDebug() << "WHOA!!!";
		// top left
		const LYTWindowFrame &fTL = *wnd->frames.at(0);
		const LYTMaterial &mTL = getMaterial(fTL.materialName);

		if (!mTL.texMaps.empty()) {
			useMaterial(mTL);

			dealWithWindowFrame(texCoords, fTL.materialName, fTL.type, frameLeft, frameTop, 0, 2, 4, 6);
			drawQuad(dX, dY, frameLeft, frameTop, 1, &texCoords, 0, effectiveAlpha);
		}

		// top right
		const LYTWindowFrame &fTR = *wnd->frames.at(1);
		const LYTMaterial &mTR = getMaterial(fTR.materialName);

		if (!mTR.texMaps.empty()) {
			useMaterial(mTR);

			dealWithWindowFrame(texCoords, fTR.materialName, fTR.type, frameRight, frameTop, 2, 0, 6, 4);

			drawQuad(dX + wnd->width - frameRight, dY, frameRight, frameTop,
					 1, &texCoords, 0, effectiveAlpha);
		}

		// bottom left
		const LYTWindowFrame &fBL = *wnd->frames.at(2);
		const LYTMaterial &mBL = getMaterial(fBL.materialName);

		if (!mBL.texMaps.empty()) {
			useMaterial(mBL);

			dealWithWindowFrame(texCoords, fBL.materialName, fBL.type, frameLeft, frameBottom, 4, 6, 0, 2);

			drawQuad(dX, dY - frameTop, frameLeft, frameBottom,
					 1, &texCoords, 0, effectiveAlpha);
		}

		const LYTWindowFrame &fBR = *wnd->frames.at(3);
		const LYTMaterial &mBR = getMaterial(fBR.materialName);

		// bottom right
		if (!mBR.texMaps.empty()) {
			useMaterial(mBR);

			dealWithWindowFrame(texCoords, fBR.materialName, fBR.type, frameRight, frameBottom, 6, 4, 2, 0);

			drawQuad(dX + frameLeft, dY - wnd->height + frameBottom, frameRight, frameBottom,
					 1, &texCoords, 0, effectiveAlpha);
		}
	}
	default:
		qDebug() << "unhandled window frame count" << wnd->frames.count();
	}
}

static GLint GLWrapModes[] = {
	GL_CLAMP_TO_EDGE, // Clamp
	GL_REPEAT, // Repeat
	GL_MIRRORED_REPEAT, // Mirror
};

void LGLWidget::useMaterial(const QString &materialName) {
	const LYTMaterial *mat = m_layout->materials.getMaterialByName(materialName);

	useMaterial(*mat);
}

void LGLWidget::useMaterial(const LYTMaterial &mat) {
	int cutAt = mat.texMaps.count();

	for (int i = 0; i < cutAt; i++) {
		const LYTTexMap &texMap = mat.texMaps.at(i);

		const QString &texName = texMap.textureName;
		GLuint texID = m_texMgr.glTextureForName(texName);

		glActiveTexture(GL_TEXTURE0 + i);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLWrapModes[texMap.wrap_s]);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLWrapModes[texMap.wrap_t]);

		// is there a matching TexSRT?
		// TODO: fix this
		glMatrixMode(GL_TEXTURE);
		glLoadIdentity();

		if (mat.texSRTs.count() > i) {
			const LYTTexSRT &srt = mat.texSRTs.at(i);

			glScalef(srt.xScale, srt.yScale, 1.0f);
			// rotate?
			glTranslatef(srt.xTrans, -srt.yTrans, 0.0f);
		}

		glMatrixMode(GL_MODELVIEW);
	}

	for (int i = cutAt; i < 8; i++) {
		glActiveTexture(GL_TEXTURE0 + i);
		glDisable(GL_TEXTURE_2D);
	}

	glActiveTexture(GL_TEXTURE0);
}

void LGLWidget::drawQuad(float x, float y, float w, float h, const QVector<LYTTexCoords> &texCoords, const QColor *colours, uchar alpha) {
	drawQuad(x, y, w, h, texCoords.count(), texCoords.constData(), colours, alpha);
}

void LGLWidget::drawQuad(float x, float y, float w, float h, int texCoordCount, const LYTTexCoords *texCoords, const QColor *colours, uchar alpha) {
	if (!colours)
		glColor4ub(255, 255, 255, alpha);

	glBegin(GL_QUADS);

	for (int i = 0; i < texCoordCount; i++)
		glMultiTexCoord2f(GL_TEXTURE0_ARB+i, texCoords[i].coord[0].x(), 1.0f-texCoords[i].coord[0].y());

	if (colours)
		glColor4ub(colours[0].red(), colours[0].green(), colours[0].blue(), (colours[0].alpha() * alpha) / 255);
	glVertex2f(x, y);

	for (int i = 0; i < texCoordCount; i++)
		glMultiTexCoord2f(GL_TEXTURE0_ARB+i, texCoords[i].coord[1].x(), 1.0f-texCoords[i].coord[1].y());

	if (colours)
		glColor4ub(colours[1].red(), colours[1].green(), colours[1].blue(), (colours[1].alpha() * alpha) / 255);
	glVertex2f(x + w, y);

	for (int i = 0; i < texCoordCount; i++)
		glMultiTexCoord2f(GL_TEXTURE0_ARB+i, texCoords[i].coord[3].x(), 1.0f-texCoords[i].coord[3].y());

	if (colours)
		glColor4ub(colours[3].red(), colours[3].green(), colours[3].blue(), (colours[3].alpha() * alpha) / 255);
	glVertex2f(x + w, y - h);

	for (int i = 0; i < texCoordCount; i++)
		glMultiTexCoord2f(GL_TEXTURE0_ARB+i, texCoords[i].coord[2].x(), 1.0f-texCoords[i].coord[2].y());

	if (colours)
		glColor4ub(colours[2].red(), colours[2].green(), colours[2].blue(), (colours[2].alpha() * alpha) / 255);
	glVertex2f(x, y - h);

	glEnd();
}


void LGLWidget::getTextureSize(const QString &materialName, float *w, float *h) {
	const LYTMaterial *mat = m_layout->materials.getMaterialByName(materialName);

	if (mat->texMaps.count() == 0) {
		*w = 0.0f;
		*h = 0.0f;
	} else {
		const LYTTexMap &tm = mat->texMaps.first();

		QImage img = m_texMgr.imageForName(tm.textureName);
		*w = img.width();
		*h = img.height();
	}
}