使用OpenGL实现一个简易的Robot模拟(3)


   glutSwapBuffers(); 
}    
//随便画一些小东西
void  Something()
{
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glTranslatef(0,-3.5f,0);
glutWireSphere(2.0f,50,50);
glutWireTorus(0.3f, 1.0f,50,50);
glutWireCone(0.30f, 1.1f, 20, 20);
glPopMatrix();
}
void renderBitmapString(float x, float y, void *font,char *string)  
{  
  char *c;  
  glRasterPos2f(x, y);  
  for (c=string; *c != '\0'; c++) {  
    glutBitmapCharacter(font, *c);  
  }  
}  
 
void reshape (int w1, int h1) 
{  
    w=w1;  
    h=h1; 
   glViewport (0, 0, (GLsizei) w1, (GLsizei) h1);  
   glMatrixMode (GL_PROJECTION); 
   glLoadIdentity (); 
   gluPerspective(65.0, (GLfloat) w1/(GLfloat) h1, 1.0, 20.0); 
   glMatrixMode(GL_MODELVIEW); 
   glLoadIdentity(); 
   glTranslatef (0.0, 0.0, -8.0); 

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

   switch (key) { 
      case 'w': //向后走
         turn1=turn; 
         forward = forward -0.04*sin((GLfloat)turn1/360*3.14*2); 
         z=z-0.05*cos((GLfloat)turn1/360*3.14*2); 
          
         if(tag==0){ 
             shoulder1 = (shoulder1 + 1); 
             shoulder2 = (shoulder2 - 1); 
             if(shoulder1>=0){elbow=elbow-1.2;} 
             else{elbow=elbow+1.2;} 
         } 
         else 
         { 
             shoulder1 = (shoulder1 - 1); 
             shoulder2 = (shoulder2 + 1); 
             if(shoulder1>=0){elbow=elbow+1.25;} 
             else{elbow=elbow-1.2;} 
         } 
         if(shoulder1>30){ 
             tag=1; 
         } 
         if(shoulder1<-30){ 
             tag=0; 
         } 
IsStop=true;
         glutPostRedisplay(); 
         break; 
      case 's': //向前走
         turn1=turn; 
         forward = forward +0.04*sin((GLfloat)turn1/360*3.14*2); 
         z=z+0.05*cos((GLfloat)turn1/360*3.14*2);
         if(tag==0){ 
             shoulder1 = (shoulder1 - 1); 
             shoulder2 = (shoulder2 + 1); 
         } 
         else 
         { 
             shoulder1 = (shoulder1 + 1); 
             shoulder2 = (shoulder2 - 1); 
         } 
         if(shoulder1>30){ 
             tag=0; 
         } 
         if(shoulder1<-30){ 
             tag=1; 
         } 
IsStop=true;
         glutPostRedisplay(); 
         break; 
      case 'd'://右转
         turn = (turn - 5) % 360; 
         glutPostRedisplay();
IsStop=true;
         break; 
      case 'a'://左转
         turn = (turn + 5) % 360; 
         glutPostRedisplay();
IsStop=true;
         break; 
      case 'l': 
         shoulder1 = (shoulder1 + 2) % 360; 
         shoulder2 = (shoulder2 - 4) % 360; 
         glutPostRedisplay(); 
IsStop=true;
         break; 
      case 'L': 
         shoulder1 = (shoulder1 - 5) % 360; 
         shoulder2 = (shoulder2 + 10) % 360; 
         glutPostRedisplay();
IsStop=true;
         break; 
 case 'P':
 IsStop=false;
 break;
 case 'p':
 IsStop=false;
 break;
      case 27: 
         exit(0); 
         break; 
      default: 
         break; 
   } 
}  
//设置视觉角度 
void setOrthographicProjection()   
{  
    glMatrixMode(GL_PROJECTION);  
    glPushMatrix();  
    glLoadIdentity();  
    gluOrtho2D(0, w, 0, h);  
    glScalef(1, -1, 1);  
    glTranslatef(0, -h, 0);  
    glMatrixMode(GL_MODELVIEW);  
}  
void resetPerspectiveProjection()   
{  
    glMatrixMode(GL_PROJECTION);  
    glPopMatrix();  
    glMatrixMode(GL_MODELVIEW);  
}
//光源的坐标变化
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
glutPostRedisplay();
}
//鼠标事件
void Mouse(int button,int state, int x, int y)     
{
    if (state == GLUT_DOWN)//鼠标按下
{
if (x<0)
{
//向左旋转
yRot -= 5.0f;
}else if (x>=0)
{
//向右旋转
yRot += 5.0f;
}else if (y>=0)
{
//向上旋转
xRot-= 5.0f;
}else if (y<0)
{
//向下旋转
xRot += 5.0f;
}
if(xRot> 356.0f)
xRot = 0.0f;
if(xRot < -1.0f)
xRot = 355.0f;
if(yRot > 356.0f)
yRot = 0.0f;
if(yRot < -1.0f)
yRot = 355.0f;
glutPostRedisplay();
}
}
//时间函数,定时刷新
void TimerFunction(int value)
{
display();
    glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
//主函数
int main(int argc, char** argv) 

   glutInit(&argc, argv); 
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
   glutInitWindowSize (800, 600);  
   glutInitWindowPosition (100, 100); 
   glutCreateWindow("机器人 按W向前走  按s向后走   按a向左转  按d向右转   按p可以自行转动");
   SetupRC();
   glutDisplayFunc(display);  
   glutReshapeFunc(reshape); 
   glutKeyboardFunc(keyboard);
   glutSpecialFunc(SpecialKeys);
   glutMouseFunc(Mouse);
   glutTimerFunc(33, TimerFunction, 1);
  // glutIdleFunc(display);
  
   glutMainLoop(); 
   return 0; 

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywdxj.html