ace
ace

Reputation: 12024

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?

Upvotes: 6

Views: 15074

Answers (5)

Brenton
Brenton

Reputation: 317

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);

Upvotes: 1

thakis
thakis

Reputation: 5889

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

Upvotes: 13

Drakk Lord
Drakk Lord

Reputation: 96

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.

Upvotes: 4

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

  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.

Upvotes: 0

Goz
Goz

Reputation: 62323

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.

Upvotes: 1

Related Questions