时间:2021-05-19
本文实例为大家分享了QPainter画一个3D正的具体代码,供大家参考,具体内容如下
My3DBox .h
#include <Eigen/Geometry>using namespace Eigen;#define SQUARE_LENGTH 200//是一个边长200的正方体#define CAMERA_DISTANCE 300//视点距离class My3DBox : public QWidget{ Q_OBJECTpublic: 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.1415926My3DBox::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);}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
学会用CSS3的3D效果制作一个正方体,有助于增强我们对3D场景的旋转和位移属性的理解。下面的动态图就是利用3D旋转位移,结合动画效果制作的。感兴趣的同学可以在
Unity3D自动寻路,供大家参考,具体内容如下1.首先在Unity3D创造中一个正方体,一个圆柱体和一个平面。2.将正方体和平面设为静态(Static前的方格
我们知道,在PPT中使用“形状”中的“正方体”形状,可以绘制正方体及长方体,可是有时需要只对正方体某个面更改填充
在我们使用word的时候,经常会遇到需要各种画形。如果使用网上搜索的图片有时不是很清晰,那么就自己动手画一个清晰的图形吧!正方体有好几种画法,下面我来介绍两种。
sketchup绘制一个环,圆环或者其他形状的环。这里用作个圆角正方体环。1、使用矩形工具绘制一个平面矩形。这里我是4000*4000。2、然后使用拉尺工具做出