[译]GLUT教程 - 移动镜头3
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的更多相关文章
- [译]GLUT教程 - 移动镜头1
		
Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera I 下面来看一个更有趣的GLUT应用.本 ...
 - [译]GLUT教程 - 移动镜头2
		
Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera II 本节的最后一个示例是回顾.现在我们 ...
 - [译]GLUT教程(目录)
		
http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...
 - [译]GLUT教程 - glutPostRedisplay函数
		
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...
 - [译]GLUT教程 - 游戏模式
		
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...
 - [译]GLUT教程 - 位图和正交投影视图
		
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Bitmap Fonts and Orthogonal Projecti ...
 - [译]GLUT教程 - 安装
		
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Setup 你需要什么 要用GLUT库开发程序,你可以下载最新版本3. ...
 - [译]GLUT教程 - 整合代码8
		
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...
 - [译]GLUT教程 - 整合代码7
		
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...
 
随机推荐
- Python的并发并行[4] -> 并发[1] -> concurrent.future 模块
			
concurrent.future 模块 1 thread模块 / thread Module 1.1 常量 / Constants Pass 1.2 函数 / Function Pass 1.3 类 ...
 - Codeforces 1037F. Maximum Reduction
			
总感觉我这种做法会T,一直没写,看了其他人的题解也是这样,,,就果断写了,,可能数据不太深,或者玄学复杂度 题意即求xk-1长度的所有区间的最大值的和,对每一个i(数组下边),他对答案的贡献数量就是在 ...
 - 十. 图形界面(GUI)设计3.标签、按钮和按钮事件
			
标签和按钮也许是图形界面中最常见的两种组件,按钮又总是与激发动作事件有关. 标签 标签(JLabel)是最简单的Swing组件.标签对象的作用是对位于其后的界面组件作说明.可以设置标签的属性,即前景色 ...
 - Ubuntu 16.04使用timedatectl进行管理时间(UTC/CST)(服务器/桌面)
			
说明:16.04开始,systemd接管了系统之后就不再使用/etc/default/rcS和ntpdate.dpkg-reconfigure tzdata进行时间的管理,所以在这些地方设置是无效的, ...
 - Windows查看所有的端口及端口对应的程序
			
步骤一.Windows查看所有的端口 点击电脑左下角的开始,然后选择运行选项,接着我们在弹出的窗口中,输入[cmd]命令,进行命令提示符.然后我们在窗口中输入[netstat -ano]按下回车,即会 ...
 - JAVA常见算法题(十)
			
package com.xiaowu.demo; /** * 一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下……求它在第10次落地时,共经过多少米?第10次反弹多高? * * @au ...
 - THINKPHP nginx设置路由为PATHINFO模式
			
首先THINKPHP配置文件中设置 //url访问模式为rewrite模式 'URL_MODEL'=>'2', 然后再在nginx.conf文件中,找到这一条语句 #access_log log ...
 - linux  selenium运行chrome
			
chrome版本要和chromedriver版本匹配才能正常运行.
 - ylbtech_dbs_article_五大主流数据库模型
			
ylbtech_dbs_article 摘要:什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型.数据模型会影响客户端通过API对数据的操作.不同的数据模型可能会提供或多或少的功能.一般而 ...
 - Console-算法:fun1(do while)
			
ylbtech-Arithmetic:Console-算法[do while]-XX 1.A,Demo(案例) 1.B,Solution(解决方案) using System; namespace ...