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>数组

相关文章
相关标签/搜索