1. 程式人生 > >openGL 頂點,座標系,紋理座標

openGL 頂點,座標系,紋理座標

瞭解座標系是繪製圖形的基礎。在使用openGL的場景中,有世界座標,區域性座標,紋理座標,和螢幕座標幾種。

openGL 座標系:


分3個軸,x,y,z 中心點為o, 箭頭方向為正方向,最大與最小值為1和-1,這是經過歸一化處理的。這樣設計是為了顯示卡計算方便。

螢幕座標系,就是應用在裝置螢幕上的座標系。也就是圖形最終繪製的地方。


左上角為原點,箭頭為正方向,大小又螢幕畫素大小決定。openGL的螢幕座標系,Y軸向上為正。相當於上面那個三維座標系擷取一個二維的XY。

紋理座標系:


也做了歸一化處理。這個座標就代表了一個紋理。openGL是基於定點的網格繪製。就是說,openGL的圖形都是由很多頂點,按照一定的規則連結起來構成的圖形。那麼紋理座標的4個座標點,對映到頂點上。openGL就會把這個紋理應用到4個定點構成的圖形上。

一般我們會把openGL的座標系的中心點,與螢幕的中心點重合。如下:


矩形為螢幕的區域。openGL的繪製方法會把這個座標系的頂點,組合成圖形呈現在螢幕上。當然這個還涉及到攝像機的位置,螢幕剪裁等設定。這裡不考慮。

那麼什麼是頂點呢,就是一個有xyz座標的點。如(0,0,0)或(1,1,1)。XY就和通常的二維座標一樣定位平面的位置。Z軸表示的是深度,openGL就是為了繪製3D圖形而誕生的。頂點座標是做了歸一化處理的float型別。那麼這裡就會涉及到螢幕座標系到openGL座標系的轉化工作。

如圖:


螢幕座標系,左上點為(0,0) 那麼螢幕中心點座標,就是(sreenWidth / 2, screenHeight / 2)。而對應openGL座標系的歸一化座標就是(0,0,0)。所以這裡需要把螢幕座標轉換成openGL的歸一化座標。

  1. /** 
  2.    * Convert x to openGL 
  3.    *  
  4.    * @param x 
  5.    *            Screen x offset top left 
  6.    * @return Screen x offset top left in OpenGL 
  7.    */
  8.   publicstaticfloat toGLX(float x) {  
  9.       return -1.0f * ratio + toGLWidth(x);  
  10.   }  
  11.   /** 
  12.    * Convert y to openGL y 
  13.    *  
  14.    * @param y 
  15.    *            Screen y offset top left
     
  16.    * @return Screen y offset top left in OpenGL 
  17.    */
  18.   publicstaticfloat toGLY(float y) {  
  19.       return1.0f - toGLHeight(y);  
  20.   }  
  21.   /** 
  22.    * Convert width to openGL width 
  23.    *  
  24.    * @param width 
  25.    * @return Width in openGL 
  26.    */
  27.   publicstaticfloat toGLWidth(float width) {  
  28.       return2.0f * (width / screenWidth) * ratio;  
  29.   }  
  30.   /** 
  31.    * Convert height to openGL height 
  32.    *  
  33.    * @param height 
  34.    * @return Height in openGL 
  35.    */
  36.   publicstaticfloat toGLHeight(float height) {  
  37.       return2.0f * (height / screenHeight);  
  38.   }  
  39.   /** 
  40.    * Convert x to screen x 
  41.    *  
  42.    * @param glX 
  43.    *            openGL x 
  44.    * @return screen x 
  45.    */
  46.   publicstaticfloat toScreenX(float glX) {  
  47.       return toScreenWidth(glX - (-1 * ratio));  
  48.   }  
  49.   /** 
  50.    * Convert y to screent y 
  51.    *  
  52.    * @param glY 
  53.    *            openGL y 
  54.    * @return screen y 
  55.    */
  56.   publicstaticfloat toScreenY(float glY) {  
  57.       return toScreenHeight(1.0f - glY);  
  58.   }  
  59.   /** 
  60.    * Convert glWidth to screen width 
  61.    *  
  62.    * @param glWidth 
  63.    * @return Width in screen 
  64.    */
  65.   publicstaticfloat toScreenWidth(float glWidth) {  
  66.       return (glWidth * screenWidth) / (2.0f * ratio);  
  67.   }  
  68.   /** 
  69.    * Convert height to screen height 
  70.    *  
  71.    * @param glHeight 
  72.    * @return Height in screen 
  73.    */
  74.   publicstaticfloat toScreenHeight(float glHeight) {  
  75.       return (glHeight * screenHeight) / 2.0f;  
  76.   }