1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests.g3d.shadows.utils; 18 19 import com.badlogic.gdx.graphics.Camera; 20 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 21 import com.badlogic.gdx.math.Vector3; 22 import com.badlogic.gdx.math.collision.BoundingBox; 23 24 /** Compute directional camera based on frustum bounding sphere. 25 * @author realitix */ 26 public class BoundingSphereDirectionalAnalyzer implements DirectionalAnalyzer { 27 /** Objects used for computation */ 28 protected BoundingBox bb = new BoundingBox(); 29 protected Vector3 tmpV = new Vector3(); 30 protected Vector3 tmpV2 = new Vector3(); 31 32 @Override 33 public Camera analyze (DirectionalLight light, Camera out, Camera mainCamera) { 34 bb.inf(); 35 36 // Create bounding box encompassing main camera frustum 37 for (int i = 0; i < mainCamera.frustum.planePoints.length; i++) { 38 bb.ext(mainCamera.frustum.planePoints[i]); 39 } 40 41 // Radius 42 float radius = bb.getDimensions(tmpV).len() * 0.5f; 43 44 // Center 45 bb.getCenter(tmpV); 46 47 // Move back from 1.5*radius 48 tmpV2.set(light.direction); 49 tmpV2.scl(radius * 1.5f); 50 51 // Position out camera 52 out.direction.set(light.direction); 53 out.position.set(tmpV.sub(tmpV2)); 54 55 // Compute near and far 56 out.near = 0.5f * radius; 57 out.far = 2.5f * radius; 58 59 // Compute up vector 60 Vector3 d = light.direction; 61 if (d.z < d.x + d.y) 62 out.up.set(-light.direction.y, light.direction.x, light.direction.z); 63 else 64 out.up.set(light.direction.x, -light.direction.z, light.direction.y); 65 66 // Compute viewport (orthographic camera) 67 out.viewportWidth = radius; 68 out.viewportHeight = radius; 69 70 return out; 71 } 72 } 73