偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Qt 3D OpenGL實(shí)現(xiàn)場(chǎng)景漫游實(shí)例

移動(dòng)開(kāi)發(fā)
本文介紹的是Qt3D OpenGL實(shí)現(xiàn)場(chǎng)景漫游實(shí)例,現(xiàn)在3D越來(lái)越流行了,要好還的研究。先來(lái)看內(nèi)容。

Qt 3D OpenGL實(shí)現(xiàn)場(chǎng)景漫游實(shí)例是本文要介紹的內(nèi)容,一個(gè)不錯(cuò)的OpenGL程序當(dāng)然免不了對(duì)整個(gè)場(chǎng)景漫游。在我程序中便是用W、A、S、D來(lái)靠近,遠(yuǎn)離,向左,向右來(lái)移動(dòng)場(chǎng)景,Q、E、Z、C則是旋轉(zhuǎn)場(chǎng)景。同時(shí),補(bǔ)充一條,我用鼠標(biāo)滾輪實(shí)現(xiàn)了物體的放大和縮小,效果上和按W、S鍵是相同的,但本質(zhì)上是有差別的,呵呵~

我要貼出的這個(gè)Camera類是從《OpenGL游戲編程》里提取出來(lái)的,并且已經(jīng)在Qt環(huán)境下運(yùn)行成功(本來(lái)代碼是在VS2005)。

Camera.h內(nèi)容:

  1. #ifndef __CAMERA_H__  
  2. #define __CAMERA_H__  
  3.  
  4. #include "Vector.h" /* 包含向量類頭文件 */  
  5.  
  6. /* 攝像機(jī)類 */  
  7. class Camera  
  8. {  
  9. public:  
  10.  
  11.         /* 構(gòu)造函數(shù)和析構(gòu)函數(shù) */  
  12.  Camera();  
  13.  ~Camera();  
  14.  
  15.         /* 獲得攝像機(jī)狀態(tài)方法 */  
  16.  Vector3 getPosition()   { return m_Position;  }  
  17.  Vector3 getView()     { return m_View;   }  
  18.  Vector3 getUpVector()   { return m_UpVector;  }  
  19.  float   getSpeed()      {   return m_Speed;         }  
  20.  
  21.         /* 設(shè)置速度 */  
  22.  void setSpeed(float speed)  
  23.  {  
  24.   m_Speed  = speed;  
  25.  }  
  26.  
  27.         /* 設(shè)置攝像機(jī)的位置, 觀察點(diǎn)和向上向量 */  
  28.  void setCamera(float positionX, float positionY, float positionZ,  
  29.         float viewX,     float viewY,     float viewZ,  
  30.        float upVectorX, float upVectorY, float upVectorZ);  
  31.         /* 旋轉(zhuǎn)攝像機(jī)方向 */  
  32.  void rotateView(float angle, float X, float Y, float Z);  
  33.         /* 根據(jù)鼠標(biāo)設(shè)置攝像機(jī)觀察方向 */  
  34.         void setViewByMouse();  
  35.         /* 左右攝像機(jī)移動(dòng) */  
  36.  void yawCamera(float speed);  
  37.         /* 前后移動(dòng)攝像機(jī) */  
  38.  void moveCamera(float speed);  
  39.         /* 放置攝像機(jī) */  
  40.  void setLook();  
  41.         /* 得到攝像機(jī)指針 */  
  42.  static Camera* GetCamera(void) { return m_pCamera;}  
  43. private:  
  44.         /* 攝像機(jī)屬性 */  
  45.         static Camera  *m_pCamera;      /* 當(dāng)前全局?jǐn)z像機(jī)指針 */  
  46.         Vector3        m_Position;      /* 位置 */  
  47.         Vector3        m_View;          /* 朝向 */  
  48.         Vector3        m_UpVector;      /* 向上向量 */  
  49.         float          m_Speed;         /* 速度 */  
  50. };  
  51. #endif //__CAMERA_H__ 

