zoukankan      html  css  js  c++  java
  • 计算机图形学实验四

    #include <gl/glut.h>
    
    static GLsizei iMode = 1;
    
    static GLfloat xRot = 0.0f; 
    
    static GLfloat yRot = 0.0f; 
    
    int winWidth = 400, winHeight = 200;
    
    int iPointNum = 0;  
    
    int x1=0,x2=0,y1=0,y2=0;
    
    GLuint Cube;
    
     
    
    void initial(void)
    
    {
    
          
    
                  glClearColor(1.0, 1.0, 1.0, 1.0);  
    
                  glMatrixMode (GL_PROJECTION);
    
                  glLoadIdentity();
    
                  gluOrtho2D(-3.0, 3.0, -3.0,3.0);
    
     
    
                  Cube=glGenLists(1);
    
     
    
           glNewList(Cube,GL_COMPILE);
    
     
    
               glBegin(GL_TRIANGLE_FAN);
    
                  glShadeModel(GL_FLAT);
    
                  glColor3f(1.0,0.0,0.0); 
    
               glVertex3f(1,1,1);
    
                         glVertex3f(2,0,0);
    
                         glVertex3f(2,1,0);  
    
                  glEnd();
    
     
    
                  glBegin(GL_TRIANGLE_FAN); 
    
                  glShadeModel(GL_FLAT);
    
                  glColor3f(1.0,1.0,0.0);
    
                   glVertex3f(1,1,1);
    
                         glVertex3f(0,1,0);
    
                         glVertex3f(2,1,0);
    
               glEnd();
    
                 
    
                  glBegin(GL_TRIANGLE_FAN);
    
                  glShadeModel(GL_FLAT);
    
                  glColor3f(0.0,0.0,1.0);
    
                   glVertex3f(1,1,1);
    
                         glVertex3f(0,1,0);
    
                         glVertex3f(2,0,0);
    
                  glEnd();
    
                 
    
                  glBegin(GL_TRIANGLE_FAN);
    
                  glShadeModel(GL_FLAT);
    
                  glColor3f(0.0,1.0,0.0);
    
                      glVertex3f(0,1,0);
    
                         glVertex3f(2,0,0);
    
                         glVertex3f(2,1,0);
    
               glEnd();
    
     
    
               glEndList();
    
    }
    
     
    
    void triangle (GLsizei mode)
    
    {
    
                  if(mode == 1)   
    
                  {
    
                         glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    
                         glMatrixMode(GL_PROJECTION);
    
                         glLoadIdentity();
    
                         gluOrtho2D(-5.0,5.0,-5.0,5.0);
    
                         glMatrixMode(GL_MODELVIEW);
    
                         glPushMatrix();
    
                         glLoadIdentity();
    
                      switch(iMode){
    
                              case 1:glCallList(Cube);break;
    
                                 case 2:glRotatef(-90.0,1.0,0.0,0.0);glCallList(Cube);break;
    
                                 case 3:glRotatef(90.0,0.0,1.0,0.0);glCallList(Cube);break;
    
                         }
    
                         glPopMatrix();
    
                  }
    
                  else
    
                  {
    
                         glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    
                      glMatrixMode(GL_PROJECTION);
    
                         glLoadIdentity();
    
                         gluPerspective(45.0f,1,1.0f,100.0f);
    
                         gluLookAt(0,0,-10,0,0,0,0,1,0);
    
                         glMatrixMode(GL_MODELVIEW);
    
                         glPushMatrix();
    
                         glRotatef(xRot,1.0f,0.0f,0.0f);
    
                         glRotatef(yRot,0.0f,1.0f,0.0f);
    
                         glCallList(Cube);
    
                         glPopMatrix();
    
                      glRotatef(xRot, 1.0f, 0.0f, 0.0f); 
    
                      glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    
                  }
    
    }
    
     
    
    void Display(void)
    
    {
    
                  glClear(GL_COLOR_BUFFER_BIT);
    
                  glViewport(0, 0, 200, 200);
    
                  triangle(1);
    
     
    
                  glViewport(200, 0, 200, 200);
    
                  triangle(2);
    
     
    
                  glFlush();
    
    }
    
     
    
    void PassiveMouseMove (GLint xMouse, GLint yMouse)
    
    {
    
           if(iPointNum == 1)       {
    
                  x2 = xMouse;
    
                  y2 = winHeight - yMouse;    
    
               if(y2<=y1)     xRot -= 0.05f;
    
                  if(y2>=y1)     xRot += 0.05f;
    
                  if(x2>=x1)     yRot -= 0.05f;
    
                  if(x2<=x1)     yRot += 0.05f;
    
                  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 ProcessMenu(int value)
    
    {
    
                  iMode = value; 
    
                  glutPostRedisplay();
    
    }
    
     
    
    void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
    
    {
    
           if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)       {
    
                  if(iPointNum == 0 || iPointNum == 2){
    
                         iPointNum = 1;
    
                         x1 = xMouse;         y1 = winHeight - yMouse;
    
                  }
    
                  else{
    
                         iPointNum = 2;
    
                         x2 = xMouse;         y2 = winHeight - yMouse;
    
                  glutPostRedisplay();                
    
                  }
    
           }
    
           if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){
    
                  iPointNum = 0;
    
                  glutPostRedisplay();
    
           }
    
    }
    
    void main()
    
    {
    
                  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    
                  glutInitWindowPosition(100, 100);
    
                  glutInitWindowSize(400,200);
    
                  glutCreateWindow("实验四-三视图与旋转");
    
     
    
                  int nMainMenu = glutCreateMenu(ProcessMenu);
    
                  glutAddMenuEntry("正视图",1);
    
                  glutAddMenuEntry("俯视图", 2);
    
                  glutAddMenuEntry("侧视图", 3);
    
                  glutAttachMenu(GLUT_RIGHT_BUTTON);
    
     
    
               glutMouseFunc(MousePlot);                
    
               glutPassiveMotionFunc(PassiveMouseMove);  
    
                  initial();
    
                  glutDisplayFunc(Display);
    
                  glutMainLoop();
    
    }
    View Code
  • 相关阅读:
    第十八篇:在SOUI中实现PreTranslateMessage
    第十七篇:使用窗口的cache属性加速SOUI的渲染
    通过驱动向打印机发送一段(ESC)控制指令
    转一个希尔排序
    关于Memo或者Edit之类控件, 直接设置Text无法撤销的解决方案
    关于创建无窗体程序的一点心得
    在Vista或更高版本Windows系统中, 获取超大图标的办法
    随笔里的标签为啥不能用空格分隔??
    一个ICMP单元
    Delphi XE5 与其他版本共存
  • 原文地址:https://www.cnblogs.com/acm-jing/p/4496665.html
Copyright © 2011-2022 走看看