Skip to content

Commit

Permalink
Re #3746. A quick fix prventing calling a gl command outside OpenGL
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 14, 2011
1 parent 89987ea commit ff67170
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/Geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set ( SRC_FILES
src/Rendering/OCGeometryGenerator.cpp
src/Rendering/OCGeometryHandler.cpp
src/Rendering/OCGeometryRenderer.cpp
src/Rendering/OpenGLContext.cpp
src/Rendering/vtkGeometryCacheReader.cpp
src/Rendering/vtkGeometryCacheWriter.cpp
src/Surfaces/Cone.cpp
Expand Down Expand Up @@ -158,6 +159,7 @@ set ( INC_FILES
inc/MantidGeometry/Rendering/OCGeometryRenderer.h
inc/MantidGeometry/Rendering/OpenCascadeConfig.h
inc/MantidGeometry/Rendering/OpenGL_Headers.h
inc/MantidGeometry/Rendering/OpenGLContext.h
inc/MantidGeometry/Rendering/vtkGeometryCacheReader.h
inc/MantidGeometry/Rendering/vtkGeometryCacheWriter.h
inc/MantidGeometry/Surfaces/BaseVisit.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef OPENGL_CONTEXT_H
#define OPENGL_CONTEXT_H

#include "MantidGeometry/DllConfig.h"
#include "MantidKernel/SingletonHolder.h"
#include "MantidKernel/Logger.h"
namespace Mantid
{

namespace Geometry
{
/**
\class OpenGLContext
\brief Keep infomation about the status of OpenGL.
\author Roman Tolchenov, Tessella plc
\date 14/11/2011
\version 1.0
This is a singleton class to keep information about the current status of OpenGL.
The minimum information it should provide is whether OpenGL is avalable and it is safe and meaninful
to issue an gl command.
It is a quick fix solution to a problem that on some systems calling gl functions outside OpenGL context
can crash Mantid.
Copyright © 2008 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
*/
class MANTID_GEOMETRY_DLL OpenGLContextImpl
{
public:
/// True if OpenGL is avalable
bool isAvailable() const {return m_isOn;}
/// Set avalability of OpenGL. Must be updated by the GL widget.
void setAvalable(bool yes) {m_isOn = yes;}
private:
friend struct Mantid::Kernel::CreateUsingNew<OpenGLContextImpl>;

OpenGLContextImpl(); ///< Constructor
OpenGLContextImpl(const OpenGLContextImpl&); ///< Private copy constructor
OpenGLContextImpl& operator=(const OpenGLContextImpl&); ///< Private assignment operator
~OpenGLContextImpl(){} ///< Destructor

bool m_isOn; ///< flag indicating whether OpenGL is avalable or not
static Kernel::Logger& g_log; ///< The logger

};

///Forward declaration of a specialisation of SingletonHolder for OpenGLContextImpl (needed for dllexport/dllimport) and a typedef for it.
#ifdef _WIN32
// this breaks new namespace declaraion rules; need to find a better fix
template class MANTID_GEOMETRY_DLL Mantid::Kernel::SingletonHolder<OpenGLContextImpl>;
#endif /* _WIN32 */
typedef MANTID_GEOMETRY_DLL Mantid::Kernel::SingletonHolder<OpenGLContextImpl> OpenGLContext;

} // NAMESPACE Geometry

} // NAMESPACE Mantid

#endif // OPENGL_CONTEXT_H
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "MantidGeometry/Rendering/CacheGeometryRenderer.h"
#include "MantidGeometry/Rendering/OpenGL_Headers.h"
#include "MantidGeometry/Rendering/OpenGLContext.h"
#include "MantidGeometry/Instrument/ObjComponent.h"
#include "MantidKernel/Quat.h"
#include <climits>
Expand Down Expand Up @@ -29,8 +30,10 @@ namespace Mantid
*/
CacheGeometryRenderer::~CacheGeometryRenderer()
{
if (boolDisplaylistCreated && glIsList(iDisplaylistId) == GL_TRUE)
glDeleteLists(iDisplaylistId, 1);
if (OpenGLContext::Instance().isAvailable() && boolDisplaylistCreated && glIsList(iDisplaylistId) == GL_TRUE)
{
glDeleteLists(iDisplaylistId, 1);
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions Code/Mantid/Framework/Geometry/src/Rendering/OpenGLContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "MantidGeometry/Rendering/OpenGLContext.h"

namespace Mantid
{
namespace Geometry
{

Kernel::Logger& OpenGLContextImpl::g_log(Kernel::Logger::get("OpenGLContextImpl"));

OpenGLContextImpl::OpenGLContextImpl():
m_isOn(false)
{
}


} // Geometry
} // Mantid
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "OpenGLError.h"
#include "UnwrappedSurface.h"

#include "MantidGeometry/Rendering/OpenGLContext.h"

#include <boost/shared_ptr.hpp>

#include <QtOpenGL>
Expand Down Expand Up @@ -152,10 +154,12 @@ void MantidGLWidget::paintEvent(QPaintEvent *event)
{
UNUSED_ARG(event)
makeCurrent();
Mantid::Geometry::OpenGLContext::Instance().setAvalable(true);
if(m_surface)
{
m_surface->draw(this);
}
Mantid::Geometry::OpenGLContext::Instance().setAvalable(false);

//swapBuffers();
OpenGLError::check("paintEvent");
Expand Down

0 comments on commit ff67170

Please sign in to comment.