Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. <Qt5.5 Problems> QOpenGLWidget not render anything in Mac OS X
Forum Updated to NodeBB v4.3 + New Features

<Qt5.5 Problems> QOpenGLWidget not render anything in Mac OS X

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 710 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KunLin
    wrote on last edited by KunLin
    #1

    Hi, Here is my problem, can anyone help me?

    I'm trying to use QOpenGLWidget to render a trivial example, a triangle. But the problem is that the widget is totally black and not rendering anything.

    In main.cpp:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);        
    
        QSurfaceFormat format;
        format.setProfile (QSurfaceFormat::CoreProfile);
        format.setVersion (4 , 3);
        format.setSwapBehavior (QSurfaceFormat::DoubleBuffer);
        format.setRenderableType (QSurfaceFormat::OpenGL);
        QSurfaceFormat::setDefaultFormat (format);
        
        MyViewer.show();
    
        return app.exec();
    }
    

    In MyViewer.cpp:

    namespace {
        static const char VertexShaderCode[] =
            "#version 410\n"
            "layout(location=0) in vec2 position;"
            "layout(location=1) in vec3 color;"
            ""
            ""
            "void main()"
            "{"
            "   gl_Position = vec4(position, 0.0, 1.0);"
            "}\0";
    
        static const char FragmentShaderCode[] =
            "#version 410\n"
            ""
            "out vec4 output_color;"
            ""
            "void main()"
            "{"
            "   output_color = vec4(1.0, 0.0, 0.0, 1.0);"
            "}\0";
    
        void InstallShaders () {
            QOpenGLShader vertex_shader (QOpenGLShader::Vertex , 0);
            QOpenGLShader fragment_shader (QOpenGLShader::Fragment , 0);
    
            vertex_shader.compileSourceCode (::VertexShaderCode);
            fragment_shader.compileSourceCode (::FragmentShaderCode);
    
            assert(vertex_shader.isCompiled () and fragment_shader.isCompiled ());
    
            QOpenGLShaderProgram program;
    
            program.addShader (& vertex_shader);
            program.addShader (& fragment_shader);
    
            program.link ();
    
            assert(program.isLinked ());
    
            assert(program.bind ());
            // glUseProgram (program.programId ());
        }
    };
    
    ...
    
    void MyViewer::InitializeGL() {
        initializeOpenGLFunctions();
        InistallShaders();
        GLuint buffer_id;
        glGenBuffers (1 , & buffer_id);
        glBindBuffer (GL_ARRAY_BUFFER , buffer_id);
    
        GLfloat vertices[] = {
            +0.0f , +0.8f , // top
            +1.0f , +0.0f , +0.0f , // red
            -0.8f , -0.8f , // left
            +0.0f , +1.0f , +0.0f , // blue
            +0.8f , -0.8f , // right
            +0.0f , +0.0f , +1.0f , // green
        };
    
        glBufferData (GL_ARRAY_BUFFER , sizeof (vertices) , vertices , GL_STATIC_DRAW);
        glEnableVertexAttribArray (0);
        glEnableVertexAttribArray (1);
    
        glVertexAttribPointer (0 , 2 , GL_FLOAT , GL_FALSE , sizeof (float) * 5 , 0);
        glVertexAttribPointer (1 , 3 , GL_FLOAT , GL_FALSE , sizeof (float) * 5 , (char *) (sizeof (float) * 2));
    }
    
    void MyViewer::paintGL () {
        glDrawArrays (GL_TRIANGLES , 0 , 3);
    }
    

    But nothing is rendered and I only got a black window.

    I tried to render the triangle without shaders too, and remove the

    glVertexAttribPointer(1);

    and

    glVertexAttribPointer (1 , 3 , GL_FLOAT , GL_FALSE , sizeof (float) * 5 , (char *) (sizeof (float) * 2));

    in order to render a white triangle on the black plane, but it's not working either. By the way, build and all the assert(...) succeeded, and I think the shader compilation worked out fine.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Because you are asking for an OpenGL 4.3 context which is currently not available on OS X. AFAIK 4.1 is the most you can get depending on the version of OS X you are running.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved