Shapes3D: Box.draw() doesn't work anymore (new version of library)

@quark

using shapes3d (from a very old Sketch from my archives) here and wanted to say (used to say) MyBoxes[i].draw();

    • doesn’t work anymore

It seems, it used to work without a parameter, but now it doesn’t?

Thanks!

Chrisir

MCVE :



// imports ================================================

import shapes3d.utils.*;
import shapes3d.*;

Box [] MyBoxes = new Box [33]; // array


void setup () {

  size (800, 600, P3D);

  // Init
  for (int i = 0; i < 33; i = i+1) {
    MyBoxes[i] = new Box( 44, 3, 44 ); 
    MyBoxes[i].visible(true); // was "false" 
  }  // for
}

void draw () {
  background(0);
  PaintIt ();
}

void PaintIt () {
  for (int i = 0; i < 33; i++) {
    fill(255); 
    MyBoxes[i].draw();
  }//for
} // function 

2 Likes

Several things here.

  1. The latest version of Shapes3D (v3.0) was a complete rewrite of the library and is not backwardly compatible
  2. In your code all the boxes are set invisible :smile:
  3. The draw method now expects a parameter of type PGraphics so to draw the shape to the main display use MyBoxes[i].draw(getGraphics());

The reason for the (3) is that it allows shapes to be drawn to offscreen buffers and to improve integration with later versions of G4P

4 Likes

This works!

Thank you so much!

// imports 
import shapes3d.*;

Box [] myBoxes = new Box [33]; // array

void setup () {
  size (800, 600, P3D);

  // Init
  for (int i = 0; i < 33; i++) {
    myBoxes[i] = new Box( 44, 3, 44 ); 
    myBoxes[i].moveTo(i*10+width/2, 100 + i*14, -3*i); 
    myBoxes[i].visible(true); // was "false"
  }  // for
} // function

void draw () {
  background(0);
  paintIt ();
} // function

void paintIt () {
  for (int i = 0; i < 33; i++) {
    // myBoxes[i].fill(255); 
    myBoxes[i].draw(getGraphics()); // !!!!!
  }//for
} // function
//

@quark: excuse me, and how can I replace the pickShape() method?

    Shape3D picked = null;
    picked = Shape3D.pickShape(this, mouseX, mouseY);
    if (picked != null) {
      done = true;
      MakeHumanMove(picked.tagNo);
    }

and could you please add Toroid back into the library? :wink:

I have answered this in your other discussion :grinning:

Think you need to expand on this question - why replace it? what to replace it with?

2 Likes

sorry, this is more a “this used to work and now it doesn’t”-question

— I was using the code (shown above) in a not so old Sketch and now it doesn’t work anymore.

Have the numbers of parameters changed or their types?

Thanks a ton!

Chrisir

1 Like

Yes there is an extra parameter. Prior to V3 the pick function assumed that all shapes were drawn to the main sketch window. In V3 this changed so that shapes could be drawn to different viewports (PGraphics instances).
So your method
picked = Shape3D.pickShape(this, mouseX, mouseY);
becomes
picked = Shape3D.pickShape(this, getGraphics(), mouseX, mouseY);

I strongly recommend you look at the examples that come with V3 and also look at the guides for V3.

3 Likes

You could easily overload that method to just 3 parameters as it was before; given getGraphics() can be invoked by using the 1st parameter, which is of PApplet datatype: :mage:

public static Shape3D pickShape(PApplet papplet, int x, int y) {
    return pickShape(papplet, papplet.getGraphics(), x, y);
}
1 Like

Good idea, wish I had thought of it first :smile:

1 Like

this doesn’t work…

in the reference Shapes 3D: Main Page there ain’t
pickShape

There is pick, but it returns Picked, which is not a Shape3D?

My mistake should be

picked = Shape3D.pick(this, getGraphics(), mouseX, mouseY);
shape = picked.shape;

The class Picked is a public access class with 3 constants
shape : reference to the shape picked
part : id of the part of the shape picked (integer)
partID : binary flag showing the part picked (integer)

Sorry about that but V3 was released in January 2020 and I can’t remember what was in previous versions because they have been entirely replaced by V3.

V3 comes with new examples including those showing shape picking.

2 Likes

thank you, this works


void mousePressedBoard () {
  // mouse evaluate.
  // Check what's been pressed by the mouse.

  // when we have a winner, we leave !!!
  if (winnerIs!=-1) {
    return; //leave
  }
  if (status != constStatusFeld_O) {
    return;
  }

  boolean done = false;

  // Wenn linke Taste UND camera not mouse controlled
  if ((mouseButton == LEFT) &&
    (camIsMouseControlled==false)) {
    // by Quark: http://www.lagers.org.uk/s3d4p/index.html

    Picked pickedTemp = Shape3D.pick(this, getGraphics(), mouseX, mouseY);

    if (pickedTemp!=null) {
      Shape3D picked = pickedTemp.shape;

      if (picked != null) {
        done = true;
        MakeHumanMove(picked.tagNo);
      }
    }
  }
} // mouseClicked
//


Screenshot:

1 Like

“Shape3D.java” from 2017-01-09

	public static Shape3D pickShape(PApplet papplet, int x, int y){
		if(pickBuffer == null || pickBuffer.width != papplet.width || pickBuffer.height != papplet.height){
			pickBuffer = (PGraphics3D) papplet.createGraphics(papplet.width, papplet.height, P3D);
		}
		pickBuffer.noSmooth();
		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.modelview.set(((PGraphicsOpenGL)papplet.g).modelview);
		pickBuffer.noLights();
		pickBuffer.noStroke();
		pickBuffer.background(WHITE);

		// Draw to the buffer
		pickModeOn = true;
		drawAll();
		pickBuffer.endDraw();

		int c = pickBuffer.get(x,y);
		pickModeOn = false;

		// Next line inserted just to get a copy of the buffer for testing
		// make sure it is commented out before release.
		//img = pickBuffer.get(); 
		
		return pickMap.get(c);
	}
1 Like