我是靠谱客的博主 动人哈密瓜,这篇文章主要介绍Qt+OpenGL绘制三维坐标系(动态曲线显示),现在分享给大家,希望可以做个参考。

*Qt使用OpenGL绘制三维曲线时,需要加入OPenGL模块。
此处的绘制的核心代码同样可以移植到MFC上显示。
如下代码可以再进行优化。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef QTDRAW3DCTRL_H #define QTDRAW3DCTRL_H #include <QtOpenGL/qgl.h> #include <QWidget> #include <QGridLayout> #include <QtGui/QtGui> #include <QtOpenGL/QtOpenGL> #include <QtOpenGL/QGLWidget> #include <QtWidgets/QOpenGLWidget> #include <QtOpenGL/QGL> #include <vector> #include <GL/glu.h> #include "ui_qtdraw3dctrl.h" #ifdef _DEBUG #pragma comment(lib, "Qt5OpenGLd") #else #pragma comment(lib, "Qt5OpenGL") #endif #ifdef _DEBUG #include <QDebug> #endif class QtDraw3DCtrl : public QGLWidget { Q_OBJECT public: QtDraw3DCtrl(QWidget *parent = 0); ~QtDraw3DCtrl(); void GLGrid(float pt1x, float pt1y, float pt1z, float pt2x, float pt2y, float pt2z, int num); protected: int m_scloe; double m_rotx; double m_roty; double m_rotz; QPoint m_rotPosOld; int m_base; int m_isize; double m_count; double m_valuex; double m_valuey; double m_valuez; std::vector<double> m_vetorx; std::vector<double> m_vetory; std::vector<double> m_vetorz; public slots: void UpdateSlots(); protected: virtual void initializeGL(); virtual void resizeGL(int w, int h); virtual void paintGL(); virtual void mousePressEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event); virtual void wheelEvent(QWheelEvent *event); private: Ui::QtDraw3DCtrl ui; }; #endif // QTDRAW3DCTRL_H
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "qtdraw3dctrl.h" QtDraw3DCtrl::QtDraw3DCtrl(QWidget *parent) : QGLWidget(parent) { ui.setupUi(this); m_scloe = -15; m_rotx = 0; m_roty = 0; m_rotz = 0; m_count = -40; m_isize = 4; QTimer* timer = new QTimer(this); timer->setInterval(10); connect(timer, SIGNAL(timeout()), this, SLOT(UpdateSlots())); timer->start(); } QtDraw3DCtrl::~QtDraw3DCtrl() { } void QtDraw3DCtrl::UpdateSlots() { m_valuex = m_count*0.1; m_valuey = sin(m_count)*0.1; m_valuez = cos(m_count)*0.1; m_count+= 0.05; update(); } void QtDraw3DCtrl::initializeGL() { glClearColor(0.0, 0.2, 0.3, 1.0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH); } void QtDraw3DCtrl::resizeGL(int w, int h) { if (h == 0)//防止height为0 { h = 1; } glViewport(0, 0,(GLint)w, (GLint)h);//重置当前的视口 glMatrixMode(GL_PROJECTION);//选择投影矩阵 glLoadIdentity();//重置投影矩阵 gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.001, 1000.0);//建立透视投影矩阵 glMatrixMode(GL_MODELVIEW);//选择模型观察矩阵 glLoadIdentity();//重置模型观察矩阵 } void QtDraw3DCtrl::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除屏幕和深度缓存 glLoadIdentity();//重置当前的模型观察矩阵。在glLoadIdentity()调用之后,函数返回之前,添加代码来创建基本的形 //目前所做的就是将屏幕清除成前面所决定的颜色,清除深度缓存并且重置场景,仍然没绘制任何东西。 //glPolygonMode(GL_FRONT_AND_BACK ,GL_LINE );//前后面,填充方式(点point、线line、FILL) //旋转显示窗口 glTranslatef(0, 0, m_scloe); glRotatef(fabs(m_roty), m_rotx, m_roty, m_rotz); GLUquadricObj *objCylinder = gluNewQuadric(); int pt = 8; int num = 40; //网格 //X glPushMatrix(); glColor3f(0.8, 0.8, 0.8); glTranslatef(-m_isize, -m_isize, -m_isize); GLGrid(0, 0, 0, pt, 0, pt, num); glPopMatrix(); glPushMatrix(); glColor3f(1, 0, 0); glTranslatef(m_isize, -m_isize, -m_isize); glRotatef(90, 0, 1, 0.0); gluCylinder(objCylinder, 0.1, 0.0, 0.2, 100, 1); glPopMatrix(); //Y glPushMatrix(); glTranslated(-m_isize, m_isize, -m_isize); glRotatef(90, 1.0, 0.0, 0.0); glColor3f(0.8, 0.8, 0.8); GLGrid(0, 0, 0, pt, 0, pt, num); glPopMatrix(); glPushMatrix(); glColor3f(0, 1, 0); glTranslatef(-m_isize, m_isize, -m_isize); glRotatef(-90, 1, 0, 0); gluCylinder(objCylinder, 0.1, 0.0, 0.2, 100, 1); glPopMatrix(); //Z glPushMatrix(); glTranslatef(-m_isize, -m_isize, -m_isize); glRotatef(90, 0.0, 0.0, 1.0); glColor3f(0.8, 0.8, 0.8); GLGrid(0, 0, 0, pt, 0, pt, num); glPopMatrix(); glPushMatrix(); glColor3f(0, 1, 1); glTranslatef(-m_isize, -m_isize, m_isize); glRotatef(90, 0, 0, 1); gluCylinder(objCylinder, 0.1, 0.0, 0.2, 100, 1); glPopMatrix(); if (m_vetorx.size() >= 2) { glBegin(GL_LINE_STRIP); glTranslatef(-m_isize, -m_isize, -m_isize); glColor3f(1, 0, 1); for (int i = 0; i < m_vetorx.size(); i++) { glVertex3f(m_vetorx[i], m_vetory[i], m_vetorz[i]); } glEnd(); glFlush(); } if (m_vetorx.size() >= 2) { glBegin(GL_LINE_STRIP); glTranslatef(-m_isize, m_isize, -m_isize); glColor3f(1, 0, 0); for (int i = 0; i < m_vetorx.size(); i++) { //glVertex3f(m_vetory[i], m_vetorx[i], m_vetorz[i]); glVertex3f(m_vetorx[i], 3*m_vetory[i], 3*m_vetorz[i]); } glEnd(); glFlush(); } if (m_vetorx.size() >= 2) { glBegin(GL_LINE_STRIP); glTranslatef(-m_isize, m_isize, -m_isize); glColor3f(1, 1, 0); for (int i = 0; i < m_vetorx.size(); i++) { glVertex3f(m_vetorz[i], m_vetory[i], m_vetorx[i]); } glEnd(); glFlush(); } m_vetorx.push_back(m_valuex); m_vetory.push_back(m_valuey); m_vetorz.push_back(m_valuez); } void QtDraw3DCtrl::mousePressEvent(QMouseEvent *event) { QPoint pos = event->pos(); qDebug() << "x: " << pos.x() << " y:" << pos.y(); } void QtDraw3DCtrl::mouseMoveEvent(QMouseEvent *event) { QPoint pos = event->pos(); qDebug() << "x: " << pos.x() << " y:" << pos.y(); if (pos.y() > m_rotPosOld.y()) { m_rotz -= 1; qDebug() << m_rotz; m_rotx -= 1; qDebug() << m_rotx; m_roty -= 1; qDebug() << m_roty; } else if(pos.y() < m_rotPosOld.y()) { m_rotz += 1; qDebug() << m_rotz; m_rotx += 1; qDebug() << m_rotx; m_roty += 1; qDebug() << m_roty; } m_rotPosOld = pos; update(); } void QtDraw3DCtrl::wheelEvent(QWheelEvent *event) { qDebug() << event->delta(); if (event->delta() < 0) { m_scloe++; } else if (event->delta() > 0) { m_scloe--; } update(); } void QtDraw3DCtrl::GLGrid(float pt1x, float pt1y, float pt1z, float pt2x, float pt2y, float pt2z, int num) { const float _xLen = (pt2x - pt1x) / num; const float _yLen = (pt2y - pt1y) / num; const float _zLen = (pt2z - pt1z) / num; glLineWidth(0.1f); //glLineStipple(1, 0x0303);//线条样式 glBegin(GL_LINES); glEnable(GL_LINE_SMOOTH); int xi = 0; int yi = 0; int zi = 0; //绘制平行于X的直线 for (zi = 0; zi <= num; zi++) { float z = _zLen * zi + pt1z; for (yi = 0; yi <= num; yi++) { float y = _yLen * yi + pt1y; glVertex3f(pt1x, y, z); glVertex3f(pt2x, y, z); } } 绘制平行于Y的直线 for (zi = 0; zi <= num; zi++) { float z = _zLen * zi + pt1z; for (xi = 0; xi <= num; xi++) { float x = _xLen * xi + pt1x; glVertex3f(x, pt1y, z); glVertex3f(x, pt2y, z); } } //绘制平行于Z的直线 for (yi = 0; yi <= num; yi++) { float y = _yLen * yi + pt1y; for (xi = 0; xi <= num; xi++) { float x = _xLen * xi + pt1x; glVertex3f(x, y, pt1z); glVertex3f(x, y, pt2z); } } glEnd(); }

在这里插入图片描述

最后

以上就是动人哈密瓜最近收集整理的关于Qt+OpenGL绘制三维坐标系(动态曲线显示)的全部内容,更多相关Qt+OpenGL绘制三维坐标系(动态曲线显示)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部