Qt C++加载多模型,机械臂联动,材质设置,灯光

4 篇文章 3 订阅
animatedentity.h

#ifndef ANIMATEDENTITY_H
#define ANIMATEDENTITY_H

#include <Qt3DExtras/QNormalDiffuseSpecularMapMaterial>
#include <Qt3DRender/qtexture.h>
#include <Qt3DCore/QJoint>
#include "renderableentity.h"

class AnimatedEntity : public RenderableEntity
{
public:
    AnimatedEntity(Qt3DCore::QNode *parent = 0);
    ~AnimatedEntity();


    void setShininess(float shininess);
    void setSource(QString source);

    float shininess() const;

    void setNormalTextureSource(const QString &texture);
    void setDiffuseTextureSource(const QString &texture);
    void setSpecularTextureSource(const QString &texture);

private:
    Qt3DExtras::QNormalDiffuseSpecularMapMaterial *m_material;
    Qt3DRender::QAbstractTexture *m_diffuseTexture;
    Qt3DRender::QAbstractTexture *m_normalTexture;
    Qt3DRender::QAbstractTexture *m_specularTexture;
    Qt3DRender::QTextureImage *m_diffuseTextureImage;
    Qt3DRender::QTextureImage *m_normalTextureImage;
    Qt3DRender::QTextureImage *m_specularTextureImage;

    void setNormalTextureSource();
    void setDiffuseTextureSource();
    void setSpecularTextureSource();

};
#endif // ANIMATEDENTITY_H
animatedentity.cpp

#include "animatedentity.h"



AnimatedEntity::AnimatedEntity(Qt3DCore::QNode *parent)
    : RenderableEntity(parent)
    , m_material(new Qt3DExtras::QNormalDiffuseSpecularMapMaterial())
    , m_diffuseTexture(m_material->diffuse())
    , m_normalTexture(m_material->normal())
    , m_specularTexture(m_material->specular())
    , m_diffuseTextureImage(new Qt3DRender::QTextureImage())
    , m_normalTextureImage(new Qt3DRender::QTextureImage())
    , m_specularTextureImage(new Qt3DRender::QTextureImage())
{


    setNormalTextureSource();
    setDiffuseTextureSource();
    setSpecularTextureSource();
    m_material->setShininess(0.0f);
    addComponent(m_material);

    //mesh()->setSource(QUrl(QStringLiteral("qrc:/new/obj/model/base.obj")));
    //transform()->setScale(0.13f);

    m_diffuseTexture->addTextureImage(m_diffuseTextureImage);
    m_normalTexture->addTextureImage(m_normalTextureImage);
    m_specularTexture->addTextureImage(m_specularTextureImage);
}

AnimatedEntity::~AnimatedEntity()
{
}

void AnimatedEntity::setSource(QString source)
{
    mesh()->setSource(QUrl(source));
}



void AnimatedEntity::setShininess(float shininess)
{
    if (shininess != m_material->shininess())
        m_material->setShininess(shininess);
}



float AnimatedEntity::shininess() const
{
    return m_material->shininess();
}

void AnimatedEntity::setNormalTextureSource()
{
    m_normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/new/pictrue/image/normal.jpg")));
}

void AnimatedEntity::setDiffuseTextureSource()
{
    m_diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/new/pictrue/image/map.jpg")));
}

void AnimatedEntity::setSpecularTextureSource()
{
    m_specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/new/pictrue/image/map.jpg")));
}


void AnimatedEntity::setNormalTextureSource(const QString &texture)
{
    m_normalTextureImage->setSource(QUrl(texture));
}
void AnimatedEntity::setDiffuseTextureSource(const QString &texture)
{
    m_diffuseTextureImage->setSource(QUrl(texture));
}
void AnimatedEntity::setSpecularTextureSource(const QString &texture)
{
    m_specularTextureImage->setSource(QUrl(texture));
}























excavator.h
#ifndef EXCAVATOR_H
#define EXCAVATOR_H

#include <QWidget>
#include <QTimer>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QPointLight>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QSceneLoader>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>

#include <Qt3DCore/QEntity>
#include <Qt3DExtras/QText2DEntity>
#include <Qt3DRender/QDirectionalLight>
#include <QEnvironmentLight>
#include "animatedentity.h"
#include "trackballcameracontroller.h"
#include <QPropertyAnimation>

#include "line3d.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Excavator; }
QT_END_NAMESPACE

