如何在QGLWidget中寻找OpenGL的等效功能? [英] How to look for equivalent functions of OpenGL in QGLWidget?

查看:134
本文介绍了如何在QGLWidget中寻找OpenGL的等效功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenGL和Glut的新手.有一个由Glut实施的项目.我搜索了一下,发现在Qt中有一个称为QGLWidget的OpenGL实现.但是,对于我来说,将旧的Glut代码转换为新的Qt代码非常困难,因为我不知道如何在Qt中找到与Glut函数等效的函数.部分代码如下所示:

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

Qt的文档中不存在上面的glut *功能.所以我的问题是如何在QGLWidget的函数中找到等效的glut函数?

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?

推荐答案

您需要实现从QGLWidget继承的自己的类,例如:

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

    };

您还需要重写三个重要函数,即准备您的OpenGL的 initializeGL(). resizeGL(),如果调整了面板的大小,则可以在其中更新视口和投影矩阵,并 paintGL()是实际的渲染.

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.

窗口初始化当然是由Qt处理的.

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

对于鼠标事件,可以重写三个功能: mousePressEvent() mouseMoveEvent() mouseReleaseEvent()

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

这篇关于如何在QGLWidget中寻找OpenGL的等效功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