How to look for equivalent functions of OpenGL in

2019-04-02 11:51发布

I'm new to OpenGL and Glut. There is a project implemented by Glut. I googled and found that there is an OpenGL implementation in Qt, called QGLWidget. However, it's hard for me converting the old Glut code to new Qt code since I don't know how to find equivalent function for Glut functions in Qt. Part of the code look like this:

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(gray1->width,gray1->height);
glutInitWindowPosition(100,100);
glutCreateWindow("hello");
init();
glutDisplayFunc(&display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(mouse_move);
glutMainLoop();

The glut* functions above don't exist in Qt's document. So my problem is how can I find equivalent glut functions in functions of QGLWidget?

2条回答
Lonely孤独者°
2楼-- · 2019-04-02 12:41

You need to implement your own class inherited from QGLWidget, for example:

    class GLWidget : public QGLWidget
    {
        Q_OBJECT

    public:
        GLWidget(QWidget *parent = 0);

    protected:
        void initializeGL();
        void resizeGL(int w, int h);
        void paintGL(); 

        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);

    };

You also need to override three important functions, initializeGL() where you're preparing your OpenGL. resizeGL() where you update the viewport and projection matrix if your panel is resized, and paintGL() the actual rendering.

The window initialization, of course, is handled by Qt.

For mouse events, there are three functions you can override: mousePressEvent(), mouseMoveEvent(), and mouseReleaseEvent()

void GLWidget::initializeGL() 
{
    glClearColor(0.5, 0.5, 0.5, 1.0);
}

void GLWidget::resizeGL(int width, int height) 
{   
    glViewport(0, 0, width(), height());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width(), 0, height());
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void GLWidget::paintGL() 
{
    glClear(GL_COLOR_BUFFER_BIT);

    // draw a red triangle
    glColor3f(1,0,0);
    glBegin(GL_POLYGON);
    glVertex2f(10,10);
    glVertex2f(10,600);
    glVertex2f(300,10);
    glEnd();
}
查看更多
Root(大扎)
3楼-- · 2019-04-02 12:42

OK. Have you looked at the HelloGL sample? So there you'll learn how to display a QGLWidget and process mouse input. I think this is what you are looking for. Since Qt provides SIGNAL and SLOTS input processing is kind of different but also very intuitive. So you have to connect mouse SIGNALS to your SLOTS. Those SLOTS will then process the mouse event.

But look at the sample, it's quite intuitive.

查看更多
登录 后发表回答