• 【转】OpenGL鼠标点击事件


    #include <windows.h>    // Windows的头文件

    #include <gl\gl.h>        // OpenGL32库的头文件

    #include <gl\glu.h>        // GLu32库的头文件

    #include <gl\glaux.h>    // GLaux库的头文件

    #include <gl\glut.h>    // Glut库头文件

    #pragma comment( lib, "opengl32.lib")    // OpenGL32连接库

    #pragma comment( lib, "glu32.lib")        // GLu32连接库

    #pragma comment( lib, "glaux.lib")        // GLaux连接库

    #pragma comment( lib, "glut.lib")        // Glut链接库

    int screenWidth=640;

    int screenHeight=480;

    void myInit()

    {

        glClearColor(1.0,1.0,1.0,0.0);            //设置背景颜色为亮白

        glColor3f(0.0f,0.0f,0.0f);                //设置绘图颜色为黑色

        glPointSize(4.0);                        //设置点的大小为4*4像素

        glMatrixMode(GL_PROJECTION);            //设置合适的矩阵

        glLoadIdentity();                       

        gluOrtho2D(0.0,screenWidth,0.0,screenHeight);

    }

    void drawDot(int x,int y)

    {

         glBegin(GL_POINTS);

            glVertex2i(x,y);                    //画一些点

        glEnd();

    }

    void myMouse(int button,int state,int x,int y)

    {

        if(state==GLUT_DOWN)

        {

            if(button==GLUT_LEFT_BUTTON)

            {

                drawDot(x,screenHeight-y);

                glFlush();

            }

            else if(button==GLUT_RIGHT_BUTTON)

            {

                glClearColor(1.0f,0.0f,0.0f,0.0f);

                glClear(GL_COLOR_BUFFER_BIT);

                glFlush();

           

            }   

       

        }

        return;

    }

    void myDisplay()

    {

        glClear(GL_COLOR_BUFFER_BIT);            //清屏

        glBegin(GL_POINTS);

            glVertex2i(100,50);                    //画一些点

            glVertex2i(100,130);

            glVertex2i(150,130);

            glVertex2i(320,240);

        glEnd();

        glFlush();                                //送所有输出到显示设备

    }

    void main(int argc, char **argv)

    {

        glutInit(&argc,argv);                        //初始化工具包   

        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置显示模式

        glutInitWindowSize(640,480);                //设置窗口大小

        glutInitWindowPosition(100,150);            //设置窗口在屏幕上的位置

        glutCreateWindow("my first attempt");        //打开屏幕窗口

        //注册回调函数

        glutDisplayFunc(myDisplay);

        glutMouseFunc(myMouse);

        myInit();

        glutMainLoop();                                //进入循环

    }

  • 相关阅读:
    GetDlgItem
    vc中调用其他应用程序的方法(函数) winexec,shellexecute ,createprocess
    C #中的几个线程同步对象方法 zz
    vc中调用exe的方法
    函数PlaySound和sndPlaySound的用法
    Radio Button的使用
    用Event实现线程同步
    C++学习随笔之九:类和动态内存分配
    C++学习随笔之七:对象和类
    C++学习随笔之二:数据处理
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2808420.html
Copyright © 2020-2023  润新知