#include "Scene.hpp" // OpenGL. #include // With this (conservative) work-around, I was able to scale the // projection matrix how painter->beginNativePainting() would do // in case it failed (for systems with OpenGL 3.1). //#define WORKAROUND_FOR_MY_PROBLEM Scene::Scene(QWidget *parent) : QGraphicsScene(parent) { } Scene::~Scene() { } void Scene::drawBackground(QPainter *painter, const QRectF &rect) { #ifdef WORKAROUND_FOR_MY_PROBLEM static bool CALL_ONLY_ONCE_FLAG = true; if(CALL_ONLY_ONCE_FLAG) { painter->beginNativePainting(); { const GLdouble identityMatrix[16] = {1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.}; GLdouble projectionMatrix[16] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}; glGetDoublev (GL_PROJECTION_MATRIX, projectionMatrix); // Check if projection matrix is identity. if(memcmp(projectionMatrix, identityMatrix, 16) == 0) { const int w = painter->device()->width(); const int h = painter->device()->height(); { // This is the Qt way of setting the viewport and perspective. // Compare with opengl/qpaintengine_opengl.cpp. glPushMatrix(); glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, w, h, 0, -999999, 999999); glMatrixMode(GL_MODELVIEW); glPopMatrix(); } } } painter->endNativePainting(); // In any case, pass by here only once! CALL_ONLY_ONCE_FLAG = false; } #endif // WORKAROUND_FOR_MY_PROBLEM // Regardless of the scanning state, set the back-ground color. painter->beginNativePainting(); { // Clear the buffers. glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Do other native painting... // // // ... } painter->endNativePainting(); } //#inlude "Scene.moc"