-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathQtRenderResources.cc
208 lines (164 loc) · 5.85 KB
/
QtRenderResources.cc
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
/*
For more information, please see: http://software.sci.utah.edu
The MIT License
Copyright (c) 2016 Scientific Computing and Imaging Institute,
University of Utah.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <Core/Utils/StringUtil.h>
#include <Core/Utils/Exception.h>
#include <Core/Interface/Interface.h>
// QtUtils includes
#include <QtUtils/Utils/QtRenderResources.h>
namespace QtUtils
{
// CLASS: QtRenderContext
// Forward declarations
// The QtRenderContext class is an implementation of the openGL context
// on top of the abstract interface that is used for managing OpenGL contexts
class QtRenderContext;
typedef boost::shared_ptr< QtRenderContext > QtRenderContextHandle;
// Shared pointer to one of Qt's internal resources
// NOTE: As GLContext objects are not managed by Qt we
// need to do this ourselves using a smart pointer
typedef boost::shared_ptr< QOpenGLContext > QOpenGLContextHandle;
class QtRenderContext : public Core::RenderContext
{
// -- constructor/ destructor --
public:
QtRenderContext( QOpenGLContextHandle context );
virtual ~QtRenderContext();
// -- context functions --
// IS_VALID:
// Test whether the context is valid
virtual bool is_valid() const;
// MAKE_CURRENT:
// Set the rendering context current to this thread
virtual void make_current();
// DONE_CURRENT:
// Indicate that rendering using this context is done for now
virtual void done_current();
// SWAP_BUFFERS:
// Swap the front and back buffers
virtual void swap_buffers() const;
private:
QOpenGLContextHandle context_;
QOffscreenSurface surface_;
};
class QtRenderResourcesContextPrivate
{
public:
// The Qt render context format options
QSurfaceFormat format_;
// The handle to the first qt widget that defines all the sharing
// between contexts
QOpenGLContext* shared_context_;
};
QtRenderContext::QtRenderContext( QOpenGLContextHandle context ) :
context_( context )
{
context_->create();
surface_.setFormat(QSurfaceFormat::defaultFormat());
surface_.create();
}
QtRenderContext::~QtRenderContext()
{
}
bool QtRenderContext::is_valid() const
{
return ( context_->isValid() );
}
void QtRenderContext::make_current()
{
//sort of a hack to allow resources to be inistilized before widget surface is created
if(context_->surface()) context_->makeCurrent(context_->surface());
else context_->makeCurrent(&surface_);
}
void QtRenderContext::done_current()
{
context_->doneCurrent();
}
void QtRenderContext::swap_buffers() const
{
std::cout << "no implementation\n";
//context_->swapBuffers();
}
// CLASS: QtRenderResourcesContext
// Implementation details of this class
QtRenderResourcesContext::QtRenderResourcesContext() :
private_( new QtRenderResourcesContextPrivate )
{
this->private_->format_ = QSurfaceFormat::defaultFormat();
this->private_->shared_context_ = new QOpenGLContext();
this->private_->shared_context_->setFormat(this->private_->format_);
this->private_->shared_context_->create();
}
QtRenderResourcesContext::~QtRenderResourcesContext()
{
delete this->private_->shared_context_;
}
bool QtRenderResourcesContext::create_render_context( Core::RenderContextHandle& context )
{
// Generate a new context
QOpenGLContextHandle qt_context = QOpenGLContextHandle(new QOpenGLContext());
qt_context->setFormat(this->private_->format_);
qt_context->setShareContext(this->private_->shared_context_);
qt_context->create();
// Bind the new context in the GUI independent wrapper class
context = Core::RenderContextHandle(new QtRenderContext(qt_context));
return context->is_valid();
}
QtRenderWidget* QtRenderResourcesContext::create_qt_render_widget( QWidget* parent,
Core::AbstractViewerHandle viewer )
{
CORE_LOG_DEBUG( "Create an OpenGL widget" );
return new QtRenderWidget(
this->private_->format_, parent,
this->private_->shared_context_, viewer );
}
QtTransferFunctionWidget* QtRenderResourcesContext::create_qt_transfer_function_widget(
QWidget* parent, Core::TransferFunctionHandle tf )
{
return new QtTransferFunctionWidget(
this->private_->format_, parent,
this->private_->shared_context_, tf );
}
bool QtRenderResourcesContext::valid_render_resources()
{
return this->private_->shared_context_->isValid();
}
class SharedPtrNopDeleter
{
public:
template< class T >
void operator()( T* p )
{
}
};
Core::RenderContextHandle QtRenderResourcesContext::get_current_context()
{
const QOpenGLContext* current_context = QOpenGLContext::currentContext();
if ( current_context != 0 )
{
// NOTE: We don't want to delete the context after the handle goes out of scope,
// so we pass a NOP deleter to shared_ptr
return Core::RenderContextHandle( new QtRenderContext( QOpenGLContextHandle(
const_cast< QOpenGLContext* >( current_context ), SharedPtrNopDeleter() ) ) );
}
return Core::RenderContextHandle();
}
} // end namespace Seg3D