1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "lightmapfile.h"
5#include "lightmapimageprovider.h"
6#include "lightmapviewerhelpers.h"
7
8#include <QCoreApplication>
9#include <QGuiApplication>
10#include <QQmlApplicationEngine>
11#include <QCommandLineParser>
12#include <QCommandLineOption>
13#include <QDebug>
14
15int main(int argc, char *argv[])
16{
17 QString input;
18
19 // We Handle all the command line parts first in its own QCoreApplication
20 // since we don't want to create a gui application which would not work if
21 // running through ssh for instance.
22 {
23 QCoreApplication app(argc, argv);
24 QCoreApplication::setApplicationName("Qt LightmapViewer");
25 QCoreApplication::setApplicationVersion("1.0");
26
27 QCommandLineParser parser;
28 parser.setApplicationDescription("A tool for viewing Qt's baked lightmaps");
29 parser.addHelpOption();
30 parser.addVersionOption();
31
32 QCommandLineOption printOption("print", "Print the content of the lightmap");
33 QCommandLineOption extractOption("extract", "Extract the lightmap contents into the current working directory");
34 parser.addOption(commandLineOption: printOption);
35 parser.addOption(commandLineOption: extractOption);
36
37 parser.addPositionalArgument(name: "input", description: "Optional path to lightmap");
38
39 parser.process(app);
40
41 bool doPrint = parser.isSet(option: printOption);
42 bool doExtract = parser.isSet(option: extractOption);
43
44 const QStringList positionalArgs = parser.positionalArguments();
45 input = positionalArgs.isEmpty() ? QString() : positionalArgs.first();
46
47 if (doPrint && input.isEmpty()) {
48 qFatal() << "Print option selected with no lightmap.";
49 return 1;
50 }
51
52 if (doExtract && input.isEmpty()) {
53 qFatal() << "Extract option selected with no lightmap.";
54 return 1;
55 }
56
57 if (doPrint || doExtract)
58 return !LightmapViewerHelpers::processLightmap(filename: input, print: doPrint, extract: doExtract);
59 }
60
61 QGuiApplication app(argc, argv);
62 QQmlApplicationEngine engine;
63 QObject::connect(sender: &engine, signal: &QQmlApplicationEngine::objectCreationFailed, context: &app, slot: []() { QCoreApplication::exit(retcode: -1); }, type: Qt::QueuedConnection);
64
65 LightmapFile *file = new LightmapFile();
66 if (!input.isEmpty())
67 file->setSource(QUrl::fromLocalFile(localfile: input));
68 file->loadData();
69 qmlRegisterSingletonInstance(uri: "LightmapFile", versionMajor: 1, versionMinor: 0, typeName: "LightmapFile", cppObject: file);
70
71 LightmapImageProvider *provider = new LightmapImageProvider;
72 engine.addImageProvider(id: QLatin1String("lightmaps"), provider);
73 engine.loadFromModule(uri: "QtQuick3D.lightmapviewer", typeName: "LightmapViewer");
74
75 return app.exec();
76}
77

source code of qtquick3d/tools/lightmapviewer/lightmapviewer.cpp