openGL线型和线宽以及线抗锯齿

一、 线宽


Opengl的线宽设置:glLineWidth(width); width为float类型值,在0~10.0,大于10以上按10来处理。

若开启线的反走样glEnable(GL_LINE_SMOOTH);,设置小数值才起作用,否则就四舍五入的处理整数了。

二、  线型


函数为glLineStipple(factor, Pattern[PatternMode]);

其中pattern值可以是任意的你想要的,把01转换为16进制的值就可以了。Factor为缩放因子。

pattern参数是由1或0组成的16位序列,它们根据需要进行重复,对一条特定的直线进行点画处理。从这个模式的低位开始,一个像素一个像素的进行处理。如果模式中对应的位是1,就绘制这个像素,否则就不绘制。模式可以使用factor参数(表示重复因子)进行扩展,它与1和0的连续子序列相乘。因此,如果模式中出现了3个1,并且factor是2,那么它们就扩展为6个连续的1。必须以GL_LINE_STIPPLE为参数调用glEnable()才能启用直线点画功能。

三、线宽代码

<span style="font-size:14px;">void show_width_lines(int width,float red,float green,float blue)
{
glColor3f(red,green,blue); width = 1.0f;
float PI = 3.1415926;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(red,green,blue);
//禁用反走样
glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH); // line_1
for (int i = 0; i < 8;i++ )
{
glLineWidth(width);
glBegin(GL_LINE_STRIP);
glVertex3f(5,5*(i+1), 0.0);
glVertex3f(100,5*(i+1)-qingxie_,0.0);
glEnd();
//直线宽度增加0.5
width += 2.0;
} //启用反走样
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); // Antialias the lines
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//初始化直线宽度
width=1.0;
// line_2
glColor4f(0,green,0,1.0);
for (int i = 0; i < 8;i++ )
{
glLineWidth(width);
glBegin(GL_LINE_STRIP);
glVertex3f(5, 50+1*(i+1), 0.0);
//glVertex3f(50,50+2*(i+1)-qingxie_, 0.0);
glVertex3f(80,50+3*(i+1)+qingxie_, 0.0);
//glVertex3f(100,50+4*(i+1)-qingxie_, 0.0);
glVertex3f(120,50+5*(i+1)+qingxie_, 0.0);
glEnd();
//宽度累加
width += 2.0;
}
glFlush();
}
</span>

四、线型代码

<span style="font-size:14px;">void show_dot_lines(int width,float red,float green,float blue)
{
int PatternMode = 0; //线型模式 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(red,green,blue);
int Pattern[6]= //定义了6种线型
{
//点线 1000100010001000, 表示实际画线的点,反序后转换成16进制就是0x1111 dotted
//. . . . . . . . . . . . . .
//0x1111,
0x0101, //点划线 1111111111100100 dot dash
//____ . ____ . _____ . _____. _____
0x27FF,
//0x1C47, //中心线 1111111111001100 centre line
//_____ _ _____ _ _____ _ _____ _ _____
0x33FF, //虚线 1111110011111100 dashed
//____ ____ ____ ____ ____ ____ ____
0x3F3F, //双点划线 1111111100100100 double dot dash
// ____ . . ____ . . ____ . . ____ . . ____
0x24FF, //三点划线 111111110101010 tri_dot_dash
// ____ . . ____ . . ____ . . ____ . . ____
0x55FF
}; static float angle = 0.0; glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_LINE_STIPPLE);
//初始化直线宽度
width=2.0;
// line_2
glColor3f(0,0,blue);
for (int i = 0; i<6; i++)
{
int PatternMode = i;
glLineStipple(1, Pattern[PatternMode]);
glLineWidth(width);
glBegin(GL_LINES);
glVertex3f(1,23+i*5, 0.0);
glVertex3f(50,23+i*5-qingxie_, 0.0);
glEnd();
} width = 1.0;
glColor3f(red,0,0);
for (int i = 0; i<6; i++)
{
int PatternMode = i;
glLineStipple(1, Pattern[PatternMode]);
glLineWidth(width);
glBegin(GL_LINES);
glVertex3f(20,50+i*5, 0.0);
glVertex3f(50,50+i*5-qingxie_, 0.0);
glEnd();
} glDisable(GL_LINE_STIPPLE);
glFlush(); }</span>

五、结果

线的线宽



很明显,绿色线开启反走样,而淡红色的没有。

线型--点划线的实现效果

开启的反走样,为fastest. 当然你可以根据你效果与效率之间选择自己需要的。

六、全部实现代码和工程

