使用QPainter画一个3D正方体
程序员文章站
2022-04-03 17:54:42
本文实例为大家分享了qpainter画一个3d正的具体代码,供大家参考,具体内容如下my3dbox .h#include using namespace e...
本文实例为大家分享了qpainter画一个3d正的具体代码,供大家参考,具体内容如下
my3dbox .h
#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
#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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。