<h2>第二章 使用OpenGL</h2> <h3>(一)OpenGl 的工做原理</h3> <p>OpenGL是過程式的而非描述性的圖形API。開發人員只須規定必要的步驟去實現顯示的效果。這些步驟包含了許多OpenGL的命令。這些命令用於繪製許多的幾何圖元,例如點,線,多邊形。OpenGL支持光照和陰影,紋理映射,混合,透明效果,動畫,還有許多特殊的效果。</p> <h3>(二)通用實現<sub></sub></h3> <p>通用實現是指軟件實現,軟件實現的好處是跨硬件。可是性能不佳,沒有硬件加速。</p> <p>軟件通用實現的OpenGL API所處的位置以下圖:</p> <p><a href="http://static.oschina.net/uploads/img/201309/02231053_ukqK.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201309/02231053_2vLk.png" width="510" height="213" /></a></p> <p>其首先用軟件實現去光柵化繪製3D圖像,而後傳給GDI,GDI負責與顯示設備交互顯示圖像。</p> <h3>(三)硬件實現</h3> <p>OpenGL API調用直接傳給硬件驅動。硬件驅動不會把圖形傳給Windows GDI進行顯示。而是直接與圖形顯示設備交互。</p> <p><a href="http://static.oschina.net/uploads/img/201309/02231054_HdbX.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/02231056_ol8a.png" width="551" height="214" /></a></p> <h3>(四)圖形管線</h3> <p>管線是用於描述一系列的處理流程。下圖是一個簡化版本的圖形管線</p> <p><a href="http://static.oschina.net/uploads/img/201309/02231056_Bukq.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/02231056_CBnZ.png" width="530" height="148" /></a></p> <p>一個應用程序調用了許多OpenGL的API,這些命令被傳送到Commond Buffer</p> <p>這個buffer填充着許多命令,頂點數據,紋理數據等等,當緩衝區的數據被刷新時,命令和數據就被傳遞給管線的下一階段。</p> <p>頂點數據首先會進行變換和光照。這個階段完成以後,數據被傳輸到管線的光柵化部分。最後圖像被放入到幀緩衝區。</p> <p>早期的OpenGL硬件加速器只是對光柵化階段進行了加速。然而如今的硬件加速器都提供了變換和光照階段。</p>性能