class Excavator : public QWidget
{
    Q_OBJECT

public:
    Excavator(QWidget *parent = nullptr);
    ~Excavator();
    void initializeLight(Qt3DCore::QEntity *sceneRoot);
    void initializeSphere(Qt3DCore::QEntity *sceneRoot);
public slots:
    void setWristTranslation(float angle);
    void setUpperArmTranslation(float angle);
    void setLowerArmTranslation(float angle);
    void setTurretTranslation(float angle);

    void setDirectionalLightColor(const QColor &color){mDirectionalLight->setColor(color);}
    void setDirectionalLightIntensity(float intensity){mDirectionalLight->setIntensity(intensity);}
    void setDirectionalLightWorldDirection(const QVector3D &v){mDirectionalLight->setWorldDirection(v.normalized());}

    void setDirectionalLight2Color(const QColor &color){mDirectionalLight2->setColor(color);}
    void setDirectionalLight2Intensity(float intensity){mDirectionalLight2->setIntensity(intensity);}
    void setDirectionalLight2WorldDirection(const QVector3D &v){mDirectionalLight2->setWorldDirection(v.normalized());}

    void setDirectionalLight3Color(const QColor &color){mDirectionalLight3->setColor(color);}
    void setDirectionalLight3Intensity(float intensity){mDirectionalLight3->setIntensity(intensity);}
    void setDirectionalLight3WorldDirection(const QVector3D &v){mDirectionalLight3->setWorldDirection(v.normalized());}

    void setPointLightLightColor(const QColor &color){mPointLight->setColor(color);}
    void setPointLightLightIntensity(float intensity){mPointLight->setIntensity(intensity);}

    void cameraPositionChanged(const QVector3D &position);

    void setShininess(float shininess){
                                       mTurret->setShininess(shininess);
                                      mLowerArm->setShininess(shininess);
                                      mUpperArm->setShininess(shininess);
                                      mWrist->setShininess(shininess);}

    void setZoomSpeed(float value){camController->setZoomSpeed(value);}
    void setPanSpeed(float value){camController->setPanSpeed(value);}
     void setRotationSpeed(float rotationSpeed){camController->setRotationSpeed(rotationSpeed);}

private slots:
     void on_horizontalSlider_valueChanged(int value);

     void on_horizontalSlider_2_valueChanged(int value);

     void on_horizontalSlider_3_valueChanged(int value);

     void on_horizontalSlider_4_valueChanged(int value);

     void lowerArmReach(const QVariant &value);
     void turretReach(const QVariant &value);
     void startAnimation();
     void startSteamshipAnimation();

     void wristMatrixChanged();

private:
    Ui::Excavator *ui;
    //AnimatedEntity *mBase = nullptr;
    AnimatedEntity *mTurret = nullptr;
    AnimatedEntity *mLowerArm = nullptr;
    AnimatedEntity *mUpperArm = nullptr;
    AnimatedEntity *mWrist = nullptr;
    AnimatedEntity *mSteamship = nullptr;
    AnimatedEntity *lowerArmHydraulic = nullptr;
    AnimatedEntity *lowerArmRod = nullptr;

    Qt3DRender::QCamera *basicCamera;
    TrackballCameraController *camController;
    Qt3DRender::QDirectionalLight *mDirectionalLight;
    Qt3DRender::QDirectionalLight *mDirectionalLight2;
    Qt3DRender::QDirectionalLight *mDirectionalLight3;
    Qt3DRender::QPointLight *mPointLight;
    Qt3DCore::QTransform *t;

    Qt3DExtras::QText2DEntity *lText;
    Qt3DExtras::QText2DEntity *hText;
    Qt3DCore::QTransform *textTransformL;
    Qt3DCore::QTransform *textTransformH;

    Qt3DCore::QEntity *m_sphereEntity;
    Qt3DCore::QTransform *sphereTransform;

    QVector3D vectL;

    Line3D *lineL;
    Line3D *lineH;
    Line3D *waterLevel;


    QPropertyAnimation *turretAnimation;
    QPropertyAnimation *lowerArmAnimation;
    QPropertyAnimation *upperArmAnimation;
    QPropertyAnimation *wristAnimation;
    QPropertyAnimation *steamshipAnimation;

    float turretAngle{0.0f};
    float wristAngle{0.0f};
    float upperArmAngle{0.0f};
    float lowerArmAngle{0.0f};

