简体   繁体   English

使用片段着色器和 GL_LINE_STRIP 在 OpenGL GLES20 android 中绘制一条简单的虚线或虚线

[英]Draw a simple dotted line or dashed line in OpenGL GLES20 android using fragment shader and GL_LINE_STRIP

I went through different sources and seen that we could draw a dotted line using a fragment shader.我查看了不同的来源,发现我们可以使用片段着色器绘制一条虚线。 Because I am new to OpenGL I couldn't able to understand.因为我是 OpenGL 的新手,所以我无法理解。

Can anyone share some code sample that plot dotted or dashed line using fragment shader and GL_LINE_STRIP in Android.任何人都可以分享一些使用片段着色器和 Android 中的GL_LINE_STRIP绘制虚线或虚线的代码示例。

References:参考:

Line stipple is not supported in OpenGL ES. OpenGL ES 不支持线条点画。

If you use OpenGL ES 1.00, then you can use the approach which is presented in the answers to OpenGL ES - Dashed Lines .如果您使用 OpenGL ES 1.00,那么您可以使用OpenGL ES - Dashed Lines的答案中提供的方法。
You have to create a 1 dimensional texture (2 dimensional Nx1 texture), and wrap the texture on the lines.您必须创建一个 1 维纹理(2 维 Nx1 纹理),并将纹理包裹在线条上。 The texture has a stipple pattern encoded in its alpha channel.纹理具有在其 Alpha 通道中编码的点画图案。 The space between the dashes is discarded by the Alpha test . Alpha 测试会舍弃短划线之间的空格。

If you use OpenGL ES 2.00 or higher (OpenGL ES 3.x), then you can't use the alpha test, because it is deprecated.如果您使用 OpenGL ES 2.00 或更高版本 (OpenGL ES 3.x),则不能使用 alpha 测试,因为它已被弃用。 You have to use a fragment shader and to skip fragments by the discard keyword.您必须使用片段着色器并通过discard关键字跳过片段。

Create a texture with a red color channel only.创建仅具有红色通道的纹理。 The stipple pattern is encoded in the red color channel, but the code is very similar to that one from OpenGL ES - Dashed Lines , beside the fact that you have to use GL10.GL_RED for the internal texture format (instead of GL10.GL_ALPHA ):点画图案在红色通道中编码,但代码与OpenGL ES - Dashed Lines 中的代码非常相似,除了您必须使用GL10.GL_RED作为内部纹理格式(而不是GL10.GL_ALPHA ) :

byte arr[] = new byte[] { 255, 0, 0, 255 };
ByteBuffer textureBuffer = ByteBuffer.wrap(arr);

gl.glGenTextures(1, texture_id_, stippleTexObj);
gl.glBindTexture(GLES20.GL_TEXTURE_2D, stippleTexObj);
gl.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
gl.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
gl.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
gl.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
gl.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RED, 4, 1, 0,
                GLES20.GL_RED, GLES20.GL_UNSIGNED_BYTE, textureBuffer);

When you draw the lines, the you have to use a shader program, that discard the fragments dependent on the red color channel of the texture.绘制线条时,您必须使用着色器程序,该程序会丢弃依赖于纹理的红色通道的片段。 A very simple GLSL ES 1.00 shader (for OpenGL ES 3.00) may look as follows:一个非常简单的 GLSL ES 1.00 着色器(用于 OpenGL ES 3.00)可能如下所示:

Vertex shader:顶点着色器:

attribute vec2  inPos;
attribute float inU;
varying   float vU;

void main()
{
    outU        = inU;
    gl_Position = vec4( inPos.xy, 0.0, 1.0 );
}

Fragment shader:片段着色器:

precision mediump float;

varying float     vU;
uniform sampler2D u_stippleTexture;

void main()
{
    float stipple = texture2D(u_stippleTexture, vec2(vU, 0.0)).r;
    if (stipple < 0.5)
        discard;
    gl_FragColor = vec4(1.0);
}

Ensure that the texture coordinates which are associated to the vertices are aligned to integral values when you draw the line, that causes the the lines start and end with a dash:确保在绘制线条时与顶点关联的纹理坐标与整数值对齐,这会导致线条以破折号开始和结束:

eg a quad with bottom left of (-0.5 -0.5) and to right of (0.5, 0.5) and the texture coordinates in range [0, 5]:例如一个四边形,左下角为 (-0.5 -0.5),右下角为 (0.5, 0.5),纹理坐标范围为 [0, 5]:

 x     y       u   
-0.5f -0.5f    0.0f
 0.5f -0.5f    5.0f
 0.5f  0.5f    0.0f
-0.5f  0.5f    5.0f

Since the wrap function is GL_REPEAT and the texture coordinates are in range [0, 5], 5 repetitions of the stipple pattern are wrapped to each edge of the quad.由于 wrap 函数是GL_REPEAT并且纹理坐标在 [0, 5] 范围内,所以点画图案的 5 次重复被包裹到四边形的每个边缘。


Instead of using a texture, you can also generate the stippling in the fragment shader.除了使用纹理,您还可以在片段着色器中生成点画。 See the solutions for desktop OpenGL cor profile:请参阅桌面 OpenGL cor 配置文件的解决方案:
glLineStipple deprecated in OpenGL 3.1 glLineStipple 在 OpenGL 3.1 中已弃用
Dashed line in OpenGL3? OpenGL3中的虚线?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM