Exemplo n.º 1
0
 void CQTOpenGLMainWindow::CreateOpenGLWidget(TConfigurationNode& t_tree) {
    /* Create user functions */
    m_pcUserFunctions = CreateUserFunctions(t_tree);
    /* Set up OpenGL features */
    QGLFormat cGLFormat = QGLFormat::defaultFormat();
    cGLFormat.setSampleBuffers(m_pcToggleAntiAliasingAction->isChecked());
    cGLFormat.setStencil(false);
    QGLFormat::setDefaultFormat(cGLFormat);
    /* Create the widget */
    QWidget* pcPlaceHolder = new QWidget(this);
    m_pcOpenGLWidget = new CQTOpenGLWidget(pcPlaceHolder, *m_pcUserFunctions);
    m_pcOpenGLWidget->setCursor(QCursor(Qt::OpenHandCursor));
    m_pcOpenGLWidget->GetCamera().Init(t_tree);
    m_pcOpenGLWidget->GetFrameGrabData().Init(t_tree);
    m_pcToggleAntiAliasingAction->setChecked(cGLFormat.sampleBuffers());
    /* Invert mouse controls? */
    bool bInvertMouse;
    GetNodeAttributeOrDefault(t_tree, "invert_mouse", bInvertMouse, false);
    m_pcOpenGLWidget->SetInvertMouse(bInvertMouse);
    /* Set the window as the central widget */
    CQTOpenGLLayout* pcQTOpenGLLayout = new CQTOpenGLLayout();
    pcQTOpenGLLayout->addWidget(m_pcOpenGLWidget);
    pcPlaceHolder->setLayout(pcQTOpenGLLayout);
    setCentralWidget(pcPlaceHolder);
 }
Exemplo n.º 2
0
/*!
    \since 4.8

    Returns a platform window format for the OpenGL format specified by \a format.
*/
QPlatformWindowFormat QGLFormat::toPlatformWindowFormat(const QGLFormat &format)
{
    QPlatformWindowFormat retFormat;
    retFormat.setAccum(format.accum());
    if (format.accumBufferSize() >= 0)
        retFormat.setAccumBufferSize(format.accumBufferSize());
    retFormat.setAlpha(format.alpha());
    if (format.alphaBufferSize() >= 0)
        retFormat.setAlphaBufferSize(format.alphaBufferSize());
    if (format.blueBufferSize() >= 0)
        retFormat.setBlueBufferSize(format.blueBufferSize());
    retFormat.setDepth(format.depth());
    if (format.depthBufferSize() >= 0)
        retFormat.setDepthBufferSize(format.depthBufferSize());
    retFormat.setDirectRendering(format.directRendering());
    retFormat.setDoubleBuffer(format.doubleBuffer());
    if (format.greenBufferSize() >= 0)
        retFormat.setGreenBufferSize(format.greenBufferSize());
    if (format.redBufferSize() >= 0)
        retFormat.setRedBufferSize(format.redBufferSize());
    retFormat.setRgba(format.rgba());
    retFormat.setSampleBuffers(format.sampleBuffers());
    if (format.samples() >= 0)
        retFormat.setSamples(format.samples());
    retFormat.setStencil(format.stencil());
    if (format.stencilBufferSize() >= 0)
        retFormat.setStencilBufferSize(format.stencilBufferSize());
    retFormat.setStereo(format.stereo());
    retFormat.setSwapInterval(format.swapInterval());
    return retFormat;
}
Exemplo n.º 3
0
GLFormat::GLFormat(const QGLFormat & format)

:   m_majorVersion(format.majorVersion())
,   m_minorVersion(format.minorVersion())

,   m_redBufferSize  (format.redBufferSize())
,   m_greenBufferSize(format.greenBufferSize())
,   m_blueBufferSize (format.blueBufferSize())
,   m_alphaBufferSize(format.alpha() ? format.alphaBufferSize() : 0)

,   m_depthBufferSize  (format.depth()   ? format.depthBufferSize()   : 0)
,   m_stencilBufferSize(format.stencil() ? format.stencilBufferSize() : 0)

,   m_doubleBuffer(format.doubleBuffer())
,   m_stereo(format.stereo())

,   m_sampleBuffers(format.sampleBuffers())
,   m_samples(format.samples())

,   m_swapInterval(format.swapInterval())
{
  switch(format.profile())
    {
    default:
    case QGLFormat::NoProfile:
        m_profile = NoProfile; 
        break;
    case QGLFormat::CoreProfile:
        m_profile = CoreProfile; 
        break;
    case QGLFormat::CompatibilityProfile:
        m_profile = CompatibilityProfile; 
        break;
    };
}
QT_BEGIN_NAMESPACE

void qt_eglproperties_set_glformat(QEglProperties& eglProperties, const QGLFormat& glFormat)
{
    int redSize     = glFormat.redBufferSize();
    int greenSize   = glFormat.greenBufferSize();
    int blueSize    = glFormat.blueBufferSize();
    int alphaSize   = glFormat.alphaBufferSize();
    int depthSize   = glFormat.depthBufferSize();
    int stencilSize = glFormat.stencilBufferSize();
    int sampleCount = glFormat.samples();

    // QGLFormat uses a magic value of -1 to indicate "don't care", even when a buffer of that
    // type has been requested. So we must check QGLFormat's booleans too if size is -1:
    if (glFormat.alpha() && alphaSize <= 0)
        alphaSize = 1;
    if (glFormat.depth() && depthSize <= 0)
        depthSize = 1;
    if (glFormat.stencil() && stencilSize <= 0)
        stencilSize = 1;
    if (glFormat.sampleBuffers() && sampleCount <= 0)
        sampleCount = 1;

    // We want to make sure 16-bit configs are chosen over 32-bit configs as they will provide
    // the best performance. The EGL config selection algorithm is a bit stange in this regard:
    // The selection criteria for EGL_BUFFER_SIZE is "AtLeast", so we can't use it to discard
    // 32-bit configs completely from the selection. So it then comes to the sorting algorithm.
    // The red/green/blue sizes have a sort priority of 3, so they are sorted by first. The sort
    // order is special and described as "by larger _total_ number of color bits.". So EGL will
    // put 32-bit configs in the list before the 16-bit configs. However, the spec also goes on
    // to say "If the requested number of bits in attrib_list for a particular component is 0,
    // then the number of bits for that component is not considered". This part of the spec also
    // seems to imply that setting the red/green/blue bits to zero means none of the components
    // are considered and EGL disregards the entire sorting rule. It then looks to the next
    // highest priority rule, which is EGL_BUFFER_SIZE. Despite the selection criteria being
    // "AtLeast" for EGL_BUFFER_SIZE, it's sort order is "smaller" meaning 16-bit configs are
    // put in the list before 32-bit configs. So, to make sure 16-bit is preffered over 32-bit,
    // we must set the red/green/blue sizes to zero. This has an unfortunate consequence that
    // if the application sets the red/green/blue size to 5/6/5 on the QGLFormat, they will
    // probably get a 32-bit config, even when there's an RGB565 config avaliable. Oh well.

    // Now normalize the values so -1 becomes 0
    redSize   = redSize   > 0 ? redSize   : 0;
    greenSize = greenSize > 0 ? greenSize : 0;
    blueSize  = blueSize  > 0 ? blueSize  : 0;
    alphaSize = alphaSize > 0 ? alphaSize : 0;
    depthSize = depthSize > 0 ? depthSize : 0;
    stencilSize = stencilSize > 0 ? stencilSize : 0;
    sampleCount = sampleCount > 0 ? sampleCount : 0;

    eglProperties.setValue(EGL_RED_SIZE,   redSize);
    eglProperties.setValue(EGL_GREEN_SIZE, greenSize);
    eglProperties.setValue(EGL_BLUE_SIZE,  blueSize);
    eglProperties.setValue(EGL_ALPHA_SIZE, alphaSize);
    eglProperties.setValue(EGL_DEPTH_SIZE, depthSize);
    eglProperties.setValue(EGL_STENCIL_SIZE, stencilSize);
    eglProperties.setValue(EGL_SAMPLES, sampleCount);
    eglProperties.setValue(EGL_SAMPLE_BUFFERS, sampleCount ? 1 : 0);
}
Exemplo n.º 5
0
void Viewer::initializeGL() {
    // Do some OpenGL setup
    QGLFormat glFormat = QGLWidget::format();
    if (!glFormat.sampleBuffers()) {
        std::cerr << "Could not enable sample buffers." << std::endl;
        return;
    }
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);
    glClearColor(0.7, 0.7, 0.7, 0.0);


    int programID = LoadShaders("/Users/Aliya/SPH/shader.vert", "/Users/Aliya/SPH/shader.frag");
    glUseProgram(programID);
    static const GLfloat g_vertex_buffer_data[] = {
       -0.01f, -0.01f, 0.01f,
       0.01f, -0.01f, 0.01f,
       0.0f,  0.01f, 0.01f,
    };
    // This will identify our vertex buffer

    float data[123];

    double radius = 0.04f;

    for(size_t i=0; i<40; ++i) {
        data[i*3] = radius * cos(i*2*3.14f/40);
        data[i*3 + 1] = radius * sin(i*2*3.14f/40);
        data[i*3 + 2] = 0.0;
    }
    data[120] = 0.0f;
    data[121] = 0.0f;
    data[122] = 0.0f;


    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);

    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

    // The VBO containing the positions and sizes of the particles
    glGenBuffers(1, &particles_position_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
    // Initialize with empty (NULL) buffer : it will be updated later, each frame.
    glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW);
    mvpMatrix = glGetUniformLocation(programID, "mvpMatrix");

    // The VBO containing the positions and sizes of the particles
    glGenBuffers(2, &particles_color_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
    // Initialize with empty (NULL) buffer : it will be updated later, each frame.
    glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW);
}
Exemplo n.º 6
0
inline void GL3DShaderWidget::checkOpenGLPixelFormat(const QGLFormat &fmt)
{
    QGLFormat lRealFormat = format();

    if(fmt.depth() && !lRealFormat.depth()) printf("OpenGL Buffer Warning: depth buffer requested but NOT granted.\n");
    if(fmt.alpha() && !lRealFormat.alpha()) printf("OpenGL Buffer Warning: alpha buffer requested but not granted.\n");

    if(!fmt.doubleBuffer() && lRealFormat.doubleBuffer()) printf("OpenGL Buffer Warning: single buffer requested but NOT granted.\n");
    if(fmt.doubleBuffer() && !lRealFormat.doubleBuffer()) printf("OpenGL Buffer Warning: double buffer requested but NOT granted.\n");
    if(fmt.stereo() && !lRealFormat.stereo()) printf("OpenGL Buffer Warning: stereo buffer requested but NOT granted.\n");

    if(!fmt.rgba() && lRealFormat.rgba()) printf("OpenGL Buffer Warning: indexed color mode requested but NOT granted.\n");
    if(fmt.rgba() && !lRealFormat.rgba()) printf("OpenGL Buffer Warning: RGBA color mode requested but NOT granted.\n");
    if(fmt.sampleBuffers() && !lRealFormat.sampleBuffers()) printf("OpenGL Buffer Warning: multisample buffers requested but NOT granted.\n");

    if(fmt.accum() && !lRealFormat.accum()) printf("OpenGL Buffer Warning: accumulation buffer requested but not granted.\n");
    if(fmt.stencil() && !lRealFormat.stencil()) printf("OpenGL Buffer Warning: stencil buffer requested but not granted.\n");
    fflush(stdout);
}
void GLWidget::initializeGL()
{
    // get context opengl-version
    qDebug() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion();
    qDebug() << "Context valid: " << context()->isValid();
    qDebug() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion();
    qDebug() << "OpenGl information: VENDOR:       " << (const char*)glGetString(GL_VENDOR);
    qDebug() << "                    RENDERDER:    " << (const char*)glGetString(GL_RENDERER);
    qDebug() << "                    VERSION:      " << (const char*)glGetString(GL_VERSION);
    qDebug() << "                    GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);

    QGLFormat glFormat = QGLWidget::format();
    if ( !glFormat.sampleBuffers() )
        qWarning() << "Could not enable sample buffers";

    // Set the clear color to black
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

    // Prepare a complete shader program...
    if ( !prepareShaderProgram( ":/simple.vert", ":/simple.frag" ) )
        return;

    // we need a VAO in core!
    GLuint VAO;
    PglGenVertexArrays glGenVertexArrays = (PglGenVertexArrays) context()->getProcAddress("glGenVertexArrays");
    PglBindVertexArray glBindVertexArray = (PglBindVertexArray) context()->getProcAddress("glBindVertexArray");
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    // We need us some vertex data. Start simple with a triangle ;-)
    float points[] = { -0.5f, -0.5f, 0.0f, 1.0f,
                        0.5f, -0.5f, 0.0f, 1.0f,
                        0.0f,  0.5f, 0.0f, 1.0f };
    m_vertexBuffer.create();
    m_vertexBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    if ( !m_vertexBuffer.bind() )
    {
        qWarning() << "Could not bind vertex buffer to the context";
        return;
    }
    m_vertexBuffer.allocate( points, 3 * 4 * sizeof( float ) );

    // Bind the shader program so that we can associate variables from
    // our application to the shaders
    if ( !m_shader.bind() )
    {
        qWarning() << "Could not bind shader program to context";
        return;
    }

    // Enable the "vertex" attribute to bind it to our currently bound
    // vertex buffer.
    m_shader.setAttributeBuffer( "vertex", GL_FLOAT, 0, 4 );
    m_shader.enableAttributeArray( "vertex" );
}
Exemplo n.º 8
0
static void qt_format_to_attrib_list(const QGLFormat &f, int attribs[])
{
    int i = 0;
    attribs[i++] = GLX_RENDER_TYPE;
    attribs[i++] = GLX_RGBA_BIT;
    attribs[i++] = GLX_DRAWABLE_TYPE;
    attribs[i++] = GLX_PBUFFER_BIT;
    attribs[i++] = GLX_RED_SIZE;
    attribs[i++] = f.redBufferSize() == -1 ? 1 : f.redBufferSize();
    attribs[i++] = GLX_GREEN_SIZE;
    attribs[i++] = f.greenBufferSize() == -1 ? 1 : f.greenBufferSize();
    attribs[i++] = GLX_BLUE_SIZE;
    attribs[i++] = f.blueBufferSize() == -1 ? 1 : f.blueBufferSize();
    if (f.doubleBuffer()) {
        attribs[i++] = GLX_DOUBLEBUFFER;
        attribs[i++] = true;
    }
    if (f.depth()) {
        attribs[i++] = GLX_DEPTH_SIZE;
        attribs[i++] = f.depthBufferSize() == -1 ? 1 : f.depthBufferSize();
    }
    if (f.stereo()) {
        attribs[i++] = GLX_STEREO;
        attribs[i++] = true;
    }
    if (f.stencil()) {
        attribs[i++] = GLX_STENCIL_SIZE;
        attribs[i++] = f.stencilBufferSize() == -1 ? 1 : f.stencilBufferSize();
    }
    if (f.alpha()) {
        attribs[i++] = GLX_ALPHA_SIZE;
        attribs[i++] = f.alphaBufferSize() == -1 ? 1 : f.alphaBufferSize();
    }
    if (f.accum()) {
        attribs[i++] = GLX_ACCUM_RED_SIZE;
        attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
        attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
        attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
        if (f.alpha()) {
            attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
            attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
        }
    }
    if (f.sampleBuffers()) {
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
        attribs[i++] = 1;
        attribs[i++] = GLX_SAMPLES_ARB;
        attribs[i++] = f.samples() == -1 ? 4 : f.samples();
    }

    attribs[i] = XNone;
}
Exemplo n.º 9
0
void GLWidget::initializeGL()
{
 QGLFormat glFormat = QGLWidget::format();
 if ( !glFormat.sampleBuffers() )
 qWarning() << "Could not enable sample buffers";

 // Set the clear color to black
 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

 // Prepare a complete shader program…
 if ( !prepareShaderProgram( ":/simple.vert", ":/simple.frag" ) )
 return;

 // We need us some vertex data. Start simple with a triangle ;-)
 float points[] = {-0.5f, -0.5f, 0.0f, 1.0f,
 0.5f, -0.5f, 0.0f, 1.0f,
 0.0f, 0.5f, 0.0f, 1.0f};
 m_vertexBuffer.create();
 m_vertexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
 if ( !m_vertexBuffer.bind() )
 {
 qWarning() << "Could not bind vertex buffer to the context";
 return;
 }
 m_vertexBuffer.allocate( points, 3 * 4 * sizeof( float ) );

 // Bind the shader program so that we can associate variables from
 // our application to the shaders
 if ( !m_shader.bind() )
 {
 qWarning() << "Could not bind shader program to context";
 return;
 }

 // Enable the "vertex" attribute to bind it to our currently bound
 // vertex buffer.
 m_shader.setAttributeBuffer( "vertex", GL_FLOAT, 0, 4 );
 m_shader.enableAttributeArray( "vertex" );

 /*glfuncs = m_context->versionFunctions();
 if (!glfuncs) {
   qDebug()<<"Не поддерживается версия OpenGL этой программой.";
   exit(1); //Хъюстон, у нас проблема номер раз
 }
 glfuncs->initializeOpenGLFunctions();

 uint vao;
 glGenVertexArrays(1, &vao);
 glBindVertexArray(vao);//*/

 qDebug() << QString((const char*)glGetString(GL_VERSION)) << "\n" << QString((const char*)glGetString(GL_VENDOR))<< "\n" << QString((const char*)glGetString(GL_RENDERER));//<< "\n" << glGetString(GL_EXTENTIONS);

}
Exemplo n.º 10
0
void GraphicsWindowQt::qglFormat2traits( const QGLFormat& format, osg::GraphicsContext::Traits* traits )
{
    traits->red = format.redBufferSize();
    traits->green = format.greenBufferSize();
    traits->blue = format.blueBufferSize();
    traits->alpha = format.alpha() ? format.alphaBufferSize() : 0;
    traits->depth = format.depth() ? format.depthBufferSize() : 0;
    traits->stencil = format.stencil() ? format.stencilBufferSize() : 0;

    traits->sampleBuffers = format.sampleBuffers() ? 1 : 0;
    traits->samples = format.samples();

    traits->quadBufferStereo = format.stereo();
    traits->doubleBuffer = format.doubleBuffer();

    traits->vsync = format.swapInterval() >= 1;
}
Exemplo n.º 11
0
string glinfo::
        pretty_format(const QGLFormat& f)
{
    QGLFormat::OpenGLVersionFlags flag = f.openGLVersionFlags();

    stringstream s;
    s << "accum=" << f.accum() << endl
      << "accumBufferSize=" << f.accumBufferSize() << endl
      << "alpha=" << f.alpha() << endl
      << "alphaBufferSize=" << f.alphaBufferSize() << endl
      << "blueBufferSize=" << f.blueBufferSize() << endl
      << "depth=" << f.depth() << endl
      << "depthBufferSize=" << f.depthBufferSize() << endl
      << "directRendering=" << f.directRendering() << endl
      << "doubleBuffer=" << f.doubleBuffer() << endl
      << "greenBufferSize=" << f.greenBufferSize() << endl
      << "hasOverlay=" << f.hasOverlay() << endl
      << "redBufferSize=" << f.redBufferSize() << endl
      << "rgba=" << f.rgba() << endl
      << "sampleBuffers=" << f.sampleBuffers() << endl
      << "samples=" << f.samples() << endl
      << "stencil=" << f.stencil() << endl
      << "stencilBufferSize=" << f.stencilBufferSize() << endl
      << "stereo=" << f.stereo() << endl
      << "swapInterval=" << f.swapInterval() << endl
      << "" << endl
      << "hasOpenGL=" << f.hasOpenGL() << endl
      << "hasOpenGLOverlays=" << f.hasOpenGLOverlays() << endl
      << "OpenGL_Version_None=" << (QGLFormat::OpenGL_Version_None == flag) << endl
      << "OpenGL_Version_1_1=" << (QGLFormat::OpenGL_Version_1_1 & flag) << endl
      << "OpenGL_Version_1_2=" << (QGLFormat::OpenGL_Version_1_2 & flag) << endl
      << "OpenGL_Version_1_3=" << (QGLFormat::OpenGL_Version_1_3 & flag) << endl
      << "OpenGL_Version_1_4=" << (QGLFormat::OpenGL_Version_1_4 & flag) << endl
      << "OpenGL_Version_1_5=" << (QGLFormat::OpenGL_Version_1_5 & flag) << endl
      << "OpenGL_Version_2_0=" << (QGLFormat::OpenGL_Version_2_0 & flag) << endl
      << "OpenGL_Version_2_1=" << (QGLFormat::OpenGL_Version_2_1 & flag) << endl
      << "OpenGL_Version_3_0=" << (QGLFormat::OpenGL_Version_3_0 & flag) << endl
      << "OpenGL_ES_CommonLite_Version_1_0=" << (QGLFormat::OpenGL_ES_CommonLite_Version_1_0 & flag) << endl
      << "OpenGL_ES_Common_Version_1_0=" << (QGLFormat::OpenGL_ES_Common_Version_1_0 & flag) << endl
      << "OpenGL_ES_CommonLite_Version_1_1=" << (QGLFormat::OpenGL_ES_CommonLite_Version_1_1 & flag) << endl
      << "OpenGL_ES_Common_Version_1_1=" << (QGLFormat::OpenGL_ES_Common_Version_1_1 & flag) << endl
      << "OpenGL_ES_Version_2_0=" << (QGLFormat::OpenGL_ES_Version_2_0 & flag) << endl;

    return s.str();
}
Exemplo n.º 12
0
FLGLWidget::FLGLWidget(const QGLFormat& format, QWidget * parent) :
    QGLWidget(format, parent),
    m_vertexBuffer( QGLBuffer::VertexBuffer )
{
    QGLFormat glFormat = QGLWidget::format();
    qDebug() << QString( "OpenGL Version = %1, %2" )
    .arg( glFormat.majorVersion() )
    .arg( glFormat.minorVersion() );
    
    if ( !glFormat.sampleBuffers() )
        qWarning() << "Could not enable sample buffers";
    
    qDebug() << "OpenGL context valid =" << context()->isValid();

    logo = 0;
   xRot = 0;
   yRot = 0;
   zRot = 0;
}
Exemplo n.º 13
0
void Viewer::initializeGL() {
  // Do some OpenGL setup
  QGLFormat glFormat = QGLWidget::format();
  if (!glFormat.sampleBuffers()) {
    std::cerr << "Could not enable sample buffers." << std::endl;
    return;
  }

  glClearColor(0.7, 0.7, 0.7, 0.0);

  if (!mProgram.addShaderFromSourceFile(QGLShader::Vertex, "shader.vert")) {
    std::cerr << "Cannot load vertex shader." << std::endl;
    return;
  }

  if (!mProgram.addShaderFromSourceFile(QGLShader::Fragment, "shader.frag")) {
    std::cerr << "Cannot load fragment shader." << std::endl;
    return;
  }

  if (!mProgram.link()) {
    std::cerr << "Cannot link shaders." << std::endl;
    return;
  }

  mVertexArrayObject.create();
  mVertexArrayObject.bind();

  mVertexBufferObject.create();
  mVertexBufferObject.setUsagePattern(QOpenGLBuffer::StaticDraw);

  if (!mVertexBufferObject.bind()) {
    std::cerr << "could not bind vertex buffer to the context." << std::endl;
    return;
  }

  mProgram.bind();

  mProgram.enableAttributeArray("vert");
  mProgram.setAttributeBuffer("vert", GL_FLOAT, 0, 3);

  mColorLocation = mProgram.uniformLocation("frag_color");
}
Exemplo n.º 14
0
void paint_RoundedRect(QPainter &p)
{
    static bool first = true;
    if (first) {
        if (testWidget) {
            QGLFormat format = testWidget->format();
            if (!format.sampleBuffers())
                qWarning() << "Cannot paint antialiased rounded rect without sampleBuffers";
        }
        first = false;
    }
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(Qt::black);
    p.setBrush(Qt::red);
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < lines; ++j) {
            QSize size((j+1)*50, spacing-1);
            p.drawRoundedRect(QRectF(QPointF(0,j*spacing), size), 8, 8);
        }
    }
}
Exemplo n.º 15
0
/*!
    Returns a window format for the OpenGL format specified by \a format.
*/
QSurfaceFormat QGLFormat::toSurfaceFormat(const QGLFormat &format)
{
    QSurfaceFormat retFormat;
    if (format.alpha())
        retFormat.setAlphaBufferSize(format.alphaBufferSize() == -1 ? 1 : format.alphaBufferSize());
    if (format.blueBufferSize() >= 0)
        retFormat.setBlueBufferSize(format.blueBufferSize());
    if (format.greenBufferSize() >= 0)
        retFormat.setGreenBufferSize(format.greenBufferSize());
    if (format.redBufferSize() >= 0)
        retFormat.setRedBufferSize(format.redBufferSize());
    if (format.depth())
        retFormat.setDepthBufferSize(format.depthBufferSize() == -1 ? 1 : format.depthBufferSize());
    retFormat.setSwapBehavior(format.doubleBuffer() ? QSurfaceFormat::DoubleBuffer : QSurfaceFormat::DefaultSwapBehavior);
    if (format.sampleBuffers())
        retFormat.setSamples(format.samples() == -1 ? 4 : format.samples());
    if (format.stencil())
        retFormat.setStencilBufferSize(format.stencilBufferSize() == -1 ? 1 : format.stencilBufferSize());
    retFormat.setStereo(format.stereo());
    return retFormat;
}
Exemplo n.º 16
0
void RCDraw::init( )
{
	zoomMul = 0.5;
	invertedVerticalAxis=false;
	visibleCenter = QVec(2,0);
	DRAW_AXIS = false;
	DRAW_PERIMETER = false;
	imageScale = 1.;
	if (qimg!=NULL)
	{
		imageScale = width/qimg->width();
	}
	else
	{
		qimg = new QImage ( width, height, QImage::Format_Indexed8 );
		qimg->fill ( 240 );
	}
	//Gray color table
	ctable.resize ( 256 );
	for ( int i=0; i < 256; i++ )
	{
		ctable[i] = qRgb ( i,i,i );
	}
	qimg->setColorTable ( ctable );
	translating = false;
	effWin = win;
	QGLFormat f = format();
	if (f.sampleBuffers())
	{
		f.setSampleBuffers( true );
		setFormat( f );
		std::cout << "Sample Buffers On in QGLWidget" << std::endl;
	}
	else
	{
		std::cout << "Sample Buffers Off in QGLWidget" << std::endl;
	}
	show();
}
Exemplo n.º 17
0
// static
void SharedGLContext::setWidget(const QGLWidget* pWidget) {
    s_pSharedGLWidget = pWidget;
    qDebug() << "Set root GL Context widget valid:"
             << pWidget << (pWidget && pWidget->isValid());
    const QGLContext* pContext = pWidget->context();
    qDebug() << "Created root GL Context valid:" << pContext
             << (pContext && pContext->isValid());
    if (pWidget) {
        QGLFormat format = pWidget->format();
        qDebug() << "Root GL Context format:";
        qDebug() << "Double Buffering:" << format.doubleBuffer();
        qDebug() << "Swap interval:" << format.swapInterval();
        qDebug() << "Depth buffer:" << format.depth();
        qDebug() << "Direct rendering:" << format.directRendering();
        qDebug() << "Has overlay:" << format.hasOverlay();
        qDebug() << "RGBA:" << format.rgba();
        qDebug() << "Sample buffers:" << format.sampleBuffers();
        qDebug() << "Samples:" << format.samples();
        qDebug() << "Stencil buffers:" << format.stencil();
        qDebug() << "Stereo:" << format.stereo();
    }
}
Exemplo n.º 18
0
void GLWidget::initializeGL()
{
    QGLFormat glFormat = QGLWidget::format();
    if (!glFormat.sampleBuffers()) { std::cerr << "Could not enable sample buffers." << std::endl; return; }

    glShadeModel(GL_SMOOTH);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    glEnable(GL_DEPTH_TEST);

    _shaderProgram = new QOpenGLShaderProgram();
    if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "../OrganicLabyrinth/shader.vert"))
        { std::cerr << "Cannot load vertex shader." << std::endl; return; }

    if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "../OrganicLabyrinth/shader.frag"))
        { std::cerr << "Cannot load fragment shader." << std::endl; return; }

    if ( !_shaderProgram->link() )
        { std::cerr << "Cannot link shaders." << std::endl; return; }

    _shaderProgram->bind();
    _mvpMatrixLocation = _shaderProgram->uniformLocation("mvpMatrix");
    _colorLocation = _shaderProgram->attributeLocation("vertexColor");
    _vertexLocation = _shaderProgram->attributeLocation("vert");
    _use_color_location = _shaderProgram->uniformLocation("use_color");


    InitCells();
    InitDots();

    // a bad thing
    ResetData();

}
Exemplo n.º 19
0
/*!
    Returns a window format for the OpenGL format specified by \a format.
*/
QSurfaceFormat QGLFormat::toSurfaceFormat(const QGLFormat &format)
{
    QSurfaceFormat retFormat;
    if (format.alpha())
        retFormat.setAlphaBufferSize(format.alphaBufferSize() == -1 ? 1 : format.alphaBufferSize());
    if (format.blueBufferSize() >= 0)
        retFormat.setBlueBufferSize(format.blueBufferSize());
    if (format.greenBufferSize() >= 0)
        retFormat.setGreenBufferSize(format.greenBufferSize());
    if (format.redBufferSize() >= 0)
        retFormat.setRedBufferSize(format.redBufferSize());
    if (format.depth())
        retFormat.setDepthBufferSize(format.depthBufferSize() == -1 ? 1 : format.depthBufferSize());
    retFormat.setSwapBehavior(format.doubleBuffer() ? QSurfaceFormat::DoubleBuffer : QSurfaceFormat::DefaultSwapBehavior);
    if (format.sampleBuffers())
        retFormat.setSamples(format.samples() == -1 ? 4 : format.samples());
    if (format.stencil())
        retFormat.setStencilBufferSize(format.stencilBufferSize() == -1 ? 1 : format.stencilBufferSize());
    retFormat.setStereo(format.stereo());
    retFormat.setMajorVersion(format.majorVersion());
    retFormat.setMinorVersion(format.minorVersion());
    retFormat.setProfile(static_cast<QSurfaceFormat::OpenGLContextProfile>(format.profile()));
    return retFormat;
}
Exemplo n.º 20
0
static void qt_format_to_attrib_list(bool has_render_texture, const QGLFormat &f, int attribs[])
{
    int i = 0;
    attribs[i++] = WGL_SUPPORT_OPENGL_ARB;
    attribs[i++] = TRUE;
    attribs[i++] = WGL_DRAW_TO_PBUFFER_ARB;
    attribs[i++] = TRUE;

    if (has_render_texture) {
        attribs[i++] = WGL_BIND_TO_TEXTURE_RGBA_ARB;
        attribs[i++] = TRUE;
    }

    attribs[i++] = WGL_COLOR_BITS_ARB;
    attribs[i++] = 32;
    attribs[i++] = WGL_DOUBLE_BUFFER_ARB;
    attribs[i++] = FALSE;

    if (f.stereo()) {
        attribs[i++] = WGL_STEREO_ARB;
        attribs[i++] = TRUE;
    }
    if (f.depth()) {
        attribs[i++] = WGL_DEPTH_BITS_ARB;
        attribs[i++] = f.depthBufferSize() == -1 ? 24 : f.depthBufferSize();
    }
    if (f.redBufferSize() != -1) {
        attribs[i++] = WGL_RED_BITS_ARB;
        attribs[i++] = f.redBufferSize();
    }
    if (f.greenBufferSize() != -1) {
        attribs[i++] = WGL_GREEN_BITS_ARB;
        attribs[i++] = f.greenBufferSize();
    }
    if (f.blueBufferSize() != -1) {
        attribs[i++] = WGL_BLUE_BITS_ARB;
        attribs[i++] = f.blueBufferSize();
    }
    if (f.alpha()) {
        attribs[i++] = WGL_ALPHA_BITS_ARB;
        attribs[i++] = f.alphaBufferSize() == -1 ? 8 : f.alphaBufferSize();
    }
    if (f.accum()) {
        attribs[i++] = WGL_ACCUM_BITS_ARB;
        attribs[i++] = f.accumBufferSize() == -1 ? 16 : f.accumBufferSize();
    }
    if (f.stencil()) {
        attribs[i++] = WGL_STENCIL_BITS_ARB;
        attribs[i++] = f.stencilBufferSize() == -1 ? 8 : f.stencilBufferSize();
    }
    if ((f.redBufferSize() > 8 || f.greenBufferSize() > 8
         || f.blueBufferSize() > 8 || f.alphaBufferSize() > 8)
        && (QGLExtensions::glExtensions() & QGLExtensions::NVFloatBuffer))
    {
        attribs[i++] = WGL_FLOAT_COMPONENTS_NV;
        attribs[i++] = TRUE;
    }
    if (f.sampleBuffers()) {
        attribs[i++] = WGL_SAMPLE_BUFFERS_ARB;
        attribs[i++] = 1;
        attribs[i++] = WGL_SAMPLES_ARB;
        attribs[i++] = f.samples() == -1 ? 16 : f.samples();
    }
    attribs[i] = 0;
}
Exemplo n.º 21
0
/*
  See qgl.cpp for qdoc comment.
 */
