博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenGL超级宝典笔记——画直线
阅读量:5964 次
发布时间:2019-06-19

本文共 7722 字,大约阅读时间需要 25 分钟。

hot3.png

<h3><b>3D中画直线</b></h3> <p>在3D中画直线需要指定两个顶点。如下代码:会在两点之间画一条直线 <p>glBegin(GL_LINES); <p>&nbsp;&nbsp;&nbsp;&nbsp; glVertex3f(0.0f, 0.0f, 0.0f); <p>&nbsp;&nbsp;&nbsp;&nbsp; glVertex3f(50.0f, 25.0f, 0.0f); <p>glEnd(); <p>这里用两个顶点指定了一个图元。注意如果为GL_LINES指定奇数个顶点,那么最后一个顶点将被忽略。 <p>static void RenderScene() <p>{ <p>&nbsp; glClear(GL_COLOR_BUFFER_BIT); <p>&nbsp; glColor3f(0.0f, 1.0f, 0.0f); <p>&nbsp; GLfloat x, y, z, angle; <p>&nbsp; z = -50.0f; <p>&nbsp; //画线 <p>&nbsp; glBegin(GL_LINES); <p>&nbsp; for (angle = 0.0f; angle &lt;= 2*GL_PI*3; angle +=0.5f) <p>&nbsp; { <p>&nbsp;&nbsp;&nbsp; x = 50.0f * cos(angle); <p>&nbsp;&nbsp;&nbsp; y = 50.0f * sin(angle); <p>&nbsp;&nbsp;&nbsp; //设置两个顶点 <p>&nbsp;&nbsp;&nbsp; glVertex3f(0.0f, 0.0f, 0.0f); <p>&nbsp;&nbsp;&nbsp; glVertex3f(x, y, z); <p>&nbsp;&nbsp;&nbsp; z += 0.5f; <p>&nbsp; } <p>&nbsp; glEnd(); <p>&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp; glVertex3f(0.0f, 0.0f, 0.0f); <p>&nbsp;&nbsp;&nbsp;&nbsp; glVertex3f(50.0f, 50.0f, 0.0f); <p>&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp; GLfloat z = -50.0f;&nbsp;&nbsp; <p>&nbsp; //清空画布 <p>&nbsp; glClear(GL_COLOR_BUFFER_BIT); <p>&nbsp; //设置画图使用的颜色 <p>&nbsp; glColor3f(1.0f, 0.0f, 0.0f); <p>&nbsp; //保存矩阵 <p>&nbsp; glPushMatrix(); <p>&nbsp; //旋转, 如果不旋转,那些不同层次的点都重叠在一起,看不出3D效果了。 <p>&nbsp; glRotatef(200.0f, 1.0f, 0.0f, 0.0f); <p>&nbsp; glRotatef(120.5f, 0.0f, 1.0f, 0.0f); <p>&nbsp; glBegin(GL_LINE_STRIP); <p>&nbsp; for (GLfloat a = 0.0; a &lt; GL_PI * 3.0f; a += 0.1f) <p>&nbsp; { <p>&nbsp;&nbsp;&nbsp; GLfloat x = 50.0f * cos(a); <p>&nbsp;&nbsp;&nbsp; GLfloat y = 50.0f * sin(a); <p>&nbsp;&nbsp;&nbsp; //设置点的大小 <p>&nbsp;&nbsp;&nbsp; //设置顶点 <p>&nbsp;&nbsp;&nbsp; glVertex3f(x, y, z); <p>&nbsp;&nbsp;&nbsp; //调整z值 <p>&nbsp;&nbsp;&nbsp; z += 0.5f; <p>&nbsp; } <p>&nbsp; glEnd(); <p>&nbsp; glPopMatrix(); <p>&nbsp; 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];&nbsp; //存储直线的宽度范围<br>GLfloat step;&nbsp; //存储受支持的直线宽度增量 <p>//获取受支持的直线宽度范围和增量&nbsp; <br>glGetFloatv(GL_LINE_WIDTH_RANGE, width);<br>glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &amp;step); <p>示例: <p>static void RenderScene() <p>{ <p>&nbsp; glClear(GL_COLOR_BUFFER_BIT); <p>&nbsp; glColor3f(0.0f, 1.0f, 0.0f); <p>&nbsp; //获取直线的宽度 <p>&nbsp; GLfloat width[2]; <p>&nbsp; GLfloat step; <p>&nbsp; glGetFloatv(GL_LINE_WIDTH_RANGE, width); <p>&nbsp; glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &amp;step); <p>&nbsp; GLfloat y, curWidht; <p>&nbsp; curWidht = width[0]; <p>&nbsp; curWidht += step; <p>&nbsp; //画线 <p>&nbsp; for (y = 80.0f; y &gt; -80.0f; y -= 10.0f) <p>&nbsp; { <p>&nbsp;&nbsp;&nbsp; glLineWidth(curWidht); <p>&nbsp;&nbsp;&nbsp; curWidht += step; <p>&nbsp;&nbsp;&nbsp; glBegin(GL_LINES); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glVertex2f(-50.0f, y); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glVertex2f(50.0f, y); <p>&nbsp;&nbsp;&nbsp; glEnd(); <p>&nbsp; } <p>&nbsp; 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&nbsp; <br><var>factor</var>, <p>GLushort&nbsp; <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>&nbsp; glClear(GL_COLOR_BUFFER_BIT); <p>&nbsp; glColor3f(0.0f, 1.0f, 0.0f); <p>&nbsp; //开启stipple模式 <p>&nbsp; glEnable(GL_LINE_STIPPLE); <p>&nbsp; GLfloat y; <p>&nbsp; GLint factor = 1; <p>&nbsp; GLushort patter = 0x5555; <p>&nbsp; //画线 <p>&nbsp; for (y = 80.0f; y &gt; -80.0f; y -= 10.0f) <p>&nbsp; { <p>&nbsp;&nbsp;&nbsp; //设置因子和模式 <p>&nbsp;&nbsp;&nbsp; glLineStipple(factor, patter); <p>&nbsp;&nbsp;&nbsp; glBegin(GL_LINES); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glVertex2f(-50.0f, y); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glVertex2f(50.0f, y); <p>&nbsp;&nbsp;&nbsp; glEnd(); <p>&nbsp;&nbsp;&nbsp; factor++; <p>&nbsp; } <p>&nbsp; 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

你可能感兴趣的文章
Socket 详解
查看>>
[Android Pro] Java进阶学习:jar打包详解
查看>>
xampp-apache配置
查看>>
zabbix专题:第十二章 zabbix proxy分布式监控配置
查看>>
tar 命令的详解
查看>>
Android Studio第二十七期 - RecycleView不同item布局
查看>>
穷人的分布式网络
查看>>
FR-TO-FR本地交换
查看>>
Python内置容器(2)——字典,迭代器,列表解析
查看>>
那年匆匆 -大学
查看>>
Internet 打印提示“打印机安装失败、打印机名称无效”的解决
查看>>
从Powershell ***脚本学到的如何执行后台runspace~
查看>>
SCCM TP4部署Office2013
查看>>
Linux系统启动过程,grub重装。
查看>>
使用Putty密钥认证机制远程登录Linux
查看>>
一不小心,老司机又翻车了
查看>>
理解思科IPS系统的traffic flow notifications
查看>>
【博客话题】技术人生之三界修炼
查看>>
Ext JS 6开发实例(三) :主界面设计
查看>>
Hyper-V 3中虚拟机CPU竞争机制
查看>>