简体   繁体   中英

Qt 5.3. QWidget direct painting with OpenGL

Good day. Is there any ways to paint QWidget -based window without QPainter . I try to use raw OpenGL for drawing with the following code:

1) Main function:

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    GLWindow* window = new GLWindow;
    window->setMinimumSize(200, 200);
    window->initializeGLContext();
    window->show();
    return app.exec(); 
}

GLWindow inhereted from QMainWindow . I use glew library in order to work with OpenGL . Below is a simplified version of the code without error handling and stupid program flow.

namespace {
    GLfloat vertices[] = {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };
    const char* vertexShaderSource =
        "in vec2 position;"
         "void main() {"
         "gl_Position = vec4(position, 0.0, 1.0); }";

    const char* fragmentShaderSource =
        "out vec4 outColor;"
        "void main() {"
        "outColor = vec4(1.0, 0.0, 0.0, 1.0); }";
}

GLWindow::GLWindow() {
    setAttribute(Qt::WA_PaintOnScreen, true); }

void GLWindow::initializeGLContext() {

    PIXELFORMATDESCRIPTOR pfd = {
        sizeof(PIXELFORMATDESCRIPTOR), 1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0,
        24, 8, 0,
        PFD_MAIN_PLANE, 0, 0, 0, 0 };

    HDC hdc = GetDC((HWND)winId());

    int windowPixelFormat = ChoosePixelFormat(hdc, &pfd);
    SetPixelFormat(hdc, windowPixelFormat, &pfd);

    HGLRC hglrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc, hglrc);
}

QPaintEngine* GLWindow::paintEngine() const { return 0; }

void GLWindow::paintEvent(QPaintEvent *) {

    glewInit();

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    SwapBuffers(GetDC((HWND)winId()));
}

After program execution all that i have is black screen without triangle. I'm a novice in Qt and if you have any suggestions what is going wrong, please share them with me.

Best regards.

您应该使用基类QGLWidget并改写paintGL

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