OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / renderer / state / TestGLSLShaderDataLogic.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine All rights reserved. Redistribution and
3  * use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: * Redistributions of source
5  * code must retain the above copyright notice, this list of conditions and the
6  * following disclaimer. * Redistributions in binary form must reproduce the
7  * above copyright notice, this list of conditions and the following disclaimer
8  * in the documentation and/or other materials provided with the distribution. *
9  * Neither the name of 'jMonkeyEngine' nor the names of its contributors may be
10  * used to endorse or promote products derived from this software without
11  * specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
12  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
14  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
17  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
18  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
21  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 package jmetest.renderer.state;
25
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import com.jme.app.SimpleGame;
30 import com.jme.bounding.BoundingBox;
31 import com.jme.image.Texture;
32 import com.jme.math.Vector3f;
33 import com.jme.scene.Geometry;
34 import com.jme.scene.Node;
35 import com.jme.scene.SharedMesh;
36 import com.jme.scene.shape.Sphere;
37 import com.jme.scene.state.CullState;
38 import com.jme.scene.state.GLSLShaderDataLogic;
39 import com.jme.scene.state.GLSLShaderObjectsState;
40 import com.jme.scene.state.TextureState;
41 import com.jme.system.DisplaySystem;
42 import com.jme.system.JmeException;
43 import com.jme.util.TextureManager;
44
45 /**
46  * <code>TestGLSLShaderDataLogic</code>
47  * 
48  * @author rherlitz
49  * @version $Id: TestGLSLShaderDataLogic.java,v 1.1 2007/08/14 10:32:15 rherlitz
50  *          Exp $
51  */
52 public class TestGLSLShaderDataLogic extends SimpleGame {
53     private static final Logger logger = Logger
54             .getLogger(TestGLSLShaderDataLogic.class.getName());
55
56     /**
57      * Entry point for the test,
58      * 
59      * @param args
60      */
61     public static void main(String[] args) {
62         TestGLSLShaderDataLogic app = new TestGLSLShaderDataLogic();
63         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
64         app.start();
65     }
66
67     /**
68      * builds the trimesh.
69      * 
70      * @see com.jme.app.SimpleGame#initGame()
71      */
72     protected void simpleInitGame() {
73         display.setTitle("jME - TestGLSLShaderDataLogic");
74
75         Sphere s = new Sphere("Sphere", 8, 8, 10);
76         s.setModelBound(new BoundingBox());
77         s.updateModelBound();
78
79         CullState cs = display.getRenderer().createCullState();
80         cs.setCullFace(CullState.Face.Back);
81         cs.setEnabled(true);
82         rootNode.setRenderState(cs);
83
84         TextureState ts = display.getRenderer().createTextureState();
85         ts.setEnabled(true);
86         ts.setTexture(TextureManager.loadTexture(
87                 TestGLSLShaderDataLogic.class.getClassLoader().getResource(
88                         "jmetest/data/images/Monkey.jpg"),
89                 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
90
91         Node n1 = new Node("n1");
92         n1.setLocalTranslation(new Vector3f(0, 0, 0));
93         n1.setRenderState(createShader());
94
95         rootNode.attachChild(n1);
96
97         for (int i = 0; i < 100; i++) {
98             SharedMesh sm = new SharedMesh("Share" + i, s);
99
100             sm.setLocalTranslation(new Vector3f(
101                     (float) Math.random() * 500 - 250,
102                     (float) Math.random() * 500 - 250,
103                     (float) Math.random() * 500 - 250));
104             sm.setRenderState(ts);
105             n1.attachChild(sm);
106         }
107
108         rootNode.updateRenderState();
109     }
110
111     private GLSLShaderObjectsState createShader() {
112
113         // Check is GLSL is supported on current hardware.
114         if (!GLSLShaderObjectsState.isSupported()) {
115             logger
116                     .severe("Your graphics card does not support GLSL programs, and thus cannot run this test.");
117             quit();
118         }
119
120         GLSLShaderObjectsState so = display.getRenderer()
121                 .createGLSLShaderObjectsState();
122
123         try {
124             so
125                     .load(
126                             TestGLSLShaderObjectsState.class
127                                     .getClassLoader()
128                                     .getResource(
129                                             "jmetest/data/images/datalogicshader.vert"),
130                             TestGLSLShaderObjectsState.class
131                                     .getClassLoader()
132                                     .getResource(
133                                             "jmetest/data/images/datalogicshader.frag"));
134             so.apply();
135             DisplaySystem.getDisplaySystem().getRenderer().checkCardError();
136         } catch (JmeException e) {
137             logger.log(Level.WARNING, "Error loading shader", e);
138             quit();
139         }
140
141         so.setUniform("baseTexture", 0);
142         so.setUniform("positionOffset", 1.0f, 1.0f, 1.0f);
143
144         so.setShaderDataLogic(new GLSLShaderDataLogic() {
145             public void applyData(GLSLShaderObjectsState shader, Geometry geom) {
146                 shader.setUniform("positionOffset", geom
147                         .getWorldTranslation());
148             }
149         });
150
151         so.setEnabled(true);
152
153         return so;
154     }
155 }