void OpenGLWindow::renderNow()
{
    if (!isExposed())
        return;

    bool needsInitialize = false;

    // Create an OpenGL context if there is not any
    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    m_funcs = m_context->versionFunctions<QOpenGLFunctions_3_3_Core>();

    if (needsInitialize) {
        initializeOpenGLFunctions();
        initialize();

    }

    render();

    m_context->swapBuffers(this);
}
void OpenGLWindow::renderNow()
{
    if (!isExposed())
        return;

    m_update_pending = false;

    bool needsInitialize = false;

    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        initializeOpenGLFunctions();
        initialize();
    }

    render();

    m_context->swapBuffers(this);

    if (m_animating)
        renderLater();
}
void OpenGLWindow::renderNow()
{
    // end function if window is not visible
    if (!isExposed())
        return;

    bool needsInitialize = false;

    /*QOpenGLContext represents the OpenGL state of an underlying OpenGL context.
    To set up a context, set its screen and format such that they match those of the surface
    or surfaces with which the context is meant to be used.*/
    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        // intern OpenGL function
        initializeOpenGLFunctions();
        initialize();
    }

    render();

    m_context->swapBuffers(this);

    if (m_animating)
        renderLater();
}
Beispiel #4
0
void yage::GLWindow::beginDraw ( void )
{
    if (!isExposed()) {
        return;
    }

    bool needInit = false;
    if (!m_context) {
        qDebug() << "start ctx";
        // Specify the format and create platform-specific surface
        
        // Create an OpenGL context
        m_context = new QOpenGLContext( this );
        m_context->setFormat( requestedFormat() );
        m_context->create();

        if (!m_context->isValid()) {
            qCritical() << "invalid context";
        }

        // Make the context current on this window

        QList<QByteArray> extensions = m_context->extensions().toList();
        qDebug() << "Version: " << m_context->format().majorVersion() << "." << m_context->format().minorVersion();
        qDebug() << "Profile: " << m_context->format().profile();
        //std::sort(extensions.first(), extensions.last());
        qDebug() << "Supported extensions (" << extensions.count() << ")";
        foreach ( const QByteArray &extension, extensions )
            qDebug() << "    " << extension;

        needInit = true;
    }
Beispiel #5
0
void MainWindow::renderNow()
{

    if (!isExposed())
        return;


    updatePending = false;

    bool needsInitialize = false;

    if (!context) {
        context = new QOpenGLContext(this);
        context->setFormat(requestedFormat());
        context->create();

        needsInitialize = true;
    }

    context->makeCurrent(this);

    if (world)
        world->simulate(timer.elapsed());
    timer.start();

    if (needsInitialize) {
        initializeOpenGLFunctions();
    }

    render();

    context->swapBuffers(this);
    if (animating)
        renderLater();
}
Beispiel #6
0
void RenderSurface::renderNow(const float time)
{
    m_globalTime = time;

    //if (!isExposed())
        //return;

    bool needsInitialize = false;

    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        initializeOpenGLFunctions();
        initialize();
    }

    render();

    m_context->swapBuffers(this);

    if (m_animating)
        renderLater();
}
    void exposeEvent(QExposeEvent *)
    {
        if (!isExposed())
            return;

        if (!gl) {
            gl = new QOpenGLContext();
            gl->setFormat(requestedFormat());
            gl->create();
        }

        gl->makeCurrent(this);

        QOpenGLShaderProgram prog;
        prog.addShaderFromSourceCode(QOpenGLShader::Vertex,
                                     "attribute highp vec4 a_Pos;"
                                     "attribute lowp vec4 a_Color;"
                                     "varying lowp vec4 v_Color;"
                                     "void main() {"
                                     "    gl_Position = a_Pos;"
                                     "    v_Color = a_Color;"
                                     "}");
        prog.addShaderFromSourceCode(QOpenGLShader::Fragment,
                                     "varying lowp vec4 v_Color;"
                                     "void main() {"
                                     "    gl_FragColor = v_Color;"
                                     "}");
        prog.bind();

        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        glViewport(0, 0, width(), height());

        prog.enableAttributeArray("a_Pos");
        prog.enableAttributeArray("a_Color");

        float coords[] = { -0.7f,  0.7f,
                           0.8f,  0.8f,
                           -0.8f, -0.8f,
                           0.7f, -0.7f
                         };
        float colors[] = { 1, 0, 0, 1,
                           0, 1, 0, 1,
                           0, 0, 1, 1,
                           0, 0, 0, 0
                         };

        prog.setAttributeArray("a_Pos", GL_FLOAT, coords, 2, 0);
        prog.setAttributeArray("a_Color", GL_FLOAT, colors, 4, 0);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        prog.disableAttributeArray("a_Pos");
        prog.disableAttributeArray("a_Color");

        gl->swapBuffers(this);
    }
