Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QOpenGLWidget WIP #379

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/Core/RenderResources/RenderResources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class RenderResourcesPrivate
void RenderResourcesPrivate::initialize_gl_log_error(const std::string& error_string)
{
this->gl_capable_ = false;
std::cout << error_string << "\n";
CORE_LOG_ERROR(error_string);
}

Expand Down Expand Up @@ -311,18 +312,19 @@ bool RenderResources::valid_render_resources()
{
if (!this->private_->gl_capable_)
{
std::cout << "not gl capable\n";
return false;
}

while (this->private_->wait_for_render_resources())
{
#ifdef LOG_RENDER_RESOURCE_INIT
//#ifdef LOG_RENDER_RESOURCE_INIT
std::cout << "Waiting..." << std::endl;
std::cout << std::boolalpha << "resources context: " << (this->private_->resources_context_ != nullptr) << " "
<< "valid render resources: " << this->private_->resources_context_->valid_render_resources() << " "
<< "delete context: " << (this->private_->delete_context_ != nullptr) << " "
<< "GL capable: " << this->private_->gl_capable_ << std::endl;
#endif
std::cout << std::boolalpha << "resources context: " << (this->private_->resources_context_ != nullptr) << "\n"
<< "valid render resources: " << this->private_->resources_context_->valid_render_resources() << "\n"
<< "delete context: " << (this->private_->delete_context_ != nullptr) << "\n"
<< "GL capable: " << this->private_->gl_capable_ << "\n";
//#endif

boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
Expand All @@ -343,6 +345,7 @@ void RenderResources::initialize_on_event_thread()

boost::unique_lock< boost::mutex > lock( this->private_->thread_mutex_ );
this->private_->resources_context_->create_render_context( this->private_->delete_context_ );

this->private_->delete_context_->make_current();
this->private_->initialize_gl();
this->private_->query_video_memory_size();
Expand Down
2 changes: 1 addition & 1 deletion src/Interface/Application/ViewerWidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void ViewerWidget::image_mode( bool picture )
if( picture )
{
this->private_->facade_widget_->setMinimumSize( this->private_->render_widget_->size() );
this->private_->facade_widget_->setPixmap( QPixmap::fromImage( this->private_->render_widget_->grabFrameBuffer() ) );
this->private_->facade_widget_->setPixmap( QPixmap::fromImage(this->private_->render_widget_->grabFramebuffer()));
this->private_->render_widget_->hide();
this->private_->facade_widget_->show();
}
Expand Down
67 changes: 41 additions & 26 deletions src/QtUtils/Utils/QtRenderResources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ 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< QGLContext > QGLContextHandle;
typedef boost::shared_ptr< QOpenGLContext > QOpenGLContextHandle;

class QtRenderContext : public Core::RenderContext
{
// -- constructor/ destructor --
public:
QtRenderContext( QGLContextHandle context );
QtRenderContext( QOpenGLContextHandle context );
virtual ~QtRenderContext();

// -- context functions --
Expand All @@ -74,23 +74,29 @@ class QtRenderContext : public Core::RenderContext
virtual void swap_buffers() const;

private:
QGLContextHandle context_;
QOpenGLContextHandle context_;
QOffscreenSurface surface_;
};

class QtRenderResourcesContextPrivate
{
public:
// The Qt render context format options
QGLFormat format_;
QSurfaceFormat format_;

// The handle to the first qt widget that defines all the sharing
// between contexts
QGLWidget* shared_widget_;
QOpenGLContext* shared_context_;
};

QtRenderContext::QtRenderContext( QGLContextHandle context ) :
QtRenderContext::QtRenderContext( QOpenGLContextHandle context ) :
context_( context )
{

context_->create();

surface_.setFormat(QSurfaceFormat::defaultFormat());
surface_.create();
}

QtRenderContext::~QtRenderContext()
Expand All @@ -104,7 +110,9 @@ bool QtRenderContext::is_valid() const

void QtRenderContext::make_current()
{
context_->makeCurrent();
//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()
Expand All @@ -114,7 +122,8 @@ void QtRenderContext::done_current()

void QtRenderContext::swap_buffers() const
{
context_->swapBuffers();
std::cout << "no implementation\n";
//context_->swapBuffers();
}


Expand All @@ -124,47 +133,53 @@ void QtRenderContext::swap_buffers() const
QtRenderResourcesContext::QtRenderResourcesContext() :
private_( new QtRenderResourcesContextPrivate )
{
this->private_->format_ = QGLFormat::defaultFormat();
this->private_->shared_widget_ = new QGLWidget( this->private_->format_ );
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_widget_;
delete this->private_->shared_context_;
}

bool QtRenderResourcesContext::create_render_context( Core::RenderContextHandle& context )
{
// Generate a new context
QGLContextHandle qt_context = QGLContextHandle( new QGLContext( this->private_->format_,
this->private_->shared_widget_->context()->device() ) );
qt_context->create( this->private_->shared_widget_->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 ) );
context = Core::RenderContextHandle(new QtRenderContext(qt_context));

return context->is_valid();
}

QtRenderWidget* QtRenderResourcesContext::create_qt_render_widget( QWidget* parent,
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_widget_, viewer );
return new QtRenderWidget(
this->private_->format_, parent,
this->private_->shared_context_, viewer );
}

QtTransferFunctionWidget* QtRenderResourcesContext::create_qt_transfer_function_widget(
QtTransferFunctionWidget* QtRenderResourcesContext::create_qt_transfer_function_widget(
QWidget* parent, Core::TransferFunctionHandle tf )
{
return new QtTransferFunctionWidget( this->private_->format_, parent,
this->private_->shared_widget_, tf );
return new QtTransferFunctionWidget(
this->private_->format_, parent,
this->private_->shared_context_, tf );
}

bool QtRenderResourcesContext::valid_render_resources()
{
return this->private_->shared_widget_->isValid();
return this->private_->shared_context_->isValid();
}

class SharedPtrNopDeleter
Expand All @@ -178,15 +193,15 @@ class SharedPtrNopDeleter

Core::RenderContextHandle QtRenderResourcesContext::get_current_context()
{
const QGLContext* current_context = QGLContext::currentContext();
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( QGLContextHandle(
const_cast< QGLContext* >( current_context ), SharedPtrNopDeleter() ) ) );
return Core::RenderContextHandle( new QtRenderContext( QOpenGLContextHandle(
const_cast< QOpenGLContext* >( current_context ), SharedPtrNopDeleter() ) ) );
}

return Core::RenderContextHandle();
}

