"gl.readPixels is not a function" on Android

Hello.
I need gl.readPixels function to check if specified pixel of sprite is transparent or not.
On iPhone emulator everything works ok, but on Android I have following error: “gl.readPixels is not a function” on Android.
I use Cocos2d-2.1rc0-x-2.1.3 and CocosBuilder 3.0-alpha4.

I printed list of gl methods on iPhone and it was:

getSupportedExtensions,activeTexture,_attachShader,_bindAttribLocation,_bindBuffer,_bindFramebuffer,_bindRenderbuffer,
_bindTexture,blendColor,blendEquation,blendEquationSeparate,blendFunc,blendFuncSeparate,bufferData,bufferSubData,
checkFramebufferStatus,clear,clearColor,clearDepthf,clearStencil,colorMask,_compileShader,compressedTexImage2D,
compressedTexSubImage2D,copyTexImage2D,copyTexSubImage2D,_createProgram,_createShader,cullFace,_deleteBuffer,
_deleteFramebuffer,_deleteProgram,_deleteRenderbuffer,_deleteShader,_deleteTexture,depthFunc,depthMask,depthRangef,
detachShader,disable,disableVertexAttribArray,drawArrays,drawElements,enable,enableVertexAttribArray,finish,flush,
framebufferRenderbuffer,framebufferTexture2D,frontFace,_createBuffer,_createFramebuffer,_createRenderbuffer,
_createTexture,generateMipmap,_getActiveAttrib,_getActiveUniform,_getAttachedShaders,_getAttribLocation,getError,
_getProgramInfoLog,_getProgramParameter,_getShaderInfoLog,_getShaderSource,_getShaderParameter,getTexParameter,
_getUniformLocation,_getUniform,hint,isBuffer,isEnabled,isFramebuffer,isProgram,isRenderbuffer,isShader,isTexture,lineWidth,
_linkProgram,pixelStorei,polygonOffset,readPixels,releaseShaderCompiler,renderbufferStorage,sampleCoverage,scissor,
_shaderSource,stencilFunc,stencilFuncSeparate,stencilMask,stencilMaskSeparate,stencilOp,stencilOpSeparate,_texImage2D,
texParameterf,texParameteri,_texSubImage2D,uniform1f,uniform1fv,uniform1i,uniform1iv,uniform2f,uniform2fv,uniform2i,
uniform2iv,uniform3f,uniform3fv,uniform3i,uniform3iv,uniform4f,uniform4fv,uniform4i,uniform4iv,uniformMatrix2fv,
uniformMatrix3fv,uniformMatrix4fv,_useProgram,_validateProgram,vertexAttrib1f,vertexAttrib1fv,vertexAttrib2f,vertexAttrib2fv,
vertexAttrib3f,vertexAttrib3fv,vertexAttrib4f,vertexAttrib4fv,vertexAttribPointer,viewport,createTexture,createBuffer,
createRenderbuffer,createFramebuffer,createProgram,createShader,deleteTexture,deleteBuffer,deleteRenderbuffer,
deleteFramebuffer,deleteProgram,deleteShader,bindTexture,bindBuffer,bindRenderBuffer,bindFramebuffer,getUniform,
compileShader,shaderSource,getShaderParameter,getShaderInfoLog,attachShader,linkProgram,getProgramParameter,useProgram,
getAttribLocation,getUniformLocation,getActiveAttrib,getActiveUniform,getAttachedShaders,texImage2D,texSubImage2D,getExtension

but on Android this list is much shorter and there is no readPixels method:

createTexture,createBuffer,createRenderbuffer,createFramebuffer,createProgram,createShader,deleteTexture,
deleteBuffer,deleteRenderbuffer,deleteFramebuffer,deleteProgram,deleteShader,bindTexture,bindBuffer,
bindRenderBuffer,bindFramebuffer,getUniform,compileShader,shaderSource,getShaderParameter,getShaderInfoLog,
attachShader,linkProgram,getProgramParameter,useProgram,getAttribLocation,getUniformLocation,getActiveAttrib,
getActiveUniform,getAttachedShaders,texImage2D,texSubImage2D,getExtension

My Android is 4.1.2 Galaxy Note 2.

Is there a way to use function similar to readPixels of other way to check if sprite is transparent in point?

