OpenGL超級寶典筆記——其餘圖元

<h3>四邊形</h3> <p>OpenGL的GL_QUADS圖元,能夠指定4個點畫出四邊形。這些多邊形具備順時針環繞的方向。<font style="background-color: #ffff00">PS:四邊形的四個角必須位於同一個平面</font></p> <p><font style="background-color: #ffffff">GL_QUADS_STRIP圖元能夠畫四邊形帶。</font></p> <p><a href="http://static.oschina.net/uploads/img/201309/15222336_6r1z.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/15222337_pqbf.png" width="498" height="204"></a></p> <p><a href="http://static.oschina.net/uploads/img/201309/15222337_JDg0.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/15222338_8lRL.png" width="494" height="165"></a></p> <h3>通用多邊形</h3> <p>GL_POLYGONS能夠使用指定的全部點繪製一個多邊形。<font style="background-color: #ffff00">PS:這些點必須在一個平面上</font></p> <h3>使用點畫方式,填充多邊形</h3> <p>開啓點畫模式</p> <p>glEnagle(GL_POLYGON_STIPPLE);</p> <p>設置填充的模型</p> <p>glPolygonStipple(pBitmap);</p> <p>pBitmap指定了一塊數據區域。而後多邊形就使用pBitmap(GLubyte*)所指定的模式進行填充。pBitmap指向的緩衝區是32*32的位模式。首先讀入的是最高有效位(MSB)這與點畫的方式恰好相反。</p> <p><a href="http://static.oschina.net/uploads/img/201309/15222339_ZqvJ.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/15222340_IbsJ.png" width="383" height="330"></a></p> <p>一個多邊形的點畫模式,篝火的圖像。這個篝火圖像的數據用32*32的數組存儲</p> <p>// Bitmap of campfire<br>GLubyte fire[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0,<br>0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0,<br>0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80,<br>0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80,<br>0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8,<br>0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70,<br>0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80,<br>0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20,<br>0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40,<br>0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80,<br>0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00,<br>0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00,<br>0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };</p> <p>數組的第一行存儲的是圖像最後一行的數據,數組的最後一行存儲的是圖像的第一行數據。點畫模式只能用於簡單的多邊形填充,由於其不會隨多邊形的變換而變換。示例以下</p> <div id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">//篝火圖像</span><br>GLubyte fire[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br>0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0,<br>0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0,<br>0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80,<br>0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80,<br>0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8,<br>0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70,<br>0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80,<br>0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20,<br>0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40,<br>0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80,<br>0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00,<br>0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00,<br>0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };<br><br><span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> RenderScene()<br>{<br> glClear(GL_COLOR_BUFFER_BIT);<br> glColor3f(1.0f, 1.0f, 0.0f);<br> <span style="color: #008000">//開啓多邊形點畫填充</span><br> glEnable(GL_POLYGON_STIPPLE);<br> <span style="color: #008000">//設置填充模式</span><br> glPolygonStipple(fire);<br> glBegin(GL_POLYGON);<br> glVertex2f(0.0f, 0.0f);<br> glVertex2f(50.0f, 0.0f);<br> glVertex2f(50.0f, 50.0f);<br> glVertex2f(0.0f, 50.0f);<br> glEnd();<br><br> glFlush();<br>}<br></pre><br></div> <p><a href="http://static.oschina.net/uploads/img/201309/15222341_JCfv.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" border="0" alt="image" src="http://static.oschina.net/uploads/img/201309/15222342_7uRW.png" width="244" height="243"></a></p> <p>源碼:<a href="https://github.com/sweetdark/openglex/tree/master/polygon">https://github.com/sweetdark/openglex/tree/master/polygon</a></p> <p><font style="background-color: #ffff00">多邊形的建立規則:多邊形不能被扭曲,邊也不能交叉。指定的一個多邊形的頂點必須在一個面上。多邊形必須是凸多邊形。</font></p> <p><a href="http://static.oschina.net/uploads/img/201309/15222345_9cwT.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/15222346_wGjT.png" width="584" height="243"></a></p> <p><a href="http://static.oschina.net/uploads/img/201309/15222347_tRPK.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/15222348_gQLE.png" width="594" height="273"></a></p>git