void Window3D::initRendering()
{
    if (!m_pContext) {
        m_pContext = new QOpenGLContext(this);
        m_pContext->setFormat(requestedFormat());
        m_pContext->create();
    }
    m_canRender = true;
    m_updateTime.start();
}
//! [4]
void OpenGLWindow::renderNow()
{
    if (!isExposed())
        return;

    bool needsInitialize = false;

    if (!m_context) {
        m_context = new QOpenGLContext(this);
        QGLFormat format = QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        initializeOpenGLFunctions();
        initialize();
        GLuint depthRenderbuffer;

        //QString tmp((const char*)glGetString(GL_VERSION));
        //GL_VENDOR, GL_RENDERER, GL_VERSION, or GL_SHADING_LANGUAGE_VERSION
        qDebug()<<"Version: "<<(const char*)glGetString(GL_VERSION);//tmp;
        qDebug()<<"Renderer: "<<(const char*)glGetString(GL_RENDERER);
        qDebug()<<"Shaders: "<<(const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
        qDebug()<<"Vendor: "<<(const char*)glGetString(GL_VENDOR);
        //        glGenRenderbuffers(1, &depthRenderbuffer);
        //                glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);

        //                // GL_DEPTH24_STENCIL8_OES: Set a depth buffer and a stencil buffer.
        //                // GL_DEPTH_COMPONENT16: Set a 16bits depth buffer.
        //                // GL_DEPTH_COMPONENT24_OES: Set a 24bits depth buffer.
        //                glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 800, 600);
        //                glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
    }

    render();

    m_context->swapBuffers(this);

    if (m_animating)
        renderLater();
}
Beispiel #10
0
void GameWindow::render()
{

    bool needsInitialize = false;
    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();
        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize)
    {
        initializeOpenGLFunctions();
    }

    if (!m_device)
        m_device = new QOpenGLPaintDevice();

    m_device->setSize(size());

    QPainter p;

    elapsed = elapsedTimer.elapsed();
    if(elapsed > timer.interval() * 0.5) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        this->render((float) elapsed * 0.0005f);
//        glDisableVertexAttribArray(GL_DEPTH_TEST);
        p.begin(m_device);
        p.setPen(Qt::yellow);
        p.setFont(QFont("Arial", 20));
        p.drawText(10, 30, QString::number((int) (1.0 / (elapsed * 0.001f))));
        p.end();
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        elapsed -= timer.interval();
        elapsedTimer.restart();
    }
}
Beispiel #11
0
Window::Window()
      : QQuickView()
      , m_terrain(nullptr)
      , m_mouseDown(false)
      , m_speed(0.01)
      , m_needsUpdate(true)
      , m_generate(false)
      , m_paused(false)
      , m_curTimeId(0)
{
    updateUi();
    rootContext()->setContextProperty("Game", this);

    setSurfaceType(QWindow::OpenGLSurface);
    setClearBeforeRendering(false);
    setPersistentOpenGLContext(true);
    setResizeMode(QQuickView::SizeRootObjectToView);
    setSource(QUrl::fromLocalFile(FileFinder::findFile(FileFinder::Type::Qml, "ui.qml")));

    QSurfaceFormat format = requestedFormat();
    format.setProfile(QSurfaceFormat::CoreProfile);
    format.setMajorVersion(3);
    format.setMinorVersion(3);
    format.setDepthBufferSize(24);
    format.setOption(QSurfaceFormat::DebugContext);
    setFormat(format);

    resize(1024, 768);

    connect(this, &QQuickWindow::beforeRendering, this, &Window::renderNow, Qt::DirectConnection);
    connect(this, &QQuickWindow::beforeSynchronizing, this, &Window::sync, Qt::DirectConnection);
    connect(this, &QQuickWindow::afterRendering, this, &Window::updateUi);
    connect(this, &QQuickWindow::afterRendering, this, &Window::update);

    show();
}
Beispiel #12
0
void MainWindow::initialize()
{
    counter = 0;

    m_context = new QOpenGLContext(this);
    m_context->setFormat(requestedFormat());
    m_context->create();
    m_context->makeCurrent(this);

    m_program = new QOpenGLShaderProgram(this);
    m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/GLSL/main.vert");
    m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/GLSL/main.frag");
    m_program->link();
    m_vertexAttr = m_program->attributeLocation("qt_Vertex");
    m_colorUni = m_program->uniformLocation("qt_Color");
    m_matrixUni = m_program->uniformLocation("qt_Matrix");

    m_glBuffer = new QOpenGLBuffer;
    m_glBuffer->create();
    m_glBuffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
    m_glBuffer->bind();
    m_glBuffer->allocate(planets.size() * 60 * 3600 * sizeof(QVector2D));
    m_glBuffer->release();
}
Beispiel #13
0
    void GLWindow::renderNow()
    {
      if (!isExposed())
        return;

      bool needsInitialize = false;
      bool enableDebug = false;

      if (std::getenv("BIOFORMATS_OPENGL_DEBUG"))
        enableDebug = true;

      if (!glcontext) {
        QSurfaceFormat format = requestedFormat();
        // OpenGL 2.0 profile with debugging.
        format.setVersion(2, 0);
        format.setProfile(QSurfaceFormat::NoProfile);
        if (enableDebug)
          {
            format.setOption(QSurfaceFormat::DebugContext);
          }
        format.setSamples(8);
        format.setDepthBufferSize(24);

        glcontext = new QOpenGLContext(this);
        glcontext->setFormat(format);
        glcontext->create();
        makeCurrent();

        if (enableDebug)
          {
            // The debug logger is broken on Windows for Qt 5.2 and earlier, so don't use.
#if !defined(Q_OS_WIN) || QT_VERSION >= 0x050300
            logger = new QOpenGLDebugLogger(this);
            connect(logger, SIGNAL(messageLogged(QOpenGLDebugMessage)),
                    this, SLOT(logMessage(QOpenGLDebugMessage)),
                    Qt::DirectConnection);
            if (logger->initialize())
              {
                logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
                logger->enableMessages();
              }
#endif // !defined(Q_OS_WIN) || QT_VERSION >= 0x050300
          }

        needsInitialize = true;
      }

      makeCurrent();

      if (needsInitialize)
        {
          initializeOpenGLFunctions();
          initialize();
        }

      render();

      glcontext->swapBuffers(this);

      if (animating)
        renderLater();
    }
