OpenGL中通过鼠标和键盘跟程序交互的实现需要实现注册鼠标和键盘响应事件,在一定条件下,该事件被触发,事件里的程序被执行,达到交互的目的。

通过glutMouseFunc(&OnMouse)注册鼠标事件,OnMouse是鼠标事件的响应,函数格式是void OnMouse(int button,int state,int x,int y);

通过glutKeyboardFunc(&KeyBoards)注册键盘事件,KeyBoards是键盘事件的响应,函数格式是

void KeyBoards(unsigned char key,int x,int y);

一、通过鼠标左键、滚轮键、右键在窗口上单击画点

#include <glut.h> 

void InitEnvironment()
{
glClearColor(0.6,0.6,0.6,0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(6);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0,400,0,400);
} void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
} void OnMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2f(x,400-y);
glEnd();
glFlush();
}
if(button==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
{
glColor3f(0,1,0);
glBegin(GL_POINTS);
glVertex2f(x,400-y);
glEnd();
glFlush();
}
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
{
glColor3f(0,0,1);
glBegin(GL_POINTS);
glVertex2f(x,400-y);
glEnd();
glFlush();
}
} int main(int argc, char *argv[])
{
glutInit(&argc, argv); //初始化GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(500, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("OpenGL");
InitEnvironment(); //初始化
glutMouseFunc(&OnMouse); //注册鼠标事件
glutDisplayFunc(&myDisplay); //回调函数
glutMainLoop(); //持续显示,当窗口改变会重新绘制图形
return 0;
}

在窗口上单击鼠标左键、滚轮键和右键分别绘制红色、绿色和蓝色的点:

二、通过鼠标左键单击控制模型旋转、右键单击暂停旋转

#include <glut.h>
#include <Windows.h> GLfloat angle=0.0f; void InitEnvironment()
{
glEnable(GL_DEPTH);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(12,12,20,0,0,0,0,1,0);
} void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0); //以下绘制一个立方体
glBegin(GL_QUADS);
//底面
glColor4f(1,0,0,1);
glVertex3f(-5,-5,-5);
glVertex3f(5,-5,-5);
glColor4f(0,0,1,1);
glVertex3f(5,5,-5);
glVertex3f(-5,5,-5);
//侧面A
glColor4f(0,0,1,1);
glVertex3f(-5,-5,-5);
glVertex3f(5,-5,-5);
glColor4f(0,1,0,1);
glVertex3f(5,-5,5);
glVertex3f(-5,-5,5);
//侧面B
glColor4f(0,1,0,1);
glVertex3f(5,-5,-5);
glVertex3f(5,5,-5);
glColor4f(0,1,1,1);
glVertex3f(5,5,5);
glVertex3f(5,-5,5);
//侧面C
glColor4f(1,1,0,1);
glVertex3f(5,5,-5);
glVertex3f(-5,5,-5);
glColor4f(1,0,1,1);
glVertex3f(-5,5,5);
glVertex3f(5,5,5);
//侧面D
glColor4f(1,0,1,1);
glVertex3f(-5,5,-5);
glVertex3f(-5,-5,-5);
glColor4f(0,1,0,1);
glVertex3f(-5,-5,5);
glVertex3f(-5,5,5);
//顶面
glColor4f(1,1,0,1);
glVertex3f(-5,-5,5);
glVertex3f(5,-5,5);
glColor4f(0,0,1,1);
glVertex3f(5,5,5);
glVertex3f(-5,5,5); glEnd();
glutSwapBuffers();
glPopMatrix();
} void RotateRect()
{
angle+=0.5;
if(angle>=360)
{
angle=0.0f;
}
Sleep(30);
myDisplay();
} void OnMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
glutIdleFunc(RotateRect);
} if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
{
glutIdleFunc(NULL);
}
} int main(int argc, char *argv[])
{
glutInit(&argc, argv); //初始化GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(500, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("OpenGL");
InitEnvironment(); //初始化显示环境
glutMouseFunc(&OnMouse); //注册鼠标事件
glutDisplayFunc(&myDisplay); //回调函数
glutMainLoop(); //持续显示,当窗口改变会重新绘制图形
return 0;
}

