Skip to content

Commit 93045f9

Browse files
committed
QtQuick sample - fix first redraw call
1 parent 7364b37 commit 93045f9

8 files changed

Lines changed: 108 additions & 66 deletions

File tree

occt-qopenglwidget/OcctQOpenGLWidgetViewer.cpp

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,10 @@ OcctQOpenGLWidgetViewer::OcctQOpenGLWidgetViewer(QWidget* theParent)
108108
setUpdatesEnabled(true);
109109
setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
110110

111-
// OpenGL setup managed by Qt
112-
QSurfaceFormat aGlFormat;
113-
aGlFormat.setDepthBufferSize(24);
114-
aGlFormat.setStencilBufferSize(8);
115-
// aGlFormat.setOption (QSurfaceFormat::DebugContext, true);
116-
aDriver->ChangeOptions().contextDebug = aGlFormat.testOption(QSurfaceFormat::DebugContext);
117-
// aGlFormat.setOption (QSurfaceFormat::DeprecatedFunctions, true);
118-
if (myIsCoreProfile)
119-
aGlFormat.setVersion(4, 5);
120-
121-
aGlFormat.setProfile(myIsCoreProfile ? QSurfaceFormat::CoreProfile : QSurfaceFormat::CompatibilityProfile);
122-
123-
// request sRGBColorSpace colorspace to meet OCCT expectations or use OcctQtFrameBuffer fallback.
124-
/*#if (QT_VERSION_MAJOR > 5) || (QT_VERSION_MAJOR == 5 && QT_VERSION_MINOR >= 10)
125-
aGlFormat.setColorSpace(QSurfaceFormat::sRGBColorSpace);
126-
setTextureFormat(GL_SRGB8_ALPHA8);
127-
#else
128-
Message::SendWarning("Warning! Qt 5.10+ is required for sRGB setup.\n"
129-
"Colors in 3D Viewer might look incorrect (Qt " QT_VERSION_STR " is used).\n");
130-
aDriver->ChangeOptions().sRGBDisable = true;
131-
#endif*/
132-
133-
setFormat(aGlFormat);
134-
135-
#if defined(_WIN32)
136-
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this
137-
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
138-
// QCoreApplication::setAttribute (Qt::AA_UseOpenGLES);
139-
#endif
111+
// OpenGL setup managed by Qt - it is better to do this globally
112+
// via QSurfaceFormat::setDefaultFormat() - see main() function
113+
//const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
114+
//setFormat(aGlFormat);
140115
}
141116

