How to get rid of Jagged edges in Android OpenGL ES?

﹥>﹥吖頭↗ 提交于 2019-12-17 20:38:52

问题


I have the following code:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

But still the edges are severely jagged in Android Emulator. What is the solution?


回答1:


Well the best way is to use Multisampling ( Antialiasing ). Since you use the emulator this is not an option for you. ( Multismapling supported in OpenGL ES 2.0 but the emulator does not support 2.0 ) According to the OpenGL specification, the device can ignore the glHint you set, so don't expect to much from it. The GL_DITHER is a way to 'fake' 24bit color depth from 16bit color depth basically it has nothing to do with the edges.

Altho there is an old method to 'fake' antialiasing, its a same that I never used so I cant tell you how, but you can find some hints on the web.

From OpenGL docs

Blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) is also useful for rendering antialiased points and lines in arbitrary order.

Polygon antialiasing is optimized using blend function (GL_SRC_ALPHA_SATURATE, GL_ONE) with polygons sorted from nearest to farthest.




回答2:


http://code.google.com/p/gdc2011-android-opengl/ has sample code for multisampling.




回答3:


You are turning on polygon smoothing using a "hint". A hint is just that a "hint" to the implementation that you want the polygon edges smoothed. The implementation is free to ignore it if it wants.

This is exactly what it is doing.

Furthermore it is highly likely that you simply can't turn on anti-aliasing on android devices because they are just not powerful enough to do it. This may be different between handsets but, again, you are setting a hint.




回答4:


I just went through the same issues as you. I think what you're looking for is the following line of code:

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);



回答5:


  1. Try on a device. I don't trust the emulator for visual/graphics-related issues.
  2. Try gl.glEnable(GL10.GL_DITHER);. I'm not sure if this is on by default. Also: this makes drawing slower.


来源:https://stackoverflow.com/questions/4934367/how-to-get-rid-of-jagged-edges-in-android-opengl-es

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