    float level;


};
#endif // EXCAVATOR_H
excavator.cpp
#include "excavator.h"
#include "ui_excavator.h"
#include <QScreen>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include "animatedentity.h"
#include "planeentity.h"

#include <QDebug>
#include <QTimer>
#include <qmath.h>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>


Excavator::Excavator(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Excavator)
{
    ui->setupUi(this);

    level = -0.15f;
    Qt3DCore::QEntity *sceneRoot = new Qt3DCore::QEntity();
    Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();

    view->defaultFrameGraph()->setClearColor(QRgb(0xffffff));

    view->setRootEntity(sceneRoot);
    initializeLight(sceneRoot);


    basicCamera = view->camera();
    basicCamera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
    basicCamera->setAspectRatio(view->width() / view->height());
    basicCamera->setFieldOfView(45);
    basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
    basicCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
    basicCamera->setPosition(QVector3D(20.7989, 3.75907, 13.1728));

    camController = new TrackballCameraController(sceneRoot);
    camController->setWindowSize(view->size());
    camController->setCamera(basicCamera);
    camController->setRotationSpeed(2.0);




//    mBase = new AnimatedEntity(sceneRoot);//0.00715,-0.63505,0.86365
//    mBase->setSource("qrc:/new/obj/model/base.obj");

    mTurret = new AnimatedEntity(sceneRoot);//0,-0.635688,0.911992
    mTurret->setSource("qrc:/new/obj/model/turret.obj");


    mLowerArm = new AnimatedEntity(sceneRoot);//-0.201742,0.039368,0.890312
    mLowerArm->setSource("qrc:/new/obj/model/lowerArm.obj");
    mLowerArm->transform()->setTranslation(QVector3D(-0.2f,0.4f,-0.35f));

    mUpperArm = new AnimatedEntity(sceneRoot);//-0.2011,1.03828,4.32464
    mUpperArm->setSource("qrc:/new/obj/model/upperArm.obj");
    mUpperArm->transform()->setTranslation(QVector3D(-0.2f,1.4f,2.5441f));

    mWrist = new AnimatedEntity(sceneRoot);//-0.20505,-0.23322,3.55813
    mWrist->setSource("qrc:/new/obj/model/wrist.obj");
    mWrist->transform()->setTranslation(QVector3D(-0.17f,0.18f,2.3441f));

    mSteamship = new AnimatedEntity(sceneRoot);
    mSteamship->setSource("qrc:/new/obj/model/steamship.obj");
    //mSteamship->transform()->setScale(1.2f);
    mSteamship->transform()->setRotationY(90);
    //mSteamship->transform()->setTranslation(QVector3D(0.0f,-0.58f,-3.2f));
    mSteamship->transform()->setTranslation(QVector3D(0.0f,-0.38f,-2.7f));


    mSteamship->setNormalTextureSource("qrc:/new/pictrue/image/steamship.jpg");
    mSteamship->setDiffuseTextureSource("qrc:/new/pictrue/image/steamship.jpg");
    mSteamship->setSpecularTextureSource("qrc:/new/pictrue/image/steamship.jpg");

    initializeSphere(sceneRoot);

    //drawLine(QVector3D(1.17f,-4.18f,4.3441f),QVector3D(1.17f,-3.18f,4.3441f),QColor(255,0,0),sceneRoot);

    waterLevel = new Line3D;
    waterLevel->setRootEntity(sceneRoot);
    waterLevel->setArrowsEnable(false);

    waterLevel->setRadius(0.05f);
    waterLevel->setLength(100);
    waterLevel->setRings(100);
    waterLevel->setSlices(20);
    waterLevel->setRotation('x',90);
    waterLevel->setPosition(QVector3D(0.0f,level,1.5f));
    waterLevel->setColor(Qt::green);



    lineL = new Line3D;
    lineL->setRootEntity(sceneRoot);
    lineL->setRadius(0.05f);
    lineL->setLength(mWrist->transform()->translation().z()+0.5f);
    lineL->setRings(100);
    lineL->setSlices(20);
    lineL->setRotation('x',90);
    lineL->setPosition(QVector3D(0.0f,-2.18f,1.5f));
    lineL->setColor(QColor(QRgb(0x928327)));
    vectL = QVector3D(0.0f,-2.18f,1.5f);

    lineH = new Line3D;
    lineH->setRootEntity(sceneRoot);
    lineH->setRadius(0.05f);
    lineH->setLength(mWrist->transform()->translation().y()+level+0.5);
    lineH->setRings(100);
    lineH->setSlices(20);
    lineH->setRotation('y',90);
    //lineH->setPosition(QVector3D(0.0f,level,2.6f));
    lineH->setPosition(QVector3D(0.0f,level,sphereTransform->translation().z()));
    lineH->setColor(QColor(QRgb(0x928327)));



    lText = new Qt3DExtras::QText2DEntity(sceneRoot);
    lText->setFont(QFont("Microsoft YaHei", 3, QFont::Bold));
    lText->setHeight(12);
    lText->setWidth(20);
    lText->setText("12");
    lText->setColor(Qt::red);
    textTransformL = new Qt3DCore::QTransform(lText);
    textTransformL->setTranslation(QVector3D(0.0f,-2.18-1.5f,1.5f));
    textTransformL->setRotationY(90);
    textTransformL->setScale(0.125f);
    lText->addComponent(textTransformL);


    hText = new Qt3DExtras::QText2DEntity(sceneRoot);
    hText->setFont(QFont("Microsoft YaHei", 3, QFont::Bold));
    hText->setHeight(12);
    hText->setWidth(20);
    hText->setText("25");
    hText->setColor(Qt::blue);
    textTransformH = new Qt3DCore::QTransform(lText);
    textTransformH->setTranslation(QVector3D(0.0f,level-1.5,2.2f));
    textTransformH->setRotationY(90);
    textTransformH->setScale(0.125f);
    hText->addComponent(textTransformH);





    QWidget *container = QWidget::createWindowContainer(view,this);
    //container->setFixedSize(960,460);
    //container->setFocusPolicy(Qt::NoFocus);
    ui->horizontalLayout_5->insertWidget(0,container);
    ui->horizontalLayout_5->setStretch(0,5);
    ui->horizontalLayout_5->setStretch(1,1);

    setWristTranslation(-1.22f);
    setUpperArmTranslation(-1.71f);
    setLowerArmTranslation(0.15f);

    connect(mWrist->transform(),&Qt3DCore::QTransform::matrixChanged,this,&Excavator::wristMatrixChanged);


    turretAnimation = new QPropertyAnimation(ui->horizontalSlider_4,"value");
    turretAnimation->setDuration(10000);
    turretAnimation->setStartValue(0);
    turretAnimation->setEndValue(255);

    lowerArmAnimation = new QPropertyAnimation(ui->horizontalSlider_3,"value");
    lowerArmAnimation->setDuration(10000);
    lowerArmAnimation->setStartValue(15);
    lowerArmAnimation->setEndValue(67);

    upperArmAnimation = new QPropertyAnimation(ui->horizontalSlider_2,"value");
    upperArmAnimation->setDuration(10000);
    upperArmAnimation->setStartValue(-171);
    upperArmAnimation->setEndValue(-129);

    wristAnimation = new QPropertyAnimation(ui->horizontalSlider,"value");
    wristAnimation->setDuration(10000);
    wristAnimation->setStartValue(-122);
    wristAnimation->setEndValue(122);
    wristAnimation->setLoopCount(1);

//    ui->horizontalSlider->setValue(-122);
//    ui->horizontalSlider_2->setValue(-171);
//    ui->horizontalSlider_3->setValue(15);
//    ui->horizontalSlider_4->setValue(0);

//    QTimer::singleShot(5000,this,SLOT(startAnimation()));

    connect(basicCamera,&Qt3DRender::QCamera::positionChanged,this,&Excavator::cameraPositionChanged);
    connect(lowerArmAnimation,&QPropertyAnimation::valueChanged,this,&Excavator::lowerArmReach);
    connect(turretAnimation,&QPropertyAnimation::valueChanged,this,&Excavator::turretReach);
}

