opengl坐标系

来源:互联网 发布:越狱录屏软件 编辑:程序博客网 时间:2024/06/05 01:13

了解坐标系是绘制图形的基础。在使用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的归一化坐标。


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