博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Opengl创建机器人手臂代码示例
阅读量:5461 次
发布时间:2019-06-15

本文共 1891 字,大约阅读时间需要 6 分钟。

/******************************************************

*robot.cpp
*基于opengl的机械手臂示例代码
*s:机械臂逆时针旋转
*S:机械臂顺时针旋转
*e:腕部逆时针旋转
*E:腕部顺时针旋转
*******************************************************/
#include <GL/glut.h>

static int shoulder = 0;

static int elbow = 0;

void init(void)

{
  glClearColor(0.0,0.0,0.0,0.0);
  glShadeModel(GL_FLAT);
}

void display(void)

{
  glClear(GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  glTranslatef(-1.0,0.0,0.0);
  glRotatef((GLfloat)shoulder,0.0,0.0,1.0);

  glTranslatef(1.0,0.0,0.0);

  glPushMatrix();
  glScalef(2.0,0.4,1.0);
  glutWireCube(1.0);

  glPopMatrix();

  glTranslatef(1.0,0.0,0.0);
  glRotatef((GLfloat)elbow,0.0,0.0,1.0);
  glTranslatef(1.0,0.0,0.0);

  glPushMatrix();

  glScalef(2.0,0.4,1.0);
  glutWireCube(1.0);
  glPopMatrix();

  glPopMatrix();

  glutSwapBuffers();
}

void reshape(int w,int h)

{
  glViewport(0,0,(GLsizei)w,(GLfloat)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(65.0,(GLfloat)w/(GLfloat)h,1.0,20.0);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();
  glTranslatef(0.0,0.0,-5.0);
}

void keyboard(unsigned char key, int x, int y)

{
  switch (key) {
  case 's':/*s key rotates at shoulder*/
    shoulder = (shoulder + 5) % 360;
    glutPostRedisplay();
    break;
  case 'S':
    shoulder = (shoulder - 5) % 360;
    glutPostRedisplay();
    break;
  case 'e':
    elbow = (elbow + 5) % 360;
    glutPostRedisplay();
    break;
  case 'E':
    elbow = (elbow - 5) % 360;
    glutPostRedisplay();
    break;
  default:
    break;
  }
}

int main(int argc, char** argv)

{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  glutInitWindowSize(500,500);
  glutInitWindowPosition(100,100);

  glutCreateWindow(argv[0]);

  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);

  glutKeyboardFunc(keyboard);

  glutMainLoop();

  return 0;

}

转载于:https://www.cnblogs.com/herd/p/8530047.html

你可能感兴趣的文章
Derek解读Bytom源码-持久化存储LevelDB
查看>>
规范化-数据库设计原则
查看>>
BASIC-24_蓝桥杯_龟兔赛跑预测
查看>>
C# 中使用Linq和Lambda表达式对List<T>进行排序
查看>>
offsetHeight, clientHeight与scrollHeight的区别
查看>>
002-python基础-hello-world
查看>>
WPF复杂形状按钮
查看>>
谈一谈循环的性能提升
查看>>
为vsftpd 本地用户指定目录
查看>>
codevs1222 信与信封的问题
查看>>
登录界面 动画背景效果
查看>>
B.xml
查看>>
支付宝(Alipay)支付,超详细使用教程讲解!
查看>>
《余额宝技术架构及演进》读后感
查看>>
手机滑动应用
查看>>
Dispose() C# 优化内存
查看>>
3. Longest Substring Without Repeating Characters
查看>>
织梦添加搜索功能
查看>>
JDK的安装和环境变量配置
查看>>
jmeter学习记录--05--Beanshell2
查看>>