void *QGLContext::chooseVisual()
{
    Q_D(QGLContext);
    static const int bufDepths[] = { 8, 4, 2, 1 };        // Try 16, 12 also?
    //todo: if pixmap, also make sure that vi->depth == pixmap->depth
    void* vis = 0;
    int i = 0;
    bool fail = false;
    QGLFormat fmt = format();
    bool tryDouble = !fmt.doubleBuffer();  // Some GL impl's only have double
    bool triedDouble = false;
    bool triedSample = false;
    if (fmt.sampleBuffers())
        fmt.setSampleBuffers(QGLExtensions::glExtensions & QGLExtensions::SampleBuffers);
    while(!fail && !(vis = tryVisual(fmt, bufDepths[i]))) {
        if (!fmt.rgba() && bufDepths[i] > 1) {
            i++;
            continue;
        }
        if (tryDouble) {
            fmt.setDoubleBuffer(true);
            tryDouble = false;
            triedDouble = true;
            continue;
        } else if (triedDouble) {
            fmt.setDoubleBuffer(false);
            triedDouble = false;
        }
        if (!triedSample && fmt.sampleBuffers()) {
            fmt.setSampleBuffers(false);
            triedSample = true;
            continue;
        }
        if (fmt.stereo()) {
            fmt.setStereo(false);
            continue;
        }
        if (fmt.accum()) {
            fmt.setAccum(false);
            continue;
        }
        if (fmt.stencil()) {
            fmt.setStencil(false);
            continue;
        }
        if (fmt.alpha()) {
            fmt.setAlpha(false);
            continue;
        }
        if (fmt.depth()) {
            fmt.setDepth(false);
            continue;
        }
        if (fmt.doubleBuffer()) {
            fmt.setDoubleBuffer(false);
            continue;
        }
        fail = true;
    }
    d->glFormat = fmt;
    return vis;
}
Exemplo n.º 22
0
void Viewer::initializeGL() {
  sceneRoot->buildJointMap(&jointMap);
  for (auto& p : jointMap) {
    auto id = p.first;
    opMap.insert({id, Op()});
  }

  QGLFormat glFormat = QGLWidget::format();
  if (!glFormat.sampleBuffers()) {
    std::cerr << "Could not enable sample buffers." << std::endl;
    return;
  }

  glShadeModel(GL_SMOOTH);
  glClearColor(0.4, 0.4, 0.4, 0.0);
  glFrontFace(GL_CW);

  if (!sphereShaders.addShaderFromSourceFile(QGLShader::Vertex, "sphere.vert")) {
    std::cerr << "Cannot load vertex shader." << std::endl;
    return;
  }

  if (!sphereShaders.addShaderFromSourceFile(QGLShader::Fragment, "sphere.frag")) {
    std::cerr << "Cannot load fragment shader." << std::endl;
    return;
  }

  if (!sphereShaders.link()) {
    std::cerr << "Cannot link shaders." << std::endl;
    return;
  }

  mVao.create();
  mVao.bind();

  float circleData[120];
  auto w = width();
  auto h = height();
  double radius = w < h ? (float)w * 0.25 : (float)h * 0.25;
  initCircleData(circleData, radius, M_PI / 40);

  mCircleBufferObject.create();
  mCircleBufferObject.setUsagePattern(QOpenGLBuffer::StaticDraw);
  if (!mCircleBufferObject.bind()) {
    std::cerr << "Could not bind circle vertex buffer." << std::endl;
    return;
  }
  mCircleBufferObject.allocate(circleData, sizeof(circleData));

  // Nine floats per triangle
  float sphereData[numTriangles * 9];
  // Three vertices per triangle, and each has a normal (3 floats)
  float sphereNormals[numTriangles * 3 * 3];
  initSphereData(sphereData, sphereNormals, theta);

  mSphereBufferObject.create();
  mSphereBufferObject.setUsagePattern(QOpenGLBuffer::StaticDraw);
  if (!mSphereBufferObject.bind()) {
    std::cerr << "Could not bind sphere vertex buffer." << std::endl;
    return;
  }
  mSphereBufferObject.allocate(sphereData, sizeof(sphereData));

  // Sphere normal buffer
  mSphereNormalBuffer.create();
  mSphereNormalBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
  if (!mSphereNormalBuffer.bind()) {
    std::cerr << "Could not bind sphere normal buffer." << std::endl;
    return;
  }
  mSphereNormalBuffer.allocate(sphereNormals, sizeof(sphereNormals));

  sphereShaders.bind();
  sphereShaders.enableAttributeArray("vert");
  sphereShaders.enableAttributeArray("norm");
  sphereShaders.enableAttributeArray("flatFlag");

  mvpMatrixLoc = sphereShaders.uniformLocation("mvpMatrix");
  mvMatrixLoc = sphereShaders.uniformLocation("mvMatrix");
  normMatrixLoc = sphereShaders.uniformLocation("normMatrix");
  lightPositionLoc = sphereShaders.uniformLocation("lightPosition");
  ambientLightLoc = sphereShaders.uniformLocation("ambientLight");
  diffuseColourLoc = sphereShaders.uniformLocation("diffuseColour");
  specularColourLoc = sphereShaders.uniformLocation("specularColour");
  shininessLoc = sphereShaders.uniformLocation("shininess");
  flatLoc = sphereShaders.uniformLocation("flatFlag");

  // Constant ambient light
  sphereShaders.setUniformValue(ambientLightLoc, QVector3D(0.1, 0.1, 0.1));
}
Exemplo n.º 23
0
void GLWidget::initializeGL()
{
	/* OpenGL format */
    QGLFormat glFormat = QGLWidget::format();
    if (!glFormat.sampleBuffers()) 
	{ 
		//std::cerr << "Could not enable sample buffers." << std::endl; 
		textEdit->append("Could not enable sample buffers.");
		return; 
	}

	/* Stuff I forget what their purposes are */
    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    glEnable(GL_DEPTH_TEST);

	/* Shaders */
    _shaderProgram = new QOpenGLShaderProgram();
    if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, SystemParams::v_shader_file.c_str()))
    { 
		//std::cerr << "Cannot load vertex shader." << std::endl; 
		textEdit->append("Cannot load vertex shader.");
		return; 
	}
    if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, SystemParams::f_shader_file.c_str()))
    { 
		//std::cerr << "Cannot load fragment shader." << std::endl; 
		textEdit->append("Cannot load fragment shader.");
		return; 
	}
    if ( !_shaderProgram->link() )
    { 
		//std::cerr << "Cannot link shaders." << std::endl; 
		textEdit->append("Cannot link shaders.");
		return; 
	}
    _shaderProgram->bind();
    _mvpMatrixLocation = _shaderProgram->uniformLocation("mvpMatrix");
    _colorLocation = _shaderProgram->attributeLocation("vertexColor");
    _vertexLocation = _shaderProgram->attributeLocation("vert");
    _use_color_location = _shaderProgram->uniformLocation("use_color");

	/* for VBO and VAO */
    _vDataHelper = new VertexDataHelper(_shaderProgram);
	_pathIO = new PathIO();

	// comment these
    //CreateCurve();
    //BuildCurveVertexData();

	// You want image or the actual path ?
	//SetImage(QString::fromStdString(SystemParams::temp_png_location));
	if (!_pathsVao.isCreated() && _paths.size() > 0)
	{
		InitializePaths();
	}

	// a box
	_boxes.push_back(ABox(AVector(0, 0),
						  AVector(0, this->_img_height),
						  AVector(this->_img_width, 0),
						  AVector(this->_img_width, this->_img_height)));
	_vDataHelper->BuildQuadsVertexData(_boxes, &_boxVbo, &_boxVao);

	// an example how to render a triangle
	//_triangles.push_back(ATriangle(AVector(1, 1), AVector(25, 25), AVector(1, 50)));
	//_vDataHelper->BuildTrianglesVertexData(_triangles, &_triangleVbo, &_triangleVao, QVector3D(1.0, 0.25, 0.25));

	// don't remove this
	textEdit->append("OpenGL is initialized");
	_isGLInitialized = true;
}
Exemplo n.º 24
0
AGLPixelFormat QGLContextPrivate::tryFormat(const QGLFormat &format)
{
    GLint attribs[40], cnt = 0;
    bool device_is_pixmap = (paintDevice->devType() == QInternal::Pixmap);

    attribs[cnt++] = AGL_RGBA;
    attribs[cnt++] = AGL_BUFFER_SIZE;
    attribs[cnt++] = device_is_pixmap ? static_cast<QPixmap *>(paintDevice)->depth() : 32;
    attribs[cnt++] = AGL_LEVEL;
    attribs[cnt++] = format.plane();

    if (format.redBufferSize() != -1) {
        attribs[cnt++] = AGL_RED_SIZE;
        attribs[cnt++] = format.redBufferSize();
    }
    if (format.greenBufferSize() != -1) {
        attribs[cnt++] = AGL_GREEN_SIZE;
        attribs[cnt++] = format.greenBufferSize();
    }
    if (format.blueBufferSize() != -1) {
        attribs[cnt++] = AGL_BLUE_SIZE;
        attribs[cnt++] = format.blueBufferSize();
    }
    if (device_is_pixmap) {
        attribs[cnt++] = AGL_PIXEL_SIZE;
        attribs[cnt++] = static_cast<QPixmap *>(paintDevice)->depth();
        attribs[cnt++] = AGL_OFFSCREEN;
        if(!format.alpha()) {
            attribs[cnt++] = AGL_ALPHA_SIZE;
            attribs[cnt++] = 8;
        }
    } else {
        if(format.doubleBuffer())
            attribs[cnt++] = AGL_DOUBLEBUFFER;
    }

    if(glFormat.stereo())
        attribs[cnt++] = AGL_STEREO;
    if(format.alpha()) {
        attribs[cnt++] = AGL_ALPHA_SIZE;
        attribs[cnt++] = format.alphaBufferSize() == -1 ? 8 : format.alphaBufferSize();
    }
    if(format.stencil()) {
        attribs[cnt++] = AGL_STENCIL_SIZE;
        attribs[cnt++] = format.stencilBufferSize() == -1 ? 8 : format.stencilBufferSize();
    }
    if(format.depth()) {
        attribs[cnt++] = AGL_DEPTH_SIZE;
        attribs[cnt++] = format.depthBufferSize() == -1 ? 32 : format.depthBufferSize();
    }
    if(format.accum()) {
        attribs[cnt++] = AGL_ACCUM_RED_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_BLUE_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_GREEN_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_ALPHA_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
    }
    if(format.sampleBuffers()) {
        attribs[cnt++] = AGL_SAMPLE_BUFFERS_ARB;
        attribs[cnt++] = 1;
        attribs[cnt++] = AGL_SAMPLES_ARB;
        attribs[cnt++] = format.samples() == -1 ? 4 : format.samples();
    }

    attribs[cnt] = AGL_NONE;
    Q_ASSERT(cnt < 40);
    return aglChoosePixelFormat(0, 0, attribs);
}
Exemplo n.º 25
0
QGLGraphicsSystem::QGLGraphicsSystem(bool useX11GL)
    : QGraphicsSystem(), m_useX11GL(useX11GL)
{
#if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
    // only override the system defaults if the user hasn't already
    // picked a visual
    if (X11->visual == 0 && X11->visual_id == -1 && X11->visual_class == -1) {
        // find a double buffered, RGBA visual that supports OpenGL
        // and set that as the default visual for windows in Qt
        int i = 0;
        int spec[16];
        spec[i++] = GLX_RGBA;
        spec[i++] = GLX_DOUBLEBUFFER;

        if (!qgetenv("QT_GL_SWAPBUFFER_PRESERVE").isNull()) {
            spec[i++] = GLX_DEPTH_SIZE;
            spec[i++] = 8;
            spec[i++] = GLX_STENCIL_SIZE;
            spec[i++] = 8;
            spec[i++] = GLX_SAMPLE_BUFFERS_ARB;
            spec[i++] = 1;
            spec[i++] = GLX_SAMPLES_ARB;
            spec[i++] = 4;
        }

        spec[i++] = XNone;

        XVisualInfo *vi = glXChooseVisual(X11->display, X11->defaultScreen, spec);
        if (vi) {
            X11->visual_id = vi->visualid;
            X11->visual_class = vi->c_class;

            QGLFormat format;
            int res;
            glXGetConfig(X11->display, vi, GLX_LEVEL, &res);
            format.setPlane(res);
            glXGetConfig(X11->display, vi, GLX_DOUBLEBUFFER, &res);
            format.setDoubleBuffer(res);
            glXGetConfig(X11->display, vi, GLX_DEPTH_SIZE, &res);
            format.setDepth(res);
            if (format.depth())
                format.setDepthBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_RGBA, &res);
            format.setRgba(res);
            glXGetConfig(X11->display, vi, GLX_RED_SIZE, &res);
            format.setRedBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_GREEN_SIZE, &res);
            format.setGreenBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_BLUE_SIZE, &res);
            format.setBlueBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_ALPHA_SIZE, &res);
            format.setAlpha(res);
            if (format.alpha())
                format.setAlphaBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_ACCUM_RED_SIZE, &res);
            format.setAccum(res);
            if (format.accum())
                format.setAccumBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_STENCIL_SIZE, &res);
            format.setStencil(res);
            if (format.stencil())
                format.setStencilBufferSize(res);
            glXGetConfig(X11->display, vi, GLX_STEREO, &res);
            format.setStereo(res);
            glXGetConfig(X11->display, vi, GLX_SAMPLE_BUFFERS_ARB, &res);
            format.setSampleBuffers(res);
            if (format.sampleBuffers()) {
                glXGetConfig(X11->display, vi, GLX_SAMPLES_ARB, &res);
                format.setSamples(res);
            }

            QGLWindowSurface::surfaceFormat = format;
            XFree(vi);

            printf("using visual class %x, id %x\n", X11->visual_class, X11->visual_id);
        }
    }
