zoukankan      html  css  js  c++  java
  • Back to openGL!

    
    
    #include <iostream>
    #include <windows.h>  
    #include <gl/glut.h>  
    #include <math.h>
    #include <gl/gl.h>
    /* back to openGL  2016/9/19  magic  */
    //thanks to:    http://blog.csdn.net/bill_ming/article/details/7662809
    using namespace std; 
    
    #define pi 3.1415926
    #define  GLUT_WHEEL_UP 3           //定义滚轮操作  
    #define  GLUT_WHEEL_DOWN 4  
    
    
    int mx,my;
    int ry=0;int rx=0;    //turnning angle around X/Y axis following right-hand rule
    int i=0;
    GLdouble a=0.2;    //size of teapot
    bool mouseisdown=false;
    bool loopr=false;
    
    void timer(int p)
    {
         ry-=5;
            glutPostRedisplay();    //marks the current window as needing to be redisplayed.
         if (loopr)
             glutTimerFunc(10,timer,0);
    }
    
    void motion(int x, int y)
    {
     if(mouseisdown==true)
        {
    //       cout<<mx<<" "<<my<<" "<<i<<endl;
            ry+=x-mx;
            rx+=y-my;
            mx=x;my=y;
            glutPostRedisplay();
        }
    }
    
    void mouse(int button, int state, int x, int y)
    {
        if(button == GLUT_LEFT_BUTTON)
         {
    //         cout<<"left click!"<<endl;
            mx=x;my=y;   //Initialize mx my 9/20
             
            if(state == GLUT_DOWN)
            {    mouseisdown=true; loopr=false;}
            else
                mouseisdown=false;
         }
        if (button== GLUT_RIGHT_BUTTON)
            if(state == GLUT_DOWN)
            {loopr=true; glutTimerFunc(200,timer,0);}  
        if (button== GLUT_MIDDLE_BUTTON)
        {
    //        cout<<"wheel"<<endl;
            if(state == GLUT_DOWN){
                a+=0.1;
                glutPostRedisplay();
            }
        }
    }
    
    void special(int key, int x, int y)
    {
        switch(key)
        {
        case GLUT_KEY_LEFT:
            ry-=5;
            glutPostRedisplay();
            break;
    
        case GLUT_KEY_RIGHT:
           ry+=5;
            glutPostRedisplay();
            break;
    
        case GLUT_KEY_UP:
            rx+=5;
            glutPostRedisplay();
            break;
    
        case GLUT_KEY_DOWN:
            rx-=5;
            glutPostRedisplay();
            break;
            
        }
    }
    
    void special_1(unsigned char key, int x, int y){
        
        switch(key){
            case 'a':
            a+=0.1;
            glutPostRedisplay();
            break;
        case 'z':
            a-=0.1;
            glutPostRedisplay();
            break;
        }
    }
    
    
    void display0(void)
    {      
            float red[3]={1,0,0};
            float blue[3]={0,1,0};
            float green[3]={0,0,1};
            float yellow[3]={1,1,0};   
            
            glClearColor(1,0,1,1);
            glClear(GL_COLOR_BUFFER_BIT);
            glLoadIdentity();
            glRotatef(ry,0,1,0);      
            glRotatef(rx,1,0,0);
            glColor3fv(green);
            
            glutWireTeapot(a);
    
            glFlush();
    }
    
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_SINGLE| GLUT_RGBA);
        glutInitWindowSize (600, 600);
        glutInitWindowPosition(100,100);     
        glutCreateWindow ("A TEAPOT");     //window initializing
    
        glutDisplayFunc (display0);    
        glutMouseFunc(mouse);    //message processing function
        glutMotionFunc(motion);
        glutSpecialFunc(special);
        glutKeyboardFunc(special_1);
        glutMainLoop();      
    
        return 0;
    }
    
    
    
    
    
    

     back to openGL


    这是一个初级的openGL编程例子,实现了程序对键鼠操作消息的处理:
    1.鼠标拖拽使模型跟随光标旋转;
    2.按'A'或双击鼠标中键放大,按'Z'缩小;
    3.单击鼠标右键使模型旋转;
    4.方向键(上下左右)使模型旋转

  • 相关阅读:
    Linux之安装python3.6.6
    Python之计算器
    springboot项目快速代码生成工具
    电脑忘记密码怎么办?
    HTML编辑器
    WCF的几种寄宿方式
    获取客户端IP 和 外网IP
    发送短信验证码
    动态库,服务tips
    asp.net WebService 与 WCF的区别
  • 原文地址:https://www.cnblogs.com/mememagic/p/5890097.html
Copyright © 2011-2022 走看看