Lighting broken in PDF export

Have jReality programming problems or questions? Post them here.
Post Reply
Joe
Posts: 61
Joined: Fri 11. Jul 2008, 19:29

Lighting broken in PDF export

Post by Joe » Wed 7. Sep 2011, 16:15

Hi,
after exporting a SceneGraphComponent into a PDF file, the lighting is broken. The displayed scene is correct (first image), but in the PDF file (second image), the whole geometry is black.

Correct lighting in JReality:
Image

Resulting PDF file:
Image

Here is the complete source code for the creation of the geometry and the PDF export:

Code: Select all

import java.awt.Color;
import java.awt.Component;
import java.io.FileOutputStream;

import javax.swing.JFrame;

import de.jreality.geometry.IndexedFaceSetFactory;
import de.jreality.io.JrScene;
import de.jreality.scene.Appearance;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.shader.CommonAttributes;
import de.jreality.ui.viewerapp.ViewerApp;
import de.jreality.util.CameraUtility;
import de.jreality.writer.pdf.WriterPDF;

public class TestPDFExport {
	private static SceneGraphComponent getFaces1(){
		Color[] colorArray = {Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED, Color.RED};
		double[][] verticesArray = {{-500.0, 500.0, -5.0}, {500.0, -500.0, -5.0}, {500.0, 500.0, -5.0}, {-500.0, -500.0, -5.0}, {500.0, 500.0, -15.0}, {-500.0, -500.0, -15.0}, {-500.0, 500.0, -15.0}, {500.0, -500.0, -15.0}};
		int[][] faceIndicesArray = {{0, 1, 2}, {0, 3, 1}, {4, 5, 6}, {4, 7, 5}, {6, 3, 0}, {6, 5, 3}, {4, 0, 2}, {4, 6, 0}, {7, 2, 1}, {7, 4, 2}, {5, 1, 3}, {5, 7, 1}};   

		IndexedFaceSetFactory indexedFaceSetFactory = new IndexedFaceSetFactory();
		indexedFaceSetFactory.setVertexCount(verticesArray.length);
		indexedFaceSetFactory.setVertexCoordinates(verticesArray);
		indexedFaceSetFactory.setFaceCount(faceIndicesArray.length);
		indexedFaceSetFactory.setFaceIndices(faceIndicesArray);
		indexedFaceSetFactory.setFaceColors(colorArray);
		indexedFaceSetFactory.setGenerateFaceNormals(true);
		indexedFaceSetFactory.update();

		SceneGraphComponent sgc = new SceneGraphComponent();
		sgc.setGeometry(indexedFaceSetFactory.getIndexedFaceSet());

		return sgc;
	}

	private static SceneGraphComponent getFaces2(){
		Color[] colorArray = {Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN};
		double[][] verticesArray = {{-12.5, -21.650634765625, 485.0}, {2.9028296151360766E-13, -25.0, 485.0}, {12.5, -21.650634765625, 485.0}, {-21.650634765625, -12.5, 485.0}, {-25.0, 1.745741680554691E-13, 485.0}, {21.650634765625, -12.5, 485.0}, {12.5, 21.650634765625, 485.0}, {-5.886537459733054E-14, 25.0, 485.0}, {-12.5, 21.650634765625, 485.0}, {21.650634765625, 12.5, 485.0}, {25.0, 3.491483361109382E-13, 485.0}, {-21.650634765625, 12.5, 485.0}, {-12.5, 21.650634765625, -15.0}, {2.9085487815959377E-13, 25.0, -15.0}, {12.5, 21.650634765625, -15.0}, {-21.650634765625, 12.5, -15.0}, {-25.0, 1.745741680554691E-13, -15.0}, {21.650634765625, 12.5, -15.0}, {12.5, -21.650634765625, -15.0}, {-5.886537459733054E-14, -25.0, -15.0}, {-12.5, -21.650634765625, -15.0}, {21.650634765625, -12.5, -15.0}, {-21.650634765625, -12.5, -15.0}, {25.0, 0.0, -15.0}};
		int[][] faceIndicesArray = {{0, 1, 2}, {3, 0, 2}, {4, 2, 5}, {4, 3, 2}, {6, 7, 8}, {9, 6, 8}, {10, 4, 5}, {10, 11, 4}, {10, 8, 11}, {10, 9, 8}, {12, 13, 14}, {15, 12, 14}, {16, 14, 17}, {16, 15, 14}, {18, 19, 20}, {21, 22, 16}, {21, 20, 22}, {21, 18, 20}, {23, 16, 17}, {23, 21, 16}, {5, 23, 10}, {5, 21, 23}, {2, 21, 5}, {2, 18, 21}, {1, 18, 2}, {1, 19, 18}, {0, 19, 1}, {0, 20, 19}, {3, 20, 0}, {3, 22, 20}, {4, 22, 3}, {4, 16, 22}, {11, 16, 4}, {11, 15, 16}, {8, 15, 11}, {8, 12, 15}, {7, 12, 8}, {7, 13, 12}, {6, 13, 7}, {6, 14, 13}, {9, 14, 6}, {9, 17, 14}, {10, 17, 9}, {10, 23, 17}};

		IndexedFaceSetFactory indexedFaceSetFactory = new IndexedFaceSetFactory();
		indexedFaceSetFactory.setVertexCount(verticesArray.length);
		indexedFaceSetFactory.setVertexCoordinates(verticesArray);
		indexedFaceSetFactory.setFaceCount(faceIndicesArray.length);
		indexedFaceSetFactory.setFaceIndices(faceIndicesArray);
		indexedFaceSetFactory.setFaceColors(colorArray);
		indexedFaceSetFactory.setGenerateFaceNormals(true);
		indexedFaceSetFactory.update();

		SceneGraphComponent sgc = new SceneGraphComponent();
		sgc.setGeometry(indexedFaceSetFactory.getIndexedFaceSet());

		return sgc;
	}

