How to draw smooth lines in OpenGL ES 2.0?

I am trying to draw some contours that I saved as vertex arrays:

typedef struct { float* vertices; int nrPoints; }VertexCurve; list<VertexCurve> CurveList; 

I use some examples from opengl es 2.0: http://opengles-book.com/

The drawing method is as follows:

  void Draw ( ESContext *esContext ) { UserData *userData = (UserData*)esContext->userData; // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); // Clear the color buffer glClear ( GL_COLOR_BUFFER_BIT ); // Use the program object glUseProgram ( userData->programObject ); //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); //glEnable(GL_SAMPLE_COVERAGE); //glSampleCoverage(0.5, GL_FALSE); glEnableVertexAttribArray ( 0 ); //glLineWidth(1); for (list<VertexCurve>::iterator it = CurveList.begin(); it != CurveList.end(); it++) { // Load the vertex data glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, (*it).vertices ); glDrawArrays ( GL_LINE_LOOP, 0, (*it).nrPoints ); } eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); } 

Also results for drawing:

enter image description here

I need OpenGL ES 2 to have smoother (smoother) lines and from what I read, what can be done with multisampling. You can see from the code that I tried using some methods specific to this technique, but I could not fully understand their use and got bad results:

enter image description here

If someone can explain to me how to get smooth lines and make the contours smoother, I will be very grateful.

+6
source share
2 answers

Multisampling can be enabled or disabled using the GL_MULTISAMPLE marker, and it is enabled by default.

To find out if multisampling is supported by the active EGL surface, request the value GL_SAMPLE_ BUFFERS: here 1 means support, 0 indicates that it is not supported. Then GL_SAMPLES reports how many pixels per pixel are stored.

So, all I had to do was add these 2 attributes to the context attribute list:

  EGLint attribList[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, (flags & ES_WINDOW_ALPHA) ? 8 : EGL_DONT_CARE, EGL_DEPTH_SIZE, (flags & ES_WINDOW_DEPTH) ? 8 : EGL_DONT_CARE, EGL_STENCIL_SIZE, (flags & ES_WINDOW_STENCIL) ? 8 : EGL_DONT_CARE, EGL_SAMPLE_BUFFERS, (flags & ES_WINDOW_MULTISAMPLE) ? 1 : 0, EGL_SAMPLES, 4, EGL_NONE }; 

I set EGL_SAMPLE_BUFFERS to 1 to have a multisample buffer and EGL_SAMPLES up to 4, so we have 4 samples per pixel (FSAA x4).

+10
source

To be able to perform multisampling, you need a multisample framebuffer. In most OpenGL-ES implementations, this is done by creating a multisampled Buffer Buffer object that renders it, and then copies its contents to the screen framebuffer.

+4
source

Source: https://habr.com/ru/post/902822/


All Articles