1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005                                                \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 #include <common/mlapplication.h>
24 #include <common/GLExtensionsManager.h>
25 #include <QMessageBox>
26 #include "mainwindow.h"
27 #include <QGLFormat>
28 #include <QString>
29 #include <clocale>
30 
31 #ifdef _WIN32
32 #include <windows.h>
33 #include <tchar.h>
34 #endif //_WIN32
35 
36 void handleCriticalError(const MLException& exc);
37 
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40 
41     MeshLabApplication app(argc, argv);
42     std::setlocale(LC_ALL, "C");
43     QLocale::setDefault(QLocale::C);
44     QCoreApplication::setOrganizationName(MeshLabApplication::organization());
45 #if QT_VERSION >= 0x050100
46     // Enable support for highres images (added in Qt 5.1, but off by default)
47     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
48 #endif
49 
50     QString tmp = MeshLabApplication::appArchitecturalName(MeshLabApplication::HW_ARCHITECTURE(QSysInfo::WordSize));
51     QCoreApplication::setApplicationName(MeshLabApplication::appArchitecturalName(MeshLabApplication::HW_ARCHITECTURE(QSysInfo::WordSize)));
52 
53     QGLFormat fmt = QGLFormat::defaultFormat();
54     fmt.setAlphaBufferSize(8);
55     QGLFormat::setDefaultFormat(fmt);
56 
57     GLExtensionsManager::init();
58     std::unique_ptr<MainWindow> window;
59     try {
60         window = std::unique_ptr<MainWindow>(new MainWindow());
61     }
62     catch (const MLException& exc) {
63         handleCriticalError(exc);
64         return -1;
65     }
66     window->showMaximized();
67 
68     // This event filter is installed to intercept the open events sent directly by the Operative System.
69     FileOpenEater *filterObj=new FileOpenEater(window.get());
70     app.installEventFilter(filterObj);
71     app.processEvents();
72 
73     // Can load multiple meshes and projects, and also a camera view
74     if(argc>1) {
75         QString helpOpt1="-h";
76         QString helpOpt2="--help";
77         if( (helpOpt1==argv[1]) || (helpOpt2==argv[1]) ) {
78             printf(
79                         "Usage:\n"
80                         "meshlab <meshfile>\n"
81                         "Look at http://www.meshlab.net\n"
82                         "for a longer documentation\n"
83                         );
84             return 0;
85         }
86 
87         std::vector<QString> cameraViews;
88         for (int i = 1; i < argc; ++i) {
89             QString arg = QString::fromLocal8Bit(argv[i]);
90             if(arg.endsWith("mlp",Qt::CaseInsensitive) || arg.endsWith("mlb",Qt::CaseInsensitive) || arg.endsWith("aln",Qt::CaseInsensitive) || arg.endsWith("out",Qt::CaseInsensitive) || arg.endsWith("nvm",Qt::CaseInsensitive))
91                 window->openProject(arg);
92             else if(arg.endsWith("xml",Qt::CaseInsensitive))
93                 cameraViews.push_back(arg);
94             else
95                 window->importMeshWithLayerManagement(arg);
96         }
97 
98         // Load the view after everything else
99         if (!cameraViews.empty()) {
100             if (cameraViews.size() > 1)
101                 printf("Multiple views specified. Loading the last one.\n");
102             window->readViewFromFile(cameraViews.back());
103         }
104     }
105     //else 	if(filterObj->noEvent) window.open();
106     return app.exec();
107 }
108 
handleCriticalError(const MLException & exc)109 void handleCriticalError(const MLException& exc){
110 
111     QString message = "MeshLab was not able to start.\n";
112     if (QString::fromStdString(exc.what()).contains("GLEW initialization failed")){
113         message.append("Please check your Graphics drivers.\n");
114     }
115     message.append("\nError: " + QString::fromStdString(exc.what()));
116 
117     QMessageBox messageBox(
118                 QMessageBox::Critical,
119                 "Critical Error",
120                 message);
121     messageBox.addButton(QMessageBox::Ok);
122 
123     #ifdef _WIN32
124     bool openGLProblem = QString::fromStdString(exc.what()).contains("GLEW initialization failed");
125     QCheckBox *cb = nullptr;
126     if (openGLProblem) {
127         cb = new QCheckBox("Use CPU OpenGL and restart MeshLab");
128         messageBox.setCheckBox(cb);
129     }
130     #endif //_WIN32
131 
132     messageBox.exec();
133 
134     #ifdef _WIN32
135     if (openGLProblem && cb->isChecked()) {
136         //start a new process "UseCPUOpenGL.exe" to copy opengl32.dll
137         SHELLEXECUTEINFO sei;
138 
139         ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
140 
141         sei.cbSize = sizeof(SHELLEXECUTEINFO);
142         sei.lpVerb = _T("runas");
143         sei.lpFile = _T("UseCPUOpenGL.exe"); //obviously not the actual file name
144                           //but it can be substituted and will work just fine in windows 7
145         sei.lpParameters = _T("1");
146 
147         ShellExecuteEx(&sei);
148     }
149     #endif //_WIN32
150 }
151