Excavator::~Excavator()
{
    delete ui;
}

void Excavator::initializeLight(Qt3DCore::QEntity *sceneRoot)
{
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(sceneRoot);
    mDirectionalLight = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight->setColor("white");
    mDirectionalLight->setIntensity(1.0);

    mDirectionalLight->setWorldDirection(QVector3D(6, -4, -1).normalized());
    lightEntity->addComponent(mDirectionalLight);

    mDirectionalLight2 = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight2->setColor("white");
    mDirectionalLight2->setIntensity(0.3f);
    mDirectionalLight2->setWorldDirection(QVector3D(-33, -16, -12).normalized());
    lightEntity->addComponent(mDirectionalLight2);

    mDirectionalLight3 = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight3->setColor(QColor(234,252,171));
    mDirectionalLight3->setIntensity(0.4f);
    mDirectionalLight3->setWorldDirection(QVector3D(-12, 0, 0).normalized());
    lightEntity->addComponent(mDirectionalLight3);

    mPointLight = new Qt3DRender::QPointLight(lightEntity);
    mPointLight->setColor("white");//设置白光
    mPointLight->setIntensity(1.0f);//设置光的强度
    mPointLight->setLinearAttenuation(1.0f);
    mPointLight->setConstantAttenuation(1.0f);
    mPointLight->setQuadraticAttenuation(0.0025f);
    mPointLight->setColor("white");
    lightEntity->addComponent(mPointLight);



}

