• OpenGL学习入门之二维机器人


    OpenGL学习入门之二维机器人

    #include "stdafx.h"
    #include "StdAfx.h"
    #include<windows.h>
    #include<gl/glut.h>
    #include <iostream>
    using namespace std;
    
    static int shoulder = 0, elbow = 0, head = 0, leg = 0, foot = 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();
    
         glPushMatrix(); //It's body
           glScalef (1.6, 2.0, 1.0);
           glutWireCube (1.0);
         glPopMatrix();
    
         glPushMatrix();
           glTranslatef (0.0, 1.4, 0.0);   //It's head
           glRotatef ((GLfloat) head, 0.0, 1.0, 0.0);
           glScalef (0.5,0.6,0.5);
           glutWireCube (1.0);
         glPopMatrix();
         
         glPushMatrix();
           glTranslatef (0.8, 0.9, 0.0);  //It's the right shoulder
           glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
           glTranslatef (0.6, 0.0, 0.0);
    
           glPushMatrix();
           glScalef (1.2, 0.4, 0.5);
           glutWireCube (1.0);
           glPopMatrix();
    
           glTranslatef (0.6, 0.0, 0.0);  //It's the right elbow
           glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
           glTranslatef (0.7, 0.0, 0.0);
    
           glPushMatrix();
           glScalef (1.4, 0.4, 0.5);
           glutWireCube (1.0);
           glPopMatrix();
    	 glPopMatrix();
    
         glPushMatrix();
           glTranslatef (-0.8, 0.9, 0.0);  //It's the left shoulder
           glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
           glTranslatef (-0.6, 0.0, 0.0);
    
           glPushMatrix();
           glScalef (1.2, 0.4, 0.5);
           glutWireCube (1.0);
           glPopMatrix();
    
           glTranslatef (-0.6, 0.0, 0.0);  //It's the left elbow
           glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
           glTranslatef (-0.7, 0.0, 0.0);
    
           glPushMatrix();
           glScalef (1.4, 0.4, 0.5);
           glutWireCube (1.0);
           glPopMatrix();
    	 glPopMatrix();
    
    	 glPushMatrix();
           glTranslatef (0.5, -1.0, 0.0); //It's the right leg
           glRotatef ((GLfloat) leg, 0.0, 1.0, 0.0);
    	   glTranslatef (0.0, -0.6, 0.0);
           
    	   glPushMatrix();
    	   glScalef (0.5, 1.2, 0.5);
    	   glutWireCube (1.0);
    	   glPopMatrix();
    
    	   glTranslatef (0.0, -0.6, 0.0);//It's the right foot
    	   glRotatef ((GLfloat) foot, 0.0, 1.0, 0.0);
    	   glTranslatef (0.0, -0.7, 0.0);
    	   
    	   glPushMatrix();
    	   glScalef (0.5, 1.4, 0.5);
    	   glutWireCube (1.0);
           glPopMatrix();
         glPopMatrix();
    
    	 glPushMatrix();
           glTranslatef (-0.5, -1.0, 0.0);//It's the left leg
           glRotatef ((GLfloat) leg, 0.0, 1.0, 0.0);
    	   glTranslatef (0.0, -0.6, 0.0);
           
    	   glPushMatrix();
    	   glScalef (0.5, 1.2, 0.5);
    	   glutWireCube (1.0);
    	   glPopMatrix();
    
    	   glTranslatef (0.0, -0.6, 0.0);//It's the left foot
    	   glRotatef ((GLfloat) foot, 0.0, 1.0, 0.0);
    	   glTranslatef (0.0, -0.7, 0.0);
    	   
    	   glPushMatrix();
    	   glScalef (0.5, 1.4, 0.5);
    	   glutWireCube (1.0);
           glPopMatrix();
         glPopMatrix();
       
       glPopMatrix();
    
       glutSwapBuffers();
    }
    
    void reshape (int w, int h)
    {
       glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
       glMatrixMode (GL_PROJECTION);
       glLoadIdentity ();
       gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
       glTranslatef (0.0, 1.0, -5.0);
    }
    
    void keyboard (unsigned char key, int x, int y)
    {
       switch (key) {
          case 's'://旋转手臂
             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;
    
    	  case 'h'://旋转头
             head = (head + 5) % 360;
             glutPostRedisplay();
             break;
          case 'H':
             head = (head - 5) % 360;
             glutPostRedisplay();
             break;
    		 	  
    	  case 'l'://旋转腿
             leg = (leg + 5) % 360;
             glutPostRedisplay();
             break;
          case 'L':
             leg = (leg - 5) % 360;
             glutPostRedisplay();
             break;
    
    	  case 'f'://旋转脚
             foot = (foot + 5) % 360;
             glutPostRedisplay();
             break;
          case 'F':
             foot = (foot - 5) % 360;
             glutPostRedisplay();
             break;
    
          case 27:
             exit(0);
             break;
          default:
             break;
       }
    }
    
    int main(int argc, char** argv)
    {
    	cout<<"s-旋转手臂,S-反转手臂"<<endl;
    	cout<<"e-旋转关节,E-反转关节"<<endl;
    	cout<<"h-旋转头部,H-反旋转头"<<endl;
    	cout<<"l-旋转腿部,L-反转腿部"<<endl;
    	cout<<"f-旋转脚部,L-反转脚部"<<endl;
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
       glutInitWindowSize (600, 600); 
       glutInitWindowPosition (100, 100);
       glutCreateWindow (argv[0]);
       init ();
       glutDisplayFunc(display); 
       glutReshapeFunc(reshape);
       glutKeyboardFunc(keyboard);
       glutMainLoop();
       return 0;
    }


     

    文献来源:

    UNDONER(小杰博客) http://blog.csdn.net/undoner

    LSOFT.CN(琅软中国) http://www.lsoft.cn

  • 相关阅读:
    Python-----面向对象三大特性之继承
    Python----从空间角度研究类,类与类的关系
    Python-------面向对象之初识
    Python------内置模块补充2
    Python-----包和日志的使用
    Python-----规范化开发
    Python----各模块
    Python----模块
    asp微信会员卡管理系统,超小的源码_带asp微信支付源码
    asp微信支付代码证书文件post_url.aspx和post_url.aspx.cs源码下载
  • 原文地址:https://www.cnblogs.com/wuyida/p/6301094.html
Copyright © 2020-2023  润新知