Kartik Watwani
Kartik Watwani

Reputation: 657

Drawing perfectly straight lines in OpenGLES android

I am using Open GLES 2.0 to build my variant of GLSurfaceView, I wanted to draw straight lines so I used the below code to draw lines (everything else is already set up)

GLES20.glDrawArrays(GL_LINES,offset,no_of_coordinates)

The problem I am facing with the above line of code is that the lines are not smooth, instead it looks like it has so many breaks. It looks like small zig zak lines have been placed together . You can see below

enter image description here

Then I read this(https://www.codeproject.com/Articles/199525/Drawing-nearly-perfect-D-line-segments-in-OpenGL) and added the below code

 glHint(GL_LINES,  GL_NICEST);

But still nothing changed. Can you guide me as to how I can get smooth straight lines ?

Upvotes: 0

Views: 265

Answers (1)

Gil Moshayof
Gil Moshayof

Reputation: 16781

You need to provide an EGLConfigChooser instance to the renderer, and define it with 4xMSAA (4 multi-samples for anti-aliasing).

Here's how you do it:

First, define a class with the definitions you need:

class MyConfigChooser implements WLGLSurfaceView.EGLConfigChooser {
        @Override
        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            int attribs[] = {
                EGL10.EGL_LEVEL, 0,
                EGL10.EGL_RENDERABLE_TYPE, 4,  
                EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
                EGL10.EGL_RED_SIZE, 8,
                EGL10.EGL_GREEN_SIZE, 8,
                EGL10.EGL_BLUE_SIZE, 8,
                EGL10.EGL_ALPHA_SIZE, 8,
                EGL10.EGL_DEPTH_SIZE, 16,
                EGL10.EGL_SAMPLE_BUFFERS, 1, 
                EGL10.EGL_SAMPLES, 4,  // This is for 4x MSAA.
                EGL10.EGL_NONE
            };

            EGLConfig[] configs = new EGLConfig[1];
            int[] configCounts = new int[1];
            egl.eglChooseConfig(display, attribs, configs, 1, configCounts);

            if (configCounts[0] == 0) {
                Log.i("OGLES20", "Config with 4MSAA failed");
                // Failed! Error handling.
                return null;
            } else {
                Log.i("OGLES20", "Config with 4MSAA succeeded");
                return configs[0];
            }
        }
    }

Next, in your surface view, add the following call:

mySufaceView.setEGLConfigChooser(new MyConfigChooser()); 

This should give you very good anti-aliasing, but the trade-off is a hit to performance, although most devices today should be able to manage without a significant drop.

Hope this helps.

Upvotes: 2

Related Questions