服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 使用QPainter画一个3D正方体

使用QPainter画一个3D正方体

2021-09-24 12:32BIG_C_GOD C/C++

这篇文章主要为大家详细介绍了使用QPainter画一个3D正方体,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了QPainter画一个3D正的具体代码,供大家参考,具体内容如下

使用QPainter画一个3D正方体

使用QPainter画一个3D正方体

My3DBox .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
#include <Eigen/Geometry>
using namespace Eigen;
 
#define SQUARE_LENGTH 200//是一个边长200的正方体
#define CAMERA_DISTANCE 300//视点距离
 
class My3DBox : public QWidget
{
  Q_OBJECT
public:
  explicit My3DBox(QWidget *parent = 0);
protected:
  QPoint m_mousePos;
  Vector3d m_vector[8];//正方形8个点坐标
 
  void mouseMoveEvent(QMouseEvent *);
  void paintEvent(QPaintEvent *);
 
  void drawPlane(const QPoint *points,QPainter &painter);
  Matrix3d getMartix();
  QPoint ToPoint(Vector3d vector);
  void drawUp(QPoint *points,QPainter &painter);//正方体面的绘制
  void drawDown(QPoint *points,QPainter &painter);
  void drawLeft(QPoint *points,QPainter &painter);
  void drawRight(QPoint *points,QPainter &painter);
};

My3DBox .cpp

?
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
#include "widget.h"
 
#define PI 3.1415926
 
My3DBox::My3DBox(QWidget *parent) :
  QWidget(parent),m_mousePos(0,0)
{
  setMouseTracking(true);//在任何情况下启用mouseMoveEvent
  setGeometry(400,200,500,500);
 
  m_vector[0] << -100,100,100;//设置正方体顶点初始位置
  m_vector[1] << 100,100,100;
  m_vector[2] << -100,-100,100;
  m_vector[3] << 100,-100,100;
  m_vector[4] << -100,100,-100;
  m_vector[5] << 100,100,-100;
  m_vector[6] << -100,-100,-100;
  m_vector[7] << 100,-100,-100;
}
 
void My3DBox::mouseMoveEvent(QMouseEvent *e)//此处把坐标原点从左上角变换至屏幕中心
{
  if(e->pos().x() < 0)
    m_mousePos.setX(-width()/2);
  else if(e->pos().x() > width())
    m_mousePos.setX(width()/2);
  else
    m_mousePos.setX(e->pos().x() - width()/2);
 
  if(e->pos().y() < 0)
    m_mousePos.setY(height()/2);
  else if(e->pos().y() > height())
    m_mousePos.setY(-height()/2);
  else
    m_mousePos.setY(-e->pos().y() + height()/2);
 
  update();
}
 
void My3DBox::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  painter.setRenderHint(QPainter::SmoothPixmapTransform);
  painter.setPen(Qt::NoPen);
 
  painter.fillRect(rect(),QColor(3,22,52));
 
  Vector3d vector[8];
  Matrix3d matrix = getMartix();//获取变换矩阵
 
  QPoint points[8];//正面0123 左面4062 右面1537 上面4501 下面 2367(以观察者的方向为主)
  for(int i = 0;i < 8;++i)//计算变换后坐标
  {
    vector[i] = matrix*m_vector[i];
    points[i] = ToPoint(vector[i]);
 
    points[i].setX(points[i].x()+width()/2);//将坐标系原点变换至左上角
    points[i].setY(-points[i].y()+height()/2);
  }
 
  //一共要画5个面,最后画正面
  if(qAbs(m_mousePos.x()) > qAbs(m_mousePos.y()))
  {
    if(m_mousePos.x() > 0)
    {
      if(m_mousePos.y() > 0)
        drawDown(points,painter);
      else
        drawUp(points,painter);
      drawLeft(points,painter);
    }
    else
    {
      if(m_mousePos.y() > 0)
        drawDown(points,painter);
      else
        drawUp(points,painter);
      drawRight(points,painter);
    }
  }
  else
  {
    if(m_mousePos.y() > 0)
    {
      if(m_mousePos.x() > 0)
        drawLeft(points,painter);
      else
        drawRight(points,painter);
      drawDown(points,painter);
    }
    else
    {
      if(m_mousePos.x() > 0)
        drawLeft(points,painter);
      else
        drawRight(points,painter);
      drawUp(points,painter);
    }
  }
  //画正面
  QPoint point[4] =
  {
    points[0],
    points[1],
    points[3],
    points[2]
  };
  drawPlane(point,painter);
}
 
void My3DBox::drawPlane(const QPoint *points,QPainter &painter)
{
  QLinearGradient linearGradient(points[1],points[3]);//设置渐变色
  linearGradient.setColorAt(0.0,QColor(150,150,250));
  linearGradient.setColorAt(1.0,QColor(170,170,255));
  painter.setBrush(QBrush(linearGradient));
  painter.drawConvexPolygon(points,4);
}
 
Matrix3d My3DBox::getMartix()
{
  double unit_x = 0;//同方向单位向量的x和y
  double unit_y = 0;
  double z = 0;
 
  if(m_mousePos.x() != 0 || m_mousePos.y() != 0)
  {
    unit_x = m_mousePos.x()/qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y());
    unit_y = m_mousePos.y()/qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y());
    z = qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y())/qSqrt(width()*width()/4 + height()*height()/4);
  }
 
  Matrix3d M_Z;
  if(unit_x == 0 && unit_y == 0)
    M_Z << 1,0,0,
        0,1,0,
        0,0,1;
  else
    M_Z << unit_y,-unit_x,0,
        unit_x,unit_y,0,
        0,0,1;
 
  Matrix3d M_X;
  if(z == 0)
    M_X << 1,0,0,
        0,1,0,
        0,0,1;
  else
    M_X << 1,0,0,
        0,cos(z*PI/2.6),sin(z*PI/2.6),
        0,-sin(z*PI/2.6),cos(z*PI/2.6);//为什么PI/2大于90度?因为没画背面所以必须小于90度(偷懒)
 
  return M_Z.inverse()*M_X*M_Z;
}
 
QPoint My3DBox::ToPoint(Vector3d vector)//将3d坐标投影至平面上
{
  QPoint point;
 
  point.setX(CAMERA_DISTANCE*vector[0]/(CAMERA_DISTANCE-vector[2]));
  point.setY(CAMERA_DISTANCE*vector[1]/(CAMERA_DISTANCE-vector[2]));
 
  return point;
}
 
void My3DBox::drawUp(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[4],
    points[5],
    points[1],
    points[0]
  };
  drawPlane(point,painter);
}
 
void My3DBox::drawDown(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[2],
    points[3],
    points[7],
    points[6]
  };
  drawPlane(point,painter);
}
 
void My3DBox::drawLeft(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[4],
    points[0],
    points[2],
    points[6]
  };
  drawPlane(point,painter);
}
 
void My3DBox::drawRight(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[1],
    points[5],
    points[7],
    points[3]
  };
  drawPlane(point,painter);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/BIG_C_GOD/article/details/53285152

延伸 · 阅读

精彩推荐