Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 883
ace Avatar asked Feb 08 '11 14:02

ace


4 Answers

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

like image 160
thakis Avatar answered Oct 22 '22 02:10

thakis


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.

like image 33
Drakk Lord Avatar answered Oct 22 '22 01:10

Drakk Lord


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.

like image 1
Goz Avatar answered Oct 22 '22 02:10

Goz


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);
like image 1
Brenton Avatar answered Oct 22 '22 02:10

Brenton