OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / renderer / loader / TestNormalmap.java
1 /*
2  * Copyright (c) 2003-2009 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
33 package jmetest.renderer.loader;
34
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;
40
41 import jmetest.renderer.TestEnvMap;
42 import jmetest.renderer.TestMipMaps;
43
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;
69
70 /**
71  * TestNormalmap
72  */
73 public class TestNormalmap extends SimpleGame {
74     private static final Logger logger = Logger.getLogger(TestNormalmap.class
75             .getName());
76     
77     private Vector3f lightDir = new Vector3f();
78     private GLSLShaderObjectsState so;
79     private String currentShaderStr = "jmetest/data/images/normalmap";
80
81     public static void main(String[] args) {
82         TestNormalmap app = new TestNormalmap();
83         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
84         app.start();
85     }
86
87     protected void simpleUpdate() {
88         if (KeyBindingManager.getKeyBindingManager().isValidCommand(
89                 "reloadShader", false)) {
90             reloadShader();
91         }
92
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();
96     }
97
98     public void reloadShader() {
99         GLSLShaderObjectsState testShader = DisplaySystem.getDisplaySystem()
100                 .getRenderer().createGLSLShaderObjectsState();
101         try {
102             testShader.load(TestColladaLoading.class.getClassLoader()
103                     .getResource(currentShaderStr + ".vert"),
104                     TestColladaLoading.class.getClassLoader().getResource(
105                             currentShaderStr + ".frag"));
106             testShader.apply();
107             DisplaySystem.getDisplaySystem().getRenderer().checkCardError();
108         } catch (JmeException e) {
109             logger.log(Level.WARNING, "Failed to reload shader", e);
110             return;
111         }
112
113         so.load(TestColladaLoading.class.getClassLoader().getResource(
114                 currentShaderStr + ".vert"), TestColladaLoading.class
115                 .getClassLoader().getResource(currentShaderStr + ".frag"));
116
117         so.setUniform("baseMap", 0);
118         so.setUniform("normalMap", 1);
119         so.setUniform("specularMap", 2);
120
121         logger.info("Shader reloaded...");
122     }
123
124     protected void simpleInitGame() {
125         KeyBindingManager.getKeyBindingManager().set("reloadShader",
126                 KeyInput.KEY_F);
127
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));
132
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);
139         dr.setEnabled(true);
140
141         lightState.detachAll();
142         lightState.attach(dr);
143
144         Box box = new Box("box", new Vector3f(), 1, 1, 1);
145         rootNode.attachChild(box);
146
147         so = display.getRenderer().createGLSLShaderObjectsState();
148
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.");
152             quit();
153         }
154
155         reloadShader();
156
157         TextureState ts = display.getRenderer().createTextureState();
158
159         // Base texture
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);
166
167         // Normal map
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);
175
176         // Specular map
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);
183
184         try {
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: "
192                     + e1.toString());
193         }
194
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");
199
200         if (modelStream == null) {
201             logger.info("Unable to find file, did you include jme-test.jar in classpath?");
202             System.exit(0);
203         }
204         // tell the importer to load the model
205         ColladaImporter.load(modelStream, "model");
206
207         Node model = ColladaImporter.getModel();
208
209         // Remove materialstates for this test because the model/importer
210         // creates lousy settings
211         removeMaterialStates(model);
212
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);
220
221         // Set all states on model
222         model.setRenderState(ts);
223         model.setRenderState(so);
224         model.setRenderState(ms);
225
226         ColladaImporter.cleanUp();
227
228         rootNode.attachChild(model);
229
230         rootNode.updateGeometricState(0, true);
231
232          input = new FirstPersonHandler( cam, 80, 1 );
233     }
234
235     public static void removeMaterialStates(Node node) {
236         node.clearRenderState(RenderState.StateType.Material);
237
238         if (node.getQuantity() == 0) {
239             return;
240         }
241         List<Spatial> children = node.getChildren();
242         for (int i = 0, cSize = children.size(); i < cSize; i++) {
243             Spatial child = children.get(i);
244             if (child != null) {
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);
252                 }
253             }
254         }
255     }
256 }