2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package jmetest.renderer.loader;
35 import java.io.InputStream;
36 import java.net.URISyntaxException;
37 import java.util.List;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
41 import jmetest.renderer.TestEnvMap;
42 import jmetest.renderer.TestMipMaps;
44 import com.jme.app.SimpleGame;
45 import com.jme.image.Image;
46 import com.jme.image.Texture;
47 import com.jme.input.FirstPersonHandler;
48 import com.jme.input.KeyBindingManager;
49 import com.jme.input.KeyInput;
50 import com.jme.light.DirectionalLight;
51 import com.jme.math.FastMath;
52 import com.jme.math.Vector3f;
53 import com.jme.renderer.ColorRGBA;
54 import com.jme.scene.Node;
55 import com.jme.scene.SharedMesh;
56 import com.jme.scene.Spatial;
57 import com.jme.scene.TriMesh;
58 import com.jme.scene.shape.Box;
59 import com.jme.scene.state.GLSLShaderObjectsState;
60 import com.jme.scene.state.MaterialState;
61 import com.jme.scene.state.RenderState;
62 import com.jme.scene.state.TextureState;
63 import com.jme.system.DisplaySystem;
64 import com.jme.system.JmeException;
65 import com.jme.util.TextureManager;
66 import com.jme.util.resource.ResourceLocatorTool;
67 import com.jme.util.resource.SimpleResourceLocator;
68 import com.jmex.model.collada.ColladaImporter;
73 public class TestNormalmap extends SimpleGame {
74 private static final Logger logger = Logger.getLogger(TestNormalmap.class
77 private Vector3f lightDir = new Vector3f();
78 private GLSLShaderObjectsState so;
79 private String currentShaderStr = "jmetest/data/images/normalmap";
81 public static void main(String[] args) {
82 TestNormalmap app = new TestNormalmap();
83 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
87 protected void simpleUpdate() {
88 if (KeyBindingManager.getKeyBindingManager().isValidCommand(
89 "reloadShader", false)) {
93 float spinValX = FastMath.sin(timer.getTimeInSeconds() * 2.0f);
94 float spinValY = FastMath.cos(timer.getTimeInSeconds() * 2.0f);
95 lightDir.set(spinValX, spinValY, -1.0f).normalizeLocal();
98 public void reloadShader() {
99 GLSLShaderObjectsState testShader = DisplaySystem.getDisplaySystem()
100 .getRenderer().createGLSLShaderObjectsState();
102 testShader.load(TestColladaLoading.class.getClassLoader()
103 .getResource(currentShaderStr + ".vert"),
104 TestColladaLoading.class.getClassLoader().getResource(
105 currentShaderStr + ".frag"));
107 DisplaySystem.getDisplaySystem().getRenderer().checkCardError();
108 } catch (JmeException e) {
109 logger.log(Level.WARNING, "Failed to reload shader", e);
113 so.load(TestColladaLoading.class.getClassLoader().getResource(
114 currentShaderStr + ".vert"), TestColladaLoading.class
115 .getClassLoader().getResource(currentShaderStr + ".frag"));
117 so.setUniform("baseMap", 0);
118 so.setUniform("normalMap", 1);
119 so.setUniform("specularMap", 2);
121 logger.info("Shader reloaded...");
124 protected void simpleInitGame() {
125 KeyBindingManager.getKeyBindingManager().set("reloadShader",
128 // Our model is Z up so orient the camera properly.
129 cam.setAxes(new Vector3f(-1, 0, 0), new Vector3f(0, 0, 1),
130 new Vector3f(0, 1, 0));
131 cam.setLocation(new Vector3f(0, -100, 0));
133 // Create a directional light
134 DirectionalLight dr = new DirectionalLight();
135 dr.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
136 dr.setAmbient(new ColorRGBA(0.2f, 0.2f, 0.2f, 1.0f));
137 dr.setSpecular(new ColorRGBA(0.7f, 0.7f, 0.7f, 1.0f));
138 dr.setDirection(lightDir);
141 lightState.detachAll();
142 lightState.attach(dr);
144 Box box = new Box("box", new Vector3f(), 1, 1, 1);
145 rootNode.attachChild(box);
147 so = display.getRenderer().createGLSLShaderObjectsState();
149 // Check is GLSL is supported on current hardware.
150 if (!GLSLShaderObjectsState.isSupported()) {
151 logger.severe("Your graphics card does not support GLSL programs, and thus cannot run this test.");
157 TextureState ts = display.getRenderer().createTextureState();
160 Texture baseMap = TextureManager.loadTexture(TestEnvMap.class
161 .getClassLoader().getResource(
162 "jmetest/data/images/Fieldstone.jpg"),
163 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);
164 baseMap.setWrap(Texture.WrapMode.Repeat);
165 ts.setTexture(baseMap, 0);
168 Texture normalMap = TextureManager.loadTexture(TestEnvMap.class
169 .getClassLoader().getResource(
170 "jmetest/data/images/FieldstoneNormal.jpg"),
171 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear,
172 Image.Format.GuessNoCompression, 0.0f, true);
173 normalMap.setWrap(Texture.WrapMode.Repeat);
174 ts.setTexture(normalMap, 1);
177 Texture specMap = TextureManager.loadTexture(TestEnvMap.class
178 .getClassLoader().getResource(
179 "jmetest/data/images/FieldstoneSpec.jpg"),
180 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);
181 specMap.setWrap(Texture.WrapMode.Repeat);
182 ts.setTexture(specMap, 2);
185 ResourceLocatorTool.addResourceLocator(
186 ResourceLocatorTool.TYPE_TEXTURE,
187 new SimpleResourceLocator(TestMipMaps.class
188 .getClassLoader().getResource(
189 "jmetest/data/model/collada/")));
190 } catch (URISyntaxException e1) {
191 logger.warning("Unable to add texture directory to RLT: "
195 // this stream points to the model itself.
196 InputStream modelStream = TestColladaLoading.class.getClassLoader()
197 .getResourceAsStream(
198 "jmetest/data/model/collada/Test_Ball_Hard.dae");
200 if (modelStream == null) {
201 logger.info("Unable to find file, did you include jme-test.jar in classpath?");
204 // tell the importer to load the model
205 ColladaImporter.load(modelStream, "model");
207 Node model = ColladaImporter.getModel();
209 // Remove materialstates for this test because the model/importer
210 // creates lousy settings
211 removeMaterialStates(model);
213 // Test materialstate (should be set through the import anyway)
214 MaterialState ms = display.getRenderer().createMaterialState();
215 ms.setColorMaterial(MaterialState.ColorMaterial.AmbientAndDiffuse);
216 ms.setAmbient(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
217 ms.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
218 ms.setSpecular(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
219 ms.setShininess(25.0f);
221 // Set all states on model
222 model.setRenderState(ts);
223 model.setRenderState(so);
224 model.setRenderState(ms);
226 ColladaImporter.cleanUp();
228 rootNode.attachChild(model);
230 rootNode.updateGeometricState(0, true);
232 input = new FirstPersonHandler( cam, 80, 1 );
235 public static void removeMaterialStates(Node node) {
236 node.clearRenderState(RenderState.StateType.Material);
238 if (node.getQuantity() == 0) {
241 List<Spatial> children = node.getChildren();
242 for (int i = 0, cSize = children.size(); i < cSize; i++) {
243 Spatial child = children.get(i);
245 child.clearRenderState(RenderState.StateType.Material);
246 if (child instanceof Node) {
247 removeMaterialStates((Node) child);
248 } else if (child instanceof SharedMesh) {
249 SharedMesh sharedMesh = (SharedMesh) child;
250 TriMesh t = sharedMesh.getTarget();
251 t.clearRenderState(RenderState.StateType.Material);