summaryrefslogtreecommitdiff
path: root/lyt/materials/material.cpp
blob: a4498c54c6d03e706c15cb50522f4b00035c8b60 (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
/*******************************************************************************
  This file is part of LayoutStudio (http://github.com/Treeki/LayoutStudio)
  Copyright (c) 2010 Treeki (treeki@gmail.com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, version 2.0.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License 2.0 for more details.

  You should have received a copy of the GNU General Public License 2.0
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

#include "material.h"
#include "../layout.h"

LYTMaterial::LYTMaterial(LYTLayout &layout) : m_layout(layout) {
}

LYTMaterial::~LYTMaterial() {
}

LYTLayout &LYTMaterial::layout() const {
	return m_layout;
}


void LYTMaterial::dumpToDebug() {
	qDebug() << "LYTMaterial" << name << "@" << (void*)this;

	for (int i = 0; i < 3; i++)
		qDebug() << "Colour" << i << ":" << colours[i];

	for (int i = 0; i < 4; i++)
		qDebug() << "TEV Constant Colour" << i << ":" << tevKColour[i];

	qDebug() << "TexMaps:" << texMaps.count();
	foreach (LYTTexMap texMap, texMaps)
		texMap.dumpToDebug();

	qDebug() << "TexSRTs:" << texSRTs.count();
	foreach (LYTTexSRT texSRT, texSRTs)
		texSRT.dumpToDebug();

	qDebug() << "TexCoordGens:" << texCoordGens.count();
	foreach (LYTTexCoordGen texCoordGen, texCoordGens)
		texCoordGen.dumpToDebug();

	if (hasChanCtrl)
		chanCtrl.dumpToDebug();
	else
		qDebug() << "ChanCtrl: none";

	if (hasMatCol)
		qDebug() << "MatCol:" << matCol;
	else
		qDebug() << "MatCol: none";

	if (hasTevSwapTable)
		tevSwapTable.dumpToDebug();
	else
		qDebug() << "TevSwapTable: none";

	qDebug() << "IndTexSRTs:" << indTexSRTs.count();
	foreach (LYTTexSRT indTexSRT, indTexSRTs)
		indTexSRT.dumpToDebug();

	qDebug() << "IndirectStages:" << indTexStages.count();
	foreach (LYTIndirectStage indTexStage, indTexStages)
		indTexStage.dumpToDebug();

	qDebug() << "TevStages:" << tevStages.count();
	foreach (LYTTevStage tevStage, tevStages)
		tevStage.dumpToDebug();

	if (hasAlphaCompare)
		alphaCompare.dumpToDebug();
	else
		qDebug() << "AlphaCompare: none";

	if (hasBlendMode)
		blendMode.dumpToDebug();
	else
		qDebug() << "BlendMode: none";
}



void LYTMaterial::writeToDataStream(QDataStream &out) {
	WriteFixedLengthASCII(out, name, 0x14);
}

void LYTMaterial::readFromDataStream(QDataStream &in) {
	name = ReadFixedLengthASCII(in, 0x14);

	for (int i = 0; i < 3; i++)
		ReadS10Color(this->colours[i], in);

	for (int i = 0; i < 4; i++)
		ReadRGBA8Color(this->tevKColour[i], in);


	quint32 resNumValue;
	in >> (quint32&)resNumValue;
	LYTMaterialResourceNum resourceNum(resNumValue);

	// this is really complicated -_-
	// first off: TexMap
	texMaps.clear();

	for (int i = 0; i < resourceNum.getTexMapNum(); i++) {
		this->readTexMap(in);
	}

	// TexSRT
	texSRTs.clear();

	for (int i = 0; i < resourceNum.getTexSRTNum(); i++) {
		this->readTexSRT(in);
	}

	// TexCoordGen
	texCoordGens.clear();

	for (int i = 0; i < resourceNum.getTexCoordGenNum(); i++) {
		this->readTexCoordGen(in);
	}

	// ChanCtrl
	if (resourceNum.hasChanCtrl()) {
		this->hasChanCtrl = true;
		this->chanCtrl.readFromDataStream(in);
	} else {
		this->hasChanCtrl = false;
	}

	// MatCol
	if (resourceNum.hasMatCol()) {
		this->hasMatCol = true;
		ReadRGBA8Color(this->matCol, in);
	} else {
		this->hasMatCol = false;
	}

	// TevSwapTable
	if (resourceNum.hasTevSwapTable()) {
		this->hasTevSwapTable = true;
		this->tevSwapTable.readFromDataStream(in);
	} else {
		this->hasTevSwapTable = false;
	}

	// IndTexSRT
	indTexSRTs.clear();

	for (int i = 0; i < resourceNum.getIndTexSRTNum(); i++) {
		this->readIndTexSRT(in);
	}

	// IndTexStage
	indTexStages.clear();

	for (int i = 0; i < resourceNum.getIndTexStageNum(); i++) {
		this->readIndirectStage(in);
	}

	// TevStage
	tevStages.clear();

	for (int i = 0; i < resourceNum.getTevStageNum(); i++) {
		this->readTevStage(in);
	}

	// AlphaCompare
	if (resourceNum.hasAlphaCompare()) {
		this->hasAlphaCompare = true;
		this->alphaCompare.readFromDataStream(in);
	} else {
		this->hasAlphaCompare = false;
	}

	// BlendMode
	if (resourceNum.hasBlendMode()) {
		this->hasBlendMode = true;
		this->blendMode.readFromDataStream(in);
	} else {
		this->hasBlendMode = false;
	}
}



void LYTMaterial::readTexMap(QDataStream &in) {
	this->texMaps.append(LYTTexMap());
	this->texMaps.last().readFromDataStream(in, m_layout);
}

void LYTMaterial::readTexSRT(QDataStream &in) {
	this->texSRTs.append(LYTTexSRT());
	this->texSRTs.last().readFromDataStream(in);
}

void LYTMaterial::readTexCoordGen(QDataStream &in) {
	this->texCoordGens.append(LYTTexCoordGen());
	this->texCoordGens.last().readFromDataStream(in);
}

void LYTMaterial::readIndTexSRT(QDataStream &in) {
	this->indTexSRTs.append(LYTTexSRT());
	this->indTexSRTs.last().readFromDataStream(in);
}

void LYTMaterial::readIndirectStage(QDataStream &in) {
	this->indTexStages.append(LYTIndirectStage());
	this->indTexStages.last().readFromDataStream(in);
}

void LYTMaterial::readTevStage(QDataStream &in) {
	this->tevStages.append(LYTTevStage());
	this->tevStages.last().readFromDataStream(in);
}