简体   繁体   中英

OpenGL cannot draw in QtWidget at macOS

I try to use OpenGL combine with Qt in macOS platform, the system version is:Monterey.
because I want to create OpenGL context by myself and split the render thread from UI thread(main thread, that's why I cannot use QOpenglWidget), so I did following step:

  1. create openGlContext using NSOpenGLContext
  2. call NSOpenGlContext.setView, the argument of setView is (NSView*)QWidget::winId()
  3. create a thread in Qt's showEvent for calling the OpenGL API

as result, the widget Windows can appear but there is nothing appear. (even I call glClear to clear color, but the color cannot appear in window(view).)
but at the same time, I can get the render result correctly when I used the RenderDoc to capture frame in macOS.
I have doubted that the QT change the default framebuffer, so I try to call glBindFrameBuffer(GL_FRAMEBUFFER,0), but it did not work.
then I use the same code in Windows(use wglXXX function to create OpenGL context ),it did work and render correct.

So how should I do to using OpenGL in QtWidget?
the code as below:

create OpenGL context in macOS:


#import <Cocoa/Cocoa.h>
#include "MacosOpenGLContext.h"
#include <iostream>



#include "MacosOpenGLContext.h"

MacosOpenGLContext::MacosOpenGLContext(NSView* winID,SHK::Setting setting){
    this->createGLContext(winID,setting);
}

void MacosOpenGLContext::makeCurrent(){
    if(this->_openGLContext!=nullptr){
     [this->_openGLContext makeCurrentContext];
    }
}
void MacosOpenGLContext::flushBuffer(){
    if(this->_openGLContext!=nullptr){
        [this->_openGLContext flushBuffer];
    }
}
void MacosOpenGLContext::setView(WinID id){
    if(this->_openGLContext!=nullptr){
        [this->_openGLContext setView:(NSView*)id];
    }
}
void MacosOpenGLContext::update(){
    if(this->_openGLContext!=nullptr){
        [this->_openGLContext update];
    }
}
bool MacosOpenGLContext::isCreateSuccess(){
    return !(this->_openGLContext==nullptr);
}
void MacosOpenGLContext::makeNullCurrent(){

}
void MacosOpenGLContext::createGLContext(NSView* winID,SHK::Setting setting){
    _openGLContext=nullptr;
    NSOpenGLPixelFormatAttribute attrs[] = {
        NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core,
        NSOpenGLPFAColorSize,32,
        NSOpenGLPFADepthSize,16,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAAccelerated,
        0
    };
   NSOpenGLPixelFormat*  _pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
   _pixelFormat=[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
   if(_pixelFormat==nullptr){
       std::cout<<"create macos pixelFormat failed";
       return;
   }
   this->_openGLContext=
           [[NSOpenGLContext alloc] initWithFormat: _pixelFormat shareContext: nullptr];
    if(this->_openGLContext==nullptr){
        std::cout<<"the opengl context create failed";
        return;
    }
    [_pixelFormat release];
    _pixelFormat=nullptr;

    this->setView((WinID)winID);
//    this->makeCurrent();

}
MacosOpenGLContext::MacosOpenGLContext(WinID winID){
    SHK::Setting settings;
    settings.colorBits = 24;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.majorVersion = 4;
    settings.minorVersion = 1;
    this->createGLContext((NSView*)winID,settings);
}

the Widget code as blow:



#include "TestQtOpenGL.hpp"
#if defined(MACOS)
#include "../platform/Macos/MacosOpenGLContext.h"
#elif defined(WIN)
#include "../platform/Windows/Win32OpenGLContext.hpp"
#endif
#include <GL/glew.h>
#include <iostream>
#include "../Shader.hpp"

const char *vertexShaderSource = "#version 330 core\n"
                                 "layout (location = 0) in vec3 aPos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
                                 "}\0";
const char *fragmentShaderSource = "#version 330 core\n"
                                   "out vec4 FragColor;\n"
                                   "void main()\n"
                                   "{\n"
                                   "   FragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);\n"
                                   "}\n\0";



TestQtOpenGL::TestQtOpenGL()
{
#if defined(MACOS)
    _context = std::make_shared<MacosOpenGLContext>((WinID)winId());
#elif defined(WIN)
    _context = std::make_shared<Win32OpenGLContext>((HWND)winId());
#endif
}
void TestQtOpenGL::paintEvent(QPaintEvent* event)
{
    QWidget::paintEvent(event);

}
void TestQtOpenGL::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);

    _thread=std::thread([this](){
        this->_context->setView(WinID (this->winId()));
        this->_context->update();
        this->_context->makeCurrent();
        if(!_inited){
            if(glewInit()!=GLEW_OK){
                std::cout<<"init glew failed"<<std::endl;
            }
            _inited = true;
        }


        {
            std::shared_ptr<TOOLS::Shader> _shader;
            unsigned int _vao, _vbo;
            if (glewInit() == GLEW_OK)
            {
                _shader = std::make_shared<TOOLS::Shader>("TestG3DShader");
                {
                    // this->makeCurrent();
                    float points[] = {0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
                    glGenVertexArrays(1, &_vao);
                    glBindVertexArray(_vao);
                    glGenBuffers(1, &_vbo);
                    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
                    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
                    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
                    glEnableVertexAttribArray(0);
                }
            }
            while(true){
                glBindFramebuffer(GL_FRAMEBUFFER,0);
                glViewport(0,0,400,400);
                glBindVertexArray(_vao);
                glClearColor(0.0f, 0.3f, 0.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                _shader->use();

                glDrawArrays(GL_TRIANGLES, 0, 3);
                this->_context->flushBuffer();
            }
        }
    });
    _thread.detach();


}
void TestQtOpenGL::resizeEvent(QResizeEvent* event)
{
    QWidget::resizeEvent(event);
    this->_context->update();
}

the renderDoc result of macOS is as blow: renderDoc result

the QWidget result is as below: enter image description here

I also encountered the same problem. QWidget can't render, but QOpenGlWidget can. It may be the problem of NSView Layer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM