OpenGL学习笔记之鼠标检测

OpenGL学习笔记之鼠标检测

//由于之前的代码我也留着的,代码有点多了,所以我稍微分了一下
//main主函数

#include "head.h"

//第一个参数是命令的个数,第二个参数是命令内容
int main(int argc,char** argv)
{
    std::cout<<argc<<std::endl;//输出命令个数

    for(int i = 0; i < argc; ++i)//输出命令内容
    {
        std::cout<<argv[i]<<std::endl;
    }

    glutInit(&argc,argv);//初始化glut库(创建窗口需要)

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);//初始化显示模式 GLUT_RGBA 颜色 GLUT_DOUBLE 双缓冲

    glutInitWindowPosition(100,100);//初始化我们创建的窗口在屏幕左上角的位置

    glutInitWindowSize(600,600);//初始化创建窗口的宽高

    int iWindowID = glutCreateWindow("OpenGL");//创建窗口,并返回窗口的ID,传窗口的标题

    glutDisplayFunc(DisplayFunc);//屏幕显示的回调函数

    glutIdleFunc(IdleFunc);//闲置时回调函数(当没有消息时调用)

    glutKeyboardFunc(KeyboardFunc);//数字、字母键的按键检测的回调函数

    glutSpecialFunc(SpecialFunc);//特殊按键检测(F1~F12,控制键)

    glutMouseFunc(MouseFunc);//鼠标检测

    glutMotionFunc(MotionFunc);//鼠标按着拖动检测

    glutPassiveMotionFunc(PassiveMotionFunc);//鼠标移动检测

    glEnable(GL_LINE_STIPPLE);//开启画虚线的模式

    glClearColor(0.0f,0.6f,0.6f,1.0f);//指定屏幕背景色,最后参数为透明度

    glutMainLoop();//消息主循环

    //glutDestroyWindow(iWindowID);//销毁窗口

    return 1;
}

//head.h

#include <gl/glew.h>
#include <gl/glut.h>
#include <iostream>

struct Vector2D
{
    float fX;
    float fY;
};

extern Vector2D g_vector2D;

void DisplayFunc();//显示函数

void IdleFunc();//闲置时调用的函数

void KeyboardFunc(unsigned char Key,int x,int y);//数字、字母键按键检测

void SpecialFunc(int Key,int x,int y);//F1~F12、控制键检测

void MouseFunc(int button,int state,int x,int y);//鼠标检测

void MotionFunc(int x,int y);//鼠标按着拖动

void PassiveMotionFunc(int x,int y);//鼠标移动

//head.cpp

#include "head.h"

Vector2D g_vector2D = {-0.2f,0.2f};

void DisplayFunc()
{
    glClear(GL_COLOR_BUFFER_BIT);//用指定的颜色清屏

    glBegin(GL_TRIANGLES);

    {
        glColor3f(0,0.6f,0);
        glVertex2f(g_vector2D.fX,g_vector2D.fY);

        glColor3f(0,0.0f,0.6f);
        glVertex2f(g_vector2D.fX+0.4f,g_vector2D.fY);

        glColor3f(0.6f,0.0f,0);
        glVertex2f(g_vector2D.fX+0.2f,g_vector2D.fY-0.4f);
    }

    glEnd();

    glFlush();//提交绘图命令

    glutSwapBuffers();//交换前台缓冲与后台缓冲(GLUT_DOUBLE双缓冲模式需要)
}
//闲置时调用的函数
void IdleFunc()
{
    glutPostRedisplay();//重新绘制(即调用DisplayFunc()函数)
}
//数字、字母键按键检测
void KeyboardFunc(unsigned char Key,int x,int y)
{
    if(Key == 'w' || Key == 'W')
    {
        if(g_vector2D.fY < 1.0f)//界限判定
            g_vector2D.fY += 0.05f;
    }

    if(Key == 's' || Key == 'S')
    {
        if(g_vector2D.fY - 0.4f > -1.0f)//界限判定
            g_vector2D.fY -= 0.05f;
    }

    if(Key == 'a' || Key == 'A')
    {
        if(g_vector2D.fX > -1.0f)//界限判定
            g_vector2D.fX -= 0.05f;
    }

    if(Key == 'd' || Key == 'D')
    {
        if(g_vector2D.fX + 0.4f< 1.0f)//界限判定
            g_vector2D.fX += 0.05f;
    }
}
//F1~F12、控制键检测
void SpecialFunc(int Key,int x,int y)
{
    if(Key == GLUT_KEY_UP)
    {
        if(g_vector2D.fY < 1.0f)//界限判定
            g_vector2D.fY += 0.05f;
    }

    if(Key == GLUT_KEY_DOWN)
    {
        if(g_vector2D.fY - 0.4f > -1.0f)//界限判定
            g_vector2D.fY -= 0.05f;
    }

    if(Key == GLUT_KEY_LEFT)
    {
        if(g_vector2D.fX > -1.0f)//界限判定
            g_vector2D.fX -= 0.05f;
    }

    if(Key == GLUT_KEY_RIGHT)
    {
        if(g_vector2D.fX + 0.4f< 1.0f)//界限判定
            g_vector2D.fX += 0.05f;
    }
}
//鼠标检测
void MouseFunc(int button,int state,int x,int y)
{
    //button  鼠标按键 0 -- 左键    1 -- 中键   2 -- 右键
    //state  鼠标状态  0 -- 按下  1 -- 抬起
    //x,y  鼠标的像素点坐标(以窗口的左上角为原点的坐标系)
    std::cout<<"鼠标的坐标:x = "<<x<<" ,y = "<<y<<std::endl;
    if(button == 0)
    {
        if(state == 0)
        {
            std::cout<<"鼠标左键按下"<<std::endl;
        }
        if(state == 1)
        {
            std::cout<<"鼠标左键抬起"<<std::endl;
        }
    }
    if(button == 1)
    {
        if(state == 0)
        {
            std::cout<<"鼠标中键按下"<<std::endl;
        }
        if(state == 1)
        {
            std::cout<<"鼠标中键抬起"<<std::endl;
        }
    }
    if(button == 2)
    {
        if(state == 0)
        {
            std::cout<<"鼠标右键按下"<<std::endl;
        }
        if(state == 1)
        {
            std::cout<<"鼠标右键抬起"<<std::endl;
        }
    }
}
//鼠标按着拖动
void MotionFunc(int x,int y)
{
    //x,y  鼠标的像素点坐标(以窗口的左上角为原点的坐标系)
    std::cout<<"鼠标按着拖动的坐标:x = "<<x<<" ,y = "<<y<<std::endl;
}
//鼠标移动
void PassiveMotionFunc(int x,int y)
{
    //x,y  鼠标的像素点坐标(以窗口的左上角为原点的坐标系)
    std::cout<<"鼠标移动的坐标:x = "<<x<<" ,y = "<<y<<std::endl;
}

//运行效果:对鼠标的操作,会在控制台屏幕上显示出鼠标的坐标

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值