In OpenGL ES 2.0 glAlphaFunc isn’t available, you have to implement it in a fragment shader. There isn’t a lot of reference out there for this (not that I could find anyway) so I thought I’d write this up.
It’s actually quite simple to implement a quick alpha test. I needed to be able to cookie cut out sprites and fonts so I simply needed to reject fragments where the alpha value was zero. Here are the guts of a shader to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
You need to set up a uniform variable u_alphatestenable which enables the alpha test. If you want to support different test types ( less than, greater than etc) then you will need two more uniform variables: one for the test type and one for the value to test against.
To enable the test you need to set uni_alphatest_enable to true:
1 2 3
note that you shouldn’t call glGetUniformLocation every frame, it should be cached somewhere. It’s quite simple and while you may be thinking oh that is so slow it not that bad. It’s faster then the fixed function pipeline which is doing tests for alpha, lights, blend modes etc. If you get paranoid then you can create multiple shaders that support different subsets of features. All you need to be careful of is the cost of calling glSetProgram (to switch shaders) which can be expensive and cause bubbles in the vertex pipeline in the hardware.
Hope it helps.