5

Does anyone know how to enable blending in OpenGL (android) on a HTC Desire. I am trying to draw colored triangles and using the alpha value of the color buffer to blend them with the background (or another triangle).

It works both on the emulator (2.1) and on a htc hero 2.1 but not on my desire with 2.2. Is there some hardware difference between a hero and a desire that causes this?

The main stuff from the code is (not in order):

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

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

private final static float[] colors = {
       1f, 0f, 0f, 0.5f, // point 0 red
       1f, 0f, 0f, 0.5f, // point 1 red
       1f, 0f, 0f, 0.5f, // point 2 red
       1f, 0f, 0f, 0.5f, // point 3 red

       1f, 0f, 0f, 0.5f, // point 4 red
       1f, 0f, 0f, 0.5f, // point 5 red
       1f, 0f, 0f, 0.5f, // point 6 red
       1f, 0f, 0f, 0.5f, // point 7 red
};

PS. I can provide more code if someone needs it...

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Sunkas
  • 9,542
  • 6
  • 62
  • 102
  • I would guess that the Desire doesn't implement the function. Unlike Direct3D, which falls back onto software rendering, with OpenGL if a function's not implemented it just doesn't work. Or at least that's how it used to work. – ChrisF Nov 17 '10 at 21:00
  • The thing is that blending triangles with textures seem to work on the Desire. But not when using color buffers. – Sunkas Nov 17 '10 at 21:21
  • 3
    Exactly the contrary is true.. With DirectX if a feature is reported to be supported it will be done in hardware. With OpenGL (including GL|ES) a feature that is not an extension must be supported. If not in hardware, then in software. For OpenGL|ES 1.0 alpha-blending is mandatory. – Nils Pipenbrinck Nov 17 '10 at 21:35
  • So you're saying it "should" work? – Sunkas Nov 17 '10 at 21:51
  • Yes, it should work. Does your framebuffer contain a alpha buffer or alpha bits? – Dr. Snoopy Nov 18 '10 at 00:18
  • Matias, it doesn't matter if there is an alpha channel in the destination framebuffer. The chosen BlendFunc relies only on SRC_ALPHA, not DST_ALPHA. I don't see anything wrong with the posted code. – Frogblast Nov 18 '10 at 01:07
  • I am using getHolder().setFormat(PixelFormat.RGBA_8888); and setEGLConfigChooser(8, 8, 8, 8, 16, 0); to the framebuffer to 32bits (8-bit alpha channel). According to http://developer.android.com/resources/articles/glsurfaceview.html "GLSurfaceView helps you choose the type of surface to render to. Different Android devices support different types of surfaces, with no common subset. This makes it tricky problem to choose the best available surface on each device." and "setEGLSurfaceChooser() method to give you control over which surface type is chosen". So could the problem be here? – Sunkas Nov 18 '10 at 12:33
  • Seem to be a problem with the lightning. When disabling the lightning the blending works on my Desire. An idea to solve my problem is to disable lightning when rendering my transparent triangles... but would've prefered to get both lightning and blending together... like I said, it worked on the emulator and the htc hero... wierd... – Sunkas Nov 18 '10 at 13:14
  • I have posted the answer [in this subject](http://stackoverflow.com/questions/8956736/glcolor-coloring-all-textures/26526213#26526213) – Airstriker Oct 24 '14 at 12:53

1 Answers1

3

Jonas, your comment about lighting seems right on, and so now I think we have an answer. The OpenGL ES 1.1.12 Specification states The value of A produced by lighting is the alpha value associated with dcm, where dcm is the material diffuse color.

If you have enabled COLOR_MATERIAL, then the material diffuse color and material ambient color both are taken from the current vertex color. This would imply the Desire is incorrect, and the emulator is correct.

If you have disabled COLROR_MATERIAL (the default state), then the diffuse color material is set with glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, ptrTo4Floats). This would imply that the Desire is correct, and the emulator is incorrect.

Frogblast
  • 1,671
  • 10
  • 9
  • Thanks for your answer. Had COLOR_MATERIAL enabled before. Tried to disable it but still no success enabling blending and light at the same time on Desire. I am using: gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0); and matAmbient and matDiffuse has alpha values (if that matters). I have also tried to disable and enable GL_DEPTH_TEST but no luck there. Any other suggestions? – Sunkas Nov 19 '10 at 22:18