Hi, now, you could use tools/cocos2d-console to compile your js to bytecode. It’ll be safer.

Hmmm. What do you mean? Why I should compile my js code to bytecode. I just want to have gl.readPixels on Android too as I have it on iOS.
You advice me to compile gl.ReadPixels function written in JS (where it is?) to Android’s bytecode? What about the same API on both platforms?

What i mean was that did you obfuscate your js sources?
If you did, you could use bytecode instead.
If not, I’ll do a test for this issue.

I didn’t obfuscate my source code.
On iOS I use following code:

gl.readPixels(point.x, point.y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);

and it works perfect, but on Android gl.readPixels is not a function. Only that.
Thanks for your reply. I’ll be waiting.

I use cons2d-console compile my js to bytecode, and use it like this: ScriptingCore::getInstance()->runScript(“wxg-min.jsc”);
But unfortunately, the project can not run now, the error is:
JS: :0:malformed UTF-8 character sequence at offset 1

What’s wrong with me?

James Chen wrote:

Hi, now, you could use tools/cocos2d-console to compile your js to bytecode. It’ll be safer.

Guys, why are you writing about compiling the bytecode?

The problem is following:
On iOS I HAVE gl.readPixels and on Android I DON’T HAVE gl.readPixels.
Both on iOS and Android I have GLES 2.0 so where this issue come from and what to do to resolve it?

Hello.
Any suggestions?

Bump.
Does anyone could help me? I really need this function on Android.

I did test the issue, but i didn’t exist.
I tested it by adding

for (key in gl)
{
    cc.log(gl[key]);
}

to samples/Javascript/Shared/tests/tests-boot-jsb.js .

It would output all properties in gl namespace.

07-30 15:18:20.280: D/cocos2d-x debug info(24197): JS: function polygonOffset() {
07-30 15:18:20.280: D/cocos2d-x debug info(24197):     [native code]
07-30 15:18:20.280: D/cocos2d-x debug info(24197): }
07-30 15:18:20.280: D/cocos2d-x debug info(24197): JS: function readPixels() {
07-30 15:18:20.280: D/cocos2d-x debug info(24197):     [native code]
07-30 15:18:20.280: D/cocos2d-x debug info(24197): }
07-30 15:18:20.280: D/cocos2d-x debug info(24197): JS: function releaseShaderCompiler() {
07-30 15:18:20.280: D/cocos2d-x debug info(24197):     [native code]
07-30 15:18:20.280: D/cocos2d-x debug info(24197): }
07-30 15:18:20.280: D/cocos2d-x debug info(24197): JS: function renderbufferStorage() {
07-30 15:18:20.280: D/cocos2d-x debug info(24197):     [native code]
07-30 15:18:20.280: D/cocos2d-x debug info(24197): }
07-30 15:18:20.285: D/cocos2d-x debug info(24197): JS: function sampleCoverage() {
07-30 15:18:20.285: D/cocos2d-x debug info(24197):     [native code]
07-30 15:18:20.285: D/cocos2d-x debug info(24197): }

Hello James Chen, thank your for your time! I really appreciate your help.
I’ve finally investigated what was wrong.
I’ll describe it step by step:

  1. I create project with create_project.py.

  2. In cocos2d-x-jsb 2.1.3 and 2.1.4 after build and run on iOS emulator i have black screen - not hello cocos builder label and button.

  3. According to this (http://2sa-studio.blogspot.fr/2013/04/setup-cross-platform-project-with.html) tutorial I’ve commented out the line 163 of /scripting/javascript/bindings/js/jsb_cocos2d_extension.js) which is: cc.GLNode.extend = cc.Class.extend;

  4. After that everything works fine on iOS but on Android I don’t have readPixels and other OpenGL functions.

  5. When I restore this line, everything works fine on Android but on iOS I have black screen back and error:

    Cocos2d: JS: :0:can’t open main.js: No such file or directory
    Cocos2d: Get data from file(main.jsc) failed!

and next:

jsb_cocos2d_extension.js:163:TypeError: cc.GLNode is undefined


Cocos2d: (evaluatedOK == JS_FALSE)
Cocos2d: (evaluatedOK == JS_FALSE)

James, is that a bug?
So I think I should use 2.1.2 hotfix version for now.