OSDN Git Service

4edd1e97333bd97612950a3d0c7793b3a24a3a9f
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / effects / TestBumpMapping.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.effects;
34
35 import com.jme.app.SimpleGame;
36 import com.jme.bounding.BoundingBox;
37 import com.jme.image.Texture;
38 import com.jme.light.DirectionalLight;
39 import com.jme.math.FastMath;
40 import com.jme.math.Vector3f;
41 import com.jme.renderer.ColorRGBA;
42 import com.jme.scene.shape.Torus;
43 import com.jme.scene.state.MaterialState;
44 import com.jme.scene.state.TextureState;
45 import com.jme.scene.state.ZBufferState;
46 import com.jme.system.DisplaySystem;
47 import com.jme.util.BumpMapColorController;
48 import com.jme.util.TextureManager;
49
50 /**
51  * <code>TestLightState</code>
52  * 
53  * @author Mark Powell
54  * @version $Id: TestBumpMapping.java,v 1.9 2007/08/21 20:10:03 nca Exp $
55  */
56 public class TestBumpMapping extends SimpleGame {
57     
58         private float angle0;
59
60         private Torus t;
61
62         /**
63          * Entry point for the test,
64          * 
65          * @param args
66          */
67         public static void main(String[] args) {
68                 TestBumpMapping app = new TestBumpMapping();
69                 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
70                 app.start();
71         }
72
73         /**
74          * Not used in this test.
75          * 
76          * @see com.jme.app.BaseGame#update(float)
77          */
78         protected void simpleUpdate() {
79         angle0 += 2 * tpf;
80         
81         ((DirectionalLight)lightState.get(0)).setDirection(new Vector3f(2.0f * 
82                         FastMath.cos(angle0), 2.0f * FastMath.sin(angle0), -1.5f));
83         }
84
85         /**
86          * builds the trimesh.
87          * 
88          * @see com.jme.app.SimpleGame#initGame()
89          */
90         protected void simpleInitGame() {
91
92                 t = new Torus("Torus", 30, 30, 5, 10);
93                 t.setModelBound(new BoundingBox());
94                 t.updateModelBound();
95
96                 BumpMapColorController c = new BumpMapColorController(t);
97                 t.addController(c);
98
99                 MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer()
100                                 .createMaterialState();
101                 ms.setColorMaterial(MaterialState.ColorMaterial.Diffuse);
102                 t.setRenderState(ms);
103                 t.updateRenderState();
104
105                 rootNode.attachChild(t);
106
107                 TextureState ts = display.getRenderer().createTextureState();
108                 ts.setEnabled(true);
109                 Texture tex = TextureManager.loadTexture(TestBumpMapping.class
110                                 .getClassLoader().getResource(
111                                                 "jmetest/data/images/FieldstoneNormal.jpg"),
112                                 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);
113
114                 tex.setWrap(Texture.WrapMode.Repeat);
115                 tex.setApply(Texture.ApplyMode.Combine);
116                 tex.setCombineFuncRGB(Texture.CombinerFunctionRGB.Dot3RGB);
117                 tex.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
118                 tex.setCombineSrc1RGB(Texture.CombinerSource.PrimaryColor);
119
120                 ts.setTexture(tex, 0);
121
122                 Texture tex2 = TextureManager.loadTexture(
123                                 TestBumpMapping.class.getClassLoader().getResource(
124                                                 "jmetest/data/texture/decalimage.png"),
125                                 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, 0.0f, true);
126                 tex2.setApply(Texture.ApplyMode.Combine);
127                 tex2.setWrap(Texture.WrapMode.Repeat);
128                 tex2.setCombineFuncRGB(Texture.CombinerFunctionRGB.Modulate);
129                 tex2.setCombineSrc0RGB(Texture.CombinerSource.Previous);
130                 tex2.setCombineSrc1RGB(Texture.CombinerSource.CurrentTexture);
131                 ts.setTexture(tex2, 1);
132
133                 t.copyTextureCoordinates(0, 1, 1.0f);
134                 t.scaleTextureCoordinates(0, 8);
135
136                 t.setRenderState(ts);
137
138                 ZBufferState buf = display.getRenderer().createZBufferState();
139                 buf.setEnabled(true);
140                 buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
141
142                 t.setRenderState(buf);
143                 
144                 
145                 DirectionalLight dr = new DirectionalLight();
146                 dr.setAmbient(new ColorRGBA(0.75f, 0.75f, 0.75f, 1));
147                 dr.setDiffuse(new ColorRGBA(1, 1, 1, 1));
148                 dr.setEnabled(true);
149                 dr.setDirection(new Vector3f(1,1,-1));
150                 
151                 lightState.detachAll();
152                 lightState.attach(dr);
153                 
154                 rootNode.updateRenderState();
155                 rootNode.updateGeometricState(0.0f, true);
156                 
157         }
158 }