OpenGL ES-虚线 [英] OpenGL ES - Dashed Lines
问题描述
im在我的Android游戏开发中使用OpenGlEs10,我想画虚线 现在,我正在使用此代码画线:
im 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?
推荐答案
在OpenGL ES 1.0中不支持glLineStipple
.也不支持1维纹理.但是支持二维纹理化,并且支持 Alpha测试.请参见 OpenGL ES 1.0规范.
一维纹理可以由二维Nx1纹理表示.
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.
点画图案可以编码为纹理.使用内部格式GL_ALPHA
和以下模式创建二维4x1纹理:
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
缩小和放大功能是GL_NEAREST
.包装参数为GL_REPEAT
(默认设置).
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);
绘制线条时,您必须启用 Alpha测试并启用二维纹理化.
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:
例如一个四边形,其左下角为(-0.5 -0.5),右下角为(0.5,0.5),纹理坐标范围为[0,5]:
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
由于环绕功能为GL_REPEAT
,并且纹理坐标在[0,5]范围内,因此点画图案的5次重复被包裹在四边形的每个边缘上.
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.
这篇关于OpenGL ES-虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!