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