0

Hello I spend many hours by trying to get to work simple picking in WebGL. I used before draw every object:

if(Window.stencilEnabled)
                    gl.stencilFunc(gl.ALWAYS, mesh.pickingID, -1);

And finally:

        mouse_x = this.lastPickingCoords.x;
        mouse_y = this.lastPickingCoords.y;

        var pixels = new Uint8Array(4);
        if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
            gl.readPixels(mouse_x, mouse_y, 1, 1, gl.STENCIL_INDEX, gl.UNSIGNED_BYTE, pixels);
        }else{
            console.log("Framebuffer error.");
        }
        oID = -1 + pixels[0];
        console.log("Picking object: " + oID + " at " + mouse_x + ":" + mouse_y);       

        if(this.mapArray[oID] != undefined){
            console.log("Object exists!");
            console.log(this.mapArray[oID]);
        }else{
            console.log("Object not found");
        }

        this.lastPickingCoords.x = 0;   
        this.lastPickingCoords.y = 0;

But it seems from error "WebGL: INVALID_ENUM: readPixels: invalid format" that reading stencil buffer is not allowed... Is there any workaround, extension? How to do it other way without writing picking shaders? Thank you.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user2621907
  • 101
  • 6
  • Maybe try `gl.STENCIL_INDEX8`? (note the 8). It's a shame webGL doesn't support rendermode GL_SELECT... Also see [this related question](http://stackoverflow.com/questions/21841483/webgl-using-framebuffers-for-picking-multiple-objects). – Kenney Nov 14 '15 at 20:26
  • No luck. gl.readPixels(mouse_x, mouse_y, 1, 1,gl.STENCIL_INDEX8, gl.UNSIGNED_BYTE, pixels) VM8709:2 WebGL: INVALID_ENUM: readPixels: invalid format – user2621907 Nov 14 '15 at 20:50
  • Ah, in that question I linked I see this (adapted): `gl.readPixels(mouse_x, mouse_y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);` – Kenney Nov 14 '15 at 20:56
  • But this returns only color information, for this I have to write another shaders for every kind of object. I will have to do it anyway. There seems to be no way to read stencil in webgl. Thank you anyway. :) – user2621907 Nov 15 '15 at 09:12
  • Right - all the solutions (workarounds really) I have seen use color coding for the object id. I figure the stencil bufffer is only used to limit the rendering to a small area around the mouse pointer coordinate. And you're welcome! – Kenney Nov 15 '15 at 15:53

0 Answers0