<p><b>OpenGL數據類型 <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c3484e03-3c82-47ba-9eed-e7b3e1fe8bc1" class="wlWriterEditableSmartContent">Technorati 標記: <a href="http://technorati.com/tags/opengl" rel="tag">opengl</a></div> </b></p> <p><a href="http://static.oschina.net/uploads/img/201309/04220303_EYrW.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201309/04220304_lE2M.png" width="1032" height="381" /></a></p> <p><b>函數命名約定</b></p> <p><b> <br /></b></p> <p><函數庫前綴><命令名稱><可選的參數個數><可選的參數類型></p> <p>例子:</p> <p>glColor3f 表明是gl函數庫裏的 color命令,此命令有3個參數,參數類型是f</p> <p><b>平臺獨立性</b></p> <p>openGL沒有建立窗口,處理按鍵輸入,鼠標點擊事件等函數。建立和管理窗口是操做系統自己應支持的功能。OpenGL是平臺獨立的圖形硬件的抽象接口庫。</p> <p><b>使用OpenGL畫圖</b></p> <p><b> <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: #include <gl/glut.h> </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: #include <gl/GL.h> </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> RenderScenceCB() </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: glClear( GL_COLOR_BUFFER_BIT ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: glColor3f( 0.0f, 1.0f, 0.0f ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: glRectf( -25.0f, 25.0f, 25.0f, -25.0f ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: glFlush(); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CreateWindow() </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: glutInitWindowPosition( 200, 200 ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: glutInitWindowSize( 200, 200 ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: glutCreateWindow( "<span style="color: #8b0000">opengl step 1</span>" ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> ChangeSize( GLsizei w, GLsizei h ) </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30: <span style="color: #008000">/* 設置視口 */</span> </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: glViewport( 0, 0, w, h ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: glMatrixMode( GL_PROJECTION ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: glLoadIdentity(); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38: <span style="color: #0000ff">if</span> ( h == 0 ) </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41: h = 1; </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44: GLfloat aspectRatio = (GLfloat) w / (GLfloat) h; </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46: <span style="color: #0000ff">if</span> ( w <= h ) </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49: glOrtho( -100.0f, 100.0f, -100.0f / aspectRatio, 100.0f / aspectRatio, 1.0, -1.0 ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50: }<span style="color: #0000ff">else</span> { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51: glOrtho( -100.0f * aspectRatio, 100.0f * aspectRatio, -100.0f, 100.0f, 1.0, -1.0 ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54: glMatrixMode( GL_MODELVIEW ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56: glLoadIdentity(); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 57: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 58: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 59: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 60: <span style="color: #0000ff">int</span> main( <span style="color: #0000ff">int</span> args, <span style="color: #0000ff">char</span> *argc[] ) </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 61: { </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 62: glutInit( &args, argc ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 63: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 64: glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 65: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 66: CreateWindow(); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 67: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 68: glutDisplayFunc( RenderScenceCB ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 69: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 70: glutReshapeFunc( ChangeSize ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 71: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 72: glClearColor( 1.0f, 0.0f, 0.0f, 0.0f ); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 73: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 74: glutMainLoop(); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 75: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 76: <span style="color: #0000ff">return</span>(0); </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 77: } </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 78: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 79: </pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 80: </pre></pre>html
<br /></b></p>
<p><a href="http://static.oschina.net/uploads/img/201309/04220305_ngK9.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image(1)" border="0" alt="Image(1)" src="http://static.oschina.net/uploads/img/201309/04220305_m762.png" width="232" height="244" /></a></p>函數
<p><b>畫一個矩形</b></p>oop
<p>設置當前使用的顏色</p>spa
<p>glColor3f(0.0f, 1.0f, 0.0f);</p>操作系統
<p>畫一個矩形,並用當前顏色填充</p>.net
<p>glRectf(-25.0f, 25.0f, 25.0f, -25.0f);</p>code
<p>這兩個函數的使用方式參考,官方函數文檔</p>xml
<p><a href="http://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml">http://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml</a></p>htm
<p><a href="http://www.opengl.org/sdk/docs/man2/xhtml/glRect.xml">http://www.opengl.org/sdk/docs/man2/xhtml/glRect.xml</a></p>接口
<p>openGL是如何把這些座標映射到真是的窗體的位置的。這個就是使用了ChangeSize這個回調函數。</p>
<p>經過glutReshapeFunc設置這個回調函數,當窗體的大小發生改變時,就會調用你設置的回調函數。</p>
<p>每次窗體大小的改變,咱們都應該從新定義視口和裁剪區域以適應新的窗口尺寸。咱們但願繪製的圖像可以適應窗體的大小。</p>
<p><b>設置視口(ViewPort)和裁剪區域(Clipping volume)</b></p>
<p>視口和裁剪區域是如何影響座標系範圍,以及2D和3D的圖像在屏幕上顯示的。以下圖:</p>
<p><a href="http://static.oschina.net/uploads/img/201309/04220305_Oym4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image(2)" border="0" alt="Image(2)" src="http://static.oschina.net/uploads/img/201309/04220306_IOHL.png" width="740" height="369" /></a></p>
<p><a href="http://static.oschina.net/uploads/img/201309/04220309_YEYn.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image(3)" border="0" alt="Image(3)" src="http://static.oschina.net/uploads/img/201309/04220309_WwSp.png" width="745" height="347" /></a></p>
<p><b>定義視口</b></p>
<p><b> <br /></b></p>
<p><b>void glViewPort(GLint x, GLint y, GLint width, GLint height);</b></p>
<p>參數x,y制定了左下角的座標。width和height參數制定了尺寸以像素爲單位。</p>
<p><a href="http://static.oschina.net/uploads/img/201309/04220309_93MX.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image(4)" border="0" alt="Image(4)" src="http://static.oschina.net/uploads/img/201309/04220311_dS1U.png" width="786" height="392" /></a></p>
<p><b>定義裁剪區域</b></p>
<p><code>void <b>glOrtho</b>(</code>
<br />GLdouble <br /><var>left</var>,</p>
<p>GLdouble   <br /><var>right</var>,</p>
<p>GLdouble   <br /><var>bottom</var>,</p>
<p>GLdouble   <br /><var>top</var>,</p>
<p>GLdouble   <br /><var>nearVal</var>,</p>
<p>GLdouble   <br /><var>farVal</var><code>)</code>;</p>
<h4>Parameters</h4> <dl><dt><em><code>left</code></em>, <em><code>right</code></em></dt><dd> <p>Specify the coordinates for the left and right vertical clipping planes.</p>
</dd><dt><em><code>bottom</code></em>, <em><code>top</code></em></dt><dd> <p>Specify the coordinates for the bottom and top horizontal clipping planes.</p>
</dd><dt><em><code>nearVal</code></em>, <em><code>farVal</code></em></dt><dd> <p>Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.</p> </dd></dl>
<p>這個函數從新定義了裁剪區域,使得縱橫比不變。縱橫比就是在垂直方向上的一個單位長度內的像素個數與水平方向上一個單位長度內的像素個數的比值。上面的例子使用的是正投影。</p>
<p>在調用glOrtho以前,要注意調用下面兩個函數</p>
<p>glMatrixMode(GL_PROJECTION);</p>
<p>glLoadIdentity();</p>
<p>投影矩陣是定義可視區域的地方。glOrtho並不創建新的裁剪區域,而是對以前的裁剪區域進行修改。它把當前裁剪區域的矩陣與參數所提供的矩陣相乘,獲得新的裁剪區域。glLoadIdentity();就是對當前的座標系系統進行重置。</p>
<p>下面的代碼能夠使得正方形保持正方形</p>
<p><code></code><code>if</code> <code>(w <= h)</code></p>
<p><code></code><code>{</code></p>
<p><code></code><code>glOrtho(-100.0f, 100.0f, -100.0f/aspectRatio, 100.0f/aspectRatio, 1.0, -1.0);</code></p>
<p><code></code><code>}</code></p>
<p><code></code><code>else</code></p>
<p><code></code><code>{</code></p>
<p><code></code><code>glOrtho(-100.0f*aspectRatio, 100.0f*aspectRatio, -100.0f, 100.0f, 1.0, -1.0);</code></p>
<p><code></code><code>}</code></p>
<p><code> <br /></code></p>
<p><code>這個函數調用使得裁剪區域 左邊老是位於-100,右邊則擴展到100除非窗口的寬度大於高度,此時水平範圍則會根據縱橫比進行縮放。同理當高度大於寬度時,上下要進行縮放。這樣就能夠保持一個200X200的正方形(中心爲(0,0))與窗口的形狀無關。以下圖:</code></p>
<p> </p>
<a href="http://static.oschina.net/uploads/img/201309/04220311_dJyk.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image" border="0" alt="Image" src="http://static.oschina.net/uploads/img/201309/04220312_Gtfr.png" width="927" height="379" /></a>