0

I am trying to create a very simple qt opengl project on mac (drawing a dot in the center of the widget). Following are my code:

  • myOpenglWidget.h
//myopenglwidget.h
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H
 
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class QOpenGLShaderProgram;
 
class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
 
public:
    explicit MyOpenGLWidget(QWidget *parent = 0);
 
protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int width, int height) override;
 
private:
    QOpenGLShaderProgram *program;
};
 
#endif // MYOPENGLWIDGET_H
  • myOpenglWidget.cpp
//myopenglwidget.cpp
#include "myopenglwidget.h"
#include <QOpenGLShaderProgram>
 
MyOpenGLWidget::MyOpenGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{
}
 
void MyOpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
 
    // Create vertex shader
    QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
    const char *vsrc =
            "void main() {                             \n"
            "   gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
            "}                                         \n";
    vshader->compileSourceCode(vsrc);

    // Create fragment shader
    QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
    const char *fsrc =
            "void main() {                              \n"
            "   gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
            "}                                          \n";
    fshader->compileSourceCode(fsrc);
 
    // create shader
    program = new QOpenGLShaderProgram;
    program->addShader(vshader);
    program->addShader(fshader);
    program->link();
    program->bind();
}
 
void MyOpenGLWidget::resizeGL(int , int )
{
}
 
void MyOpenGLWidget::paintGL()
{
    //draw
    glDrawArrays(GL_POINTS, 0, 1);
}
  • main.cpp
//main.cpp
#include <QApplication>
#include "myopenglwidget.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    MyOpenGLWidget w;
    w.resize(400, 300);
    w.show();
    return app.exec();
}

I tried it on windows and it works perfectly fine. However, when I wrtie the same code on Qt creator on mac, what it shows me is only a blank black window. I know Apple has started to stop support opengl, but have no idea whether it's related to my issue. I wonder is it possible to use shader in this way to make it work on mac?

My mac version is MAC OSX 10.15

Kitty
  • 1
  • 1
  • The answer should be: You should check for errors in your code. Specifically, you should check for errors generated after a function call. – user14063792468 Dec 27 '20 at 15:01
  • @yvw I feel very sorry if I give you an impression that I want to get the answer without making any effort. I am new to opengl and I have worked on this for 2 days.i have read different documents and questions but still don't know if it is possible to use glDrawArrays(or shaders) to draw the dot on mac. – Kitty Dec 27 '20 at 15:08
  • That's not about the impression. `QOpenGLWidget` may throw an exception(actually I do not know, but you read the documentation, you shall know better), other objects may also throw an exception on their construction. `glDrawArrays` sets an error flag on failure. There is a `glGetError` for the `OpenGL` functions. So, as was stated by me, you should check for errors. – user14063792468 Dec 27 '20 at 15:16
  • @Kitty Did you check [`glGetError()`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetError.xhtml)? Also check out ["What is the best way to debug OpenGL?"](https://stackoverflow.com/questions/518063/what-is-the-best-way-to-debug-opengl). Unsure if [Debug Output](https://www.khronos.org/opengl/wiki/Debug_Output) is supported on OS X today, it wasn't 5 years ago. – vallentin Dec 27 '20 at 15:17
  • Thank you ywn and vallentin, I will learn to use glGetError() to debug the opengl code. qt creator compilered the project successfully and didn't throw any error, so I guess I have to go deep into the code. – Kitty Dec 27 '20 at 15:35
  • It is not answer but you can make your code shorter. See my example that draw a triangle: https://rextester.com/ZTRI45421 Try to run it on macOS. – 8Observer8 Dec 31 '20 at 18:58

0 Answers0