<span style="font-size:14px;">
#include <Windows.h>
#include "glew.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"./glew32.lib")
#include <math.h>
// #include "glut.h"
#include<stdio.h>
#include<stdlib.h> #define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);glVertex3f((x1),(y1),0); glVertex3f((x2),(y2),0);glEnd(); #define qingxie_ 2.0
// function declear
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
} void show_width_lines(int width,float red,float green,float blue)
{
glColor3f(red,green,blue); width = 1.0f;
float PI = 3.1415926;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(red,green,blue);
//禁用反走样
glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH); // line_1
for (int i = 0; i < 8;i++ )
{
glLineWidth(width);
glBegin(GL_LINE_STRIP);
glVertex3f(5,5*(i+1), 0.0);
glVertex3f(100,5*(i+1)-qingxie_,0.0);
glEnd();
//直线宽度增加0.5
width += 2.0;
} //启用反走样
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); // Antialias the lines
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//初始化直线宽度
width=1.0;
// line_2
glColor4f(0,green,0,1.0);
for (int i = 0; i < 8;i++ )
{
glLineWidth(width);
glBegin(GL_LINE_STRIP);
glVertex3f(5, 50+1*(i+1), 0.0);
//glVertex3f(50,50+2*(i+1)-qingxie_, 0.0);
glVertex3f(80,50+3*(i+1)+qingxie_, 0.0);
//glVertex3f(100,50+4*(i+1)-qingxie_, 0.0);
glVertex3f(120,50+5*(i+1)+qingxie_, 0.0);
glEnd();
//宽度累加
width += 2.0;
}
glFlush();
} void Line3f(GLfloat fromX, GLfloat fromY, GLfloat fromZ,
GLfloat toX, GLfloat toY, GLfloat toZ)
{
glBegin(GL_LINES);
glVertex3f(fromX, fromY, fromZ);
glVertex3f(toX, toY, toZ);
glEnd();
} void show_dot_lines(int width,float red,float green,float blue)
{
int PatternMode = 0; //线型模式 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(red,green,blue);
int Pattern[6]= //定义了6种线型
{
//点线 1000100010001000, 表示实际画线的点,反序后转换成16进制就是0x1111 dotted
//. . . . . . . . . . . . . .
//0x1111,
0x0101, //点划线 1111111111100100 dot dash
//____ . ____ . _____ . _____. _____
0x27FF,
//0x1C47, //中心线 1111111111001100 centre line
//_____ _ _____ _ _____ _ _____ _ _____
0x33FF, //虚线 1111110011111100 dashed
//____ ____ ____ ____ ____ ____ ____
0x3F3F, //双点划线 1111111100100100 double dot dash
// ____ . . ____ . . ____ . . ____ . . ____
0x24FF, //三点划线 111111110101010 tri_dot_dash
// ____ . . ____ . . ____ . . ____ . . ____
0x55FF
}; static float angle = 0.0; glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); // 反走样的fastest效果,也可以根据需要选择GL_NICEST.etc.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_STIPPLE); //初始化直线宽度
width=5.0;
// line_2
glColor3f(0,0,blue);
for (int i = 0; i<6; i++)
{
int PatternMode = i;
glLineStipple(i, Pattern[PatternMode]);
glLineWidth(width);
glBegin(GL_LINES);
glVertex3f(1,23+i*5, 0.0);
glVertex3f(50,23+i*5-qingxie_, 0.0);
glEnd();
} width = 1.0;
glColor3f(red,0,0);
for (int i = 0; i<6; i++)
{
int PatternMode = i;
glLineStipple(i, Pattern[PatternMode]);
glLineWidth(width);
glBegin(GL_LINES);
glVertex3f(2,50+i*5, 0.0);
glVertex3f(50,50+i*5-qingxie_, 0.0);
glEnd();
} glDisable(GL_LINE_STIPPLE);
glDisable(GL_BLEND);
glFlush(); } void pointFun()
{
float red = 1.0,green = 0.5,blue = 0.5;
glClear (GL_COLOR_BUFFER_BIT); // 去掉注释就可以看线宽效果了。
//show_width_lines(5,red,green,blue);
show_dot_lines(5,red,green,blue);
glFlush();
} int main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (1000, 800); // Set display-window width and height.
//glutFullScreen();
glutCreateWindow ("An Example OpenGL Program By Qiu"); // Create display window.
init(); // Execute initialization procedure. // draw position.
glTranslatef(50.0f, 50.0f,0.0f); glutDisplayFunc (pointFun); // Send graphics to display window.
glutMainLoop ( );// Send graphics to display window. // Display everything and wait.
return 0;
}
</span>

工程下载地址:http://download.csdn.net/detail/cartzhang/7648119

免分源码工程

end

多多指教!