Camera.cpp內(nèi)容:

  1. #include "Stdafx.h"  
  2. #include "Camera.h"                    /* 包含攝像機(jī)頭文件 */  
  3. #include "Vector.h"                    /* 包含向量類 */  
  4. #include "math.h"  
  5.  
  6. Camera* Camera::m_pCamera = NULL;  
  7.  
  8. /* 構(gòu)造函數(shù) */  
  9. Camera::Camera()  
  10. {  
  11.     /* 初始化向量值 */  
  12.     Vector3 zero = Vector3(0.0, 0.0, 0.0);  
  13.     Vector3 view = Vector3(0.0, 1.0, 0.5);  
  14.     Vector3 up   = Vector3(0.0, 0.0, 1.0);  
  15.  
  16.     /* 初始化攝像機(jī) */  
  17.  
  18.     //觀察位置 Eye  
  19.     m_Position = zero;  
  20.  
  21.     //被觀察點(diǎn)  
  22.     m_View  = view;  
  23.  
  24.     //倒立還是正立  
  25.     m_UpVector = up;  
  26.  
  27.     //前進(jìn)速度  
  28.     m_Speed     = 0.05f;  
  29.  
  30.     //相機(jī)指針  
  31.     m_pCamera = this;  
  32.  
  33. }  
  34.  
  35. Camera::~Camera()  
  36. {  
  37. }  
  38.  
  39. /* 設(shè)置攝像機(jī)的位置,朝向和向上向量 */  
  40. void Camera::setCamera( float positionX, float positionY, float positionZ,  
  41.                         float viewX,     float viewY,     float viewZ,  
  42.                         float upVectorX, float upVectorY, float upVectorZ)  
  43. {  
  44.     /* 構(gòu)造向量 */  
  45.     Vector3 Position = Vector3(positionX, positionY, positionZ);  
  46.     Vector3 View  = Vector3(viewX, viewY, viewZ);  
  47.     Vector3 UpVector = Vector3(upVectorX, upVectorY, upVectorZ);  
  48.  
  49.     /* 設(shè)置攝像機(jī) */  
  50.     m_Position = Position;  
  51.     m_View     = View;  
  52.     m_UpVector = UpVector;  
  53. }  
  54.  
  55. /*  旋轉(zhuǎn)攝像機(jī)方向  */  
  56. void Camera::rotateView(float angle, float x, float y, float z)  
  57. {  
  58.     Vector3 newView;  
  59.  
  60.     /* 計(jì)算方向向量 */  
  61.     Vector3 view = m_View - m_Position;  
  62.  
  63.     /* 計(jì)算 sin 和cos值 */  
  64.     float cosTheta = (float)cos(angle);  
  65.     float sinTheta = (float)sin(angle);  
  66.  
  67.     /* 計(jì)算旋轉(zhuǎn)向量的x值 */  
  68.     newView.x  = (cosTheta + (1 - cosTheta) * x * x)  * view.x;  
  69.     newView.x += ((1 - cosTheta) * x * y - z * sinTheta) * view.y;  
  70.     newView.x += ((1 - cosTheta) * x * z + y * sinTheta) * view.z;  
  71.  
  72.     /* 計(jì)算旋轉(zhuǎn)向量的y值 */  
  73.     newView.y  = ((1 - cosTheta) * x * y + z * sinTheta) * view.x;  
  74.     newView.y += (cosTheta + (1 - cosTheta) * y * y)  * view.y;  
  75.     newView.y += ((1 - cosTheta) * y * z - x * sinTheta) * view.z;  
  76.  
  77.     /* 計(jì)算旋轉(zhuǎn)向量的z值 */  
  78.     newView.z  = ((1 - cosTheta) * x * z - y * sinTheta) * view.x;  
  79.     newView.z += ((1 - cosTheta) * y * z + x * sinTheta) * view.y;  
  80.     newView.z += (cosTheta + (1 - cosTheta) * z * z)  * view.z;  
  81.  
  82.     /* 更新攝像機(jī)的方向 */  
  83.     m_View = m_Position + newView;  
  84. }  
  85.  
  86. /* 用鼠標(biāo)旋轉(zhuǎn)攝像機(jī) */  
  87. void Camera::setViewByMouse()  
  88. {/*此函數(shù)已放棄。如要使用,在Update處調(diào)用即可*/  
  89.  
  90.     /*< 保存當(dāng)前鼠標(biāo)位置 */     POINT mousePos;   
  91.     int middleX = GetSystemMetrics(SM_CXSCREEN) >> 1; /*< 得到屏幕寬度的一半 */   
  92.     int middleY = GetSystemMetrics(SM_CYSCREEN) >> 1; /*< 得到屏幕高度的一半 */  
  93.  
  94.     float angleY = 0.0f;/*< 攝像機(jī)左右旋轉(zhuǎn)角度 */  
  95.     float angleZ = 0.0f;/*< 攝像機(jī)上下旋轉(zhuǎn)角度 */   
  96.     static float currentRotX = 0.0f;     /* 得到當(dāng)前鼠標(biāo)位置 */   
  97.     GetCursorPos(&mousePos);     ShowCursor(TRUE);     /* 如果鼠標(biāo)沒(méi)有移動(dòng),則不用更新 */  
  98.      if( (mousePos.x == middleX) && (mousePos.y == middleY) )  
  99.          return;     /* 設(shè)置鼠標(biāo)位置在屏幕中心 */   
  100.     SetCursorPos(middleX, middleY);     /* 得到鼠標(biāo)移動(dòng)方向 */  
  101.      angleY = (float)( (middleX - mousePos.x) ) / 1000.0f;  
  102.      angleZ = (float)( (middleY - mousePos.y) ) / 1000.0f;  
  103.      static float lastRotX = 0.0f;      /* 用于保存旋轉(zhuǎn)角度 */   
  104.     lastRotX = currentRotX;     /* 跟蹤攝像機(jī)上下旋轉(zhuǎn)角度 */  
  105.      currentRotX += angleZ;     /* 如果上下旋轉(zhuǎn)弧度大于1.0,我們截取到1.0并旋轉(zhuǎn) */   
  106.     if(currentRotX > 1.0f)  
  107.     {  
  108.         currentRotX = 1.0f;  
  109.         /* 根據(jù)保存的角度旋轉(zhuǎn)方向 */  
  110.         if(lastRotX != 1.0f)  
  111.         {  
  112.             /* 通過(guò)叉積找到與旋轉(zhuǎn)方向垂直的向量 */  
  113.             Vector3 vAxis = m_View - m_Position;  
  114.             vAxisvAxis = vAxis.crossProduct(m_UpVector);  
  115.             vAxisvAxis = vAxis.normalize();  
  116.             ///旋轉(zhuǎn)  
  117.             rotateView( 1.0f - lastRotX, vAxis.x, vAxis.y, vAxis.z);  
  118.         }  
  119.     }  
  120.     /* 如果旋轉(zhuǎn)弧度小于-1.0,則也截取到-1.0并旋轉(zhuǎn) */  
  121.     else if(currentRotX < -1.0f)  
  122.     {  
  123.         currentRotX = -1.0f;  
  124.  
  125.         if(lastRotX != -1.0f)  
  126.         {  
  127.             /* 通過(guò)叉積找到與旋轉(zhuǎn)方向垂直的向量 */  
  128.             Vector3 vAxis = m_View - m_Position;  
  129.             vAxisvAxis = vAxis.crossProduct(m_UpVector);  
  130.             vAxisvAxis = vAxis.normalize();  
  131.  
  132.             ///旋轉(zhuǎn)  
  133.             rotateView( -1.0f - lastRotX, vAxis.x, vAxis.y, vAxis.z);  
  134.         }  
  135.     }  
  136.     /* 否則就旋轉(zhuǎn)angleZ度 */  
  137.     else  
  138.     {  
  139.         /* 找到與旋轉(zhuǎn)方向垂直向量 */  
  140.         Vector3 vAxis = m_View - m_Position;  
  141.         vAxisvAxis = vAxis.crossProduct(m_UpVector);  
  142.         vAxisvAxis = vAxis.normalize();  
  143.         ///旋轉(zhuǎn)  
  144.         rotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);  
  145.     }  
  146.     /* 總是左右旋轉(zhuǎn)攝像機(jī) */  
  147.     rotateView(angleY, 0, 1, 0);  
  148. }  
  149. /* 左右移動(dòng)攝像機(jī) */  
  150. void Camera::yawCamera(float speed)  
  151. {  
  152.     Vector3 yaw;  
  153.     Vector3 cross = m_View - m_Position;  
  154.     crosscross = cross.crossProduct(m_UpVector);  
  155.     ///歸一化向量  
  156.     yaw = cross.normalize();  
  157.     m_Position.x += yaw.x * speed;  
  158.     m_Position.z += yaw.z * speed;  
  159.     m_View.x += yaw.x * speed;  
  160.     m_View.z += yaw.z * speed;  
  161. }  
  162. /* 前后移動(dòng)攝像機(jī) */  
  163. void Camera::moveCamera(float speed)  
  164. {  
  165.     /* 計(jì)算方向向量 */  
  166.     Vector3 vector = m_View - m_Position;  
  167.     vectorvector = vector.normalize();         /*< 單位化 */  
  168.     /* 更新攝像機(jī) */  
  169.     m_Position.x += vector.x * speed;    /*< 根據(jù)速度更新位置 */  
  170.     m_Position.y += vector.y * speed;  
  171.     m_Position.z += vector.z * speed;  
  172.     m_View.x += vector.x * speed;   /*< 根據(jù)速度更新方向 */  
  173.     m_View.y += vector.y * speed;  
  174.     m_View.z += vector.z * speed;  
  175. }  
  176. /* 設(shè)置視點(diǎn) */  
  177. void Camera::setLook()  
  178. {  
  179.     /* 設(shè)置視口 */  
  180.     gluLookAt(m_Position.x, m_Position.y, m_Position.z,  
  181.               m_View.x,  m_View.y,     m_View.z,  
  182.               m_UpVector.x, m_UpVector.y, m_UpVector.z);  

使用方法:

  1. /* 設(shè)置全局相機(jī) */  
  2. m_Camera.setLook();  
  3.  
  4. /* 初始化相機(jī) */  
  5. m_Camera.setCamera(0.0f, 0.0f, -3.0f,   //Eye  
  6.                    0.0f, 0.0f, -7.0f,   //Center  
  7.                    0.0f, 1.0f, 0.0f);  //Up  
  8.  
  9. case Qt::Key_W://鏡頭靠近  
  10.     m_Camera.moveCamera(m_Camera.getSpeed());  
  11.     break;  
  12. case Qt::Key_S://鏡頭遠(yuǎn)離  
  13.     m_Camera.moveCamera(-m_Camera.getSpeed()); 

上面記得要先初始化相機(jī),然后給其“擺放”好,然后利用鍵盤(pán)事件改變其視點(diǎn)就好了

小結(jié):Qt 3D OpenGL實(shí)現(xiàn)場(chǎng)景漫游實(shí)例的內(nèi)容介紹完了,希望本文對(duì)你有幫助!

責(zé)任編輯:zhaolei 來(lái)源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2011-06-20 14:40:19

Qt 3D

2016-04-07 09:46:50

H5VR3d

2025-03-31 08:27:00

3D模型AI

2012-06-16 16:57:52

WebGL

2012-02-27 10:00:50

HTML 5

2015-04-27 15:35:42

Cocos3D場(chǎng)景編輯器

2013-07-25 09:32:26

OpenGL ESAndroid4.3

2009-03-19 20:41:31

VirtualBox GuestsOpenGL 3D

2025-01-07 13:19:48

模型AI訓(xùn)練

2010-02-14 15:27:25

2011-05-26 10:55:39

2021-09-16 07:52:18

SwiftUScroll效果

2022-09-19 19:16:42

輪播圖has

2024-07-31 15:30:05

2024-12-10 15:17:11

2011-10-06 13:30:45

宏碁投影儀

2012-11-26 12:51:44

木材3D打

2021-03-08 15:40:46

開(kāi)源技術(shù) 軟件

2023-05-26 07:08:05

CSS模糊實(shí)現(xiàn)文字

2010-06-09 10:50:08

OpenSUSE 3D
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)