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. hadoop之深入浅出

    分布式文件系统与HDFS lHDFS体系结构与基本概念*** l数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管 ...

  2. 数学【p2117】 小z的矩阵

    题目描述-->p2117 小z的矩阵 分析: 题目给定我们一个正方形. 容易想到,正方形是对称的. 推敲一下 如果我们的矩阵是这样的↓ 闭眼瞎敲出来的. \[\begin{bmatrix} {0 ...

  3. [USACO17DEC] Barn Painting

    题目描述 Farmer John has a large farm with NN barns (1 \le N \le 10^51≤N≤105 ), some of which are alread ...

  4. 1.11(java学习笔记)封装

    封装将内部细节封装起来,只暴露外部接口. 比如我们的电视就将复杂的内部线路用外壳封装起来,只留下外部按钮或遥控,用户只需要知道按钮或遥控的作用就可以,无需明白电视内部是如何工作. 而且封装也保障了安全 ...

  5. 关于UIWebView设置高度自适应的问题

    - (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMa ...

  6. centos下配置ssh使用密钥

    查询了网上的一些教程,然后根据自己的实际操作,记录自己实际配置ssh密钥的过程: 首先在centos终端切换到要链接的用户,比如用户ssh 使用该用户生成密钥: ssh-keygen -t rsa 中 ...

  7. tiny4412 串口驱动分析四 --- 修改默认的串口输出

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...

  8. 设计模式之空对象模式(php实现)

    github地址:https://github.com/ZQCard/design_pattern /** * 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象 ...

  9. ylbtech-WelfareSystem(福利发放管理系统)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-WelfareSystem(福利发放管理系统)-数据库设计 1.A,数据库关系图(Database Diagram) 1.B,数据库设计脚 ...

  10. haproxy + rabbitmq + keepalived的高可用环境搭建

    一.rabbitmq的搭建:参考rabbimq的安装及集群设置 二.安装和配置haproxy 1.安装haproxyyum install haproxy 2.安装rsysloga. 检查rsyslo ...