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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值