openGL线型和线宽以及线的抗锯齿的更多相关文章

  1. OpenGL ES3使用MSAA(多重采样抗锯齿)的方法

    昨晚花费了我2个多小时的时间终于把OpenGL ES3.0中的MSAA给搞定了.在OpenGL ES2.0中,Khronos官方没有引入标准的MSAA全屏抗锯齿的方法,而Apple则采用了自己的GL_ ...

  2. OpenGL之抗锯齿 以及 线宽的设置

    转自原文 OpenGL之抗锯齿 以及 线宽的设置 抗锯齿 1.线的抗锯齿 glEnable(GL_LINE_SMOOTH); //启用 glHint(GL_LINE_SMOOTH,GL_NICEST) ...

  3. PHP合成图片、生成文字、居中对齐、画线、矩形、三角形、多边形、图片抗锯齿、不失真 高性能源码示例

    function generateImg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') { $date = '' . date ( ...

  4. SolidEdge如何修改线型和线宽

    选中一条直线,然后点击如下所示两个按钮,可以分别修改线型和线宽.    

  5. 在qt的QOpenGLWidget开启opengl的抗锯齿

    在QOpenGLWidget的构造函数添加下面几句代码即可 QSurfaceFormat surfaceFormat; surfaceFormat.setSamples();//多重采样 setFor ...

  6. 给Libgdx的ShapeRenderer开启抗锯齿

    http://blog.rpsg-team.com/?p=134 ——————————————————————————————————————————————————————————————————— ...

  7. 回击MLAA:NVIDIA FXAA抗锯齿性能实測、画质对照

    PC游戏玩家肯定会对各式各样的AA抗锯齿技术很熟悉,而今天本文的主角就是NVIDIA今年才推出的新型抗锯齿技术"FXAA". FXAA在某种程度上有些类似于AMD之前宣传的MLAA ...

  8. 【ShaderToy】基础篇之再谈抗锯齿(antialiasing,AA)

    写在前面 在之前的基础篇中,我们讲到了在绘制点线时如何处理边缘的锯齿,也就是使用smoothstep函数.而模糊参数是一些定值,或者是跟屏幕分辨率相关的数值,例如分辨率宽度的5%等等.但这种方法其实是 ...

  9. 【ShaderToy】抗锯齿相关函数

    *示例代码可以直接在ShaderToy中运行. *我放在这里咯ShaderToy基础学习中~欢迎交流(ノ>ω<)ノ 先上未抗锯齿的两个圆形图案,可以清楚看清图案边缘像素块,即“锯齿”. 附 ...

随机推荐

  1. sessionStorage的使用方法

    本篇是关于sessionStorage的使用方法的介绍,简单几行代码,实现sessionStorage,请大家查阅 (1)在需要设置sessionStorage的页面写如下代码可以存入sessionS ...

  2. 【例题 8-13 UVA - 11093】Just Finish it up

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法. 假设现在取[l..r]这一段. 然后发现累加的和小于0了. 那么方法只能是不走l..l+1这一段了 即delta递减(p[ ...

  3. zookeeper提供了什么

    简单的说,zookeeper=文件系统+通知机制. 每个子目录项如 NameService 都被称作为 znode,和文件系统一样,我们能够自由的增加.删除znode,在一个znode下增加.删除子z ...

  4. ThinkPad E431 获取无限网络的驱动

    sudo apt-get install linux-headers-generic build-essential dkms    sudo apt-get install linux-source ...

  5. 徒弟们对话,遇到sb领导,离职吧

     由于涉及私密,所以qq号做了干扰.见谅. 非常多人以为在公司,你优化了几十个sql老板就给你涨工资了.或者你bbed搞点特殊恢复就涨工资了. 或者解决某些棘手问题就涨工资了. 那是不正确的. 遇 ...

  6. 分析深圳电信的新型HTTP劫持方式

    昨天深圳下了一天的暴雨,2014年的雨水真是够多的. 用户的资源就是金钱,怎的也要好好利用嘛不是? ISP的劫持手段真是花样百出.从曾经的DNS(污染)劫持到后来的共享检測.无不通过劫持正常的请求来达 ...

  7. [NOI.AC#31]MST 计数类DP

    链接 注意到 \(n\) 只有40,爆搜一下发现40的整数拆分(相当于把 \(n\) 分成几个联通块)很少 因此可以枚举联通块状态来转移,这个状态直接用vector存起来,再用map映射,反正40也不 ...

  8. .Net Standard和各平台关系

    .NET Standard      1.0      1.1      1.2      1.3      1.4 1.5 1.6 2.0 .NET 核心 1.0 1.0 1.0 1.0 1.0 1 ...

  9. C#解析HTML源码

    刚做了一个小任务,需要抓取其他网站的部分数据,这里就顺便介绍使用Winista.Text.HtmlParser这个类库如何解析HTML并抓取部分数据 1.获取指定网站的页面源码 string url ...

  10. vue中v-for的用法以及参数的作用

    <template> <div> <div class="content clearfix" v-on:click="goorderingD ...