• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package com.jme3.scene.plugins.blender.lights;
33 
34 import com.jme3.asset.BlenderKey.FeaturesToLoad;
35 import com.jme3.light.DirectionalLight;
36 import com.jme3.light.Light;
37 import com.jme3.light.PointLight;
38 import com.jme3.light.SpotLight;
39 import com.jme3.math.ColorRGBA;
40 import com.jme3.math.FastMath;
41 import com.jme3.scene.plugins.blender.AbstractBlenderHelper;
42 import com.jme3.scene.plugins.blender.BlenderContext;
43 import com.jme3.scene.plugins.blender.BlenderContext.LoadedFeatureDataType;
44 import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
45 import com.jme3.scene.plugins.blender.file.Structure;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
48 
49 /**
50  * A class that is used in light calculations.
51  * @author Marcin Roguski
52  */
53 public class LightHelper extends AbstractBlenderHelper {
54 
55     private static final Logger LOGGER = Logger.getLogger(LightHelper.class.getName());
56 
57     /**
58      * This constructor parses the given blender version and stores the result. Some functionalities may differ in
59      * different blender versions.
60      * @param blenderVersion
61      *        the version read from the blend file
62      * @param fixUpAxis
63      *        a variable that indicates if the Y asxis is the UP axis or not
64      */
LightHelper(String blenderVersion, boolean fixUpAxis)65     public LightHelper(String blenderVersion, boolean fixUpAxis) {
66         super(blenderVersion, fixUpAxis);
67     }
68 
toLight(Structure structure, BlenderContext blenderContext)69     public Light toLight(Structure structure, BlenderContext blenderContext) throws BlenderFileException {
70         Light result = (Light) blenderContext.getLoadedFeature(structure.getOldMemoryAddress(), LoadedFeatureDataType.LOADED_FEATURE);
71         if (result != null) {
72             return result;
73         }
74         int type = ((Number) structure.getFieldValue("type")).intValue();
75         switch (type) {
76             case 0://Lamp
77                 result = new PointLight();
78                 float distance = ((Number) structure.getFieldValue("dist")).floatValue();
79                 ((PointLight) result).setRadius(distance);
80                 break;
81             case 1://Sun
82                 LOGGER.log(Level.WARNING, "'Sun' lamp is not supported in jMonkeyEngine.");
83                 break;
84             case 2://Spot
85             	result = new SpotLight();
86             	//range
87             	((SpotLight)result).setSpotRange(((Number) structure.getFieldValue("dist")).floatValue());
88             	//outer angle
89             	float outerAngle = ((Number) structure.getFieldValue("spotsize")).floatValue()*FastMath.DEG_TO_RAD * 0.5f;
90             	((SpotLight)result).setSpotOuterAngle(outerAngle);
91 
92             	//inner angle
93             	float spotblend = ((Number) structure.getFieldValue("spotblend")).floatValue();
94                 spotblend = FastMath.clamp(spotblend, 0, 1);
95                 float innerAngle = outerAngle * (1 - spotblend);
96             	((SpotLight)result).setSpotInnerAngle(innerAngle);
97                 break;
98             case 3://Hemi
99                 LOGGER.log(Level.WARNING, "'Hemi' lamp is not supported in jMonkeyEngine.");
100                 break;
101             case 4://Area
102                 result = new DirectionalLight();
103                 break;
104             default:
105                 throw new BlenderFileException("Unknown light source type: " + type);
106         }
107         if (result != null) {
108             float r = ((Number) structure.getFieldValue("r")).floatValue();
109             float g = ((Number) structure.getFieldValue("g")).floatValue();
110             float b = ((Number) structure.getFieldValue("b")).floatValue();
111             result.setColor(new ColorRGBA(r, g, b, 1.0f));
112         }
113         return result;
114     }
115 
116     @Override
shouldBeLoaded(Structure structure, BlenderContext blenderContext)117     public boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext) {
118     	return (blenderContext.getBlenderKey().getFeaturesToLoad() & FeaturesToLoad.LIGHTS) != 0;
119     }
120 }
121