main.cpp 5.3 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

/**
 * Author: Hossain Mahmud <mahmud@in.tum.de>
 * Date: March 2014
 */


31
32
#include "gtest/gtest.h"
#include <stdio.h>
33
34
35
#include <QApplication>

#include "sigslot/sigslot.h"
36

37
38
39
40
41
42
43
#include "cgt/glcontextmanager.h"
#include "cgt/gpucapabilities.h"
#include "cgt/init.h"
#include "cgt/logmanager.h"
#include "cgt/opengljobprocessor.h"
#include "cgt/shadermanager.h"
#include "cgt/qt/qtthreadedcanvas.h"
44

45
#include "core/tools/simplejobprocessor.h"
46
47
#include "core/tools/quadrenderer.h"

48
49
50
#ifdef Q_WS_X11
#include <X11/Xlib.h>
#endif
51
52
53
54
55
56
57
58

QApplication *app;
/// Flag, whether CampVisApplication was correctly initialized
bool _initialized;
/// A local OpenGL context used for initialization
tgt::GLCanvas* _localContext = nullptr;
static const std::string loggerCat_;

59
60

void init() {
61
62
63
    // Make Xlib and GLX thread safe under X11
    QApplication::setAttribute(Qt::AA_X11InitThreads);

64
65
66
67
    sigslot::signal_manager::init();
    sigslot::signal_manager::getRef().setSignalHandlingMode(sigslot::signal_manager::FORCE_DIRECT);
    sigslot::signal_manager::getRef().start();

68
    tgt::GlContextManager::init();
69

70
    tgt::OpenGLJobProcessor::init();
71
    campvis::SimpleJobProcessor::init();
72

73
74
    tgtAssert(_initialized == false, "Tried to initialize CampVisApplication twice.");
    
75
76
77
    // Init TGT
    tgt::InitFeature::Features featureset = tgt::InitFeature::ALL;
    tgt::init(featureset);
78
    LogMgr.getConsoleLog()->addCat("", true, tgt::Info);
79
80

    // create a local OpenGL context and init GL
81
82
83
84
85
    tgt::QtThreadedCanvas* backgroundCanvas = new tgt::QtThreadedCanvas("", tgt::ivec2(16, 16));
    GLCtxtMgr.registerContextAndInitGlew(backgroundCanvas, "Background Context");
    GLCtxtMgr.releaseContext(backgroundCanvas, false);
    GLJobProc.setContext(backgroundCanvas);
    GLJobProc.start();
86
    
87
88
    _localContext = new tgt::QtThreadedCanvas("", tgt::ivec2(16, 16));
    tgt::GlContextManager::getRef().registerContextAndInitGlew(_localContext, "Local Context");
89
90
91
92
93
94

    tgt::initGL(featureset);
    ShdrMgr.setDefaultGlslVersion("330");

    campvis::QuadRenderer::init();

Hossain Mahmud's avatar
Hossain Mahmud committed
95
96
97
98
99
100
101
102
#ifdef CAMPVIS_SOURCE_DIR
    {
        std::string sourcePath = CAMPVIS_SOURCE_DIR;
        ShdrMgr.addPath(sourcePath);
        ShdrMgr.addPath(sourcePath + "/core/glsl");
    }
#endif

103
104
105
106
107
108
109
110
111
    // ensure matching OpenGL specs
    LINFO("Using Graphics Hardware " << GpuCaps.getVendorAsString() << " " << GpuCaps.getGlRendererString() << " on " << GpuCaps.getOSVersionString());
    LINFO("Supported OpenGL " << GpuCaps.getGlVersion() << ", GLSL " << GpuCaps.getShaderVersion());
    if (GpuCaps.getGlVersion() < tgt::GpuCapabilities::GlVersion::TGT_GL_VERSION_3_3) {
        LERROR("Your system does not support OpenGL 3.3, which is mandatory. CAMPVis will probably not work as intended.");
    }
    if (GpuCaps.getShaderVersion() < tgt::GpuCapabilities::GlVersion::SHADER_VERSION_330) {
        LERROR("Your system does not support GLSL Shader Version 3.30, which is mandatory. CAMPVis will probably not work as intended.");
    }
112
113
114

    GLCtxtMgr.releaseContext(_localContext, false);

115
    _initialized = true;
116
117
118
}

void deinit() {
119
120
121
122
123
124
125
126
    tgtAssert(_initialized, "Tried to deinitialize uninitialized CampVisApplication.");

    {
        // Deinit everything OpenGL related using the local context.
        tgt::GLContextScopedLock lock(_localContext);

        campvis::QuadRenderer::deinit();

127
128
129
130
        campvis::SimpleJobProcessor::deinit();
        GLJobProc.stop();
        tgt::OpenGLJobProcessor::deinit();

131
132
133
134
        tgt::deinitGL();
    }


135
    tgt::GlContextManager::deinit();
136
137
    tgt::deinit();

138
139
140
    sigslot::signal_manager::getRef().stop();
    sigslot::signal_manager::deinit();

141
    _initialized = false;
142
143
144
145
}

GTEST_API_ int main(int argc, char **argv) {
    printf("Running main() from main.cpp\n");
146
147
148
149
150

#ifdef Q_WS_X11
    XInitThreads();
#endif

151
    app = new QApplication(argc, argv);
152
153
154
155

    // Make Xlib and GLX thread safe under X11
    QApplication::setAttribute(Qt::AA_X11InitThreads);

156
    testing::InitGoogleTest(&argc, argv);
157
    int ret;
158

159
160
161
162
    init();
    {
        tgt::GLContextScopedLock lock(_localContext);
        ret= RUN_ALL_TESTS();
163

164
    }
165
166
    deinit();

167
168
    delete app;
    printf("main() returned with %d\n", ret);
169
170
    return 0;
}