OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / renderer / TestBezierMesh.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;
34
35 import com.jme.app.SimpleGame;
36 import com.jme.bounding.BoundingSphere;
37 import com.jme.image.Texture;
38 import com.jme.light.LightNode;
39 import com.jme.light.PointLight;
40 import com.jme.math.FastMath;
41 import com.jme.math.Quaternion;
42 import com.jme.math.Vector3f;
43 import com.jme.renderer.ColorRGBA;
44 import com.jme.scene.BezierMesh;
45 import com.jme.scene.BezierPatch;
46 import com.jme.scene.Spatial;
47 import com.jme.scene.shape.Box;
48 import com.jme.scene.state.MaterialState;
49 import com.jme.scene.state.TextureState;
50 import com.jme.util.TextureManager;
51
52 /**
53  * <code>TestLightState</code>
54  * @author Mark Powell
55  * @version $Id: TestBezierMesh.java,v 1.26 2006/05/12 21:29:20 nca Exp $
56  */
57 public class TestBezierMesh extends SimpleGame {
58   private BezierMesh bez;
59   private PointLight pl;
60   private LightNode lightNode;
61   private Vector3f currentPos;
62   private Vector3f newPos;
63
64   /**
65    * Entry point for the test,
66    * @param args
67    */
68   public static void main(String[] args) {
69     TestBezierMesh app = new TestBezierMesh();
70     app.setConfigShowMode(ConfigShowMode.AlwaysShow);
71     app.start();
72
73   }
74
75   protected void simpleUpdate() {
76     // update light pos
77     if ( (int) currentPos.x == (int) newPos.x
78         && (int) currentPos.y == (int) newPos.y
79         && (int) currentPos.z == (int) newPos.z) {
80       newPos.x = (float) Math.random() * 10 - 5;
81       newPos.y = (float) Math.random() * 10 - 5;
82       newPos.z = (float) Math.random() * 10 - 5;
83     }
84
85     currentPos.x -= (currentPos.x - newPos.x)
86         / (timer.getFrameRate() / 2);
87     currentPos.y -= (currentPos.y - newPos.y)
88         / (timer.getFrameRate() / 2);
89     currentPos.z -= (currentPos.z - newPos.z)
90         / (timer.getFrameRate() / 2);
91
92     lightNode.setLocalTranslation(currentPos);
93   }
94
95   protected void simpleInitGame() {
96     display.setTitle("Bezier Mesh Test");
97     cam.setLocation(new Vector3f(0, 0, 5));
98     cam.update();
99     currentPos = new Vector3f();
100     newPos = new Vector3f();
101
102     BezierPatch bp = new BezierPatch();
103     bp.setAnchor(0, 0, new Vector3f( -0.75f, -0.75f, -0.5f));
104     bp.setAnchor(0, 1, new Vector3f( -0.25f, -0.75f, 0.0f));
105     bp.setAnchor(0, 2, new Vector3f(0.25f, -0.75f, 0.5f));
106     bp.setAnchor(0, 3, new Vector3f(0.75f, -0.75f, 0.5f));
107     bp.setAnchor(1, 0, new Vector3f( -0.75f, -0.25f, -0.75f));
108     bp.setAnchor(1, 1, new Vector3f( -0.25f, -0.25f, 0.5f));
109     bp.setAnchor(1, 2, new Vector3f(0.25f, -0.25f, 0.5f));
110     bp.setAnchor(1, 3, new Vector3f(0.75f, -0.25f, 0.75f));
111     bp.setAnchor(2, 0, new Vector3f( -0.75f, 0.25f, -0.5f));
112     bp.setAnchor(2, 1, new Vector3f( -0.25f, 0.25f, -0.5f));
113     bp.setAnchor(2, 2, new Vector3f(0.25f, 0.25f, -0.5f));
114     bp.setAnchor(2, 3, new Vector3f(0.75f, 0.25f, 0.0f));
115     bp.setAnchor(3, 0, new Vector3f( -0.75f, 0.75f, -0.5f));
116     bp.setAnchor(3, 1, new Vector3f( -0.25f, 0.75f, -1.0f));
117     bp.setAnchor(3, 2, new Vector3f(0.25f, 0.75f, -1.0f));
118     bp.setAnchor(3, 3, new Vector3f(0.75f, 0.75f, -0.5f));
119     bp.setDetailLevel(64);
120
121     bez = new BezierMesh("Bezier Mesh");
122     bez.setPatch(bp);
123     Quaternion rot = new Quaternion();
124     rot.fromAngleAxis(FastMath.PI, new Vector3f(0,0,1));
125     bez.setLocalRotation(rot);
126     bez.setModelBound(new BoundingSphere());
127     bez.updateModelBound();
128     rootNode.attachChild(bez);
129
130     MaterialState ms = display.getRenderer().createMaterialState();
131     ms.setEmissive(new ColorRGBA(0.1f, 0.1f, 0.1f, 1.0f));
132 //    ms.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
133 //    ms.setDiffuse(new ColorRGBA(1.0f, 0.85f, 0.75f, 1.0f));
134 //    ms.setSpecular(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f));
135     ms.setShininess(128.0f);
136     ms.setEnabled(true);
137     bez.setRenderState(ms);
138
139     pl = new PointLight();
140     pl.setAmbient(new ColorRGBA(0, 0, 0, 1));
141     pl.setDiffuse(new ColorRGBA(1, 1, 1, 1));
142     pl.setSpecular(new ColorRGBA(.2f, .2f, .2f, 1));
143     pl.setEnabled(true);
144
145     lightState.detachAll();
146     lightState.attach(pl);
147     
148     lightState.setTwoSidedLighting(true);
149     lightNode = new LightNode("Light Node");
150     lightNode.setLight(pl);
151     bez.setRenderState(lightState);
152
153     Vector3f min = new Vector3f( -0.15f, -0.15f, -0.15f);
154     Vector3f max = new Vector3f(0.15f, 0.15f, 0.15f);
155     Box lightBox = new Box("box", min, max);
156     lightBox.setLightCombineMode(Spatial.LightCombineMode.Off);
157     lightBox.setModelBound(new BoundingSphere());
158     lightBox.updateModelBound();
159
160     lightNode.attachChild(lightBox);
161     lightNode.setCullHint(Spatial.CullHint.Never);
162
163     rootNode.attachChild(lightNode);
164
165     TextureState ts = display.getRenderer().createTextureState();
166     ts.setEnabled(true);
167     ts.setTexture(
168         TextureManager.loadTexture(
169         TestBezierMesh.class.getClassLoader().getResource(
170         "jmetest/data/images/Monkey.jpg"),
171         Texture.MinificationFilter.BilinearNearestMipMap,
172         Texture.MagnificationFilter.Bilinear));
173
174     bez.setRenderState(ts);
175   }
176 }