• qt-双臂SCARA机器人动画



    在Qt + opengl中完成的双臂SCARA机器人的简单模拟。

    一、演示效果

    在这里插入图片描述

    二、核心程序

    #include "glwidget.h"
    
    #include 
    
    GLWidget::GLWidget(QWidget *parent) :
        QGLWidget(parent),
        pitch(30.0), yaw(0.0), distance(7.0)
    {
    }
    
    void GLWidget::initializeGL()
    {
        glEnable(GL_DEPTH_TEST);
        glClearDepth(1.0);
    
        glEnable(GL_CULL_FACE);
    
        glEnable(GL_POINT_SMOOTH);
        glPointSize(10.0);
    
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    
        glClearColor(0.0, 0.0, 0.0, 0.0);
    }
    
    void GLWidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        setView();
        setLight();
    
        glEnable(GL_COLOR_MATERIAL);
        glDisable(GL_LIGHTING);
    
        glColor4f(0.0, 0.5, 0.0, 0.5);
        for(int i = 0; i <= 20; ++i) {
            glBegin(GL_LINES);
                glVertex3f(-5.0+0.5*i, -5.0, 0.0); glVertex3f(-5.0+0.5*i, 5.0, 0.0);
                glVertex3f(-5.0, -5.0+0.5*i, 0.0); glVertex3f(5.0, -5.0+0.5*i, 0.0);
            glEnd();
        }
        glColor4f(0.5, 0.5, 0.0, 0.5);
        glBegin(GL_LINES);
            glVertex3f(robot->getY(), -robot->getX(), 0.0); glVertex3f(robot->getY(), -robot->getX(), robot->getZ()+0.5);
        glEnd();
    
        glColor4f(0.9, 0.0, 0.0, 0.5);
        robot->displayPath();
    
        glDisable(GL_COLOR_MATERIAL);
        glEnable(GL_LIGHTING);
    
        robot->display();
    
        glFlush();
    }
    
    void GLWidget::resizeGL(int _w, int _h)
    {
        w = _w; h = _h;
    
        glViewport(0.0, 0.0, w, h);
    
        setView();
    }
    
    void GLWidget::setPitch(double _pitch)
    {
        pitch = _pitch;
    }
    
    void GLWidget::setYaw(double _yaw)
    {
        yaw = _yaw;
    }
    
    void GLWidget::setDistance(double _distance)
    {
        distance = _distance;
    }
    
    void GLWidget::setView()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, 1.0*w/h, 0.1, 100.0);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        gluLookAt(-distance, 0.0, 0.0,
                  0.0, 0.0, 0.0,
                  0.0, 0.0, 1.0);
        glRotatef(pitch, 0.0, -1.0, 0.0);
        glRotatef(yaw, 0.0, 0.0, -1.0);
    }
    
    void GLWidget::setLight()
    {
        GLfloat lamb[] = { 0.1, 0.1, 0.1, 1.0 };
        GLfloat ldif[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat lpos[] = { -10.0, -10.0, 10.0, 1.0 };
    
        glEnable(GL_COLOR_MATERIAL);
        glDisable(GL_LIGHTING);
        glDisable(GL_LIGHT0);
        glColor4fv(ldif);
        glBegin(GL_POINTS);
            glVertex4fv(lpos);
        glEnd();
    
        glDisable(GL_COLOR_MATERIAL);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
    
        glLightfv(GL_LIGHT0, GL_POSITION, lpos);
        glLightfv(GL_LIGHT0, GL_AMBIENT, lamb);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, ldif);
    }
    
    
    • 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

    三、下载链接

    https://download.csdn.net/download/u013083044/88851543

  • 相关阅读:
    Linux利用源码包升级Nginx到1.23.1用以解决nginx安全漏洞问题
    SAP ABAP RESTFUL “x-www-form-urlencoded“ Body format
    内核将驱动编译成模块报函数或者变量undefined的错误
    Revel敏捷后台开发框架
    call apply bind
    一套Altair Feko复杂结构模型散射和天线辐射仿真建模攻略
    44、基于深度学习的癌症检测(matlab)
    python中pytest库用法详解
    Qt|多个窗口共有一个提示框类
    自己动手实现rpc框架(一) 实现点对点的rpc通信
  • 原文地址:https://blog.csdn.net/u013083044/article/details/136179669