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.
24 package jmetest.renderer.state;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
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;
46 * <code>TestGLSLShaderDataLogic</code>
49 * @version $Id: TestGLSLShaderDataLogic.java,v 1.1 2007/08/14 10:32:15 rherlitz
52 public class TestGLSLShaderDataLogic extends SimpleGame {
53 private static final Logger logger = Logger
54 .getLogger(TestGLSLShaderDataLogic.class.getName());
57 * Entry point for the test,
61 public static void main(String[] args) {
62 TestGLSLShaderDataLogic app = new TestGLSLShaderDataLogic();
63 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
70 * @see com.jme.app.SimpleGame#initGame()
72 protected void simpleInitGame() {
73 display.setTitle("jME - TestGLSLShaderDataLogic");
75 Sphere s = new Sphere("Sphere", 8, 8, 10);
76 s.setModelBound(new BoundingBox());
79 CullState cs = display.getRenderer().createCullState();
80 cs.setCullFace(CullState.Face.Back);
82 rootNode.setRenderState(cs);
84 TextureState ts = display.getRenderer().createTextureState();
86 ts.setTexture(TextureManager.loadTexture(
87 TestGLSLShaderDataLogic.class.getClassLoader().getResource(
88 "jmetest/data/images/Monkey.jpg"),
89 Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
91 Node n1 = new Node("n1");
92 n1.setLocalTranslation(new Vector3f(0, 0, 0));
93 n1.setRenderState(createShader());
95 rootNode.attachChild(n1);
97 for (int i = 0; i < 100; i++) {
98 SharedMesh sm = new SharedMesh("Share" + i, s);
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);
108 rootNode.updateRenderState();
111 private GLSLShaderObjectsState createShader() {
113 // Check is GLSL is supported on current hardware.
114 if (!GLSLShaderObjectsState.isSupported()) {
116 .severe("Your graphics card does not support GLSL programs, and thus cannot run this test.");
120 GLSLShaderObjectsState so = display.getRenderer()
121 .createGLSLShaderObjectsState();
126 TestGLSLShaderObjectsState.class
129 "jmetest/data/images/datalogicshader.vert"),
130 TestGLSLShaderObjectsState.class
133 "jmetest/data/images/datalogicshader.frag"));
135 DisplaySystem.getDisplaySystem().getRenderer().checkCardError();
136 } catch (JmeException e) {
137 logger.log(Level.WARNING, "Error loading shader", e);
141 so.setUniform("baseTexture", 0);
142 so.setUniform("positionOffset", 1.0f, 1.0f, 1.0f);
144 so.setShaderDataLogic(new GLSLShaderDataLogic() {
145 public void applyData(GLSLShaderObjectsState shader, Geometry geom) {
146 shader.setUniform("positionOffset", geom
147 .getWorldTranslation());