Orthographic instead of perspective mode

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

Orthographic instead of perspective mode

Post by Joe » Sun 13. Jul 2008, 19:35

Hi,
I would like to render a scene in orthographic mode. I tried the following code, but the scene ist still in perspective mode. Can anybody give me a hint?
The code is a complete example. If you start it, you will see a box which lines run together on z axis (perspective) instead of beeing parallel.

Thanks in advance, Joe

Code: Select all

package de.jreality.tutorial;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;

import de.jreality.geometry.IndexedFaceSetFactory;
import de.jreality.scene.Camera;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.ui.viewerapp.ViewerApp;

public class SimpleViewer {
	public static void main(String[] args){
		SceneGraphComponent sceneGraphComponent = new SceneGraphComponent();

		IndexedFaceSetFactory indexedFaceSetFactory = new IndexedFaceSetFactory();
		double[][] vertices = new double[][]{	{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0},
												{-1, -1, 5}, {1, -1, 5}, {1, 1, 5}, {-1, 1, 5}};
		int[][] faceIndices = new int[][]{	{0, 1, 2, 3}, {7, 6, 5, 4}, {0, 1, 5, 4}, 
											{1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7}};

		Camera camera = new Camera();
		camera.setPerspective(false);
		camera.setNear(-10.0);
		camera.setFar(10.0);
		sceneGraphComponent.setCamera(camera);

		indexedFaceSetFactory.setVertexCount(vertices.length);
		indexedFaceSetFactory.setVertexCoordinates(vertices);
		indexedFaceSetFactory.setFaceCount(faceIndices.length);
		indexedFaceSetFactory.setFaceIndices(faceIndices);
		indexedFaceSetFactory.setFaceColors(new Color[]{});
		indexedFaceSetFactory.setGenerateEdgesFromFaces(true);
		indexedFaceSetFactory.setGenerateFaceNormals(true);
		indexedFaceSetFactory.update();
		sceneGraphComponent.setGeometry(indexedFaceSetFactory.getGeometry());

		ViewerApp viewerApp = new ViewerApp(sceneGraphComponent);
		JFrame frame = new JFrame();
		frame.getContentPane().add((Component)viewerApp.getViewingComponent());

		frame.setSize(512, 512);
		frame.validate();
		frame.setVisible(true);
	}
}

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Post by gunn » Mon 14. Jul 2008, 15:34

The problem with the code as it is listed, is that the camera which is created, is not used. To be used, a camera has to be part of the camera path set on the Viewer class using setCameraPath(). When you use the default ViewerApp, as here, you don't notice that this is happening, but a camera path is created along with a camera. And the camera you created is ignored.

The easiest fix is presented in the revised code which I've included below. Basically, get the camera from the ViererApp after you've created it, and set it as you wish. To see the box you've created, use the 'e' key to encompass the scene. Hope this helps.

BTW, there is currently work underway to improve the tutorial documentation. E-mail me with specific suggestions for improvements (gunn at math.tu-berlin.de).

Code: Select all

package de.jreality.tutorial;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;

import de.jreality.geometry.IndexedFaceSetFactory;
import de.jreality.scene.Camera;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.ui.viewerapp.ViewerApp;
import de.jreality.util.CameraUtility;

public class SimpleViewer {
   public static void main(String[] args){
      SceneGraphComponent sceneGraphComponent = new SceneGraphComponent();

      IndexedFaceSetFactory indexedFaceSetFactory = new IndexedFaceSetFactory();
      double[][] vertices = new double[][]{   {-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0},
                                    {-1, -1, 5}, {1, -1, 5}, {1, 1, 5}, {-1, 1, 5}};
      int[][] faceIndices = new int[][]{   {0, 1, 2, 3}, {7, 6, 5, 4}, {0, 1, 5, 4},
                                 {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7}};

      indexedFaceSetFactory.setVertexCount(vertices.length);
      indexedFaceSetFactory.setVertexCoordinates(vertices);
      indexedFaceSetFactory.setFaceCount(faceIndices.length);
      indexedFaceSetFactory.setFaceIndices(faceIndices);
      indexedFaceSetFactory.setFaceColors(new Color[]{});
      indexedFaceSetFactory.setGenerateEdgesFromFaces(true);
      indexedFaceSetFactory.setGenerateFaceNormals(true);
      indexedFaceSetFactory.update();
      sceneGraphComponent.setGeometry(indexedFaceSetFactory.getGeometry());

      ViewerApp viewerApp = new ViewerApp(sceneGraphComponent);
 
      Camera camera = CameraUtility.getCamera(viewerApp.getCurrentViewer());
      camera.setPerspective(false);
      camera.setNear(-10.0);
      camera.setFar(10.0);

      JFrame frame = new JFrame();
      frame.getContentPane().add((Component)viewerApp.getViewingComponent());

      frame.setSize(512, 512);
      frame.validate();
      frame.setVisible(true);
   }
} 
jReality core developer

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

Post by Joe » Thu 17. Jul 2008, 20:31

Thanks for your quick answer.
It works, but not as expected. Without setting orthographic mode, the rendered box is in the center of the screen. After switching to orthographic mode, the box disappears. It has to be moved with the scale tool to be visible in the panel.
Is there a way to switch between orthographic and perspective mode without changing the size or the position of the displayed geometry?
BTW encompassing the scene with 'e' seems not to work with this code. For example CATIA V5 provides a button wich centers the geometries and scales them to fit in the current panel. If I start the code above and press 'e', nothing seems to happen.

Many thank and best regards, Joe

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Post by gunn » Fri 18. Jul 2008, 11:09

Hi Joe,

I'm surprised that 'e' didn't work. Make sure that the ViewerApp window is selected (has the focus) before typing 'e'. You can typically do this by clicking the left mouse button while over the window.

If that still doesn't work, check in the navigator (the left panel labeled "Scene Graph") under the path "root->scene" to make sure there is an "Encompass tool" present. If not, you probably are running an older version of jReality, please update.

If the encompass tool is present, please make sure that you have an up-to-date version of jReality (either via svn or downloading the jar file) before we proceed to try to debug the problem.

Charles
jReality core developer

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

Post by Joe » Mon 21. Jul 2008, 19:23

Hi Charles,
I downloaded the last JReality Version (I think two weeks ago). I have no "Scene Graph" panel, because I run the code from the posts above. If I start your revised code and set "camera.setPerspective(true);" to true, the encompassing tool works as expected (so I'm sure its present). If I set "camera.setPerspective(false);" to false, the encompassing tool doesn't work.
Could it be a bug? What happens if you start the code and switch between "true" and "false"?

Joe

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Post by gunn » Mon 21. Jul 2008, 21:48

Joe,

Two weeks may not be good enough. On July 11, the encompass tool was improved; it should work better with non-perspective cameras than before. I just looked at the source; it still doesn't handle the case of the non-perspective camera completely correctly; hopefully it may work well enough for you now, and if not, it will be hopefully be fixed soon.

If you still have problems after you update, or you want to start off with the scene encompassed, here's a workaround. Add the following line after you've set the camera parameters:

Code: Select all

      CameraUtility.encompass(viewerApp.getCurrentViewer());
Hope this helps,

Charles
jReality core developer

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

Post by Joe » Tue 22. Jul 2008, 18:20

Hi,
I updated jReality.jar and added the new code line, but it still doesn't work. I'll wait for some later versions. Maybe it works than.

Is there a change log available or a version counter? I am looking for a way to be informed that the jReality.jar file has been changed and what the changes are.

Many thanks for your help

Post Reply