	public static void main(String[] arguments){
		try {
			// Create the geometry
			SceneGraphComponent root = new SceneGraphComponent();
			root.addChild(getFaces1());
			root.addChild(getFaces2());

			Appearance appearance = new Appearance();
			appearance.setAttribute(CommonAttributes.EDGE_DRAW, false);
			appearance.setAttribute(CommonAttributes.FACE_DRAW, true);
			appearance.setAttribute(CommonAttributes.VERTEX_DRAW, false);
			appearance.setAttribute(CommonAttributes.POLYGON_SHADER + "." + CommonAttributes.SMOOTH_SHADING, false);
			root.setAppearance(appearance);

			// Display the geometry
			ViewerApp viewerApp = new ViewerApp(root);
			CameraUtility.encompass(viewerApp.getCurrentViewer());
			JFrame frame = new JFrame();
			frame.getContentPane().add((Component)viewerApp.getViewingComponent());
			frame.setSize(512, 512);
			frame.validate();
			frame.setVisible(true);

			// Write the geometry into a PDF file
			WriterPDF writerPDF = new WriterPDF();
			writerPDF.writeScene(new JrScene(root), new FileOutputStream("d:\\test.pdf"));

		} catch(Exception exception){
			exception.printStackTrace();
		}
	}
}
Can you tell me, what's wrong? I have update the packages "de.jreality.writer.pdf" and "de.jreality.writer.u3d" from SVN. I think the problem is in the way, I created the SceneGraphComponents. Maybe some important attributes for the PDF export are missing.

Best regards, Joe

sechel
Posts: 69
Joined: Tue 2. Oct 2007, 09:20

Re: Lighting broken in PDF export

Post by sechel » Fri 9. Sep 2011, 14:46

Please set the diffuse color of your object to white when using face or vertex color. The Adobe renderer blends the material color and the node colors. Also lighting will not work with node colors set. These are all issues of the current PDF renderer. Add the following line to your code:

Code: Select all

appearance.setAttribute(POLYGON_SHADER + "." + DIFFUSE_COLOR, Color.GRAY);
For your scene its better to use appearance attributes. Then you will have correct lighting and colors.

Joe
Posts: 61
Joined: Fri 11. Jul 2008, 19:29

Re: Lighting broken in PDF export

Post by Joe » Sun 11. Sep 2011, 20:34

Thanks, I got it running. I had to remove

Code: Select all

//indexedFaceSetFactory.setFaceColors(colorArray);
and add

Code: Select all

appearance.setAttribute(CommonAttributes.DIFFUSE_COLOR, Color.GREEN);
for each SceneGraphComponent.

What do I have to do, if a SceneGraphComponents has triangles with different colors? Do I have to split this SceneGraphComponents into many SceneGraphComponents (one for each color)?
I think, that after http://www3.math.tu-berlin.de/jreality/ ... 45&start=0 it was possible to export the face colors into U3D / PDF (Adobe Reader 9). Isn't this possible with Adobe Reader X?

sechel
Posts: 69
Joined: Tue 2. Oct 2007, 09:20

Re: Lighting broken in PDF export

Post by sechel » Mon 12. Sep 2011, 15:20

As said in the mentioned post
"- The adobe renderer does not overwrite the material properties by the node colors as jreality does."
You'll have to have white diffuse colors on your faces to see the face colors. And lighting will be disabled.

Post Reply