绘制了一个立方体模型,单击左键开始旋转,右键暂停:

三、 通过键盘控制模型各个方向旋转和视角变化

#include <glut.h>
#include <Windows.h> GLfloat angle=10.0f;
GLfloat xDirection=0.0f;
GLfloat yDirection=0.0f;
GLfloat zDirection=10.0f; void InitEnvironment()
{
glEnable(GL_DEPTH);
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor4f(0,0,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
} void KeyBoards(unsigned char key,int x,int y)
{
switch (key)
{
case 'w':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle,-1,0,0);
glutPostRedisplay();
break;
case 'a':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle,0,0,-1);
glutPostRedisplay();
break;
case 's':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle,1,0,0);
glutPostRedisplay();
break;
case 'd':
glMatrixMode(GL_MODELVIEW);
glRotatef(angle,0,0,1);
glutPostRedisplay();
break;
case '4':
xDirection+=0.5; glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break;
case '5':
yDirection+=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break; case '6':
zDirection+=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break; case '1':
xDirection-=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break;
case '2':
xDirection-=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break;
case '3':
xDirection-=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(65,1,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xDirection,yDirection,zDirection,0,0,0,0,1,0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
} void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutWireTeapot(4);
glutSwapBuffers();
} void RotateRect()
{
angle+=0.5;
if(angle>=360)
{
angle=0.0f;
}
Sleep(30);
myDisplay();
} void OnMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
glutIdleFunc(RotateRect);
} if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
{
glutIdleFunc(NULL);
}
} int main(int argc, char *argv[])
{
glutInit(&argc, argv); //初始化GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(500, 200);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL");
InitEnvironment(); //初始化显示环境
glutKeyboardFunc(&KeyBoards); //注册键盘事件
glutDisplayFunc(&myDisplay); //回调函数
glutMainLoop(); //持续显示,当窗口改变会重新绘制图形
return 0;
}

通过键盘上W、A、S、D键控制模型向上、向左、向下、向右旋转:

W、S键控制:

A、D键控制:

数字键4、5、6控制视角向X、Y、Z正方向移动,1、2、3控制视角向X、Y、Z负方向移动:

最新文章

  1. ae GP制作缓冲区分析
  2. Linux NetHogs监控工具介绍
  3. 系统安全:Nessus Home版安装使用
  4. PMP 第三章 单个项目的项目管理标准
  5. 【HTML5 video】video标签的部分属性解析
  6. 【KVM】Ubuntu14.04 安装KVM
  7. linux下top命令查看cpu占用情况
  8. bzoj3790
  9. iOS通过代码关闭程序
  10. Python--多线程、多进程常用概念
  11. JavaScript深度克隆
  12. unp第七章补充之TCP半开连接与半闭连接
  13. where语句中不能直接使用聚合函数
  14. QT 控制LED实验
  15. 实用爬虫-02-爬虫真正使用代理 ip
  16. PHP自动加载下——PSR4
  17. Python基础学习----字符串的常用方法
  18. sizeof笔试题--转
  19. Java小记(1)
  20. In function &#39;int av_clipl_int32_c(int64_t)&#39;: error: &#39;UINT64_C&#39; was not declared in this scope

热门文章

  1. php实现求链表中倒数第k个节点
  2. A Guide to Python&#39;s Magic Methods
  3. USB 3.0规范中译本 附录
  4. UVALive - 3977 Summits (BFS染色)
  5. [Webpack] Configure Prepack with Webpack
  6. [Now] Update an application hosted with Zeit’s Now
  7. js进阶 11-9/10/11 jquery创建和插入节点
  8. 【C/C++学院】(23)Mysql数据库编程--C语言编程实现mysqlclient
  9. 代码在线执行工具(PHP,Java,C++ 等)
  10. Path类的最全面具体解释 - 自己定义View应用系列