void Excavator::initializeSphere(Qt3DCore::QEntity *sceneRoot)
{
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();
    sphereMesh->setRings(8);
    sphereMesh->setSlices(8);
    sphereMesh->setRadius(0.1f);
    sphereTransform = new Qt3DCore::QTransform();
    sphereTransform->setTranslation(QVector3D(-0.17f,0.98f,5.4441f));
    Qt3DExtras::QPhongMaterial *sphereMaterial = new Qt3DExtras::QPhongMaterial();
    sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));

    // Sphere
    m_sphereEntity = new Qt3DCore::QEntity(sceneRoot);
    m_sphereEntity->addComponent(sphereMesh);
    m_sphereEntity->addComponent(sphereMaterial);
    m_sphereEntity->addComponent(sphereTransform);
    m_sphereEntity->setEnabled(false);
}


void Excavator::cameraPositionChanged(const QVector3D &position)
{
    Q_UNUSED(position)
    //t->setTranslation(position);
}

void Excavator::setWristTranslation(float angle)
{
    wristAngle = angle;
    QMatrix4x4 m(1, 0,              0,          0,
                 0, qCos(angle),    -qSin(angle) ,-1.3595f,
                 0, qSin(angle),    qCos(angle)  ,-0.29633f,
                 0, 0,              0            ,1);
    QMatrix4x4 wristMatrix = mUpperArm->transform()->matrix()*m;

    QMatrix4x4 n(1, 0,              0,          0,
                 0, qCos(angle),    -qSin(angle) ,-0.145708f,
                 0, qSin(angle),    qCos(angle)  ,-0.930605f,
                 0, 0,              0            ,1);

    QMatrix4x4 mSphere = wristMatrix*n;
    sphereTransform->setMatrix(mSphere);
    mWrist->transform()->setMatrix(wristMatrix);
}

void Excavator::setUpperArmTranslation(float angle)
{
    upperArmAngle = angle;
    QMatrix4x4 m(1, 0,              0,              0,
                 0, qCos(angle),    -qSin(angle),   1.05664f,
                 0, qSin(angle),    qCos(angle),    3.02617f,
                 0, 0,              0,              1);
    QMatrix4x4 upperArmMatrix = mLowerArm->transform()->matrix()*m;
    mUpperArm->transform()->setMatrix(upperArmMatrix);
    setWristTranslation(wristAngle);
}
void Excavator::setLowerArmTranslation(float angle)
{
    lowerArmAngle = angle;
    QMatrix4x4 m(1, 0,              0,              -0.197071f,
                 0, qCos(angle),    -qSin(angle),   0.804971f,
                 0, qSin(angle),    qCos(angle),    0.0f,
                 0, 0,              0,              1);
    QMatrix4x4 lowerArmMatrix = mTurret->transform()->matrix()*m;
    mLowerArm->transform()->setMatrix(lowerArmMatrix);

//    float lowerArmHydraulicAngle = angle + 13*M_PI/180;
//    QMatrix4x4 n(1, 0,              0,              -0.195911f,
//                 0, qCos(lowerArmHydraulicAngle),    -qSin(lowerArmHydraulicAngle),   0.649544f,
//                 0, qSin(lowerArmHydraulicAngle),    qCos(lowerArmHydraulicAngle),    0.562113f,
//                 0, 0,              0,              1);
//    QMatrix4x4 matrix4x4LowerArmHydraulic = mTurret->transform()->matrix()*n;
//    lowerArmHydraulic->transform()->setMatrix(matrix4x4LowerArmHydraulic);

//    float lowerArmRodAngle = angle + 13*M_PI/180;
//    QMatrix4x4 k(1, 0,              0,              0.0f,
//                 0, qCos(lowerArmRodAngle),    -qSin(lowerArmRodAngle),   1.01665f,
//                 0, qSin(lowerArmRodAngle),    qCos(lowerArmRodAngle),    1.28401f,
//                 0, 0,              0,              1);
//    QMatrix4x4 matrix4x4lowerArmRod = mLowerArm->transform()->matrix()*k;
//    lowerArmRod->transform()->setMatrix(matrix4x4lowerArmRod);


    setUpperArmTranslation(upperArmAngle);
}

