博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GLUT Trackball Demo
阅读量:7031 次
发布时间:2019-06-28

本文共 9264 字,大约阅读时间需要 30 分钟。

GLUT Trackball Demo

1.Introduction

在三维场景中建立模型后,为了方便用户从各个角度观察模型,从而需要对三维视图进行控制。常见的视图交互控制方式有:Trackball控制器、飞行控制器,还有三维游戏常用的第一人称控制器。这些视图控制器的根本是对模型视图矩阵MODELVIEW进行变换。

Trackball控制器以一种用户友好的交互方式来变换视图,原理是由Trackball激发,Trackball如下图所示:

Figure 1. Trackball

通过手指在球面上滚动,就可以对三维视图进行控制。现在需要用鼠标的拖动来模拟Trackball以实现对三维视图的控制。在OpenGL中实现Trackball控制视图分为以下几步:

1.将鼠标移动时的屏幕坐标点映射到单位球上;

2.将开始旋转视图时鼠标点到球心的向量与鼠标移动过程中的坐标点球心的向量叉乘,即可得到旋转轴;

根据叉乘的定义,可以得到旋转角度:

有了旋转轴和旋转角度,就可以对视图进行旋转操作了。

2.GLUT Test

为了简明地说明Trackball的原理,这里只使用了GLUT库和OpenCASCADE中的四元数和向量相关的类。如果其他开源库也有向量计算和四元数据计算类,也可以将代码很快移植到使用其他库,如矩阵计算库Eigen等。下面给出GLUT的示例代码:

/*
Copyright(C) 2017 Shing Liu(eryar@163.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <gp_XYZ.hxx>
#include <gp_Trsf.hxx>
#include <gp_Quaternion.hxx>
#include <gl/glut.h>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
GLint VIEWPORT_WIDTH = 0;
GLint VIEWPORT_HEIGHT = 0;
gp_XYZ U;
gp_XYZ V;
gp_Quaternion R;
gp_Quaternion Q;
void init(
void)
{
    GLfloat aSpecularMaterial[]  = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat aLightPosition[] = {1.0, 1.0, 1.0, 0.0};
    GLfloat aWhiteLight[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat aModelAmbient[] = {0.1, 0.1, 0.1, 1.0};
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
    glMaterialfv(GL_FRONT, GL_SPECULAR, aSpecularMaterial);
    glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
    glLightfv(GL_LIGHT0, GL_POSITION, aLightPosition);
    glLightfv(GL_LIGHT0, GL_SPECULAR, aWhiteLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, aWhiteLight);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, aModelAmbient);
    
//
 Enable lighting
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}
void display(
void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1.0);
    
//
 draw mouse motion point.
    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(U.X() * 2.0, U.Y() * 2.0, U.Z() * 2.0);
    glEnd();
    glutSwapBuffers();
}
void reshape(GLint theWidth, GLint theHeight)
{
    VIEWPORT_WIDTH = theWidth;
    VIEWPORT_HEIGHT = theHeight;
    
//
 Reset viewport and projection parameter
    glViewport(0, 0, theWidth, theHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
if (theWidth <= theHeight)
    {
        glOrtho(-1.5, 1.5, -1.5 * theHeight / theWidth, 1.5 * theHeight / theWidth, -10.0, 10.0);
    }
    
else
    {
        glOrtho(-1.5 * theWidth / theHeight, 1.5 * theWidth / theHeight, -1.5, 1.5, -10.0, 10.0);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void mapToSphere(GLint theX, GLint theY, gp_XYZ& thePnt)
{
    GLfloat aX = (theX - 0.5 * VIEWPORT_WIDTH) / VIEWPORT_WIDTH;
    GLfloat aY = (0.5 * VIEWPORT_HEIGHT - theY) / VIEWPORT_HEIGHT;
    GLfloat aSinx = sin(M_PI * aX * 0.5);
    GLfloat aSiny = sin(M_PI * aY * 0.5);
    GLfloat aSxy2 = aSinx * aSinx + aSiny * aSiny;
    thePnt.SetX(aSinx);
    thePnt.SetY(aSiny);
    thePnt.SetZ(aSxy2 < 1.0 ? sqrt(1.0 - aSxy2) : 0.0);
}
void mouse(GLint theButton, GLint theState, GLint theX, GLint theY)
{
    mapToSphere(theX, theY, U);
    glutPostRedisplay();
}
void motion(GLint theX, GLint theY)
{
    mapToSphere(theX, theY, V);
    gp_XYZ W = U.Crossed(V);
    
if (W.Modulus() < gp::Resolution())
    {
        
return;
    }
    GLfloat aAngle = W.Modulus() / (U.Modulus() * V.Modulus());
    aAngle = asin(aAngle);
    glRotatef(aAngle * 180.0 / M_PI, W.X(), W.Y(), W.Z());
    glutPostRedisplay();
    U = V;
}
int main(
int argc, 
char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 300);
    glutCreateWindow("Trackball Demo");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
    
return 0;
}

上述程序运行结果如下动图所示:

从上图可知,当旋转几次后视图并没有得到预期的结果。因为程序将鼠标映射后坐标与球心得到的向量进行了显示,发现当旋转几次后,这个向量并没有跟随鼠标。

3.Transform

通过观察上面代码程序运行的结果,可以发现鼠标映射函数得到的映射点始终是位于XOY平面上的一个半球面上。当视图被旋转后,视图的坐标系已经发生了变化,而映射点并没有。为了跟踪这个变换用四元数进行累乘来记录这一系列的旋转变换。最后在映射函数中将映射点变换到已经改变的视图坐标系中。

即在鼠标移动处理函数中增加记录变换:

    gp_Quaternion q(W, aAngle);
    R.Multiply(q);

在mapToSphere函数中增加:

gp_Trsf aTrsf;
aTrsf.SetRotation(Q.Inverted());
aTrsf.Transforms(thePnt);

列出升级后的全部代码如下所示:

/*
Copyright(C) 2017 Shing Liu(eryar@163.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <gp_XYZ.hxx>
#include <gp_Trsf.hxx>
#include <gp_Quaternion.hxx>
#include <gl/glut.h>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
GLint VIEWPORT_WIDTH = 0;
GLint VIEWPORT_HEIGHT = 0;
gp_XYZ U;
gp_XYZ V;
gp_Quaternion R;
gp_Quaternion Q;
void init(
void)
{
    GLfloat aSpecularMaterial[]  = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat aLightPosition[] = {1.0, 1.0, 1.0, 0.0};
    GLfloat aWhiteLight[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat aModelAmbient[] = {0.1, 0.1, 0.1, 1.0};
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
    glMaterialfv(GL_FRONT, GL_SPECULAR, aSpecularMaterial);
    glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
    glLightfv(GL_LIGHT0, GL_POSITION, aLightPosition);
    glLightfv(GL_LIGHT0, GL_SPECULAR, aWhiteLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, aWhiteLight);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, aModelAmbient);
    
//
 Enable lighting
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}
void display(
void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1.0);
    
//
 draw mouse motion point.
    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(U.X() * 2.0, U.Y() * 2.0, U.Z() * 2.0);
    glEnd();
    glutSwapBuffers();
}
void reshape(GLint theWidth, GLint theHeight)
{
    VIEWPORT_WIDTH = theWidth;
    VIEWPORT_HEIGHT = theHeight;
    
//
 Reset viewport and projection parameter
    glViewport(0, 0, theWidth, theHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
if (theWidth <= theHeight)
    {
        glOrtho(-1.5, 1.5, -1.5 * theHeight / theWidth, 1.5 * theHeight / theWidth, -10.0, 10.0);
    }
    
else
    {
        glOrtho(-1.5 * theWidth / theHeight, 1.5 * theWidth / theHeight, -1.5, 1.5, -10.0, 10.0);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void mapToSphere(GLint theX, GLint theY, gp_XYZ& thePnt)
{
    GLfloat aX = (theX - 0.5 * VIEWPORT_WIDTH) / VIEWPORT_WIDTH;
    GLfloat aY = (0.5 * VIEWPORT_HEIGHT - theY) / VIEWPORT_HEIGHT;
    GLfloat aSinx = sin(M_PI * aX * 0.5);
    GLfloat aSiny = sin(M_PI * aY * 0.5);
    GLfloat aSxy2 = aSinx * aSinx + aSiny * aSiny;
    thePnt.SetX(aSinx);
    thePnt.SetY(aSiny);
    thePnt.SetZ(aSxy2 < 1.0 ? sqrt(1.0 - aSxy2) : 0.0);
    gp_Trsf aTrsf;
    aTrsf.SetRotation(Q.Inverted());
    aTrsf.Transforms(thePnt);
}
void mouse(GLint theButton, GLint theState, GLint theX, GLint theY)
{
    mapToSphere(theX, theY, U);
    Q = R;
    glutPostRedisplay();
}
void motion(GLint theX, GLint theY)
{
    mapToSphere(theX, theY, V);
    gp_XYZ W = U.Crossed(V);
    
if (W.Modulus() < gp::Resolution())
    {
        
return;
    }
    GLfloat aAngle = W.Modulus() / (U.Modulus() * V.Modulus());
    aAngle = asin(aAngle);
    glRotatef(aAngle * 180.0 / M_PI, W.X(), W.Y(), W.Z());
    glutPostRedisplay();
    gp_Quaternion q(W, aAngle);
    R.Multiply(q);
    U = V;
}
int main(
int argc, 
char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 300);
    glutCreateWindow("Trackball Demo");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
    
return 0;
}

这次程序运行和预期结果一致,旋转很流畅:

4.Conclusion

程序员总是有很强的控制欲,希望一切尽在掌握之中。在三维场景中建立模型后,如何对视图进行控制来方便地观察模型呢?最常见的控制方式就是Trackball. OpenSceneGraph、Eigen等开源库都有相关的实现。

Trackball的实现主要是将鼠标点映射到一个球面上,然后使用叉乘得到旋转轴和旋转角度。为了旋转的流畅,使用四元数记录了一系列的旋转变换,最后通过将映射点进行坐标变换得到满意的效果。

5.References

1. Virtual Trackball.

2. Object Mouse Trackball

转载于:https://www.cnblogs.com/opencascade/p/glut_trackball.html

你可能感兴趣的文章
信号处理
查看>>
【100题】第五十九题 用C++编写不能被继承的类
查看>>
轻描淡写
查看>>
mysql基本操作
查看>>
39.CSS3弹性伸缩布局【下】
查看>>
[javascript]图解+注释版 Ext.extend()
查看>>
我的前端工具集(七)div背景网格
查看>>
linux 下mongo 基础配置
查看>>
【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)...
查看>>
JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结
查看>>
java之jvm学习笔记六(实践写自己的安全管理器)
查看>>
Docker容器查看ip地址
查看>>
在PC端或移动端应用中接入商业QQ
查看>>
将python3.6软件的py文件打包成exe程序
查看>>
DataTable 排序
查看>>
大白话5分钟带你走进人工智能-第二十节逻辑回归和Softmax多分类问题(5)
查看>>
嵌入式系统在工业控制中的应用
查看>>
使用httpclient异步调用WebAPI接口
查看>>
c++ 类的对象与指针
查看>>
SSTI(模板注入)
查看>>