My Shapes3D library implements shape picking by
The actual code is shown below. In mousePresssed I call this method to identify the shape clicked on unfortunately it doesn't work now (it used to in 1.5.1) i.e.
but it keeps missing the boxes unless I duplicate the camera transformation used in the draw() method in the mousePressed method e.g.
So my questions are
Thanks
Shape picking code
- creating an off-screen buffer (pickBuffer)
- set the pickBuffer transofrmation matrix to match the on-screen matrix
- then draw each shape onto the buffer in a different colour
- find the colour at the mouse position
- find the matching object (if any) and return it.
The actual code is shown below. In mousePresssed I call this method to identify the shape clicked on unfortunately it doesn't work now (it used to in 1.5.1) i.e.
- void mouseClicked(){
- Shape3D picked = Shape3D.pickShape(this,mouseX, mouseY);
- if(picked != null){
- picked.fill(randomColor());
- println("Hit > " + picked.tag);
- }
- else {
- println("Missed " + millis());
- }
- }
but it keeps missing the boxes unless I duplicate the camera transformation used in the draw() method in the mousePressed method e.g.
- void mouseClicked(){
- camera(c * sin(a), 10, c * cos(a), 0, 0, 0, 0, 1, 0); // copied from draw()
- Shape3D picked = Shape3D.pickShape(this,mouseX, mouseY);
- ...
So my questions are
- when the mousePressed (or any input event handler) method is executed does the transformation matrix still reflect changes made in draw?
- would it be better to call this shape picker method from inside draw after any camera() commands?
- is there a better / more 'proper' way of setting the transformation matrix in the off-screen buffer than the way I have done it (lines 7 & 8 in the code below)?
Thanks
Shape picking code
- public static Shape3D pickShape(PApplet papplet, int x, int y){
- if(pickBuffer == null || pickBuffer.width != papplet.width || pickBuffer.height != papplet.height){
- pickBuffer = (PGraphicsOpenGL) papplet.createGraphics(papplet.width, papplet.height, OPENGL);
- }
- pickBuffer.beginDraw();
- // Set the camera same as the drawing surface
- pickBuffer.camera.set(((PGraphicsOpenGL)papplet.g).camera);
- pickBuffer.projection.set(((PGraphicsOpenGL)papplet.g).projection);
- pickBuffer.noLights();
- pickBuffer.noStroke();
- pickBuffer.background(WHITE);
- // Draw to the buffer
- pickModeOn = true;
- drawAll();
- int c = pickBuffer.get(x,y);
- pickModeOn = false;
- pickBuffer.endDraw();
- return pickMap.get(c);
- }
1