How to look for equivalent functions of OpenGL in QGLWidget?

故事扮演 提交于 2019-12-05 07:14:25

问题


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?


回答1:


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();
}



回答2:


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.



来源:https://stackoverflow.com/questions/17790171/how-to-look-for-equivalent-functions-of-opengl-in-qglwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!