#elif defined(Q_WS_WIN)
    QGLWindowSurface::surfaceFormat.setDoubleBuffer(true);

    qt_win_owndc_required = true;
#endif
}
Exemplo n.º 26
0
bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
{
    QGLTemporaryContext tempContext;

    PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB =
        (PFNWGLCREATEPBUFFERARBPROC) wglGetProcAddress("wglCreatePbufferARB");
    PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB =
        (PFNWGLGETPBUFFERDCARBPROC) wglGetProcAddress("wglGetPbufferDCARB");
    PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB =
        (PFNWGLQUERYPBUFFERARBPROC) wglGetProcAddress("wglQueryPbufferARB");
    PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB =
        (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");

    if (!wglCreatePbufferARB) // assumes that if one can be resolved, all of them can
        return false;

    dc = wglGetCurrentDC();
    Q_ASSERT(dc);
    has_render_texture = false;

    // sample buffers doesn't work in conjunction with the render_texture extension
    if (!f.sampleBuffers()) {
        PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB =
                (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");

        if (wglGetExtensionsStringARB) {
            QString extensions(QLatin1String(wglGetExtensionsStringARB(dc)));
            has_render_texture = extensions.contains(QLatin1String("WGL_ARB_render_texture"));
        }
    }

    int attribs[40];
    qt_format_to_attrib_list(has_render_texture, f, attribs);

    // Find pbuffer capable pixel format.
    unsigned int num_formats = 0;
    int pixel_format;
    wglChoosePixelFormatARB(dc, attribs, 0, 1, &pixel_format, &num_formats);

    // some GL implementations don't support pbuffers with accum
    // buffers, so try that before we give up
    if (num_formats == 0 && f.accum()) {
        QGLFormat tmp = f;
        tmp.setAccum(false);
        qt_format_to_attrib_list(has_render_texture, tmp, attribs);
        wglChoosePixelFormatARB(dc, attribs, 0, 1, &pixel_format, &num_formats);
    }

    if (num_formats == 0) {
        qWarning("QGLPixelBuffer: Unable to find a pixel format with pbuffer  - giving up.");
        return false;
    }
    format = pfiToQGLFormat(dc, pixel_format);

    // NB! The below ONLY works if the width/height are powers of 2.
    // Set some pBuffer attributes so that we can use this pBuffer as
    // a 2D RGBA texture target.
    int pb_attribs[] = {WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
                        WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, 0};
    int pb_attribs_null[] = {0};

    pbuf = wglCreatePbufferARB(dc, pixel_format, size.width(), size.height(),
                               has_render_texture ? pb_attribs : pb_attribs_null);
    if (!pbuf) {
        // try again without the render_texture extension
        pbuf = wglCreatePbufferARB(dc, pixel_format, size.width(), size.height(), pb_attribs_null);
        has_render_texture = false;
        if (!pbuf) {
            qWarning("QGLPixelBuffer: Unable to create pbuffer [w=%d, h=%d] - giving up.", size.width(), size.height());
            return false;
        }
    }

    dc = wglGetPbufferDCARB(pbuf);
    ctx = wglCreateContext(dc);
    if (!dc || !ctx) {
        qWarning("QGLPixelBuffer: Unable to create pbuffer context - giving up.");
        return false;
    }

    // Explicitly disable the render_texture extension if we have a 
    // multi-sampled pbuffer context. This seems to be a problem only with 
    // ATI cards if multi-sampling is forced globally in the driver.
    wglMakeCurrent(dc, ctx);
    GLint samples = 0;
    glGetIntegerv(GL_SAMPLES_ARB, &samples);
    if (has_render_texture && samples != 0)
        has_render_texture = false;

    HGLRC share_ctx = shareWidget ? shareWidget->d_func()->glcx->d_func()->rc : 0;
    if (share_ctx && !wglShareLists(share_ctx, ctx))
        qWarning("QGLPixelBuffer: Unable to share display lists - with share widget.");

    int width, height;
    wglQueryPbufferARB(pbuf, WGL_PBUFFER_WIDTH_ARB, &width);
    wglQueryPbufferARB(pbuf, WGL_PBUFFER_HEIGHT_ARB, &height);
    return true;
}