image a P3D PGraphics buffer in/over a 2D "main window"

edited April 2017 in Questions about Code

This is driving me slightly mad... I have so far tried using a second applet to achieve the same but the results aren't great, as the second window has a border, can be dragged about etc. Whilst I would prefer the 3D "view" to just look like part of the rest of my 2D GUI.

I'm now trying to use a P3D offscreen buffer and then using image(), draw that to the 2D sketch... but I'm stuck with an error asking for size(). But I have no idea where/how I should be supplying this size() value, if I put it in settings() it applies to the main 2D sketch which I don't want. Can anyone help please?!

simple version code :

    PGraphics p3d;

    DBox myBox;
    void settings() {
      //size(400,400,P3D);
    }

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

      p3d = createGraphics(400, 400, P3D);
      p3d.beginDraw();
      p3d.noStroke();
      myBox = new DBox();
      p3d.background(128);
      myBox.render(this.g);
      p3d.endDraw();
    }

    void draw() {
      background(100);
      fill(255, 0, 0);
      ellipse(400, 400, 650, 650);
      image(p3d, 200, 200);
    }

    class DBox {

      DBox() {
      }

      void render(PGraphics pg) {
        pg.fill(0, 0, 255);
        pg.box(40);
      }
    }

Answers

  • edited April 2017

    @mala

    I'm stuck with an error asking for size()

    What is the exact error message? Is it associated with a line number?

    I'm not seeing any errors or warnings when running the sketch above in Processing 3.3.

  • sorry the line : size(400,400,P3D); in settings should have been commented out. The problem i'm having is that if I leave size there (which gets rid off error of course) It is applying the size and P3D to the main 2D window to make it 3D, rather than to the "p3d" 3d graphics i'm wanting to create and draw on top of the 2D window.

  • edited April 2017

    I get the feeling I'm asking the wrong questions here and have missed something fundamental. So here's a simple question, Is it actually possible to add 3D graphics on top of 2d base sketch without using a second Applet window and having all the issues that involves with borders and keeping it on top ?

    My main (2D) sketch is a GUI with a large number of buttons, scroll boxes etc. All these elements are built from processing "primitives" and created in offscreen buffers which are only updated on mouse over/off/click etc...This all works really nicely.

    The 3D view I want to add in the middle of this, has one main 3d shape ( an imported .obj) then 156 custom Pshapes( basically cube shapes) arranged over the imported obj. A picker system lets the user select any of these 156 "nodes" whilst navagating around the scene using a basic custom camera.....by itself all works nicely.

    How do I get this 3D view nicely sat in the middle of main 2D GUI without having a floating second window with borders that disappears behind everything else whenever I click on a button in the 2D GUI ?

    The only alternative I can think of is to have the main sketch window as 3D, draw my 3D parts just in the middle of this(whilst somehow constraining camera nav) Then use hint(DISABLE_DEPTH_TEST) draw all my 2D gui bits, before going back to 3D with hint(ENABLE_DEPTH_TEST).... Though I get the distinct feeling this is going to casue a lot of problems with my GUI 2D elements and the 3D picker system. :(

  • Answer ✓
    • As you already know by now, we can't use any OpenGL-based API directly on a JAVA2D PApplet instance canvas. :(
    • For that we'd really need a P2D or P3D renderer PApplet instance canvas.
    • But I've got an idea for such dilemma: have a separate P3D PApplet, but w/ getGraphics().setVisible(false);
    • The idea is to get PImage snapshots of its PGraphics canvas via get(). :ar!
    • Then use image() or set() in order to display those in the visible JAVA2D PApplet. *-:)
  • Does it work if you use P2D instead for the main window?

  • edited April 2017

    @Lord_of_Galaxy No it doesn't seem to work with P2D as main window, that I can see.

    @GoToLoop Thanks for suggestion, at one point I though that should be possible in theory, but had no idea how to go about doing it. I may try that later.

    What I've done for now is rearrange it all so it is a P3D sketch, as mentioned in my last post, disabling depth and drawing 2D GUI bits over the top. Initially it halved the frame rate, but after masking the 3D view area part, and setting up a "over3DView" function based on same area to restrict what gets refreshed and mouse input I mangaed to get the frame rate back up.

    I'm going to move forward based on this approach, but will have to keep an eye on it as all the above is just the GUI and I have a load of stuff going on under the hood including a lot of time sensitive serial comms.

  • have the main sketch window as 3D, draw my 3D parts just in the middle of this(whilst somehow constraining camera nav) Then use hint(DISABLE_DEPTH_TEST) draw all my 2D gui bits, before going back to 3D with hint(ENABLE_DEPTH_TEST)....

    I did that, look here

    but I agree it maybe will cause other errors...

    https://github.com/Kango/Games/tree/master/TicTacToe_3D

Sign In or Register to comment.