<h3>凹多邊形及邊界</h3> <p>直接畫凹多邊形在OpenGL中是非法的,但能夠使用多個多邊形的拼接來實現繪製凹多邊形。當咱們使用填充模式的時候,看不出這些由多個多邊形拼接起來的凹多邊形的內部線條。可是當咱們使用多邊形畫線模式glPolygonMode(GL_FRONT, GL_LINE)的時候會出現這些內部的線。此時咱們能夠經過OpenGL提供的glEdageFlag()來處理這些邊的問題。當這個函數接受true值時,接下來的頂點都將做爲多邊形邊界線的起點。例子:</p> <div id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><br><span style="color: #008000">//五角星的8個點</span><br><span style="color: #0000ff">static</span> GLfloat points[16] =<br>{<br>-50.0f, 50.0f,<br>0.0f, 125.0f,<br>50.0f, 50.0f,<br>125.0f, 0.0f,<br>50.0f, -50.0f,<br>0.0f, -125.0f,<br>-50.0f, -50.0f,<br>-125.0f, 0.0f<br>};<br><br><span style="color: #0000ff">static</span> GLboolean bFlag = true;<br><br><span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> RenderScene()<br>{<br> glClear(GL_COLOR_BUFFER_BIT);<br> glColor3f(1.0f, 1.0f, 0.0f);<br> glPolygonMode(GL_FRONT, GL_LINE);<br> glPolygonMode(GL_BACK, GL_LINE);<br> glBegin(GL_TRIANGLES);<br> <span style="color: #008000">//五角星的內部兩個三角形不是邊界</span><br> glEdgeFlag(bFlag);<br> glVertex2f(points[0], points[1]);<br> glVertex2f(points[4], points[5]);<br> glVertex2f(points[12], points[13]);<br><br> glVertex2f(points[4], points[5]);<br> glVertex2f(points[8], points[9]);<br> glVertex2f(points[12], points[13]);<br> glEdgeFlag(true);<br><br> glVertex2f(points[0], points[1]);<br> glVertex2f(points[2], points[3]);<br> glEdgeFlag(bFlag);<br> glVertex2f(points[4], points[5]);<br><br> glVertex2f(points[4], points[5]);<br> glEdgeFlag(true);<br> glVertex2f(points[8], points[9]);<br> glVertex2f(points[6], points[7]);<br><br> glVertex2f(points[8], points[9]);<br> glVertex2f(points[10], points[11]);<br> glEdgeFlag(bFlag);<br> glVertex2f(points[12], points[13]);<br><br> glVertex2f(points[12], points[13]);<br> glEdgeFlag(true);<br> glVertex2f(points[0], points[1]);<br> glVertex2f(points[14], points[15]);<br><br> glEnd();<br> glutSwapBuffers();<br>}<br></pre><br></div> <p>bFlag=true和bFlag=false的區別:</p> <p><a href="http://static.oschina.net/uploads/img/201309/15222352_6CaR.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" border="0" alt="image" src="http://static.oschina.net/uploads/img/201309/15222352_zXY6.png" width="228" height="244"></a><a href="http://static.oschina.net/uploads/img/201309/15222353_nL3o.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" border="0" alt="image" src="http://static.oschina.net/uploads/img/201309/15222355_B3Y2.png" width="228" height="244"></a></p> <p>&nbsp;</p>github

<p>源碼:<a href="https://github.com/sweetdark/openglex/tree/master/triangleedge">https://github.com/sweetdark/openglex/tree/master/triangleedge</a></p>數組

相關文章
相關標籤/搜索