阅读量:0
在Qt中,可以使用QOpenGLWidget类来绘制三维坐标系。下面是一个简单的示例代码,演示如何在QOpenGLWidget中绘制一个三维坐标系:
#include #include #include class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions { public: void initializeGL() override { initializeOpenGLFunctions(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } void resizeGL(int w, int h) override { glViewport(0, 0, w, h); } void paintGL() override { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 modelViewMatrix; modelViewMatrix.setToIdentity(); QMatrix4x4 projectionMatrix; projectionMatrix.perspective(45.0f, width() / height(), 0.1f, 100.0f); QMatrix4x4 mvpMatrix = projectionMatrix * modelViewMatrix; // 绘制坐标系 glBegin(GL_LINES); // x轴 glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f); // y轴 glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.0f, -1.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); // z轴 glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(0.0f, 0.0f, 1.0f); glEnd(); } };
在这个示例中,我们继承了QOpenGLWidget,并实现了initializeGL、resizeGL和paintGL这三个函数。在paintGL函数中,我们使用OpenGL的基本绘制函数来绘制三个坐标轴。我们首先设置了模型视图矩阵和投影矩阵,然后将它们相乘得到MVP矩阵。最后,我们使用glBegin和glEnd来绘制三个坐标轴。