-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtexturetonsbtx.cpp
More file actions
252 lines (205 loc) · 7.07 KB
/
texturetonsbtx.cpp
File metadata and controls
252 lines (205 loc) · 7.07 KB
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
#include "texturetonsbtx.h"
#include "ui_texturetonsbtx.h"
#include "textureentry.h"
#include <QTextStream>
#include <QFileDialog>
static QString exeDir;
static QVector<TextureEntry*> texEntries;
TextureToNSBTX::TextureToNSBTX(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TextureToNSBTX)
{
ui->setupUi(this);
exeDir = QApplication::applicationDirPath();
QVBoxLayout* scrollarea_layout = new QVBoxLayout();
scrollarea_layout->setMargin(0);
scrollarea_layout->setSpacing(0);
TextureEntry* texEntry = new TextureEntry();
texEntries.append(texEntry);
scrollarea_layout->addWidget(texEntry);
ui->textures_sa_widget->setLayout(scrollarea_layout);
}
void TextureToNSBTX::on_selectNsbtxPath_pb_clicked()
{
static QString lastDirSelected = QDir::homePath();
if(lastDirSelected == QDir::homePath() && ui->nsbtxPath_le->text() != "")
lastDirSelected = ui->nsbtxPath_le->text().split('.')[0];
QString fileName = QFileDialog::getSaveFileName(this, tr("Save NSBTX File"), lastDirSelected, tr("NSBTX File (*.nsbtx)"));
if(fileName == "")
return;
lastDirSelected = fileName;
ui->nsbtxPath_le->setText(fileName);
}
TextureToNSBTX::~TextureToNSBTX()
{
delete ui;
}
void TextureToNSBTX::on_addTexture_pb_clicked()
{
TextureEntry* texEntry = new TextureEntry();
texEntries.append(texEntry);
texEntry->SetID(texEntries.size());
ui->textures_sa_widget->layout()->addWidget(texEntry);
}
void TextureToNSBTX::on_removeTexture_pb_clicked()
{
TextureEntry* texEntry = texEntries.last();
ui->textures_sa_widget->layout()->removeWidget(texEntry);
delete texEntry;
texEntry = nullptr;
texEntries.removeLast();
//If all got removed add 1 back
if(texEntries.size() == 0)
{
on_addTexture_pb_clicked();
}
}
//CONVERSION STUFF AHEAD
void TextureToNSBTX::printToConsole(const QString &text)
{
ui->console_tb->setTextColor(QColor(0, 255, 0));
QString textToLower = text.toLower();
if(textToLower.contains("error"))
ui->console_tb->setTextColor(QColor("red"));
else if(textToLower.contains("warning"))
ui->console_tb->setTextColor(QColor("yellow"));
ui->console_tb->append(text);
QApplication::processEvents(); //Force UI update
}
void TextureToNSBTX::printAppOutputToConsole(QProcess* process)
{
QString line;
do
{
line = process->readLine();
if (line.toLower().contains("error:") || line.toLower().contains("warning:"))
{
printToConsole(line.trimmed());
}
}
while (!line.isNull());
}
void TextureToNSBTX::WriteOBJFile(QFile* file)
{
QTextStream stream(file);
stream << R"(mtllib temp.mtl
o MaterialArray
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.0000 1.0000 0.0000
)";
for(int i = 0; i < texEntries.size(); i++)
{
stream << "usemtl Material" << i+1 << "\n";
stream << "f 1/1/1 2/2/1 4/3/1 3/4/1" << "\n";
}
}
void TextureToNSBTX::WriteMTLFile(QFile* file)
{
QTextStream stream(file);
QDir tempDir(exeDir + "/temp");
for(int i = 0; i < texEntries.size(); i++)
{
QString tempTexPath = tempDir.path() + "/" + texEntries[i]->GetNewFileNameWithExtension();
QFile::copy(texEntries[i]->GetPath(), tempTexPath);
stream << "newmtl Material" << i+1 << "\n";
stream << "map_Kd " << tempTexPath << "\n";
}
}
void TextureToNSBTX::on_convert_pb_clicked()
{
ui->console_tb->clear();
QString destinationPath = ui->nsbtxPath_le->text();
if(destinationPath == "")
{
printToConsole("Error: No destination NSBTX path was specified!");
return;
}
// CHECK ALL PATHS -----------------------------------------------------------
for(int i = 0; i < texEntries.size(); i++)
{
if(texEntries[i]->GetPath() == "")
{
printToConsole("Error: At least one of the texture paths specified was empty!");
return;
}
}
// TEMP CLEANUP --------------------------------------------------------------
QDir tempDir(exeDir + "/temp");
printToConsole("Cleaning temp folder...");
tempDir.removeRecursively(); //Remove temp folder
while(tempDir.exists()) {} //Wait for temp folder to be deleted
QDir().mkdir(tempDir.path()); //Add temp folder back but empty
// OBJ GENERATION ------------------------------------------------------------
printToConsole("Generating temporary OBJ file...");
QFile tempObjFile(tempDir.path() + "/temp.obj");
if(!tempObjFile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
WriteOBJFile(&tempObjFile);
tempObjFile.close();
// MTL GENERATION ------------------------------------------------------------
printToConsole("Generating temporary MTL file...");
QFile tempMtlFile(tempDir.path() + "/temp.mtl");
if(!tempMtlFile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
WriteMTLFile(&tempMtlFile);
tempMtlFile.close();
// IMD CONVERSION ------------------------------------------------------------
printToConsole("Starting conversion to IMD...");
QProcess* ass2imd = new QProcess();
ass2imd->setProgram(exeDir + "/bin/ass2imd/AssToImd");
ass2imd->setArguments({tempDir.path() + "/temp.obj", "-o", tempDir.path() + "/temp.imd"});
ass2imd->start();
if(ass2imd->state() == QProcess::NotRunning)
{
printToConsole("Error: AssToImd failed to start or could not be found!");
return;
}
connect(ass2imd, &QProcess::readyRead, [=](){ printAppOutputToConsole(ass2imd); });
ass2imd->waitForFinished(-1);
delete ass2imd;
ass2imd = nullptr;
if(QFile::exists(tempDir.path() + "/temp.imd"))
printToConsole("Success: IMD generated successfully!");
else
{
printToConsole("Error: IMD failed to generate!");
return;
}
// NSBTX CONVERSION ----------------------------------------------------------
printToConsole("Starting conversion to NSBTX...");
if(QFile::exists(destinationPath))
{
printToConsole("Deleting existing destination NSBTX...");
QFile::remove(destinationPath);
}
while(QFile::exists(destinationPath)) {} //Wait for removal
QProcess* imd2nsbmd = new QProcess();
imd2nsbmd->setProgram(exeDir + "/bin/imd2bin/imd2bin");
imd2nsbmd->setArguments({tempDir.path() + "/temp.imd", "-etex", "-o", destinationPath});
imd2nsbmd->start();
if(imd2nsbmd->state() == QProcess::NotRunning)
{
printToConsole("Error: imd2bin failed to start or could not be found!");
return;
}
connect(imd2nsbmd, &QProcess::readyRead, [=](){ printAppOutputToConsole(imd2nsbmd); });
imd2nsbmd->waitForFinished(-1);
delete imd2nsbmd;
imd2nsbmd = nullptr;
if(QFile::exists(destinationPath))
{
printToConsole("Success: NSBTX generated successfully!");
}
else
{
printToConsole("Error: NSBTX failed to generate!");
return;
}
}