OpenGL ES - 虚线 [英] OpenGL ES - Dashed Lines

查看:18
本文介绍了OpenGL ES - 虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenGLEs10 进行 Android 游戏开发,我想绘制虚线现在我用这段代码来画我的线:

gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);gl.glLineWidth(4.0f);gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mBufDestVertices);gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, mDesPoly.getNumPoints());

我应该如何改变它?我必须纹理线条吗?如果是,怎么做?

解决方案

在 OpenGL ES 1.0 中,不支持 glLineStipple.也不支持一维纹理.但是支持二维纹理,并且支持


<子>

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

I'm Using OpenGlEs10 for My Android Game Development, I Want to Draw Dashed-lines Now Im Using This Code to Draw My Line:

gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
// gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glLineWidth(4.0f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mBufDestVertices);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, mDesPoly.getNumPoints());

How Should I Change It? Does I have to Texture The line? And if so, How?

解决方案

In OpenGL ES 1.0 glLineStipple is not supported. 1 dimensional textures are not supported, too. But 2 dimensional texturing is supported and the Alpha test is supported. See OpenGL ES 1.0 Specification.
A 1 dimensional textures, can be represented by a 2 dimensional Nx1 texture.

The stipple pattern can be encoded in a texture. Create 2 dimensional 4x1 texture with the internal format GL_ALPHA and the following pattern:

1 0 0 1

The minifying and magnification function is GL_NEAREST. The wrap parameters are GL_REPEAT (that is default).

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

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

When you draw the lines, the you have to enable the Alpha test and enable 2 dimensional texturing.

gl.glAlphaFunc(GL10.GL_GEQUAL, 0.5f);
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);

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:

e.g. 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]:

 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.


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

这篇关于OpenGL ES - 虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