-1

I am new to openGL. I want to draw a rectangle with a dotted boundary on top of a GLES11 canvas. I am able to draw a normal rectangle using the drawRect() method present here:

@Override
public void drawRect(float x, float y, float width, float height, GLPaint paint) {
    GL11 gl = mGL;

    mGLState.setColorMode(paint.getColor(), mAlpha);
    mGLState.setLineWidth(paint.getLineWidth());

    saveTransform();
    translate(x, y);
    scale(width, height, 1);

    gl.glLoadMatrixf(mMatrixValues, 0);
    gl.glDrawArrays(GL11.GL_LINE_LOOP, OFFSET_DRAW_RECT, 4);

    restoreTransform();
    mCountDrawLine++;
}

Complete class can be found here.

https://github.com/classified/Android-Work/blob/master/android_packages_apps_Gallery2-cm-10.2/src/com/android/gallery3d/glrenderer/GLES11Canvas.java

Can someone help me out?

Note the GLES 11 does not support GL_LINE_STIPPLE

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Gaurav Mishra
  • 71
  • 1
  • 6
  • Related: https://stackoverflow.com/questions/37975618/opengl-es-dashed-lines? – BDL Feb 12 '20 at 12:55
  • 1
    @BDL Thank you for responding but As I already told, I cant use glLineStipple() – Gaurav Mishra Feb 12 '20 at 13:11
  • @GauravMishra Since you use OpenGL ES 1.1 you can't use `glLineStipple` and you can't use a shader. In the the answer to [OpenGL ES - Dashed Lines](https://stackoverflow.com/questions/37975618/opengl-es-dashed-lines) a solution with a 1D texture and alpha test is suggested. Probably that is the way to go. – Rabbid76 Feb 12 '20 at 13:22
  • @GauravMishra: Yes, I know. That's why I linked to a question where one of the answers suggests to use a 1D texture. If you neither use Shader nor glLineStipple, that's the only possible solution. – BDL Feb 12 '20 at 14:19
  • 3
    Does this answer your question? [OpenGL ES - Dashed Lines](https://stackoverflow.com/questions/37975618/opengl-es-dashed-lines) – Rabbid76 Feb 13 '20 at 10:39

1 Answers1

0

In OpenGL ES 1.1 you can't use glLineStipple and you can't use a shader. But you can use a 1 dimensional texture and the Alpha test. See OpenGL ES 1.1 Full Specification. OpenGL ES 1.1 does not support 1 dimensional textures, too. But that can be substituted by a 2 dimensional Nx1 texture, with ease.

Create 2D (4x1) texture with the internal format GL_ALPHA and th 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);

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

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

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

gl.glEnable(GL11.GL_TEXTURE_2D);

Ensure that the texture coordinates which are associated to the vertices are aligned to integral values when ypu draw the line :

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174