<h3><b>3D中画直线</b></h3> <p>在3D中画直线需要指定两个顶点。如下代码:会在两点之间画一条直线 <p>glBegin(GL_LINES); <p> glVertex3f(0.0f, 0.0f, 0.0f); <p> glVertex3f(50.0f, 25.0f, 0.0f); <p>glEnd(); <p>这里用两个顶点指定了一个图元。注意如果为GL_LINES指定奇数个顶点,那么最后一个顶点将被忽略。 <p>static void RenderScene() <p>{ <p> glClear(GL_COLOR_BUFFER_BIT); <p> glColor3f(0.0f, 1.0f, 0.0f); <p> GLfloat x, y, z, angle; <p> z = -50.0f; <p> //画线 <p> glBegin(GL_LINES); <p> for (angle = 0.0f; angle <= 2*GL_PI*3; angle +=0.5f) <p> { <p> x = 50.0f * cos(angle); <p> y = 50.0f * sin(angle); <p> //设置两个顶点 <p> glVertex3f(0.0f, 0.0f, 0.0f); <p> glVertex3f(x, y, z); <p> z += 0.5f; <p> } <p> glEnd(); <p> glutSwapBuffers(); <p>} <p>效果如下图: <p><a href="http://static.oschina.net/uploads/img/201309/10213825_MjQm.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/10213826_bx0B.png" width="353" height="411"></a> <hr> <h3><b>连续的线以及线环</b></h3> <p>绘制一条连续的线,使用GL_LINE_STRIP,会绘制一条经过所有这些顶点的连续的线。实例如下: <p>glBegin(GL_LINE_STRIP); <p> glVertex3f(0.0f, 0.0f, 0.0f); <p> glVertex3f(50.0f, 50.0f, 0.0f); <p> glVertex3f(50.0f, 100.0f, 0.0f); <p>glEnd(); <p><a href="http://static.oschina.net/uploads/img/201309/10213828_Bp9r.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(1)" border="0" alt="Image(1)" src="http://static.oschina.net/uploads/img/201309/10213829_3uT4.png" width="773" height="318"></a> <p>GL_LINE_LOOP,与GL_LINE_STRIP相似。只是最后一个顶点和第一个顶点之间也会绘制一条直线,形成闭合的线。 <p><a href="http://static.oschina.net/uploads/img/201309/10213829_meaV.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(2)" border="0" alt="Image(2)" src="http://static.oschina.net/uploads/img/201309/10213830_2H80.png" width="768" height="338"></a> <h3><b>用直线绘制近似的曲线</b></h3> <p>绘制近似曲线可以使用GL_LINE_STRIP来链接所有的点。当点之间的距离越来越近时,就可以形成一条平滑的曲线。示例如下: <p>//绘制场景 <p>static void RenderSences() <p>{ <p> GLfloat z = -50.0f; <p> //清空画布 <p> glClear(GL_COLOR_BUFFER_BIT); <p> //设置画图使用的颜色 <p> glColor3f(1.0f, 0.0f, 0.0f); <p> //保存矩阵 <p> glPushMatrix(); <p> //旋转, 如果不旋转,那些不同层次的点都重叠在一起,看不出3D效果了。 <p> glRotatef(200.0f, 1.0f, 0.0f, 0.0f); <p> glRotatef(120.5f, 0.0f, 1.0f, 0.0f); <p> glBegin(GL_LINE_STRIP); <p> for (GLfloat a = 0.0; a < GL_PI * 3.0f; a += 0.1f) <p> { <p> GLfloat x = 50.0f * cos(a); <p> GLfloat y = 50.0f * sin(a); <p> //设置点的大小 <p> //设置顶点 <p> glVertex3f(x, y, z); <p> //调整z值 <p> z += 0.5f; <p> } <p> glEnd(); <p> glPopMatrix(); <p> glFlush(); <p>} <p><a href="http://static.oschina.net/uploads/img/201309/10213830_0N4f.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(3)" border="0" alt="Image(3)" src="http://static.oschina.net/uploads/img/201309/10213831_x2sq.png" width="314" height="386"></a> <hr> <h3><b>设置直线的宽度</b></h3> <p>与设置点的宽度类似,我们也可以设置直线的宽度。但直线的宽度设置也是有限制的。我们可以取得直线宽度的范围,以及其宽度增长的最小间隔。 <p>代码如下 <p>GLfloat width[2]; //存储直线的宽度范围<br>GLfloat step; //存储受支持的直线宽度增量 <p>//获取受支持的直线宽度范围和增量 <br>glGetFloatv(GL_LINE_WIDTH_RANGE, width);<br>glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &step); <p>示例: <p>static void RenderScene() <p>{ <p> glClear(GL_COLOR_BUFFER_BIT); <p> glColor3f(0.0f, 1.0f, 0.0f); <p> //获取直线的宽度 <p> GLfloat width[2]; <p> GLfloat step; <p> glGetFloatv(GL_LINE_WIDTH_RANGE, width); <p> glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &step); <p> GLfloat y, curWidht; <p> curWidht = width[0]; <p> curWidht += step; <p> //画线 <p> for (y = 80.0f; y > -80.0f; y -= 10.0f) <p> { <p> glLineWidth(curWidht); <p> curWidht += step; <p> glBegin(GL_LINES); <p> glVertex2f(-50.0f, y); <p> glVertex2f(50.0f, y); <p> glEnd(); <p> } <p> glutSwapBuffers(); <p>} <p><a href="http://static.oschina.net/uploads/img/201309/10213831_0yoj.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(4)" border="0" alt="Image(4)" src="http://static.oschina.net/uploads/img/201309/10213832_fq7J.png" width="393" height="423"></a> <hr> <h3><b>直线点画</b></h3> <p>使用点线或虚线模式来绘制直线,称为点画。启用点画模式glEnable(GL_LINE_STIPPLE); <p>然后可以使用glLineStipple函数创建这种模式。 <h4>C Specification</h4> <p><code>void <b>glLineStipple</b>(</code><br>GLint <br><var>factor</var>, <p>GLushort <br><var>pattern</var><code>)</code>; <p><a></a> <h4><font style="font-weight: normal">其中pattern参数是一个16位的值, 它指定了一个模式,供绘直线所用。它的每个位都表示线段的一部分是画还是不画。在默认情况下,每个位对应一个像素,但factor参数可以作为乘法因子增加模式的宽度。例如把factor设置为5会导致模式中的每个位代表一行中的连续5个像素。模式的第0位(最低有效位)首先被用于指定直线。</font></h4> <p><a href="http://static.oschina.net/uploads/img/201309/10213832_4cUe.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(5)" border="0" alt="Image(5)" src="http://static.oschina.net/uploads/img/201309/10213833_4foY.png" width="702" height="376"></a> <p>注意:模式的顺序是反过来的,这样的原因是OpenGL左移一位获得下一个掩码值的速度更快。 <p>例子: <p>static void RenderScene() <p>{ <p> glClear(GL_COLOR_BUFFER_BIT); <p> glColor3f(0.0f, 1.0f, 0.0f); <p> //开启stipple模式 <p> glEnable(GL_LINE_STIPPLE); <p> GLfloat y; <p> GLint factor = 1; <p> GLushort patter = 0x5555; <p> //画线 <p> for (y = 80.0f; y > -80.0f; y -= 10.0f) <p> { <p> //设置因子和模式 <p> glLineStipple(factor, patter); <p> glBegin(GL_LINES); <p> glVertex2f(-50.0f, y); <p> glVertex2f(50.0f, y); <p> glEnd(); <p> factor++; <p> } <p> glutSwapBuffers(); <p>} <p><b><a href="http://static.oschina.net/uploads/img/201309/10213834_mbUS.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(6)" border="0" alt="Image(6)" src="http://static.oschina.net/uploads/img/201309/10213835_spDq.png" width="412" height="433"></a></b> <dl> <dd></dd></dl> <p>示例代码地址:<a href="https://github.com/sweetdark/openglex/tree/master/drawline">https://github.com/sweetdark/openglex/tree/master/drawline</a></p>
转载于:https://my.oschina.net/sweetdark/blog/160492