• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            GLUT Trackball Demo

            Posted on 2017-06-07 23:01 eryar 閱讀(2162) 評(píng)論(2)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

            GLUT Trackball Demo

            eryar@163.com

            1.Introduction

            在三維場(chǎng)景中建立模型后,為了方便用戶從各個(gè)角度觀察模型,從而需要對(duì)三維視圖進(jìn)行控制。常見的視圖交互控制方式有:Trackball控制器、飛行控制器,還有三維游戲常用的第一人稱控制器。這些視圖控制器的根本是對(duì)模型視圖矩陣MODELVIEW進(jìn)行變換。

            Trackball控制器以一種用戶友好的交互方式來(lái)變換視圖,原理是由Trackball激發(fā),Trackball如下圖所示:

            wpsD30B.tmp

            Figure 1. Trackball

            通過(guò)手指在球面上滾動(dòng),就可以對(duì)三維視圖進(jìn)行控制。現(xiàn)在需要用鼠標(biāo)的拖動(dòng)來(lái)模擬Trackball以實(shí)現(xiàn)對(duì)三維視圖的控制。在OpenGL中實(shí)現(xiàn)Trackball控制視圖分為以下幾步:

            1.將鼠標(biāo)移動(dòng)時(shí)的屏幕坐標(biāo)點(diǎn)映射到單位球上;
            wpsD32B.tmp

            2.將開始旋轉(zhuǎn)視圖時(shí)鼠標(biāo)點(diǎn)到球心的向量與鼠標(biāo)移動(dòng)過(guò)程中的坐標(biāo)點(diǎn)球心的向量叉乘,即可得到旋轉(zhuǎn)軸;

            wpsD32C.tmp
            根據(jù)叉乘的定義,可以得到旋轉(zhuǎn)角度:
            wpsD32D.tmp

            有了旋轉(zhuǎn)軸和旋轉(zhuǎn)角度,就可以對(duì)視圖進(jìn)行旋轉(zhuǎn)操作了。

            2.GLUT Test

            為了簡(jiǎn)明地說(shuō)明Trackball的原理,這里只使用了GLUT庫(kù)和OpenCASCADE中的四元數(shù)和向量相關(guān)的類。如果其他開源庫(kù)也有向量計(jì)算和四元數(shù)據(jù)計(jì)算類,也可以將代碼很快移植到使用其他庫(kù),如矩陣計(jì)算庫(kù)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;
            }

            上述程序運(yùn)行結(jié)果如下動(dòng)圖所示:

            trackball1

            從上圖可知,當(dāng)旋轉(zhuǎn)幾次后視圖并沒(méi)有得到預(yù)期的結(jié)果。因?yàn)槌绦驅(qū)⑹髽?biāo)映射后坐標(biāo)與球心得到的向量進(jìn)行了顯示,發(fā)現(xiàn)當(dāng)旋轉(zhuǎn)幾次后,這個(gè)向量并沒(méi)有跟隨鼠標(biāo)。

            3.Transform

            通過(guò)觀察上面代碼程序運(yùn)行的結(jié)果,可以發(fā)現(xiàn)鼠標(biāo)映射函數(shù)得到的映射點(diǎn)始終是位于XOY平面上的一個(gè)半球面上。當(dāng)視圖被旋轉(zhuǎn)后,視圖的坐標(biāo)系已經(jīng)發(fā)生了變化,而映射點(diǎn)并沒(méi)有。為了跟蹤這個(gè)變換用四元數(shù)進(jìn)行累乘來(lái)記錄這一系列的旋轉(zhuǎn)變換。最后在映射函數(shù)中將映射點(diǎn)變換到已經(jīng)改變的視圖坐標(biāo)系中。

            即在鼠標(biāo)移動(dòng)處理函數(shù)中增加記錄變換:

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

            在mapToSphere函數(shù)中增加:

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

            列出升級(jí)后的全部代碼如下所示:

            /*
            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;
            }

            這次程序運(yùn)行和預(yù)期結(jié)果一致,旋轉(zhuǎn)很流暢:

            trackball2

            4.Conclusion

            程序員總是有很強(qiáng)的控制欲,希望一切盡在掌握之中。在三維場(chǎng)景中建立模型后,如何對(duì)視圖進(jìn)行控制來(lái)方便地觀察模型呢?最常見的控制方式就是Trackball. OpenSceneGraph、Eigen等開源庫(kù)都有相關(guān)的實(shí)現(xiàn)。

            Trackball的實(shí)現(xiàn)主要是將鼠標(biāo)點(diǎn)映射到一個(gè)球面上,然后使用叉乘得到旋轉(zhuǎn)軸和旋轉(zhuǎn)角度。為了旋轉(zhuǎn)的流暢,使用四元數(shù)記錄了一系列的旋轉(zhuǎn)變換,最后通過(guò)將映射點(diǎn)進(jìn)行坐標(biāo)變換得到滿意的效果。

            5.References

            1. Virtual Trackball. http://gukewen.sdu.edu.cn/panrj/courses/4-AngelCGE2-Virtual-Trackball.pdf

            2. Object Mouse Trackball https://www.khronos.org/opengl/wiki/Object_Mouse_Trackball

            Feedback

            # re: GLUT Trackball Demo  回復(fù)  更多評(píng)論   

            2017-06-16 17:23 by ccsdu2009
            用glfw

            # re: GLUT Trackball Demo  回復(fù)  更多評(píng)論   

            2017-06-16 19:37 by eryar
            @ccsdu2009
            這個(gè)是學(xué)習(xí)用的。

            好用的庫(kù)有很多,像OpenSceneGraph, OpenCASCADE.

            哈哈~~
            亚洲国产精品久久| 久久久久亚洲av综合波多野结衣 | 国产高清国内精品福利99久久| 久久精品中文字幕无码绿巨人 | 女同久久| 人妻精品久久久久中文字幕一冢本 | 久久九九全国免费| 三级韩国一区久久二区综合| 亚洲人成网亚洲欧洲无码久久| 久久亚洲日韩精品一区二区三区 | 国产精品无码久久综合| 国产精品久久久久久久午夜片| 久久久久久国产a免费观看不卡| 久久亚洲sm情趣捆绑调教| 久久99国产综合精品| 久久青青草原亚洲av无码| 无码国产69精品久久久久网站| 久久免费小视频| 亚洲国产欧洲综合997久久| 国产精品美女久久久网AV| 精品国产青草久久久久福利| 青青草原综合久久大伊人精品| 亚洲午夜无码AV毛片久久| 日本一区精品久久久久影院| 久久久久久久久久久| 欧美一级久久久久久久大片| 国产成人综合久久综合| 中文字幕久久久久人妻| 久久久久久av无码免费看大片| 久久久精品人妻一区二区三区四| 国产精品久久新婚兰兰| 欧美午夜A∨大片久久| 久久99精品久久久久久齐齐| 国内精品久久九九国产精品| 久久精品人人做人人爽97| 亚洲色欲久久久综合网东京热| 中文字幕久久亚洲一区| 中文字幕无码久久久| 久久频这里精品99香蕉久| 国产精品亚洲综合专区片高清久久久 | 亚洲AV伊人久久青青草原|