1 package com.jme3.scene.plugins.blender.cameras; 2 3 import com.jme3.asset.BlenderKey.FeaturesToLoad; 4 import com.jme3.renderer.Camera; 5 import com.jme3.scene.plugins.blender.AbstractBlenderHelper; 6 import com.jme3.scene.plugins.blender.BlenderContext; 7 import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; 8 import com.jme3.scene.plugins.blender.file.Structure; 9 import java.util.logging.Level; 10 import java.util.logging.Logger; 11 12 /** 13 * A class that is used to load cameras into the scene. 14 * @author Marcin Roguski 15 */ 16 public class CameraHelper extends AbstractBlenderHelper { 17 18 private static final Logger LOGGER = Logger.getLogger(CameraHelper.class.getName()); 19 protected static final int DEFAULT_CAM_WIDTH = 640; 20 protected static final int DEFAULT_CAM_HEIGHT = 480; 21 22 /** 23 * This constructor parses the given blender version and stores the result. Some functionalities may differ in 24 * different blender versions. 25 * @param blenderVersion 26 * the version read from the blend file 27 * @param fixUpAxis 28 * a variable that indicates if the Y asxis is the UP axis or not 29 */ CameraHelper(String blenderVersion, boolean fixUpAxis)30 public CameraHelper(String blenderVersion, boolean fixUpAxis) { 31 super(blenderVersion, fixUpAxis); 32 } 33 34 /** 35 * This method converts the given structure to jme camera. 36 * 37 * @param structure 38 * camera structure 39 * @return jme camera object 40 * @throws BlenderFileException 41 * an exception is thrown when there are problems with the 42 * blender file 43 */ toCamera(Structure structure)44 public Camera toCamera(Structure structure) throws BlenderFileException { 45 if (blenderVersion >= 250) { 46 return this.toCamera250(structure); 47 } else { 48 return this.toCamera249(structure); 49 } 50 } 51 52 /** 53 * This method converts the given structure to jme camera. Should be used form blender 2.5+. 54 * 55 * @param structure 56 * camera structure 57 * @return jme camera object 58 * @throws BlenderFileException 59 * an exception is thrown when there are problems with the 60 * blender file 61 */ toCamera250(Structure structure)62 public Camera toCamera250(Structure structure) throws BlenderFileException { 63 Camera result = new Camera(DEFAULT_CAM_WIDTH, DEFAULT_CAM_HEIGHT); 64 int type = ((Number) structure.getFieldValue("type")).intValue(); 65 if (type != 0 && type != 1) { 66 LOGGER.log(Level.WARNING, "Unknown camera type: {0}. Perspective camera is being used!", type); 67 type = 0; 68 } 69 //type==0 - perspective; type==1 - orthographic; perspective is used as default 70 result.setParallelProjection(type == 1); 71 float aspect = 0; 72 float clipsta = ((Number) structure.getFieldValue("clipsta")).floatValue(); 73 float clipend = ((Number) structure.getFieldValue("clipend")).floatValue(); 74 if (type == 0) { 75 aspect = ((Number) structure.getFieldValue("lens")).floatValue(); 76 } else { 77 aspect = ((Number) structure.getFieldValue("ortho_scale")).floatValue(); 78 } 79 result.setFrustumPerspective(45, aspect, clipsta, clipend); 80 return result; 81 } 82 83 /** 84 * This method converts the given structure to jme camera. Should be used form blender 2.49. 85 * 86 * @param structure 87 * camera structure 88 * @return jme camera object 89 * @throws BlenderFileException 90 * an exception is thrown when there are problems with the 91 * blender file 92 */ toCamera249(Structure structure)93 public Camera toCamera249(Structure structure) throws BlenderFileException { 94 Camera result = new Camera(DEFAULT_CAM_WIDTH, DEFAULT_CAM_HEIGHT); 95 int type = ((Number) structure.getFieldValue("type")).intValue(); 96 if (type != 0 && type != 1) { 97 LOGGER.log(Level.WARNING, "Unknown camera type: {0}. Perspective camera is being used!", type); 98 type = 0; 99 } 100 //type==0 - perspective; type==1 - orthographic; perspective is used as default 101 result.setParallelProjection(type == 1); 102 float aspect = 0; 103 float clipsta = ((Number) structure.getFieldValue("clipsta")).floatValue(); 104 float clipend = ((Number) structure.getFieldValue("clipend")).floatValue(); 105 if (type == 0) { 106 aspect = ((Number) structure.getFieldValue("lens")).floatValue(); 107 } else { 108 aspect = ((Number) structure.getFieldValue("ortho_scale")).floatValue(); 109 } 110 result.setFrustumPerspective(aspect, result.getWidth() / result.getHeight(), clipsta, clipend); 111 return result; 112 } 113 114 @Override shouldBeLoaded(Structure structure, BlenderContext blenderContext)115 public boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext) { 116 return (blenderContext.getBlenderKey().getFeaturesToLoad() & FeaturesToLoad.CAMERAS) != 0; 117 } 118 } 119