proscene, pixel picking
in
Contributed Library Questions
•
2 years ago
hello All,
I'm trying to use an auxiliary buffer to draw objects that I don't want to display in the main scene
the reason for this is to be able to pick objects like lines for which would be very expensive to draw boxes
also I want to be able to pick the lines at any point along their length and not just in the middle (for instance if using proscene interactiveFrame and placing a point in the middle)
I'm trying to use an auxiliary buffer to draw objects that I don't want to display in the main scene
the reason for this is to be able to pick objects like lines for which would be very expensive to draw boxes
also I want to be able to pick the lines at any point along their length and not just in the middle (for instance if using proscene interactiveFrame and placing a point in the middle)
I've seen the example here, where a Scene is instantiated using an auxiliary buffer and then in the main draw-method it uses buffer.beginDraw and scene.BeginDraw etc. etc.
but this is doing the opposite of what I want since it is drawing and displaying everything in the auxiliary buffer....
I attach here the code, the picking for the lines it is a simple "pixel based picking" (original code here) so that each lines is draw in both the main scene and in the auxiliary buffer. In the auxiliary buffer each line is drawn with a different color so that it is easy to pick them. In the main scene the lines are just drawn in black. This works fine with any cam I tried so far as long as the camera of the buffer is synchronized with that of the main scene (i.e. cam.getState().apply(buffer))
any solutions using proscene and some HUD with controlP5 (see the code below) ?
but this is doing the opposite of what I want since it is drawing and displaying everything in the auxiliary buffer....
I attach here the code, the picking for the lines it is a simple "pixel based picking" (original code here) so that each lines is draw in both the main scene and in the auxiliary buffer. In the auxiliary buffer each line is drawn with a different color so that it is easy to pick them. In the main scene the lines are just drawn in black. This works fine with any cam I tried so far as long as the camera of the buffer is synchronized with that of the main scene (i.e. cam.getState().apply(buffer))
any solutions using proscene and some HUD with controlP5 (see the code below) ?
- import remixlab.proscene.*;
- import controlP5.*;
- import processing.opengl.*;
- ControlP5 controlP5;
- Scene scene;
- PGraphics backBuffer;
- PGraphics backBuffer2;
- PGraphics3D backBuffer3D;
- ArrayList<box> boxes;
- ArrayList<line3D> myLines;
- PApplet aa;
- void setup()
- {
- size(500, 500, OPENGL);
- smooth();
- backBuffer = createGraphics(width, height, P3D);
- backBuffer3D=(PGraphics3D)this.g;
- backBuffer2 = createGraphics(width, height, P3D);
- scene = new Scene(this);
- // press 'f' to display frame selection hints
- scene.setShortcut('f', Scene.KeyboardAction.DRAW_FRAME_SELECTION_HINT);
- controlP5 = new ControlP5(this);
- controlP5.addButton("button", 10, 100, 60, 80, 20).setId(1);
- controlP5.addButton("buttonValue", 4, 100, 90, 80, 20).setId(2);
- controlP5.setAutoDraw(false);
- boxes = new ArrayList<box>();
- for (int i = 0; i < 50; i++)
- {
- box obj = new box(15f, 15f, 15f, i);
- boxes.add(obj);
- }
- myLines= new ArrayList<line3D>();
- for (int i = 0; i < boxes.size()-1; i++)
- {
- line3D obj = new line3D(boxes.get(i).mFrame.position(), boxes.get(i+1).mFrame.position(), i);
- myLines.add(obj);
- }
- }
- void draw()
- {
- background(255);
- stroke(0);
- strokeWeight(2);
- Iterator it = boxes.iterator();
- while (it.hasNext ())
- {
- box obj = (box)it.next();
- pushMatrix();
- obj.display();
- popMatrix();
- }
- for (int i = 0; i < boxes.size()-1; i++)
- {
- myLines.get(i).display(g, boxes.get(i).mFrame.position(), boxes.get(i+1).mFrame.position());
- }
- backBuffer.beginDraw();
- for (int i = 0; i < boxes.size()-1; i++)
- {
- myLines.get(i).drawBuffer(backBuffer);
- }
- backBuffer.endDraw();
- backBuffer.applyMatrix(backBuffer3D.camera);
- //HUD to be implemented
- //drawGUI();
- }
- void drawGUI()
- {
- controlP5.draw();
- }
- // test your frame rate, mine is ~40fps
- void keyPressed()
- {
- if (key == 'x') println(frameRate);
- }
- void mouseClicked()
- {
- // get the pixel color under the mouse
- color pick = backBuffer.get(mouseX, mouseY);
- // get object id
- int id = getId(pick);
- print(id);
- // if id > 0 (background id = -1)
- if (id >= 0 & id< myLines.size())
- {
- // change the line color
- myLines.get(id).changeColor();
- }
- }
- // color -2 gives 0, etc.
- int getId(color c)
- {
- return -(c + 2);
- }
- // id 0 gives color -2, etc.
- color getColor(int id)
- {
- return -(id + 2);
- }
- ////////////////////////////////////////////////////////
- class box
- {
- public float mWidth,mHeight,mDepth;
- public PVector mPosition;
- public InteractiveFrame mFrame;
- public int ID;
- public boolean selected;
- box(float w, float h, float d, int ID)
- {
- mWidth = w; mHeight = h;mDepth = d;
- this.ID=ID;
- float low = -100;float high = 100;
- mPosition = new PVector(random(low, high), random(low, high), random(low, high));
- mFrame = new InteractiveFrame(scene);mFrame.setPosition(mPosition);
- }
- void display()
- {
- // No need to call translationbackBuffer.translate(mPosition.x, mPosition.y, mPosition.z);
- // Frame.applyTransformation does it.
- mFrame.applyTransformation();
- isSelected();
- if (selected==true)
- {
- g.fill(255, 0, 0);
- }
- else
- {
- g.fill(200);
- }
- g.stroke(0);
- g.box(mWidth, mHeight, mDepth);
- }
- public void isSelected()
- {
- if (mFrame.grabsMouse())
- {
- selected=true;
- }
- else
- {
- selected=false;
- }
- }
- }
- ////////////////////////////////////////////////////////
- class line3D
- {
- // variables
- int id; // id
- color c; // color (in scene, not buffer)
- public PVector p0;
- public PVector p1;
- // constructor
- public line3D(PVector p0, PVector p1, int id)
- {
- this.id=id;this.p0=p0;
- this.p1=p1;c = color(0, 0, 0);
- }
- // make the color change
- public void changeColor()
- {
- int r = (int)red(c);int g = (int)green(c);
- int b = (int)blue(c);c = color(255-c, 0, 0);
- }
- // display the line on screen
- public void display(PGraphics ecran,PVector p0,PVector p1)
- {
- this.p0=p0;this.p1=p1;
- ecran.stroke(c);drawLine(ecran);
- }
- // draw the line in the buffer
- public void drawBuffer(PGraphics buffer)
- {
- color idColor = getColor(id);buffer.strokeWeight(10);
- buffer.stroke(idColor);drawLine(buffer);
- }
- private void drawLine(PGraphics g)
- {
- g.line(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z);
- }
- }
1