void OpenGLWindow::renderNow()
{
    if (!isExposed())
        return;

    // initialize opengl context

    bool needsInitialize = false;

    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());

        if (m_context->create()) {
            m_gl = m_context->versionFunctions<GLFunctions>();
            if (!m_gl) {
                qCritical("Critical: Unable to initialize OpenGL 4.1 Functions");
                exit(EXIT_FAILURE);
            }

#ifdef DEBUG_OPENGL
            m_logger = new QOpenGLDebugLogger(this);
#endif
            needsInitialize = true;

            m_tickTimer.start();
            m_fpsTimer.start();
        } else {
            qWarning("Warning: Unable to create OpenGL context");
            return;
        }
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        m_gl->initializeOpenGLFunctions();
        g_glFunctions = m_gl;
#ifdef DEBUG_OPENGL
        if (m_context->hasExtension(QByteArrayLiteral("GL_KHR_debug"))) {
            m_logger->initialize();
            connect(m_logger,
                    &QOpenGLDebugLogger::messageLogged,
                    this,
                    &OpenGLWindow::handleLogMessage,
                    Qt::DirectConnection);
            m_logger->enableMessages();
            m_logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
            const QList<QOpenGLDebugMessage> &preInitErrors = m_logger->loggedMessages();
            if (preInitErrors.size()) {
                qWarning() << "Warning: Pre-Initialization OpenGL Errors (Qt's fault)";
                qWarning() << "--[" << preInitErrors << "]--";
            }
        } else {
            qWarning("OpenGL KHR Debugging not available.");
        }
#endif
        initialize();
    }

    // check initialize qpainter context

    if (!m_device) {
        m_device = new QOpenGLPaintDevice;
        m_device->setSize(size());

        m_painter = new QPainter(m_device);
        m_painter->setRenderHint(QPainter::Antialiasing);
        m_painter->setRenderHint(QPainter::TextAntialiasing);
    }

    if (!m_painter->isActive()) {
        m_painter->begin(m_device);
    }

    // render

    float seconds = m_tickTimer.restart() * 0.001f;
    if (seconds > 1.0f) seconds = 1.0f;

    onTick(seconds);
    render();

    m_context->swapBuffers(this);

    if (m_animating)
        renderLater();
}