-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.cpp
377 lines (302 loc) · 13 KB
/
main.cpp
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
/*
* Laigter: an automatic map generator for lighting effects.
* Copyright (C) 2019 Pablo Ivan Fonovich
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Contact: azagaya.games@gmail.com
*/
#include "gui/presets_manager.h"
#include "main_window.h"
#include "src/image_processor.h"
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QOpenGLContext>
#include <QSplashScreen>
#include <QStandardPaths>
#include <QTranslator>
#define cimg_use_openmp
#define CHECK_CHANGES(outFileInfo, info) (outFileInfo.fileTime(QFile::FileModificationTime) < info.fileTime(QFile::FileModificationTime))
QCoreApplication *createApplication(int &argc, char *argv[])
{
bool gui = true;
for (int i = 1; i < argc; ++i)
{
if (!qstrcmp(argv[i], "--no-gui") || !qstrcmp(argv[i], "-g"))
{
gui = false;
break;
}
}
return gui ? new QApplication(argc, argv) : new QCoreApplication(argc, argv);
}
int main(int argc, char *argv[])
{
QCoreApplication::setApplicationName("laigter");
QCoreApplication::setApplicationVersion("1.12.0");
//QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#ifndef PORTABLE
QString appData =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(appData);
if (!dir.exists())
dir.mkpath(".");
dir = QDir(appData + "/presets");
if (!dir.exists())
dir.mkpath(".");
dir = QDir(appData + "/plugins");
if (!dir.exists())
dir.mkpath(".");
#else
QDir dir("./presets");
if (!dir.exists())
dir.mkpath(".");
#endif
QCommandLineParser argsParser;
argsParser.setApplicationDescription("Test helper");
argsParser.addHelpOption();
argsParser.addVersionOption();
QCommandLineOption softOpenGl(QStringList() << "s"
<< "software-opengl",
"Use software opengl renderer.");
argsParser.addOption(softOpenGl);
QCommandLineOption noGuiOption(QStringList() << "g"
<< "no-gui",
"do not start graphical interface");
argsParser.addOption(noGuiOption);
QCommandLineOption inputDiffuseTextureOption(QStringList() << "d"
<< "diffuse",
"diffuse path",
"Path to a list of diffuse images or to a directory");
argsParser.addOption(inputDiffuseTextureOption);
QCommandLineOption outputNormalTextureOption(QStringList() << "n"
<< "normal",
"generate normals");
argsParser.addOption(outputNormalTextureOption);
QCommandLineOption normalSuffixOption("normal-suffix", "Suffix for normal maps");
argsParser.addOption(normalSuffixOption);
QCommandLineOption outputSpecularTextureOption(QStringList() << "c"
<< "specular",
"generate specular");
argsParser.addOption(outputSpecularTextureOption);
QCommandLineOption specularSuffixOption("specular-suffix", "Suffix for specular maps");
argsParser.addOption(specularSuffixOption);
QCommandLineOption outputOcclusionTextureOption(QStringList()
<< "o"
<< "occlusion",
"generate occlusion");
argsParser.addOption(outputOcclusionTextureOption);
QCommandLineOption occlusionSuffixOption("occlusion-suffix", "Suffix for occlusion maps");
argsParser.addOption(occlusionSuffixOption);
QCommandLineOption outputParallaxTextureOption(QStringList() << "p"
<< "parallax",
"generate parallax");
argsParser.addOption(outputParallaxTextureOption);
QCommandLineOption paralaxSuffixOption("paralax-suffix", "Suffix for paralax maps");
argsParser.addOption(paralaxSuffixOption);
QCommandLineOption pressetOption(QStringList() << "r"
<< "preset",
"presset to load", "preset file path");
argsParser.addOption(pressetOption);
QCommandLineOption outputLocationOption(QStringList() << "l"
<< "location",
"output directory", "output path");
argsParser.addOption(outputLocationOption);
QCommandLineOption notRecursiveOption("not-recursive", "don't look for images recursively");
argsParser.addOption(notRecursiveOption);
QCommandLineOption flattenOutputOption("flatten", "don't save in subdirectories");
argsParser.addOption(flattenOutputOption);
QCommandLineOption checkChangesOption("check-changes", "only generate maps if the maps are older than the diffuse");
argsParser.addOption(checkChangesOption);
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
fmt.setSamples(16);
fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(fmt);
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
argsParser.process(*app.data());
QImage auximage;
ImageProcessor *processor = new ImageProcessor();
bool success = false;
QString inputDiffuseTextureOptionValue =
argsParser.value(inputDiffuseTextureOption);
if (!inputDiffuseTextureOptionValue.trimmed().isEmpty())
{
QFileInfo info(inputDiffuseTextureOptionValue);
QStringList fileList;
QDir inputDir;
QString outPath = argsParser.value(outputLocationOption);
QDir outDir(outPath);
if (info.isFile())
{
fileList.append(inputDiffuseTextureOptionValue);
inputDir = QDir(info.absolutePath());
}
else if (info.isDir())
{
inputDir = QDir(inputDiffuseTextureOptionValue);
QDirIterator it(inputDir, argsParser.isSet(notRecursiveOption) ? QDirIterator::NoIteratorFlags : QDirIterator::Subdirectories);
while (it.hasNext())
{
QString found = it.next();
QFileInfo info(found);
if (info.isDir())
continue;
if (!outDir.relativeFilePath(found).startsWith(".."))
continue;
fileList.append(info.absoluteFilePath());
}
}
else
{
fileList.append(inputDiffuseTextureOptionValue.split(","));
// TODO: look for actual common prefix
inputDir = QDir(QFileInfo(fileList[0]).absolutePath());
}
QString pressetOptionValue = argsParser.value(pressetOption);
ImageLoader il;
bool has_normal, has_parallax, has_occlusion, has_specular;
has_normal = argsParser.isSet(outputNormalTextureOption);
has_parallax = argsParser.isSet(outputParallaxTextureOption);
has_occlusion = argsParser.isSet(outputOcclusionTextureOption);
has_specular = argsParser.isSet(outputSpecularTextureOption);
foreach (QString imagePath, fileList)
{
QFileInfo info = QFileInfo(imagePath);
QString suffix = info.suffix();
QDir outputDir = outPath.isEmpty() ? QDir(info.path()) : outDir;
QString relativeToInputPath = argsParser.isSet(flattenOutputOption) ? info.fileName() : inputDir.relativeFilePath(imagePath);
QString pathWithoutExtension = relativeToInputPath.remove("." + suffix);
bool changed = true;
if(argsParser.isSet(checkChangesOption))
{
changed = false;
QString typeSuffix = argsParser.isSet(normalSuffixOption) ? argsParser.value(normalSuffixOption) : "_n.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
QString name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
QFileInfo outInfo(name);
changed |= has_normal && CHECK_CHANGES(outInfo, info);
typeSuffix = argsParser.isSet(specularSuffixOption) ? argsParser.value(specularSuffixOption) : "_s.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
outInfo = QFileInfo(name);
changed |= has_specular && CHECK_CHANGES(outInfo, info);
typeSuffix = argsParser.isSet(occlusionSuffixOption) ? argsParser.value(specularSuffixOption) : "_o.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
outInfo = QFileInfo(name);
changed |= has_occlusion && CHECK_CHANGES(outInfo, info);
typeSuffix = argsParser.isSet(paralaxSuffixOption) ? argsParser.value(paralaxSuffixOption) : "_p.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
outInfo = QFileInfo(name);
changed |= has_parallax && CHECK_CHANGES(outInfo, info);
}
if (!changed)
{
continue;
}
auximage = il.loadImage(imagePath, &success);
if (!success) continue;
auximage = auximage.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
ImageProcessor processor;
if (!pressetOptionValue.trimmed().isEmpty())
{
processor.recalculate_timer.stop();
PresetsManager::applyPresets(pressetOptionValue, processor);
}
processor.has_normal = has_normal;
processor.has_occlusion = has_occlusion;
processor.has_parallax = has_parallax;
processor.has_specular = has_specular;
processor.loadImage(imagePath, auximage);
if (has_normal)
{
QString typeSuffix = argsParser.isSet(normalSuffixOption) ? argsParser.value(normalSuffixOption) : "_n.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
outputDir.mkpath(QFileInfo(relativeToInputPath).path());
QString name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
QFileInfo outFileInfo(name);
QImage normal = *processor.get_normal();
normal.save(name);
}
if (has_specular)
{
QString typeSuffix = argsParser.isSet(specularSuffixOption) ? argsParser.value(specularSuffixOption) : "_s.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
QImage specular = *processor.get_specular();
QString name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
specular.save(name);
}
if (has_occlusion)
{
QString typeSuffix = argsParser.isSet(occlusionSuffixOption) ? argsParser.value(occlusionSuffixOption) : "_o.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
QImage occlusion = *processor.get_occlusion();
QString name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
occlusion.save(name);
}
if (has_parallax)
{
QString typeSuffix = argsParser.isSet(paralaxSuffixOption) ? argsParser.value(paralaxSuffixOption) : "_p.";
if (!typeSuffix.endsWith("."))
typeSuffix += ".";
QImage parallax = *processor.get_parallax();
QString name = outputDir.filePath(pathWithoutExtension + typeSuffix + suffix);
parallax.save(name);
}
}
}
QApplication *a = qobject_cast<QApplication *>(app.data());
int returnCode;
if (a)
{
bool softOpenGlValue = argsParser.isSet(softOpenGl);
if (softOpenGlValue)
{
a->setAttribute(Qt::AA_UseSoftwareOpenGL);
}
MainWindow w;
QGuiApplication::setWindowIcon(QIcon(":/images/laigter_icon.png"));
w.show();
qRegisterMetaType<ProcessedImage>("ProcessedImage");
if (success)
w.add_processor(processor);
else
delete processor;
/* Load Project if dropped. Only supports one project */
if (argsParser.positionalArguments().count() > 0)
{
QString project_path = argsParser.positionalArguments().at(0);
w.LoadProject(project_path);
}
returnCode = app->exec();
}
else
{
// do CLI only things here
returnCode = 0;
}
return returnCode;
}