Rendering 3D scenes correctly from headless Processing instance

I have the following test sketch, which aims to render a cube with perspective and shading, and write the rendered scene to a PDF file:

import processing.pdf.*;

void setup() {
  size(640, 360, P3D);
  noStroke();
  fill(204);
}

void draw() {
  beginRaw(PDF, "output.pdf");

  // Do all your drawing here
  background(0);
  lights();

  float fov = PI/3.0;
  float cameraZ = (height/2.0) / tan(fov/2.0);
  perspective(fov, float(width)/float(height), cameraZ/2.0, cameraZ*2.0);

  translate(width/2, height/2, 0);
  rotateX(-PI/6);
  rotateY(PI/3);
  box(160);

  endRaw();
  println("Finished.");
  exit();
}

If I render this scene from a headless instance of processing-java via an Xvfb frame buffer, then I get a cube with the wrong background color and no shading (lighting):

If I modify the code so that I don't exit from the script, e.g.:

import processing.pdf.*;

void setup() {
  size(640, 360, P3D);
  noStroke();
  fill(204);
}

void draw() {
  beginRaw(PDF, "output.pdf");

  // Do all your drawing here
  background(0);
  lights();

  float fov = PI/3.0;
  float cameraZ = (height/2.0) / tan(fov/2.0);
  perspective(fov, float(width)/float(height), cameraZ/2.0, cameraZ*2.0);

  translate(width/2, height/2, 0);
  rotateX(-PI/6);
  rotateY(PI/3);
  box(160);

  endRaw();
  //println("Finished.");
  //exit();
}

Then the "headed"-display renders with correct shading:

So I think the script itself is correct, insofar as it creates the scene I want so long as I don't call exit(), but I'm not doing something correct to get a rendering into a PDF file. Is there something I am missing or doing incorrectly here? Thanks for any advice.

Answers

  • Would it be better if I moved this question to the GLSL/Shaders section?

  • No. It's not a shader problem.

    I'm not sure but the exit in the first one means it doesn't get too the end of the draw loop so there's no screen output. You've posted the PDF. In the second one it does get to the end of draw and you've posted a screen grab. These are two different things. I think that if you look at the PDF produced by the second example it'll look the same as the PDF from the first.

    And that might be because PDF has no idea about lights, it's basically 2d, so all you get is the geometry.

    In the past I've done "3d" looking PDFs by doing all the 3d stuff myself, writing my own 3d rotations and perspective transforms, modeling cubes with planes, calculating normals and colouring faces according to angles between those normals and the camera, all the low level stuff you had to do before they had 3d libraries (or even raster output devices). It's a pain, but it's possible.

  • edited June 2016

    I don't get a PDF from the second example. That's why I did a screenshot.

    I'm not sure about your theory, because I should at least get a background color in the first image, which doesn't happen here.

  • edited June 2016

    With the second one you are constantly writing to a file using the same name every frame. Try using a different name per frame, or add noLoop() so it only draws one.

Sign In or Register to comment.