Issue with Android OpenGL Multisampling/Antialiasing

萝らか妹 提交于 2019-12-10 10:27:31

问题


I'm working on an app for Android allows the user to tap the screen to draw colors. I've got all of the drawing code working nicely under OpenGL (testing on Android 4.0.4, Galaxy Nexus, though I'm trying to make this backward compatible as far as possible; my SDK targets API 14 but has a minSDK of 8).

The issue I've run into is with antialiasing; I want all my polygons and lines to be antialiased, but they're coming out jagged. I'm positive the Galaxy Nexus supports antialiasing (I've seen it in other apps), so I'm sure I'm doing something wrong.

I've been up and down Google for over an hour now, and through several StackOverflow Q/As, and I've found a few answers:

gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_ALPHA_BITS);
gl.glEnable(GL10.GL_MULTISAMPLE);
gl.glEnable(GL10.GL_SMOOTH);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
gl.glHint(GL10.GL_POINT_SMOOTH_HINT, GL10.GL_NICEST);

I've added some or all of these lines in various orders, and to no effect. (These were added in onSurfaceCreated.)

gl.glEnable(GL10.GL_DITHER);

I think this one helped slightly... but that might be my mind playing tricks on me. Even when using it, though, there are still jagged lines to be found. (Also added in onSurfaceCreated.)

gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

This one seems to be the most common answer. But when doing this, everything is drawn invisible; that is, when starting with a black background, everything is just black, always. (I know it's still being drawn due to the memory flushing messages in LogCat.) I've also tried this in combination with all the other methods above. (And this was added in onSurfaceCreated, as well.)

To recap: I'm using OpenGL on Android 4+ and no multisampling methods appear to be working; while most just have no effect, using glBlendFunc seems to break the rendering entirely.

So, I'm quite stumped. I'm open to any suggestions at all... they will surely help more than defenestrating my computer!

Thanks in advance to everyone patient enough to read this.


回答1:


If you have not requested multisampling on EGL context, you can not turn it on with just the GL functions. See here how to do that:

  • http://code.google.com/p/gdc2011-android-opengl/source/browse/trunk/src/com/example/gdc11/MultisampleConfigChooser.java
  • https://stackoverflow.com/a/7388176/675078



回答2:


You can enable multisampling esay on c++ (android ndk)
If you can not programing with c++ srry.

  1. Install android-ndk (my android ndk version is r8b)
  2. Open android-ndk-r8b/samples/android-native-egl-example/jni/renderer.cpp
  3. Change add to includes EGL/egl.h GLES/gl.h GLES2/gl2.h GLES2/gl2ext.h files
  4. in bool Renderer::initialize() function :

change

   const EGLint attribs to {
                EGL_RED_SIZE, 5,  
                EGL_GREEN_SIZE, 6,  
                EGL_BLUE_SIZE, 5,  
                EGL_DEPTH_SIZE, 16,  
                // Requires that setEGLContextClientVersion(2) is called on the view.
                EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */,
                EGL_SAMPLE_BUFFERS, 1 /* true */,
                EGL_SAMPLES, 2,
                EGL_NONE  };

EGL_SAMPLES is important this arg change number of sample



来源:https://stackoverflow.com/questions/10907758/issue-with-android-opengl-multisampling-antialiasing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!