歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenGL超級寶典學習筆記——Using OpenGL(二)

OpenGL超級寶典學習筆記——Using OpenGL(二)

日期:2017/3/1 9:32:30   编辑:Linux編程

OpenGL數據類型

Technorati 標記: opengl

函數命名約定

<函數庫前綴><命令名稱><可選的參數個數><可選的參數類型>

例子:

glColor3f 代表是gl函數庫裡的 color命令,此命令有3個參數,參數類型是f

平台獨立性

openGL沒有創建窗口,處理按鍵輸入,鼠標點擊事件等函數。創建和管理窗口是操作系統本身應支持的功能。OpenGL是平台獨立的圖形硬件的抽象接口庫。

使用OpenGL畫圖

  1: #include 
  
   
  
    
   
  2: 
  3: #include 
  
   
  
    
   
  4: 
  5: static void RenderScenceCB()
  6: {
  7:     glClear( GL_COLOR_BUFFER_BIT );
  8: 
  9:     glColor3f( 0.0f, 1.0f, 0.0f );
 10: 
 11:     glRectf( -25.0f, 25.0f, 25.0f, -25.0f );
 12: 
 13:     glFlush();
 14: }
 15: 
 16: 
 17: static void CreateWindow()
 18: {
 19:     glutInitWindowPosition( 200, 200 );
 20: 
 21:     glutInitWindowSize( 200, 200 );
 22: 
 23:     glutCreateWindow( "opengl step 1" );
 24: }
 25: 
 26: 
 27: static void ChangeSize( GLsizei w, GLsizei h )
 28: 
 29: {
 30: /* 設置視口 */
 31: 
 32:     glViewport( 0, 0, w, h );
 33: 
 34:     glMatrixMode( GL_PROJECTION );
 35: 
 36:     glLoadIdentity();
 37: 
 38:     if ( h == 0 )
 39: 
 40:     {
 41:         h = 1;
 42:     }
 43: 
 44:     GLfloat aspectRatio = (GLfloat) w / (GLfloat) h;
 45: 
 46:     if ( w <= h )
 47: 
 48:     {
 49:         glOrtho( -100.0f, 100.0f, -100.0f / aspectRatio, 100.0f / aspectRatio, 1.0, -1.0 );
 50:     }else  {
 51:         glOrtho( -100.0f * aspectRatio, 100.0f * aspectRatio, -100.0f, 100.0f, 1.0, -1.0 );
 52:     }
 53: 
 54:     glMatrixMode( GL_MODELVIEW );
 55: 
 56:     glLoadIdentity();
 57: }
 58: 
 59: 
 60: int main( int args, char *argc[] )
 61: {
 62:     glutInit( &args, argc );
 63: 
 64:     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
 65: 
 66:     CreateWindow();
 67: 
 68:     glutDisplayFunc( RenderScenceCB );
 69: 
 70:     glutReshapeFunc( ChangeSize );
 71: 
 72:     glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
 73: 
 74:     glutMainLoop();
 75: 
 76:     return(0);
 77: }
 78: 
 79: 
 80: 
<br /></b></p>

畫一個矩形

設置當前使用的顏色

glColor3f(0.0f, 1.0f, 0.0f);

畫一個矩形,並用當前顏色填充

glRectf(-25.0f, 25.0f, 25.0f, -25.0f);

這兩個函數的使用方式參考,官方函數文檔

http://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml

http://www.opengl.org/sdk/docs/man2/xhtml/glRect.xml

openGL是如何把這些坐標映射到真是的窗體的位置的。這個就是使用了ChangeSize這個回調函數。

通過glutReshapeFunc設置這個回調函數,當窗體的大小發生改變時,就會調用你設置的回調函數。

每次窗體大小的改變,我們都應該重新定義視口和裁剪區域以適應新的窗口尺寸。我們希望繪制的圖像能夠適應窗體的大小。

設置視口(ViewPort)和裁剪區域(Clipping volume)

視口和裁剪區域是如何影響坐標系范圍,以及2D和3D的圖像在屏幕上顯示的。如下圖:

定義視口

void glViewPort(GLint x, GLint y, GLint width, GLint height);

參數x,y制定了左下角的坐標。width和height參數制定了尺寸以像素為單位。

定義裁剪區域

void glOrtho(
GLdouble
left,

GLdouble
right,

GLdouble
bottom,

GLdouble
top,

GLdouble
nearVal,

GLdouble
farVal);

Parameters

left, right

Specify the coordinates for the left and right vertical clipping planes.

bottom, top

Specify the coordinates for the bottom and top horizontal clipping planes.

nearVal, farVal

Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.

這個函數重新定義了裁剪區域,使得縱橫比不變。縱橫比就是在垂直方向上的一個單位長度內的像素個數與水平方向上一個單位長度內的像素個數的比值。上面的例子使用的是正投影。

在調用glOrtho之前,要注意調用下面兩個函數

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

投影矩陣是定義可視區域的地方。glOrtho並不建立新的裁剪區域,而是對之前的裁剪區域進行修改。它把當前裁剪區域的矩陣與參數所提供的矩陣相乘,得到新的裁剪區域。glLoadIdentity();就是對當前的坐標系系統進行重置。

下面的代碼可以使得正方形保持正方形

if (w <= h)

{

glOrtho(-100.0f, 100.0f, -100.0f/aspectRatio, 100.0f/aspectRatio, 1.0, -1.0);

}

else

{

glOrtho(-100.0f*aspectRatio, 100.0f*aspectRatio, -100.0f, 100.0f, 1.0, -1.0);

}

這個函數調用使得裁剪區域 左邊總是位於-100,右邊則擴展到100除非窗口的寬度大於高度,此時水平范圍則會根據縱橫比進行縮放。同理當高度大於寬度時,上下要進行縮放。這樣就可以保持一個200X200的正方形(中心為(0,0))與窗口的形狀無關。如下圖:

OpenGL超級寶典 第4版 中文版PDF+英文版+源代碼 見 http://www.linuxidc.com/Linux/2013-10/91413.htm

OpenGL編程指南(原書第7版)中文掃描版PDF 下載 http://www.linuxidc.com/Linux/2012-08/67925.htm

OpenGL 渲染篇 http://www.linuxidc.com/Linux/2011-10/45756.htm

Ubuntu 13.04 安裝 OpenGL http://www.linuxidc.com/Linux/2013-05/84815.htm

OpenGL三維球體數據生成與繪制【附源碼】 http://www.linuxidc.com/Linux/2013-04/83235.htm

Ubuntu下OpenGL編程基礎解析 http://www.linuxidc.com/Linux/2013-03/81675.htm

如何在Ubuntu使用eclipse for c++配置OpenGL http://www.linuxidc.com/Linux/2012-11/74191.htm

更多《OpenGL超級寶典學習筆記》相關知識 見 http://www.linuxidc.com/search.aspx?where=nkey&keyword=34581

Copyright © Linux教程網 All Rights Reserved