2 <h1><a>Multiple Camera Views</a></h1>
7 You can split the screen and look into the 3D scene from different camera angles at the same time. E.g. you can have two rootnodes with different scene graphs, and two viewPorts, each of which can only see its own subset of the scene with its own subset of port-processing filters, so you get two very different views of the scene.
11 The packages used in this example are <code>com.jme3.renderer.Camera</code> and <code>com.jme3.renderer.ViewPort</code>. You can get the full sample code here: <object classid="java:org.netbeans.modules.javahelp.BrowserDisplayer"><param name="content" value="http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/renderer/TestMultiViews.java"><param name="text" value="<html><u>TestMultiViews.java</u></html>"><param name="textColor" value="blue"></object>
15 <!-- EDIT1 SECTION "Multiple Camera Views" [1-650] -->
16 <h2><a>How to resize and Position ViewPorts</a></h2>
21 The default viewPort is as big as the window. If you have several, they must be of different sizes, either overlapping or adjacent to one another. How do you tell jME which of the ViewPorts should appear where on the screen, and how big they should be?
25 Imagine the window as a 1.0f x 1.0f rectangle. The default cam's viewPort is set to
28 <pre>cam.setViewPort(0f, 1f, 0f, 1f);</pre>
32 This setting makes the ViewPort take up the whole rectangle.
36 The four values are read in the following order:
38 <pre>cam.setViewPort(x1,x2 , y1,y2);</pre>
40 <li><div> <strong>X-axis</strong> from left to right</div>
42 <li><div> <strong>Y-axis</strong> upwards from bottom to top</div>
48 Here are a few examples:
51 <pre>cam1.setViewPort( 0.0f , 1.0f , 0.0f , 1.0f );
52 cam2.setViewPort( 0.5f , 1.0f , 0.0f , 0.5f );</pre>
55 These viewport parameters are, (in this order) the left-right extend, and the bottom-top extend of a views's rectangle on the screen.
57 <pre>0.0 , 1.0 1.0 , 1.0
65 0.0 , 0.0 1.0 , 0.0</pre>
68 Example: Cam2's rectangle is int he bottom right: It extends from mid (x1=0.5f) bottom (y1=0.0f), to right (x2=1.0f) mid (y2=0.5f)
72 <p><div>If you scale the views in a way so that the aspect ratio of a ViewPort is different than the window's aspect ratio, then the ViewPort appears distorted. In these cases, you must recreate (not clone) the ViewPort's cam object with the right aspect ratio. For example: <code>Camera cam5 = new Camera(100,100);</code>
77 <!-- EDIT2 SECTION "How to resize and Position ViewPorts" [651-2294] -->
78 <h2><a>Four-Time Split Screen</a></h2>
83 In this example, you create four views (2x2) with the same aspect ratio as the window, but each is only half the width and height.
87 <!-- EDIT3 SECTION "Four-Time Split Screen" [2295-2463] -->
88 <h3><a>Set up the First View</a></h3>
93 You use the preconfigured Camera <code>cam</code> and <code>viewPort</code> from <code>SimpleApplication</code> for the first view. It's in the bottom right.
96 <pre>cam.setViewPort(.5f, 1f, 0f, 0.5f); // Resize the viewPort to half its size, bottom right.</pre>
100 Optionally, place the main camera in the scene and rotate it in its start position.
103 <pre>cam.setLocation(new Vector3f(3.32f, 4.48f, 4.28f));
104 cam.setRotation(new Quaternion (-0.07f, 0.92f, -0.25f, -0.27f));</pre>
107 <!-- EDIT4 SECTION "Set up the First View" [2464-2960] -->
108 <h3><a>Set Up Three More Views</a></h3>
113 Here is the outline for how you create the three other cams and viewPorts (<object classid="java:org.netbeans.modules.javahelp.BrowserDisplayer"><param name="content" value="http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/renderer/TestMultiViews.java"><param name="text" value="<html><u>Full code sample is here</u></html>"><param name="textColor" value="blue"></object>.) In the code snippet, <code>cam_n</code> stand for <code>cam_2</code> - <code>cam_4</code>, respectively, same for <code>view_n</code>.
117 <li><div> Clone the first cam to reuse its settings</div>
119 <li><div> Resize and position the cam's viewPort with setViewPort().</div>
121 <li><div> (Optionally) Move the cameras in the scene and rotate them so they face what you want to see.</div>
123 <li><div> Create a ViewPort for each camera</div>
125 <li><div> Reset the camera's enabled statuses</div>
127 <li><div> Attach the Node to be displayed to this ViewPort. <br/>
128 The camera doesn't have to look at the rootNode, but that is the most common use case.</div>
134 Here is the abstract code sample for camera <code>n</code>:
137 <pre>Camera cam_n = cam.clone();
138 cam_n.setViewPort(...); // resize the viewPort
139 cam_n.setLocation(new Vector3f(...));
140 cam_n.setRotation(new Quaternion(...));
142 ViewPort view_n = renderManager.createMainView("View of camera #n", cam_n);
143 view_n.setClearEnabled(true);
144 view_n.attachScene(rootNode);
145 view_n.setBackgroundColor(ColorRGBA.Black);</pre>
148 To visualize what you do, use the following drawing of the viewport positions:
150 <pre>0.0 , 1.0 1.0 , 1.0
158 0.0 , 0.0 1.0 , 0.0</pre>
161 This are the lines of code that set the four cameras to create a four-times split screen.
163 <pre>cam1.setViewPort( 0.5f , 1.0f , 0.0f , 0.5f);
165 cam2.setViewPort( 0.0f , 0.5f , 0.0f , 0.5f);
167 cam3.setViewPort( 0.0f , 0.5f , 0.5f , 1.0f);
169 cam4.setViewPort( 0.5f , 1.0f , 0.5f , 1.0f);</pre>
172 <!-- EDIT5 SECTION "Set Up Three More Views" [2961-4771] -->
173 <h2><a>Picture in Picture</a></h2>
178 The following code snippet sets up two views, one covers the whole screen, and the second is a small view in the top center.
187 <pre>// Setup first full-window view
188 cam.setViewPort(0f, 1f, 0f, 1f);
189 cam.setLocation(new Vector3f(3.32f, 4.48f, 4.28f));
190 cam.setRotation(new Quaternion(-0.07f, 0.92f, -0.25f, -0.27f));
192 // Setup second, smaller PiP view
193 Camera cam2 = cam.clone();
194 cam2.setViewPort(.4f, .6f, 0.8f, 1f);
195 cam2.setLocation(new Vector3f(-0.10f, 1.57f, 4.81f));
196 cam2.setRotation(new Quaternion(0.00f, 0.99f, -0.04f, 0.02f));
197 ViewPort viewPort2 = renderManager.createMainView("PiP", cam2);
198 viewPort2.setClearFlags(true, true, true);
199 viewPort2.attachScene(rootNode);</pre>
202 <!-- EDIT6 SECTION "Picture in Picture" [4772-5652] -->
203 <h2><a>ViewPort Settings</a></h2>
208 You can customize the camera and the viewPort of each view individually. For example, each view can have a different background color:
211 <pre>viewPort.setBackgroundColor(ColorRGBA.Blue);</pre>
215 You have full control to determine which Nodes the camera can see! It can see the full rootNode???
218 <pre>viewPort1.attachScene(rootNode);</pre>
222 ??? or you can give each camera a special node whose content it can see:
225 <pre>viewPort2.attachScene(spookyGhostDetectorNode);</pre>
227 <a href="/wiki/doku.php/tag:camera?do=showtag&tag=tag%3Acamera">camera</a>,
228 <a href="/wiki/doku.php/tag:documentation?do=showtag&tag=tag%3Adocumentation">documentation</a>
232 <!-- EDIT7 SECTION "ViewPort Settings" [5653-] -->
233 <p><em><a href="http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:multiple_camera_views?do=export_xhtmlbody">view online version</a></em></p>