Expand Down
28 changes: 20 additions & 8 deletions src/QtUtils/Utils/QtRenderWidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ void QtRenderWidgetPrivate::exit_size_move()
// Class QtRenderWidget
//////////////////////////////////////////////////////////////////////////

QtRenderWidget::QtRenderWidget( const QGLFormat& format, QWidget* parent,
QGLWidget* share, Core::AbstractViewerHandle viewer ) :
QGLWidget( format, parent, share ),
QtRenderWidget::QtRenderWidget( const QSurfaceFormat& format, QWidget* parent,
QOpenGLContext* share, Core::AbstractViewerHandle viewer ) :
QOpenGLWidget(parent),
private_( new QtRenderWidgetPrivate )
{
this->setFormat(format);
this->share=share;

this->private_->viewer_ = viewer;
this->private_->render_widget_ = this;
this->private_->in_size_move_ = false;
Expand Down Expand Up @@ -120,34 +123,43 @@ static void UpdateDisplay( QtRenderWidgetWeakHandle qpointer )

if ( !qpointer.isNull() && !QCoreApplication::closingDown() )
{
qpointer->updateGL();
qpointer->update();
}
}

void QtRenderWidget::initializeGL()
{
glClearColor( 0.3f, 0.3f, 0.3f, 1.0f );
this->context()->setShareContext(share);

glClearColor( 1.0f, 0.3f, 1.0f, 1.0f );

if ( Core::RenderResources::Instance()->valid_render_resources() )
{
std::cout << "RenderResources valid\n";
Core::Texture::SetActiveTextureUnit( 0 );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );

QtRenderWidgetWeakHandle qpointer( this );
this->add_connection( this->private_->viewer_->update_display_signal_.connect(
boost::bind( &UpdateDisplay, qpointer ) ) );
}
else
{
std::cout << "RenderResources not valid\n";
}
}

void QtRenderWidget::paintGL()
{
std::cout << "paintGL\n";
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

if ( !Core::RenderResources::Instance()->valid_render_resources() )
{
glColor3f( 1.0f, 0.5f, 0.0f );
QString error_str( "Rendering disabled because the system doesn't have required OpenGL support." );
this->renderText( 5, 20, error_str );
//TODO update this to work with QOpenGLWidget
//this->renderText( 5, 20, error_str );
return;
}

Expand Down Expand Up @@ -334,8 +346,8 @@ void QtRenderWidget::saveOverlayOnly(const std::string& name)
void QtRenderWidget::saveComposite(const std::string& name)
{
// Force a normal rendering (compositing) of the two textures.
glDraw();
QImage img = grabFrameBuffer();
update();
QImage img = grabFramebuffer();
img.save(name.c_str());
}

Expand Down
Loading