void Excavator::setTurretTranslation(float angle)
{

//    QMatrix4x4 m(qCos(angle),   0,  qSin(angle),    0,
//                 0,             1,  0,              0,
//                 -qSin(angle),  0,  qCos(angle),    0,
//                 0,             0,  0,              1);
//    QMatrix4x4 turretMatrix = mBase->transform()->matrix()*m;
//    mTurret->transform()->setMatrix(turretMatrix);
//    setLowerArmTranslation(lowerArmAngle);

}

void Excavator::startSteamshipAnimation()
{

}


void Excavator::startAnimation()
{
    wristAnimation->setStartValue(-122);
    wristAnimation->setEndValue(122);

    lowerArmAnimation->start();
    upperArmAnimation->start();
    wristAnimation->start();
}


void Excavator::lowerArmReach(const QVariant &value)
{
//    float lowerArmAngle = value.toFloat();
//    if(lowerArmAngle >= 27){
//        turretAnimation->start();
//        wristAnimation->stop();
//    }
//    if(lowerArmAngle <= 15){
//        turretAnimation->stop();
//    }
    float turretAngle = value.toFloat();
    if(turretAngle >= 255){
        wristAnimation->setStartValue(122);
        wristAnimation->setEndValue(-122);
        wristAnimation->start();
    }
}


void Excavator::turretReach(const QVariant &value)
{
    float turretAngle = value.toFloat();
    if(turretAngle >= 255){
        wristAnimation->setStartValue(122);
        wristAnimation->setEndValue(-122);
        wristAnimation->start();
    }
}








void Excavator::on_horizontalSlider_valueChanged(int value)
{
     setWristTranslation(value/100.f);

     //lText->setText(QString::number(value));
}

void Excavator::on_horizontalSlider_2_valueChanged(int value)
{
     setUpperArmTranslation(value/100.f);
     hText->setText(QString::number(value));
}

void Excavator::on_horizontalSlider_3_valueChanged(int value)
{
    setLowerArmTranslation(value/100.f);

    //qDebug()<<"=========="<<mWrist->transform()->translation()<<"--------"<<mTurret->transform()->translation();



}

void Excavator::wristMatrixChanged()
{
    float length = sphereTransform->translation().z();
    QVector3D v(vectL.x(),vectL.y(),vectL.z()-1.5f+length/2.0f);//+0.968774f
    lineL->setLength(length-0.65f);
    lineL->setPosition(v);
    lText->setText(QString::number(length,'f',2));
    textTransformL->setTranslation(QVector3D (vectL.x(),vectL.y()-1.5f,vectL.z()-1.5f+length/2.0f));


    float y = sphereTransform->translation().y();
    float lineHlength = qAbs(qAbs(level) + qAbs(y) - 0.5);

    lineH->setLength(lineHlength);

    QVector3D vH(0.0f,level+lineHlength/2.0f,2.6f);
    textTransformH->setTranslation(QVector3D(0.0f,level+y/2.0f-1.5f,sphereTransform->translation().z()));

    lineH->setPosition(QVector3D (0.0f,level+y/2.0f,sphereTransform->translation().z()));
    if(y + level > 0){
        hText->setText(QString::number(lineHlength,'f',2));
    }else{
        hText->setText(QString::number(-lineHlength,'f',2));
    }


}

void Excavator::on_horizontalSlider_4_valueChanged(int value)
{
     setTurretTranslation(value/100.0f);
}

链接:https://pan.baidu.com/s/1n1lH_lLpk6StGIpep9RDxA
提取码:m004

所有代码:https://download.csdn.net/download/yu_20501253/89770471?spm=1001.2014.3001.5503

评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值