Lighthouse3d.com >> GLUT Tutorial >> Input >> Moving the Camera III

上一节的示例中我们用键盘更改镜头的方向.这一节我们用鼠标来代替.

当用户按下鼠标左键时我们会记录鼠标的X轴位置.当鼠标移动时我们会检测新的X轴位置,并利用位移差距设置一个deltaAngle变量.该变量会加到初始角度以计算镜头当前的方向.

鼠标点击时的X轴位置也需要变量来保存.

float deltaAngle = 0.0f;
int xOrigin = -;

注意xOrigin变量应初始化为一个鼠标输入时永远不会产生的值(至少必须为0).这样可以让我们区分开用户输入的是左键还是其它键.

下面这函数是用来响应处理按键状态的变更:

void mouseButton(int button, int state, int x, int y) {

    // only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) { // when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
}

留意到xOrigin变量会在左键松开的时候设置为-1.

处理鼠标移动事件的函数实现如下:

void mouseMove(int x, int y) {

    // this will only be true when the left button is down
if (xOrigin >= ) { // update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f; // update camera's direction
lx = sin(angle + deltaAngle);
lz = -cos(angle + deltaAngle);
}
}

在main函数中我们要注册两个新的回调函数:

int main(int argc, char **argv) {

    // init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Lighthouse3D - GLUT Tutorial"); // register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene); glutIgnoreKeyRepeat();
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey); // here are the two new functions
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove); // OpenGL init
glEnable(GL_DEPTH_TEST); // enter GLUT event processing cycle
glutMainLoop(); return ;
}

完整代码会在下一节给出.

[译]GLUT教程 - 移动镜头3的更多相关文章

  1. [译]GLUT教程 - 移动镜头1

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera I 下面来看一个更有趣的GLUT应用.本 ...

  2. [译]GLUT教程 - 移动镜头2

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera II 本节的最后一个示例是回顾.现在我们 ...

  3. [译]GLUT教程(目录)

    http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...

  4. [译]GLUT教程 - glutPostRedisplay函数

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...

  5. [译]GLUT教程 - 游戏模式

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...

  6. [译]GLUT教程 - 位图和正交投影视图

    Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Bitmap Fonts and Orthogonal Projecti ...

  7. [译]GLUT教程 - 安装

    Lighthouse3d.com >> GLUT Tutorial >> Basics >> Setup 你需要什么 要用GLUT库开发程序,你可以下载最新版本3. ...

  8. [译]GLUT教程 - 整合代码8

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...

  9. [译]GLUT教程 - 整合代码7

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...

随机推荐

  1. Python与数据库[2] -> 关系对象映射/ORM[0] -> ORM 与 sqlalchemy 模块

    ORM 与 sqlalchemy 1 关于ORM / About ORM 1.1 ORM定义 / Definition of ORM ORM(Object Relational Mapping),即对 ...

  2. cake-walk

    Of course it was not a cake-walk in the beginning 3. This is going to be a cakewalk 这将易如反掌. 4. Julia ...

  3. Shellcode开发辅助工具shellnoob

    Shellcode开发辅助工具shellnoob   Shellcode开发的过程中会遇到很多繁杂的工作,如编译.反编译.调试等.为了减少这部分工作,Kali Linux提供了开发辅助工具shelln ...

  4. CodeForces - 985F Isomorphic Strings

    假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...

  5. Lucas定理模板【bzoj2982】【combination】

    (上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...

  6. Android Handler 消息循环机制

    前言 一问起Android应用程序的入口,很多人会说是Activity中的onCreate方法,也有人说是ActivityThread中的静态main方法.因为Java虚拟机在运行的时候会自动加载指定 ...

  7. kubernetes社区项目生态概览

    原文  http://dockone.io/article/2075 作为容器集群管理技术的最流行的技术,kubernetes,自从2014在github上开源后,已经通过多个项目形成了一个生态,以下 ...

  8. 阿里云域名绑定IP

    前提条件:拥有一个阿里云域名,拥有一台自己的服务器,并且知道ip,我的是nginx 1.登陆阿里云https://www.aliyun.com/ 2.选择域名与网站,会看到自己拥有的域名,比如我的是m ...

  9. 如何使用apache的 work模式还是 prefork 模式

    注意: 2.4之前版本默认为prefork, 2.4已经变为event模式.三种模式比较: http://www.cnblogs.com/fnng/archive/2012/11/20/2779977 ...

  10. 2017.3.31 spring mvc教程(一)核心类与接口

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...