Move context creation to qglviewer. Replace initializeGL() by init()in demo viewer

This commit is contained in:
Maxime Gimeno 2018-03-28 10:54:43 +02:00
parent 4c6546a558
commit 50c471253a
5 changed files with 58 additions and 147 deletions

View File

@ -965,7 +965,7 @@ public Q_SLOTS:
void setSelectedName(int id) { selectedObjectId_ = id; }
protected:
virtual void beginSelection(const QPoint &);
virtual void beginSelection(const QPoint &point);
/*! This method is called by select() and should draw selectable entities.
Default implementation is empty. Overload and draw the different elements of
@ -1477,7 +1477,7 @@ private:
QString snapshotFileName_, snapshotFormat_;
int snapshotCounter_, snapshotQuality_;
TileRegion *tileRegion_;
// Q G L V i e w e r p o o l
static QList<QGLViewer *> QGLViewerPool_;
@ -1515,6 +1515,14 @@ private:
// O f f s e t
qglviewer::Vec _offset;
//C o n t e x t
bool is_ogl_4_3;
public:
//! Is used to know if the openGL context is 4.3 or 2.1.
//! @returns `true` if the context is 4.3.
//! @returns `false` if the context is 2.1.
bool isOpenGL_4_3()const {return is_ogl_4_3; }
};
#endif // QGLVIEWER_QGLVIEWER_H

View File

@ -220,39 +220,42 @@ static Qt::MouseButton mouseButtonFromState(unsigned int state) {
This method is automatically called once, before the first call to paintGL().
Overload init() instead of this method to modify viewer specific OpenGL state or
to create display lists.
Overload init() instead of this method to modify viewer specific OpenGL state.
To make beginners' life easier and to simplify the examples, this method
slightly modifies the standard OpenGL state: \code glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
\endcode
If you port an existing application to QGLViewer and your display changes, you
probably want to disable these flags in init() to get back to a standard OpenGL
state. */
If a 4.3 context could not be set, a 2.1 context will be used instead.
\see `isOpenGL_4_3()`
*/
void QGLViewer::initializeGL() {
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(4,3);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setSamples(0);
context()->setFormat(format);
bool created = context()->create();
if(!created || context()->format().profile() != QSurfaceFormat::CompatibilityProfile) {
// impossible to get a 4.3 compatibility profile, retry with 2.0
format.setVersion(2,1);
context()->setFormat(format);
created = context()->create();
is_ogl_4_3 = false;
}
else
{
is_ogl_4_3 = true;
}
makeCurrent();
QOpenGLFunctions_2_1::initializeOpenGLFunctions();
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
// Default colors
setForegroundColor(QColor(180, 180, 180));
setBackgroundColor(QColor(51, 51, 51));
// Clear the buffer where we're going to draw
if (format().stereo()) {
glDrawBuffer(GL_BACK_RIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawBuffer(GL_BACK_LEFT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} else
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Calls user defined method. Default emits a signal.
init();
//OpenGL buffers and programs initialization
for(int i=0; i<VAO_size; ++i)
{
@ -400,9 +403,6 @@ void QGLViewer::initializeGL() {
qDebug() << rendering_program_light.log();
}
}
// Calls user defined method. Default emits a signal.
init();
// Give time to glInit to finish and then call setFullScreen().
if (isFullScreen())
@ -1111,80 +1111,20 @@ void QGLViewer::select(const QPoint &point) {
/*! This method should prepare the selection. It is called by select() before
drawWithNames().
The default function is empty.
*/
void QGLViewer::beginSelection(const QPoint &)
void QGLViewer::beginSelection(const QPoint &point)
{
makeCurrent();
glEnable(GL_SCISSOR_TEST);
glScissor(point.x(), camera()->screenHeight()-1-point.y(), 1, 1);
}
/*! This method is called by select() after scene elements were drawn by
drawWithNames(). It should analyze the selection result to determine which
object is actually selected.
The default implementation relies on \c GL_SELECT mode (see beginSelection()).
It assumes that names were pushed and popped in drawWithNames(), and analyzes
the selectBuffer() to find the name that corresponds to the closer (z min)
object. It then setSelectedName() to this value, or to -1 if the selectBuffer()
is empty (no object drawn in selection region). Use selectedName() (probably in
the postSelection() method) to retrieve this value and update your data
structure accordingly.
This default implementation, although sufficient for many cases is however
limited and you may have to overload this method. This will be the case if
drawWithNames() uses several push levels in the name heap. A more precise depth
selection, for instance privileging points over edges and triangles to avoid z
precision problems, will also require an overloading. A typical implementation
will look like:
\code
glFlush();
// Get the number of objects that were seen through the pick matrix frustum.
// Resets GL_RENDER mode.
GLint nbHits = glRenderMode(GL_RENDER);
if (nbHits <= 0)
setSelectedName(-1);
else
{
// Interpret results: each object created values in the selectBuffer().
// See the glSelectBuffer() man page for details on the buffer structure.
// The following code depends on your selectBuffer() structure.
for (int i=0; i<nbHits; ++i)
if ((selectBuffer())[i*4+1] < zMin)
setSelectedName((selectBuffer())[i*4+3])
}
\endcode
See the <a href="../examples/multiSelect.html">multiSelect example</a> for
a multi-object selection implementation of this method. */
drawWithNames().
It clears the OpenGL state set by beginSelection*/
void QGLViewer::endSelection(const QPoint &point) {
Q_UNUSED(point);
// Flush GL buffers
glFlush();
// Get the number of objects that were seen through the pick matrix frustum.
// Reset GL_RENDER mode.
GLint nbHits = glRenderMode(GL_RENDER);
if (nbHits <= 0)
setSelectedName(-1);
else {
// Interpret results: each object created 4 values in the selectBuffer().
// selectBuffer[4*i+1] is the object minimum depth value, while
// selectBuffer[4*i+3] is the id pushed on the stack. Of all the objects
// that were projected in the pick region, we select the closest one (zMin
// comparison). This code needs to be modified if you use several stack
// levels. See glSelectBuffer() man page.
GLuint zMin = (selectBuffer())[1];
setSelectedName(int((selectBuffer())[3]));
for (int i = 1; i < nbHits; ++i)
if ((selectBuffer())[4 * i + 1] < zMin) {
zMin = (selectBuffer())[4 * i + 1];
setSelectedName(int((selectBuffer())[4 * i + 3]));
}
}
glDisable(GL_SCISSOR_TEST);
}
/*! Sets the selectBufferSize().

View File

@ -37,7 +37,6 @@ public:
bool _displayMessage;
QTimer messageTimer;
QOpenGLFunctions_4_3_Compatibility* _recentFunctions;
bool is_ogl_4_3;
bool is_2d_selection_mode;
//! The buffers used to draw the axis system
@ -195,36 +194,17 @@ void Viewer::fastDraw()
d->draw_aux(false, this);
}
void Viewer::initializeGL()
void Viewer::init()
{
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(4,3);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setSamples(0);
context()->setFormat(format);
bool created = context()->create();
if(!created || context()->format().profile() != QSurfaceFormat::CompatibilityProfile) {
// impossible to get a 4.3 compatibility profile, retry with 2.0
format.setVersion(2,1);
context()->setFormat(format);
created = context()->create();
d->is_ogl_4_3 = false;
if(!isOpenGL_4_3())
{
std::cerr<<"The openGL context initialization failed "
"and the default context (2.1) will be used" <<std::endl;
}
else
{
d->is_ogl_4_3 = true;
d->_recentFunctions = new QOpenGLFunctions_4_3_Compatibility();
}
CGAL_warning_msg(created && context()->isValid(), "The openGL context initialization failed "
"and the default context (2.0) will be used" );
makeCurrent();
QGLViewer::initializeGL();
initializeOpenGLFunctions();
if(isOpenGL_4_3())
{
d->_recentFunctions->initializeOpenGLFunctions();
d->_recentFunctions->initializeOpenGLFunctions();
}
glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDARBPROC)this->context()->getProcAddress("glDrawArraysInstancedARB");
if(!glDrawArraysInstanced)
@ -247,14 +227,12 @@ void Viewer::initializeGL()
setBackgroundColor(::Qt::white);
d->vao.create();
d->buffer.create();
d->buffer.create();
QOpenGLShader *vertex_shader, *fragment_shader;
//setting the program used for the distance
{
d->vao.create();
d->buffer.create();
//Vertex source code
const char vertex_source_dist[] =
{
@ -680,14 +658,12 @@ void Viewer::attribBuffers(int program_name) const {
void Viewer::beginSelection(const QPoint &point)
{
makeCurrent();
glEnable(GL_SCISSOR_TEST);
glScissor(point.x(), camera()->screenHeight()-1-point.y(), 1, 1);
d->scene->setPickedPixel(point);
QGLViewer::beginSelection(point);
d->scene->setPickedPixel(point);
}
void Viewer::endSelection(const QPoint&)
void Viewer::endSelection(const QPoint& point)
{
glDisable(GL_SCISSOR_TEST);
QGLViewer::endSelection(point);
//redraw the true scene for the glReadPixel in postSelection();
d->draw_aux(false, this);
}
@ -745,10 +721,6 @@ void Viewer::drawVisualHints()
d->textRenderer->removeText(message_text);
}
void Viewer::resizeGL(int w, int h)
{
QGLViewer::resizeGL(w,h);
}
QOpenGLShaderProgram* Viewer::declare_program(int name,
const char* v_shader,
const char* f_shader) const
@ -1249,9 +1221,6 @@ void Viewer::enableClippingBox(QVector4D box[6])
d->clipbox[i] = box[i];
}
bool Viewer::isOpenGL_4_3() const { return d->is_ogl_4_3; }
QOpenGLFunctions_4_3_Compatibility* Viewer::openGL_4_3_functions() { return d->_recentFunctions; }
void Viewer::set2DSelectionMode(bool b) { d->is_2d_selection_mode = b; }

View File

@ -47,7 +47,7 @@ public:
void fastDraw()Q_DECL_OVERRIDE;
bool isExtensionFound()Q_DECL_OVERRIDE;
//! Initializes the OpenGL functions and sets the backGround color.
void initializeGL()Q_DECL_OVERRIDE;
void init()Q_DECL_OVERRIDE;
//! Draws the scene "with names" to allow picking.
void drawWithNames()Q_DECL_OVERRIDE;
/*! Uses the parameter pixel's coordinates to get the corresponding point
@ -142,15 +142,12 @@ protected:
//!Defines the behaviour for the key release events
void keyReleaseEvent(QKeyEvent *)Q_DECL_OVERRIDE;
void resizeGL(int w, int h)Q_DECL_OVERRIDE;
protected:
friend class Viewer_impl;
Viewer_impl* d;
double prev_radius;
public:
bool isOpenGL_4_3() const Q_DECL_OVERRIDE;
QOpenGLFunctions_4_3_Compatibility* openGL_4_3_functions() Q_DECL_OVERRIDE;
}; // end class Viewer

View File

@ -249,10 +249,7 @@ public Q_SLOTS:
virtual bool moveCameraToCoordinates(QString target,
float animation_duration = 0.5f) = 0;
public:
//! Is used to know if the openGL context is 4.3 or 2.1.
//! @returns `true` if the context is 4.3.
//! @returns `false` if the context is 2.1.
virtual bool isOpenGL_4_3() const = 0;
//! Gives acces to recent openGL(4.3) features, allowing use of things like
//! Geometry Shaders or Depth Textures.
//! @returns a pointer to an initialized QOpenGLFunctions_4_3_Compatibility if `isOpenGL_4_3()` is `true`