142117
// ================================================================
@@ -191,8 +166,16 @@ void OcctQOpenGLWidgetViewer::dumpGlInfo(bool theIsBasic, bool theToPrint)
191166
// ================================================================
192167
void OcctQOpenGLWidgetViewer::initializeGL()
193168
{
194-
const QRect aRect = rect();
195-
const Graphic3d_Vec2i aViewSize(aRect.right() - aRect.left(), aRect.bottom() - aRect.top());
169+
const Graphic3d_Vec2i aViewSize(rect().right() - rect().left(), rect().bottom() - rect().top());
170+
const QSurfaceFormat aQtGlFormat = format();
171+
172+
Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(myViewer->Driver());
173+
aDriver->ChangeOptions().contextDebug = aQtGlFormat.testOption(QSurfaceFormat::DebugContext);
174+
aDriver->ChangeOptions().contextSyncDebug = aDriver->Options().contextDebug;
175+
aDriver->ChangeOptions().contextCompatible = aQtGlFormat.profile() != QSurfaceFormat::CoreProfile;
176+
aDriver->ChangeOptions().buffersDeepColor = aQtGlFormat.redBufferSize() == 10
177+
&& aQtGlFormat.greenBufferSize() == 10
178+
&& aQtGlFormat.blueBufferSize() == 10;
196179

197180
Aspect_Drawable aNativeWin = (Aspect_Drawable)winId();
198181
#ifdef _WIN32
@@ -202,7 +185,7 @@ void OcctQOpenGLWidgetViewer::initializeGL()
202185
#endif
203186

204187
Handle(OpenGl_Context) aGlCtx = new OpenGl_Context();
205-
if (!aGlCtx->Init(myIsCoreProfile))
188+
if (!aGlCtx->Init(!aDriver->Options().contextCompatible))
206189
{
207190
Message::SendFail() << "Error: OpenGl_Context is unable to wrap OpenGL context";
208191
QMessageBox::critical(0, "Failure", "OpenGl_Context is unable to wrap OpenGL context");

occt-qopenglwidget/OcctQOpenGLWidgetViewer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class OcctQOpenGLWidgetViewer : public QOpenGLWidget, public AIS_ViewController
9090
Handle(V3d_View) myFocusView;
9191

9292
QString myGlInfo;
93-
bool myIsCoreProfile = true;
9493
bool myHasTouchInput = false;
9594
};
9695

occt-qopenglwidget/main.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "OcctQOpenGLWidgetViewer.h"
44

5+
#include "../occt-qt-tools/OcctQtTools.h"
6+
57
#include <Standard_WarningsDisable.hxx>
68
#include <QApplication>
79
#include <QSurfaceFormat>
@@ -188,18 +190,14 @@ int main(int theNbArgs, char** theArgVec)
188190
QCoreApplication::setOrganizationName("OpenCASCADE");
189191
QCoreApplication::setApplicationVersion(OCC_VERSION_STRING_EXT);
190192

191-
#ifdef __APPLE__
192-
// suppress Qt warning "QCocoaGLContext: Falling back to unshared context"
193-
bool isCoreProfile = true;
194-
QSurfaceFormat aGlFormat;
195-
aGlFormat.setDepthBufferSize(24);
196-
aGlFormat.setStencilBufferSize(8);
197-
if (isCoreProfile)
198-
aGlFormat.setVersion(4, 5);
199-
200-
aGlFormat.setProfile(isCoreProfile ? QSurfaceFormat::CoreProfile : QSurfaceFormat::CompatibilityProfile);
201-
QSurfaceFormat::setDefaultFormat(aGlFormat);
193+
// OpenGL setup managed by Qt
194+
#if defined(_WIN32)
195+
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this
196+
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
197+
// use Qt::AA_UseOpenGLES for embedded systems
202198
#endif
199+
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
200+
QSurfaceFormat::setDefaultFormat(aGlFormat);
203201

204202
MyMainWindow aMainWindow;
205203
aMainWindow.resize(aMainWindow.sizeHint());

occt-qt-tools/OcctQtTools.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
#include <Standard_WarningsDisable.hxx>
1010
#include <QColor>
1111
#include <QMouseEvent>
12+
#include <QSurfaceFormat>
1213
#include <Standard_WarningsRestore.hxx>
1314

1415
//! Auxiliary tools between Qt and OCCT definitions.
1516
namespace OcctQtTools
1617
{
18+
//! Return default Qt surface format for GL context.
19+
inline QSurfaceFormat qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile = QSurfaceFormat::NoProfile,
20+
bool theToDebug = false);
21+
1722
//! Map QColor into Quantity_Color.
1823
inline Quantity_Color qtColorToOcct(const QColor& theColor)
1924
{
@@ -37,6 +42,48 @@ inline Aspect_VKeyFlags qtMouseModifiers2VKeys(Qt::KeyboardModifiers theModifier
3742
inline Aspect_VKey qtKey2VKey(int theKey);
3843
} // namespace OcctQtTools
3944

45+
// ================================================================
46+
// Function : qtGlSurfaceFormat
47+
// ================================================================
48+
inline QSurfaceFormat OcctQtTools::qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile,
49+
bool theToDebug)
50+
{
51+
const bool isDeepColor = false;
52+
QSurfaceFormat::OpenGLContextProfile aProfile = theProfile;
53+
if (theProfile == QSurfaceFormat::NoProfile)
54+
{
55+
aProfile = QSurfaceFormat::CompatibilityProfile;
56+
#ifdef __APPLE__
57+
// suppress Qt warning "QCocoaGLContext: Falling back to unshared context"
58+
aProfile = QSurfaceFormat::CoreProfile;
59+
#endif
60+
}
61+
62+
QSurfaceFormat aGlFormat;
63+
if (isDeepColor)
64+
{
65+
aGlFormat.setRedBufferSize(10);
66+
aGlFormat.setGreenBufferSize(10);
67+
aGlFormat.setBlueBufferSize(10);
68+
aGlFormat.setAlphaBufferSize(2);
69+
}
70+
aGlFormat.setDepthBufferSize(24);
71+
aGlFormat.setStencilBufferSize(8);
72+
aGlFormat.setProfile(theProfile);
73+
if (theProfile == QSurfaceFormat::CoreProfile)
74+
aGlFormat.setVersion(4, 5);
75+
76+
// request sRGBColorSpace colorspace to meet OCCT expectations or use OcctQtFrameBuffer fallback.
77+
/*#if (QT_VERSION_MAJOR > 5) || (QT_VERSION_MAJOR == 5 && QT_VERSION_MINOR >= 10)
78+
aGlFormat.setColorSpace(QSurfaceFormat::sRGBColorSpace);
79+
#endif*/
80+
81+
if (theToDebug)
82+
aGlFormat.setOption(QSurfaceFormat::DebugContext, true);
83+
84+
return aGlFormat;
85+
}
86+
4087
// ================================================================
4188
// Function : qtMouseButtons2VKeys
4289
// ================================================================

occt-qtquick/OcctQQuickFramebufferViewer.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,16 @@ QOpenGLFramebufferObject* OcctQQuickFramebufferViewer::Renderer::createFramebuff
408408
{
409409
QOpenGLFramebufferObjectFormat aQFormat;
410410
aQFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
411-
//aQFormat.setInternalTextureFormat(isSrgb ? GL_SRGB8_ALPHA8 ? GL_RGBA8);
411+
//aQFormat.setInternalTextureFormat(GL_RGBA8);
412412
//aQFormat.setSamples(4); do not create MSAA buffer here
413+
414+
/*#if (QT_VERSION_MAJOR > 5) || (QT_VERSION_MAJOR == 5 && QT_VERSION_MINOR >= 10)
415+
const QQuickWindow* aQWindow = window();
416+
const QSurfaceFormat aQtGlFormat = aQWindow->format();
417+
if (aQtGlFormat.colorSpace() == QSurfaceFormat::sRGBColorSpace)
418+
aQFormat.setInternalTextureFormat(GL_SRGB8_ALPHA8);
419+
#endif*/
420+
413421
return new QOpenGLFramebufferObject(theSize, aQFormat);
414422
}
415423

@@ -438,13 +446,10 @@ void OcctQQuickFramebufferViewer::Renderer::render()
438446
// ================================================================
439447
// Function : synchronize
440448
// ================================================================
441-
void OcctQQuickFramebufferViewer::synchronize(QOpenGLFramebufferObject* theFbo)
449+
void OcctQQuickFramebufferViewer::synchronize(QOpenGLFramebufferObject* )
442450
{
443451
// this method will be called from GL rendering thread while GUI thread is locked,
444452
// the place to sycnhronize GUI / GL rendering states
445-
446-
if (myView.IsNull() || myView->Window().IsNull())
447-
initializeGL(theFbo);
448453
}
449454

450455
// ================================================================
@@ -456,9 +461,17 @@ void OcctQQuickFramebufferViewer::initializeGL(QOpenGLFramebufferObject* theFbo)
456461
if (theFbo == nullptr)
457462
return;
458463

464+
const Graphic3d_Vec2i aViewSize(theFbo->size().width(), theFbo->size().height());
459465
const QQuickWindow* aQWindow = window();
460-
const QSize aRect = theFbo->size();
461-
const Graphic3d_Vec2i aViewSize(aRect.width(), aRect.height());
466+
const QSurfaceFormat aQtGlFormat = aQWindow->format();
467+
468+
Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(myViewer->Driver());
469+
aDriver->ChangeOptions().contextDebug = aQtGlFormat.testOption(QSurfaceFormat::DebugContext);
470+
aDriver->ChangeOptions().contextSyncDebug = aDriver->Options().contextDebug;
471+
aDriver->ChangeOptions().contextCompatible = aQtGlFormat.profile() != QSurfaceFormat::CoreProfile;
472+
aDriver->ChangeOptions().buffersDeepColor = aQtGlFormat.redBufferSize() == 10
473+
&& aQtGlFormat.greenBufferSize() == 10
474+
&& aQtGlFormat.blueBufferSize() == 10;
462475

463476
Aspect_Drawable aNativeWin = aQWindow != nullptr ? (Aspect_Drawable)aQWindow->winId() : 0;
464477
#ifdef _WIN32
@@ -468,7 +481,7 @@ void OcctQQuickFramebufferViewer::initializeGL(QOpenGLFramebufferObject* theFbo)
468481
#endif
469482

470483
Handle(OpenGl_Context) aGlCtx = new OpenGl_Context();
471-
if (!aGlCtx->Init(myIsCoreProfile))
484+
if (!aGlCtx->Init(!aDriver->Options().contextCompatible))
472485
{
473486
Message::SendFail() << "Error: OpenGl_Context is unable to wrap OpenGL context";
474487
QMessageBox::critical(0, "Failure", "OpenGl_Context is unable to wrap OpenGL context");
@@ -512,7 +525,7 @@ void OcctQQuickFramebufferViewer::render(QOpenGLFramebufferObject* theFbo)
512525
{
513526
// this method is called from GL rendering thread;
514527
// accessing GUI items is not allowed here!
515-
if (theFbo == nullptr || myView.IsNull() || myView->Window().IsNull())
528+
if (theFbo == nullptr || myView.IsNull())
516529
return;
517530

518531
QQuickWindow* aQWindow = window();
@@ -524,7 +537,12 @@ void OcctQQuickFramebufferViewer::render(QOpenGLFramebufferObject* theFbo)
524537
#endif
525538

526539
Standard_Mutex::Sentry aLock(myViewerMutex);
527-
if (myView->Window()->NativeHandle() != aNativeWin)
540+
if (myView->Window().IsNull())
541+
{
542+
initializeGL(theFbo);
543+
theFbo->bind();
544+
}
545+
else if (myView->Window()->NativeHandle() != aNativeWin)
528546
{
529547
// workaround window recreation done by Qt on monitor (QScreen) disconnection
530548
Message::SendWarning() << "Native window handle has changed by QQuickWindow!";

occt-qtquick/OcctQQuickFramebufferViewer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class OcctQQuickFramebufferViewer : public QQuickFramebufferObject, public AIS_V
127127
Standard_Mutex myViewerMutex;
128128

129129
QString myGlInfo;
130-
bool myIsCoreProfile = false;
131130
bool myHasTouchInput = false;
132131
};
133132

occt-qtquick/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QApplication>
77
#include <QQmlApplicationEngine>
88
#include <QQmlContext>
9+
#include <QSurfaceFormat>
910
#include <Standard_WarningsRestore.hxx>
1011

1112
#include <OSD_Environment.hxx>
@@ -47,11 +48,15 @@ int main(int theNbArgs, char** theArgVec)
4748
QCoreApplication::setOrganizationName("OpenCASCADE");
4849
QCoreApplication::setApplicationVersion(OCC_VERSION_STRING_EXT);
4950
//QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
51+
52+
// OpenGL setup managed by Qt
5053
#if defined(_WIN32)
5154
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this
5255
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
5356
// QCoreApplication::setAttribute (Qt::AA_UseOpenGLES);
5457
#endif
58+
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
59+
QSurfaceFormat::setDefaultFormat(aGlFormat);
5560

5661
qmlRegisterType<OcctQQuickFramebufferViewer>("OcctQQuickFramebufferViewer", 1, 0, "OcctQQuickFramebufferViewer");
5762

occt-qwidget/main.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "OcctQWidgetViewer.h"
44

5+
#include "../occt-qt-tools/OcctQtTools.h"
6+
57
#include <Standard_WarningsDisable.hxx>
68
#include <QApplication>
79
#include <QSurfaceFormat>
@@ -190,18 +192,9 @@ int main(int theNbArgs, char** theArgVec)
190192
QCoreApplication::setOrganizationName("OpenCASCADE");
191193
QCoreApplication::setApplicationVersion(OCC_VERSION_STRING_EXT);
192194

193-
#ifdef __APPLE__
194-
// suppress Qt warning "QCocoaGLContext: Falling back to unshared context"
195-
bool isCoreProfile = true;
196-
QSurfaceFormat aGlFormat;
197-
aGlFormat.setDepthBufferSize(24);
198-
aGlFormat.setStencilBufferSize(8);
199-
if (isCoreProfile)
200-
aGlFormat.setVersion(4, 5);
201-
202-
aGlFormat.setProfile(isCoreProfile ? QSurfaceFormat::CoreProfile : QSurfaceFormat::CompatibilityProfile);
195+
// request OpenGL-compatible surface from Qt
196+
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
203197
QSurfaceFormat::setDefaultFormat(aGlFormat);
204-
#endif
205198

206199
MyMainWindow aMainWindow;
207200
aMainWindow.resize(aMainWindow.sizeHint());

0 commit comments

Comments
 (0)